How to write lat and lon as 'data' in a netcdf file (in addition to the other variable(s))

I have a question about writing out netcdf files:
My netcdf files are being written with a variable (with three dimensions), but there is no list of the lat or lon as separate variables, like I had in my input files which I used to create the output variable...
When I do an ncdump of the MyVar.nc file, I get the dimensions (time, lat, lon), the variable (MyVar (lat,lon,time), and the data, MyVar= (lots and lots of MyVar values), but no actual data for the lon, or lat associated with this variable (I searched for these using the less command, couldn't find them). So I am having trouble plotting the MyVar variable. My code is:
MyVar(:,:,:)=MyArray(:,:,:)
ncid=netcdf.create('myvar_model1.nc','NOCLOBBER');
dimid(1)=netcdf.defDim(ncid,'time',348);
dimid(2)=netcdf.defDim(ncid,'lon',144);
dimid(3)=netcdf.defDim(ncid,'lat',91);
varid=netcdf.defVar(ncid,'MyVar',NC_DOUBLE',dimid);
netcdf.endDef(ncid);
netcdf.putVar(ncid,varid,MyVar);
netcdf.close(ncid)
--do i have to add another varid? I tried this, it wouldn't allow me to specify the dimension I wanted to write as a variable... any further help would be appreciated. Thanks!

 Accepted Answer

At a guess:
before the "endDef",
varid2 = netcdf.defVar(ncid, 'lat', NC_DOUBLE, dimid(2));
varid3 = netcdf.defVar(ncid, 'long', NC_DOUBLE, dimid(3));
Then after the putVar you have now, but before the close.
netcdf.putVar(ncid,varid2,lat);
netcdf.putVar(ncid,varid3,lon);

5 Comments

I tried your suggestion, I put
dimid(1)=netcdf.defDim(ncid,'time',348);
dimid(2)=netcdf.defDim(ncid,'lon',144);
dimid(1)=netcdf.defDim(ncid,'lat',91);
varid1= netcdf.defVar(ncid, 'myvar', NC_DOUBLE, dimid);
varid2= netcdf.defVar(ncid, 'lon', NC_DOUBLE, dimid(2));
varid3= netcdf.defVar(ncid, 'lat', NC_DOUBLE, dimid(3));
netcdf.endDef(ncid)
netcdf.putVar(ncid,varid1,myvar);
netcdf.putVar(ncid,varid2,lon);
...and I get the error 'not a valid data type or _Fillvalue type mismatch. And I have already read in the lon and lat arrays from the original file...
Actually this could be because the lat and lon are integer and I am assigning them to be double....let me look up how to assign vars as integers...
You could also try
netcdf.putVar(ncid,varid2,double(lon));
When you ask for the schema (content field and type description) for the original file, how do lat and lon show up?
(Note: I have never worked with netcdf files before.)
Hi Walter, your suggestion worked (netcdf.putVar(ncid,varid2,double(lon));), but I had to delete lat and lon arrays first and then read them in again -in their native 'double' data format, from the input file (as I had previously had to transform them to integers do calculate the variable I'm wanting to write to netcdf. Many thanks again :)
By the way, I must try that schema command -I have been wondering what the matlab version of this file query is! (I assume it's just schema(var) ?)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!