How to convert an ASCII to NetCDF?

5 views (last 30 days)
Christopher
Christopher on 12 Jul 2013
Commented: Walter Roberson on 27 Mar 2021
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!

Answers (2)

Ashish Uthama
Ashish Uthama on 12 Jul 2013
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 on 27 Mar 2021
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 Comments
Walter Roberson
Walter Roberson on 27 Mar 2021
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 on 29 Mar 2021
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

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!