Time axis in netcdf files

4 views (last 30 days)
Isabella
Isabella on 31 Oct 2011
Hello,
I am trying to build a netcdf file from an array but I have some problems in assigning the time scale.
This array (lon, lat, sea level pressure) was obtained by averaging monthly values from the original dataset, provided as a netcdf file.
Now, I need to create a new netcdf file with this annual values but I do not know how to assign the temporal information to my array.
Here there is the code I used:
clear all
%Open the monthly values netcdf file
ncid = netcdf.open('slp20c.nc','NC_NOWRITE');
[n0, x0, vD0, varAtt0] = netcdf.inqVar(ncid,0);
[n1, x1, vD1, varAtt1] = netcdf.inqVar(ncid,1);
[n2, x2, vD2, varAtt2] = netcdf.inqVar(ncid,2);
[n3, x3, vD3, varAtt3] = netcdf.inqVar(ncid,3);
varid0 = netcdf.inqVarID(ncid,'time');
varid1 = netcdf.inqVarID(ncid,'lon');
varid2 = netcdf.inqVarID(ncid,'lat');
varid3 = netcdf.inqVarID(ncid,'prmsl');
data0 = netcdf.getVar(ncid,0);
data1 = netcdf.getVar(ncid,1);
data2 = netcdf.getVar(ncid,2);
data3 = netcdf.getVar(ncid,3);
%Select data in the desired time range, Jan1871-Dec2007
data=data3(:,:,1:1644);
data=permute(data,[3 1 2]);
a=1:12:1644; b=12:12:1644;
%Compute the annual average of the field values
for i=1:length(a)
yearavg(i,:,:)=mean(data(a(i):b(i),:,:));
end
yearavg=permute(yearavg,[2 3 1]);
%Create new netcdf file with the values obtained in yearavg
nt=137; %nr of years
nx=51; %nr of longitude points
ny=46; %nr of latitude points
lon=310:2:410; lon=lon.';
lat=90:-2:0; lat=lat.';
time=[1871 1 1 0 0 0];
time=repmat(time,137,1);
year=1871:2007; year=year.';
time(:,1)=year;
time=datenum(time);
filenc='test.nc';
ncid = netcdf.create(filenc,'CLASSIC_MODEL');
dimid_lon = netcdf.defDim(ncid,'lon',nx);
dimid_lat = netcdf.defDim(ncid,'lat',ny);
dimid_time = netcdf.defDim(ncid,'time',nt);
varid_lon = netcdf.defVar(ncid,'lon','double',dimid_lon);
netcdf.putAtt(ncid,varid_lon,'long_name','Longitude')
netcdf.putAtt(ncid,varid_lon,'units','degree_e')
varid_lat = netcdf.defVar(ncid,'lat','double',dimid_lat);
netcdf.putAtt(ncid,varid_lat,'long_name','Latitude')
netcdf.putAtt(ncid,varid_lat,'units','degree_n')
varid_time = netcdf.defVar(ncid,'time','double',dimid_time);
netcdf.putAtt(ncid,varid_time,'long_name','Time')
netcdf.putAtt(ncid,varid_time,'units','month')
varid_slp = netcdf.defVar(ncid,'slp','double',[dimid_lon,dimid_lat,dimid_time]);
netcdf.putAtt(ncid,varid_slp,'long_name','slp')
netcdf.putAtt(ncid,varid_slp,'units','hPa')
netcdf.putAtt(ncid,varid_slp,'missing_value',-9999)
count=[nx ny nt];
start=[0 0 0];
netcdf.endDef(ncid)
netcdf.putVar(ncid,varid_slp,start,count,yearavg);
netcdf.close(ncid)
When I then open test.nc file, and I check the data, the new array seems to be correct, but all the longitude,latitude,time axes values are now fixed to a number (9.96920996838687e+36).
May anyone help? Thank you Isabella

Accepted Answer

Ashish Uthama
Ashish Uthama on 31 Oct 2011
Isabella, I dont see a netcdf.putVar for varid_lon, varid_lat etc, so I guess what you are seeing is the default fill value.
Maybe you missed the netcdf.putVar calls?

More Answers (1)

Isabella
Isabella on 31 Oct 2011
Thank you very much Ashish! Now in Matlab everything seems to be properly defined.
However, when uploading test.nc on http://climexp.knmi.nl, (that was actually my aim), Climexp gives me this error: "netcdf error: -43 time_origin NetCDF: Attribute not found".
To me it seems I followed all the necessary instructions http://climexp.knmi.nl/help/netcdf.shtml in order to create a readible netcdf file, but, apparently, this is not the case. :(
What am I missing?
Thank you again
  2 Comments
Ashish Uthama
Ashish Uthama on 31 Oct 2011
Not familiar with that website to say anything specific. However, looking at the code pasted above, the actual time variable does not appear to be in the units claimed by its attribute (months). Maybe the website does some validity checks.
Isabella
Isabella on 1 Nov 2011
I have found the improper definition of the time axis and now it works. Thank you again.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!