netcdf dimension and write problem

3 views (last 30 days)
Jonathan
Jonathan on 13 Nov 2023
Commented: Jonathan on 14 Nov 2023
Hi, I am new to working with netcdf. I am trying to update some code (written ~2011) from a colleague that writes global WWIII model data to a netcdf file. The procedures for doing this in matlab have changed significantly. I have been able to update the code to broadly specify the nc dimensions, variables and attributes. However, I'm stuck on a couple of items.
The original code specified the dimensions in this form:
nc('latitude') = 337; % Define dimensions.
nc('longitude') = 900;
nc('time') = length(model_date_start);
which I rewote to reflect:
lat_dimID = netcdf.defDim(nc,'latitude',337); %constrains the grid to -65S, 70N
long_dimID = netcdf.defDim(nc,'longitude',900);
time_dimID = netcdf.defDim(nc,'time',length(model_date_start));
where nc here is my filename.
the old code also included the following after specifying the dimensions:
nc{'latitude'} = ncfloat('latitude', 'longitude');
nc{'longitude'} = ncfloat('latitude', 'longitude');
nc{'time'} = ncdouble('time');
nc{'sig_wav_hgt'} = ncfloat('latitude', 'longitude', 'time');
However, its not clear to me how this is written in the new format. I have looked through a multitude of examples throughout the newsgroup, without any clear direction.
Would appreciate any guidance.
Thanks
Jon

Answers (1)

Pratyush
Pratyush on 14 Nov 2023
Hi Jonathan,
I understand that you need a way to create variables and assign dimensions in a "netcdf" file. You could use
"netcdf.defVar" function for it. Here's how you can update the code to specify the variables and their dimensions:
% Create latitude variable
lat_varID = netcdf.defVar(nc, 'latitude', 'NC_FLOAT', lat_dimID);
netcdf.putAtt(nc, lat_varID, 'long_name', 'Latitude');
netcdf.putAtt(nc, lat_varID, 'units', 'degrees_north');
% Create longitude variable
lon_varID = netcdf.defVar(nc, 'longitude', 'NC_FLOAT', long_dimID);
netcdf.putAtt(nc, lon_varID, 'long_name', 'Longitude');
netcdf.putAtt(nc, lon_varID, 'units', 'degrees_east');
% Create time variable
time_varID = netcdf.defVar(nc, 'time', 'NC_DOUBLE', time_dimID);
netcdf.putAtt(nc, time_varID, 'long_name', 'Time');
netcdf.putAtt(nc, time_varID, 'units', 'days since 1900-01-01 00:00:00');
% Create sig_wav_hgt variable
sig_wav_hgt_varID = netcdf.defVar(nc, 'sig_wav_hgt', 'NC_FLOAT', [lat_dimID, long_dimID, time_dimID]);
netcdf.putAtt(nc, sig_wav_hgt_varID, 'long_name', 'Significant Wave Height');
netcdf.putAtt(nc, sig_wav_hgt_varID, 'units', 'meters');
The dimensions are specified as an array of dimension IDs ([lat_dimID, long_dimID, time_dimID] for "sig_wav_hgt"). Then, "netcdf.putAtt" is used to set the attributes for each variable.
Call netcdf.endDeff(nc) after defining all the dimensions and variables to finalize the netCDF file definition.
You can refer to the following documentations for more detail:
  1 Comment
Jonathan
Jonathan on 14 Nov 2023
Thank you for your response... very much appreciated. In the end I did manage to figure it out late last night, but thank you for confirming.
Cheers
Jon

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!