ncfile='sfc_pres_temp.nc' % % These are used to construct some example data. sample_pressure = 900; sample_temp = 9.0; % % Create some pretend data. If this wasn't an example program, we % would have some real data to write, for example, model % output. lats = [25:0.5:27.5]'; NLAT = length(lats); lons = [-125:0.5:-119.5]'; NLON = length(lons); [Lon,Lat] = meshgrid([0:NLON-1],[0:NLAT-1]); pres_out = sample_pressure + (Lon*NLAT+Lat); temp_out = sample_temp + (Lon*NLON+Lat)/4; % % for the units attributes PRES_UNITS='hPa'; TEMP_UNITS='celsius'; LAT_UNITS='degrees_north'; LON_UNITS='degrees_east'; % % We will use mexnc to inquire about the number of variables, % dimensions, global attributes, etc. We could use SNCTOOLS/nc_info % to indirectly do many of the same things, though. [ncid, status] = mexnc ( 'OPEN', ncfile, nc_nowrite_mode ); if status, error(mexnc('STRERROR',status)), end % % There are a number of inquiry functions in netCDF which can be used % to learn about an unknown netCDF file. NC_INQ tells how many netCDF % variables, dimensions, and global attributes are in the file; also the % dimension id of the unlimited dimension, if there is one. [ndims_in, nvars_in, ngatts_in, unlimdimid_in, status] = mexnc ( 'INQ', ncid ); if status, error(mexnc('STRERROR',status)), end % % In this case we know that there are 2 netCDF dimensions, 4 % netCDF variables, no global attributes, and no unlimited % dimension. */ if (ndims_in ~= 2 || nvars_in ~= 4 || ngatts_in ~= 0 || unlimdimid_in ~= -1) error ( 'mexnc:inq did not return the right information!\n' ); end status = mexnc ( 'CLOSE', ncid ); if status, error(mexnc('STRERROR',status)), end % % Read the coordinate variable data. lats_in = nc_varget ( ncfile, 'latitude' ); lons_in = nc_varget ( ncfile, 'longitude' ); if any(lats_in - lats) error ( 'latitude did not match\n' ); end if any(lons_in - lons) error ( 'longitude did not match\n' ); end % % Read the data. Since we know the contents of the file we know % that the data arrays in this program are the correct size to % hold all the data. pres_in = nc_varget ( ncfile, 'pressure' ); temp_in = nc_varget ( ncfile, 'temperature' ); % % Check the data. if any(pres_in - pres_out) error ( 'pressure did not match\n' ); end if any(temp_in - temp_out) error ( 'temperature did not match\n' ); end % % Each of the netCDF variables has a "units" attribute. Let's read % them and check them. lat_units_in = nc_attget ( ncfile, 'latitude', 'units' ); lon_units_in = nc_attget ( ncfile, 'longitude', 'units' ); pres_units_in = nc_attget ( ncfile, 'pressure', 'units' ); temp_units_in = nc_attget ( ncfile, 'temperature', 'units' ); if ~strcmp ( lat_units_in, LAT_UNITS ) error ( 'latitude units did not match\n' ); end if ~strcmp ( lon_units_in, LON_UNITS ) error ( 'longitude units did not match\n' ); end if ~strcmp ( temp_units_in, TEMP_UNITS ) error ( 'temperature units did not match\n' ); end if ~strcmp ( pres_units_in, PRES_UNITS ) error ( 'pressure units did not match\n' ); end fprintf(1, '*** SUCCESS reading example file sfc_pres_temp.nc!\n');