필터 지우기
필터 지우기

How to convert an ASCII to NetCDF?

조회 수: 7 (최근 30일)
Christopher
Christopher 2013년 7월 12일
댓글: Walter Roberson 2021년 3월 27일
Hi, i'm trying to convert a large ASCII file into a NetCDF through matlab. It's a climate file 15329 x 16 columns.
nccreate('monthly_tmp.nc', 'monthly_temperatures', 'Dimensions', {'ff' 12 'lon' 180 'lat' 180}, 'Format','classic')
fid=fopen('tmp.mat')
var=zeros(12)
while ~feof(fid)
coor=fscanf(fid, '%d', 2);
var=fscanf(fid, '%f', inf);
lat=coor(1); lon=coor(2);
ncwrite('monthly_tmp.nc', 'monthly_temperatures', var (1 lat, lon))
end
The columns are set out as latitude, longitude, year and 12 monthly values.
I need to first read lat and lon and then assign a monthly value to that grid.
So i need columns 1 and 2, and then 4-15 for my temperature values.
I'm really stuck with how I can feed my variables into the loop!
Thanks for your help!

답변 (2개)

Ashish Uthama
Ashish Uthama 2013년 7월 12일
Hope this helps you get started:
%%set up dummy data
fid = fopen('tmp.mat','w');
fmtStr = repmat(' %f',[1,13]);
for lonInd=1:18
for latInd=1:18
fprintf(fid,['%d %d' fmtStr,'\n'], lonInd, latInd, 2013,(1:12)*latInd*lonInd);
end
end
fclose(fid);
nccreate('monthly_tmp.nc', 'monthly_temperatures', 'Dimensions', {'ff' 12 'lon' 18 'lat' 18}, 'Format','classic')
fid=fopen('tmp.mat');
while ~feof(fid)
coor=fscanf(fid, '%d', 2);
var =fscanf(fid, '%f', 13);
% assuming the lat and lon are whole numbers from 1 to 180
lat=coor(1); lon=coor(2);
% assuming var(1) is the year
temp = var(2:end);
ncwrite('monthly_tmp.nc', 'monthly_temperatures', temp, [1 lon lat]);
end
fclose(fid);
%%spot check
d = ncread('monthly_tmp.nc','monthly_temperatures',[1,2,2],[12, 1,1])

Darwin Aramburo Palacios
Darwin Aramburo Palacios 2021년 3월 27일
Good morning I have several series of wave spectrum (Matrix of 110 rows * 121 columns) in .txt files, the data is configured in the following dates
Years: 2015 to 2018
Month: January to December
Day: 1:31
Time from 00 to 23
My intention is to convert these files from .txt format to netcdf format for YEARS
Example of how the data is written
SpecDimarAno_2012_Mes_1_Dia_1_Hora_0
SpecDimarAno_2012_Mes_1_Dia_2_Hora_1
.
.
.
I appreciate if you can give me advice
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 3월 27일
110 * 121 / 365 / 24
ans = 1.5194
Your file appears to have enough information for 36 readings per day instead of 24 readings per day, so it is not obvious how the data is organized? The organization will be important for creating proper netcdf indices.
Darwin Aramburo Palacios
Darwin Aramburo Palacios 2021년 3월 29일
actually
SpecDimarAno_2012_Mes_1_Dia_1_Hora_0
SpecDimarAno_2012_Mes_1_Dia_1_Hora_1
SpecDimarAno_2012_Mes_1_Dia_1_Hora_2
SpecDimarAno_2012_Mes_1_Dia_1_Hora_3
.
.
.
SpecDimarAno_2012_Mes_1_Dia_1_Hora_23
so it continues for the other months and other years

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 NetCDF에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by