Write data in a netcdf file

7 views (last 30 days)
Julien
Julien on 15 May 2012
Hey,
First and foremost, I'm french, so sorry if my english is bad.
I'm a student and for a project I have to manipulate a netcdf file.
I have a matlab file with data from a sea experiment, during which researchers have followed a container. As a consequence, I have in this matlab file the speed data of the container for each latitude and longitude of a located zone and for each timestep for two days.
My aim is to create a grib file, but first I have to create from this data a netcdf file (and then convert netcdf to grib with a tool found on the internet).
I have spent quite a day on creating an empty netcdf file with matlab with the "structure" wanted.
What I did:
ncid = netcdf.create('iroise.nc','CLASSIC_MODEL');
dim_lon = netcdf.defDim(ncid,'lon',50);
dim_lat = netcdf.defDim(ncid,'lat',50);
dim_depth = netcdf.defDim(ncid,'depth',50);
dim_time= netcdf.defDim(ncid,'time',50);
var_lon = netcdf.defVar(ncid,'lon','double',dim_lon);
var_lat = netcdf.defVar(ncid,'lat','double',dim_lat);
var_dep = netcdf.defVar(ncid,'depth','double',dim_depth);
var_ti = netcdf.defVar(ncid,'time','double',dim_time);
var_49 = netcdf.defVar(ncid,'var49','float', [dim_time dim_depth dim_lat dim_lon]);
var_50 = netcdf.defVar(ncid,'var50','float', [dim_time dim_depth dim_lat dim_lon]);
netcdf.putAtt(ncid,var_lon,'standard_name','longitude');
netcdf.putAtt(ncid,var_lon,'long_name','longitude');
netcdf.putAtt(ncid,var_lon,'units','degrees_east');
netcdf.putAtt(ncid,var_lon,'axis','X');
netcdf.putAtt(ncid,var_lat,'standard_name','latitude');
netcdf.putAtt(ncid,var_lat,'long_name','latitude');
netcdf.putAtt(ncid,var_lat,'units','degrees_north');
netcdf.putAtt(ncid,var_lat,'axis','Y');
netcdf.putAtt(ncid,var_dep,'standard_name','depth');
netcdf.putAtt(ncid,var_dep,'long_name','depth_below_sea');
netcdf.putAtt(ncid,var_dep,'units','m');
netcdf.putAtt(ncid,var_dep,'positive','down');
netcdf.putAtt(ncid,var_dep,'axis','Z');
netcdf.putAtt(ncid,var_ti,'standard_name','time');
netcdf.putAtt(ncid,var_ti,'units','hours since 2008-09-22 00:00:00');
netcdf.putAtt(ncid,var_ti,'calendar','proleptic_gregorian');
netcdf.putAtt(ncid,var_49,'table','3');
netcdf.putAtt(ncid,var_50,'table','3');
It works well and "ncdump -h iroise.nc" gives me
netcdf iroise {
dimensions:
lon = 50 ;
lat = 50 ;
depth = 50 ;
time = 50 ;
variables:
double lon(lon) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:axis = "X" ;
double lat(lat) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:axis = "Y" ;
double depth(depth) ;
depth:standard_name = "depth" ;
depth:long_name = "depth_below_sea" ;
depth:units = "m" ;
depth:positive = "down" ;
depth:axis = "Z" ;
double time(time) ;
time:standard_name = "time" ;
time:units = "hours since 2008-09-22 00:00:00" ;
time:calendar = "proleptic_gregorian" ;
float var49(lon, lat, depth, time) ;
var49:table = "3" ;
float var50(lon, lat, depth, time) ;
var50:table = "3" ;
}
(I arbitrarily put the dimension length to 50, with no particular reason)
But now I would like to "insert" the data from the matlab file to this netcdf file. As I don't know if I'm allowed to give you the data file, I prefer just show you the "structure" of the matlab file:
X - <6x9 double> (corresponding to the longitude)
Y - <6x9 double> (corresponding to the latitude)
mtime - <1x48 double> (corresponding to the time, 48 hours)
U - <6x432 double> (corresponding to the first component of the speed)
V - <6x432 double> (corresponding to the other component of the speed)
In the netcdf file, the var49 variable corresponds to U and the var50 variable to V.
How can I do?
Thanks in advance,
Julien

Answers (1)

John
John on 16 May 2012
The extents you used to define your dimensions don't match up with the size of your matlab variables.
Should the length of the "time" dimension be 48 instead of 50?
Should depth be 432 instead of 50?
It looks like X and Y are describing a possibly irregular grid that is 6x9?

Tags

Community Treasure Hunt

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

Start Hunting!