How to convert an ASCII to NetCDF?
3 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
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])
0 个评论
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
2021-3-27
110 * 121 / 365 / 24
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
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 Center 和 File Exchange 中查找有关 NetCDF 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!