% make_cdf.m - create a new netCDF file
[cdfid,rcode] = mexcdf('nccreate', 'test.cdf', 'NC_CLOBBER');

% create some dimensions: lat, lng and time
[latid,rcode] = mexcdf('ncdimdef', cdfid, 'lat', 5);
[lngid,rcode] = mexcdf('ncdimdef', cdfid, 'lng', 3);
[timeid,rcode] = mexcdf('ncdimdef', cdfid, 'time', 'NC_UNLIMITED');

% create some variables: v1, v2, v3, v4, v5, v6, v7, v8 
[v1_id,rcode] = mexcdf('ncvardef', cdfid, 'v1', 'NC_BYTE', 1, latid);
[v2_id,rcode] = mexcdf('ncvardef', cdfid, 'v2', 'NC_CHAR', 1, latid);
[v3_id,rcode] = mexcdf('ncvardef', cdfid, 'v3', 'NC_SHORT', 2, [latid lngid]);
[v4_id,rcode] = mexcdf('ncvardef', cdfid, 'v4', 'NC_LONG', 2, [latid lngid]);
[v5_id,rcode] = ...
	mexcdf('ncvardef', cdfid, 'v5', 'NC_FLOAT', 2, [latid lngid]);
[v6_id,rcode] = ...
	mexcdf('ncvardef', cdfid, 'v6', 'NC_FLOAT', 3, [timeid latid lngid]);
[v7_id,rcode] = ...
	mexcdf('ncvardef', cdfid, 'v7', 'NC_DOUBLE', 2, [latid lngid]);
[v8_id,rcode] = ...
	mexcdf('ncvardef', cdfid, 'v8', 'NC_DOUBLE', 3, [timeid latid lngid]);

% create some attributes
[rcode] = mexcdf('ncattput',cdfid,'NC_GLOBAL','title',...
	'NC_CHAR',18,'This is the title');
[rcode] = mexcdf('ncattput',cdfid,v1_id,'valid_min','NC_BYTE',1,0);
[rcode] = mexcdf('ncattput',cdfid,v3_id,'valid_max','NC_SHORT',1,99);
[rcode] = mexcdf('ncattput',cdfid,v4_id,'add_offset','NC_FLOAT',1,-5);
[rcode] = mexcdf('ncattput',cdfid,v4_id,'scale_factor','NC_FLOAT',1,2.0);
[rcode] = mexcdf('ncattput',cdfid,...
	v5_id,'valid_range','NC_FLOAT',2,[-1.e16 1.e16]); 
[rcode] = mexcdf('ncattput',cdfid,v8_id,'long_name',...
	'NC_CHAR',15,'Variable Eight');

% leave define mode
[rcode] = mexcdf('ncendef', cdfid);

% write single data value to variable 1 
[rcode] = mexcdf('ncvarput1',cdfid,v1_id,0,1);

% write hyperslab of data to variable 1 
values = [2 3 4 5];
[rcode] = mexcdf('ncvarput',cdfid,v1_id,1,4,values);

% write single data value to variable 2 
[rcode] = mexcdf('ncvarput1',cdfid,v2_id,0,'a');

% write hyperslab of data to variable 2 
corner = [1];
count = [4];
values = ['b' 'c' 'd' 'e']; 
[rcode] = mexcdf('ncvarput',cdfid,v2_id,corner,count,values);

% write hyperslab of data to variable 3 
corner = [0 0];
count = [5 3];
values = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
[rcode] = mexcdf('ncvarput',cdfid,v3_id,corner,count,values);

% write single data value to variable 3 
[rcode] = mexcdf('ncvarput1',cdfid,v3_id,[3 2],99);

% write hyperslab of data to variable 4 
corner = [0 0];
count = [5 3];
values = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
[rcode] = mexcdf('ncvarput',cdfid,v4_id,corner,count,values);

% write single data value to variable 4 
[rcode] = mexcdf('ncvarput1',cdfid,v4_id,[2 1],99);

% write hyperslab of data to variable 5 
corner = [0 0];
count = [5 3];
values = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
[rcode] = mexcdf('ncvarput',cdfid,v5_id,corner,count,values);

% write single data value to variable 5 
[rcode] = mexcdf('ncvarput1',cdfid,v5_id,[1 2],99);

% write hyperslab of data to variable 6 
corner = [0 0 0];
count = [3 5 3];
values = [1 1 1 1 1 2 2 2 2 2 3 3 3 3 3;
          4 4 4 4 4 5 5 5 5 5 6 6 6 6 6;
          7 7 7 7 7 8 8 8 8 8 9 9 9 9 9];
[rcode] = mexcdf('ncvarput',cdfid,v6_id,corner,count,values);

% write single data value to variable 6 
[rcode] = mexcdf('ncvarput1',cdfid,v6_id,[1 2 1],99);

% write hyperslab of data to variable 7 
corner = [0 0];
count = [5 3];
values = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
[rcode] = mexcdf('ncvarput',cdfid,v7_id,corner,count,values);

% write single data value to variable 6 
[rcode] = mexcdf('ncvarput1',cdfid,v6_id,[2 3 2],99);

% write several hyperslabs of data to variable 8 
corner = [0 1 1];
count = [3 4 2];
values = [2 2 3 3 4 4 5 5 7 7 8 8 9 9 10 10 12 12 13 13 14 14 15 15];
[rcode] = mexcdf('ncvarput',cdfid,v8_id,corner,count,values);

corner = [0 0 0];
count = [3 1 3];
values = [1 1 1 6 6 6 11 11 11];
[rcode] = mexcdf('ncvarput',cdfid,v8_id,corner,count,values);

corner = [1 1 0];
count = [2 4 1];
values = [7 8 9 10 12 13 14 15];
[rcode] = mexcdf('ncvarput',cdfid,v8_id,corner,count,values);

% write single data values to variable 8 
[rcode] = mexcdf('ncvarput1',cdfid,v8_id,[0 1 0],2);
[rcode] = mexcdf('ncvarput1',cdfid,v8_id,[0 2 0],3);
[rcode] = mexcdf('ncvarput1',cdfid,v8_id,[0 3 0],4);
[rcode] = mexcdf('ncvarput1',cdfid,v8_id,[0 4 0],5);

% close the netCDF file
[rcode] = mexcdf('ncclose', cdfid);
