Home > fvcom_prepro > write_FVCOM_Gridded_nc.m

write_FVCOM_Gridded_nc

PURPOSE ^

Write data out to FVCOM NetCDF heating file (for HEATING_CALCULATED).

SYNOPSIS ^

function write_FVCOM_Gridded_nc(Mobj, fileprefix, data)

DESCRIPTION ^

 Write data out to FVCOM NetCDF heating file (for HEATING_CALCULATED).

 write_FVCOM_Gridded_nc(Mobj, fileprefix, data)

 DESCRIPTION:
   Takes the given interpolated data (e.g. from grid2fvcom) and writes out
   to a NetCDF file.

 INPUT:
   Mobj - MATLAB mesh object containing fields:
       tri - triangulation table for the unstructured grid
       nVerts - number of grid vertices (nodes)
       nElems - number of grid elements
       nativeCoords - model coordinate type ('cartesian' or 'spherical')
       x, y or lon, lat - node positions (depending on nativeCoords value)
   fileprefix - Output NetCDF file prefix (plus path) will be
       fileprefix_{wnd,hfx,evap}.nc if fver is '3.1.0', otherwise output
       files will be fileprefix_wnd.nc.
   data - Struct of the data to be written out.

 The fields in data may be called any of:
     - 'slp' or 'pres'     - sea level pressure
     - 'rhum'              - relative humidity
     - 'dlwrf'             - downward longwave radiation
     - 'dswrf'             - downward shortwave radiation
     - 'air'               - air temperature
     - 'lon'               - longitude (vector)
     - 'lat'               - latitude (vector)
     - 'x'                 - eastings (vector)
     - 'y'                 - northings (vector)

 OUTPUT:
   FVCOM heating NetCDF file.

 EXAMPLE USAGE:
   heatBase = '/path/to/output/casename_wnd.nc';
   write_FVCOM_heating(Mobj, heatBase, data);

 Author(s):
   Pierre Cazenave (Plymouth Marine Laboratory)

   2013-07-18 - First version based on write_FVCOM_heating and PML
   python's code XXXXXX

==========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_FVCOM_Gridded_nc(Mobj, fileprefix, data)
0002 % Write data out to FVCOM NetCDF heating file (for HEATING_CALCULATED).
0003 %
0004 % write_FVCOM_Gridded_nc(Mobj, fileprefix, data)
0005 %
0006 % DESCRIPTION:
0007 %   Takes the given interpolated data (e.g. from grid2fvcom) and writes out
0008 %   to a NetCDF file.
0009 %
0010 % INPUT:
0011 %   Mobj - MATLAB mesh object containing fields:
0012 %       tri - triangulation table for the unstructured grid
0013 %       nVerts - number of grid vertices (nodes)
0014 %       nElems - number of grid elements
0015 %       nativeCoords - model coordinate type ('cartesian' or 'spherical')
0016 %       x, y or lon, lat - node positions (depending on nativeCoords value)
0017 %   fileprefix - Output NetCDF file prefix (plus path) will be
0018 %       fileprefix_{wnd,hfx,evap}.nc if fver is '3.1.0', otherwise output
0019 %       files will be fileprefix_wnd.nc.
0020 %   data - Struct of the data to be written out.
0021 %
0022 % The fields in data may be called any of:
0023 %     - 'slp' or 'pres'     - sea level pressure
0024 %     - 'rhum'              - relative humidity
0025 %     - 'dlwrf'             - downward longwave radiation
0026 %     - 'dswrf'             - downward shortwave radiation
0027 %     - 'air'               - air temperature
0028 %     - 'lon'               - longitude (vector)
0029 %     - 'lat'               - latitude (vector)
0030 %     - 'x'                 - eastings (vector)
0031 %     - 'y'                 - northings (vector)
0032 %
0033 % OUTPUT:
0034 %   FVCOM heating NetCDF file.
0035 %
0036 % EXAMPLE USAGE:
0037 %   heatBase = '/path/to/output/casename_wnd.nc';
0038 %   write_FVCOM_heating(Mobj, heatBase, data);
0039 %
0040 % Author(s):
0041 %   Pierre Cazenave (Plymouth Marine Laboratory)
0042 %
0043 %   2013-07-18 - First version based on write_FVCOM_heating and PML
0044 %   python's code XXXXXX
0045 %
0046 %==========================================================================
0047 
0048 assert(nargin == 3, 'Incorrect number of arguments')
0049 
0050 subname = 'write_FVCOM_heating';
0051 
0052 global ftbverbose
0053 if ftbverbose
0054     fprintf('\nbegin : %s \n', subname)
0055 end
0056 
0057 tri = Mobj.tri;
0058 nNodes = Mobj.nVerts;
0059 nElems = Mobj.nElems;
0060 ntimes = numel(data.time);
0061 
0062 if strcmpi(Mobj.nativeCoords, 'cartesian')
0063     x = Mobj.x;
0064     y = Mobj.y;
0065 else
0066     x = Mobj.lon;
0067     y = Mobj.lat;
0068     % Make the range of lon 0-360
0069     x(x < 0) = x(x < 0) + 360;
0070 end
0071 % Create a string for each variable's coordinate attribute
0072 coordString = sprintf('FVCOM %s coordinates', Mobj.nativeCoords);
0073 
0074 % Create element vertices positions
0075 xc = nodes2elems(x, Mobj);
0076 yc = nodes2elems(y, Mobj);
0077 
0078 %--------------------------------------------------------------------------
0079 % Create the NetCDF header for the FVCOM forcing file
0080 %--------------------------------------------------------------------------
0081 
0082 nc = netcdf.create([fileprefix, '_hfx.nc'], 'clobber');
0083 
0084 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'title', 'FVCOM Forcing File')
0085 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'institution', 'Plymouth Marine Laboratory')
0086 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'source', 'FVCOM grid (unstructured) surface forcing')
0087 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'history', ['File created on ', datestr(now, 'yyyy-mm-dd HH:MM:SS'), ' with write_FVCOM_forcing.m from the MATLAB fvcom-toolbox (https://github.com/pwcazenave/fvcom-toolbox)'])
0088 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'references', 'http://fvcom.smast.umassd.edu, http://codfish.smast.umassd.edu')
0089 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'Conventions', 'CF-1.0')
0090 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'CoordinateSystem', Mobj.nativeCoords)
0091 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'CoordinateProjection', 'init=epsg:4326') % WGS84?
0092 
0093 % Dimensions
0094 nele_dimid=netcdf.defDim(nc, 'nele', nElems);
0095 node_dimid=netcdf.defDim(nc, 'node', nNodes);
0096 three_dimid=netcdf.defDim(nc, 'three', 3);
0097 time_dimid=netcdf.defDim(nc, 'time', netcdf.getConstant('NC_UNLIMITED'));
0098 datestrlen_dimid=netcdf.defDim(nc, 'DateStrLen', 26);
0099 
0100 % Space variables
0101 if strcmpi(Mobj.nativeCoords, 'cartesian')
0102     x_varid=netcdf.defVar(nc, 'x', 'NC_FLOAT', node_dimid);
0103     netcdf.putAtt(nc, x_varid, 'long_name', 'nodal x-coordinate');
0104     netcdf.putAtt(nc, x_varid, 'units', 'meters');
0105 
0106     y_varid=netcdf.defVar(nc, 'y', 'NC_FLOAT', node_dimid);
0107     netcdf.putAtt(nc, y_varid, 'long_name', 'nodal y-coordinate');
0108     netcdf.putAtt(nc, y_varid, 'units', 'meters');
0109 
0110     xc_varid=netcdf.defVar(nc, 'xc','NC_FLOAT', nele_dimid);
0111     netcdf.putAtt(nc, xc_varid, 'long_name', 'zonal x-coordinate');
0112     netcdf.putAtt(nc, xc_varid, 'units', 'meters');
0113 
0114     yc_varid=netcdf.defVar(nc, 'yc','NC_FLOAT', nele_dimid);
0115     netcdf.putAtt(nc, yc_varid, 'long_name', 'zonal y-coordinate');
0116     netcdf.putAtt(nc, yc_varid, 'units', 'meters');
0117 
0118 elseif strcmpi(Mobj.nativeCoords, 'spherical')
0119     x_varid=netcdf.defVar(nc, 'lon','NC_FLOAT', node_dimid);
0120     netcdf.putAtt(nc, x_varid, 'long_name', 'nodal longitude');
0121     netcdf.putAtt(nc, x_varid, 'units', 'degrees_east');
0122 
0123     y_varid = netcdf.defVar(nc, 'lat', 'NC_FLOAT', node_dimid);
0124     netcdf.putAtt(nc, y_varid, 'long_name', 'nodal latitude');
0125     netcdf.putAtt(nc, y_varid, 'units', 'degrees_north');
0126 
0127     xc_varid = netcdf.defVar(nc, 'lonc', 'NC_FLOAT', nele_dimid);
0128     netcdf.putAtt(nc, xc_varid, 'long_name', 'zonal longitude');
0129     netcdf.putAtt(nc, xc_varid, 'units', 'degrees_east');
0130 
0131     yc_varid = netcdf.defVar(nc, 'latc', 'NC_FLOAT', nele_dimid);
0132     netcdf.putAtt(nc, yc_varid, 'long_name', 'zonal latitude');
0133     netcdf.putAtt(nc, yc_varid, 'units', 'degrees_north');   
0134 else
0135     error('Unknown coordinate type (%s)', Mobj.nativeCoords)
0136 end
0137 
0138 nv_varid=netcdf.defVar(nc, 'nv', 'NC_INT', [nele_dimid, three_dimid]);
0139 netcdf.putAtt(nc, nv_varid, 'long_name', 'nodes surrounding element');
0140 
0141 % Time variables
0142 time_varid=netcdf.defVar(nc, 'time', 'NC_FLOAT', time_dimid);
0143 netcdf.putAtt(nc, time_varid, 'long_name', 'time');
0144 netcdf.putAtt(nc, time_varid, 'units', 'days since 1858-11-17 00:00:00');
0145 netcdf.putAtt(nc, time_varid, 'format', 'modified julian day (MJD)');
0146 netcdf.putAtt(nc, time_varid, 'time_zone', 'UTC');
0147 
0148 itime_varid=netcdf.defVar(nc, 'Itime', 'NC_INT', time_dimid);
0149 netcdf.putAtt(nc, itime_varid, 'units', 'days since 1858-11-17 00:00:00');
0150 netcdf.putAtt(nc, itime_varid, 'format', 'modified julian day (MJD)');
0151 netcdf.putAtt(nc, itime_varid, 'time_zone', 'UTC');
0152 
0153 itime2_varid=netcdf.defVar(nc, 'Itime2', 'NC_INT', time_dimid);
0154 netcdf.putAtt(nc, itime2_varid, 'units', 'msec since 00:00:00');
0155 netcdf.putAtt(nc, itime2_varid, 'time_zone', 'UTC');
0156 netcdf.putAtt(nc, itime2_varid, 'long_name', 'time');
0157 
0158 times_varid=netcdf.defVar(nc, 'Times', 'NC_CHAR', [datestrlen_dimid, time_dimid]);
0159 netcdf.putAtt(nc, times_varid, 'long_name', 'Calendar Date');
0160 netcdf.putAtt(nc, times_varid, 'format', 'String: Calendar Time');
0161 netcdf.putAtt(nc, times_varid, 'time_zone', 'UTC');
0162 
0163 airt_varid = netcdf.defVar(nc, 'air_temperature', 'NC_FLOAT', [node_dimid, time_dimid]);
0164 netcdf.putAtt(nc, airt_varid, 'long_name', 'Surface air temperature');
0165 netcdf.putAtt(nc, airt_varid, 'units', 'Celsius Degree');
0166 netcdf.putAtt(nc, airt_varid, 'grid', 'fvcom_grid');
0167 netcdf.putAtt(nc, airt_varid, 'coordinates', coordString);
0168 netcdf.putAtt(nc, airt_varid, 'type', 'data');
0169 
0170 rhum_varid = netcdf.defVar(nc, 'relative_humidity', 'NC_FLOAT', [node_dimid, time_dimid]);
0171 netcdf.putAtt(nc, rhum_varid, 'long_name', 'surface air relative humidity');
0172 netcdf.putAtt(nc, rhum_varid, 'units', 'percentage');
0173 netcdf.putAtt(nc, rhum_varid, 'grid', 'fvcom_grid');
0174 netcdf.putAtt(nc, rhum_varid, 'coordinates', coordString);
0175 netcdf.putAtt(nc, rhum_varid, 'type', 'data');
0176 
0177 dlwrf_varid = netcdf.defVar(nc, 'long_wave', 'NC_FLOAT', [node_dimid, time_dimid]);
0178 netcdf.putAtt(nc, dlwrf_varid, 'long_name', 'Downward solar longwave radiation flux');
0179 netcdf.putAtt(nc, dlwrf_varid, 'units', 'Watts meter-2');
0180 netcdf.putAtt(nc, dlwrf_varid, 'grid', 'fvcom_grid');
0181 netcdf.putAtt(nc, dlwrf_varid, 'coordinates', coordString);
0182 netcdf.putAtt(nc, dlwrf_varid, 'type', 'data');
0183 
0184 dswrf_varid = netcdf.defVar(nc, 'short_wave', 'NC_FLOAT', [node_dimid, time_dimid]);
0185 netcdf.putAtt(nc, dswrf_varid, 'long_name', 'Downward solar shortwave radiation flux');
0186 netcdf.putAtt(nc, dswrf_varid, 'units', 'Watts meter-2');
0187 netcdf.putAtt(nc, dswrf_varid, 'grid', 'fvcom_grid');
0188 netcdf.putAtt(nc, dswrf_varid, 'coordinates', coordString);
0189 netcdf.putAtt(nc, dswrf_varid, 'type', 'data');
0190 
0191 slp_varid = netcdf.defVar(nc, 'air_pressure', 'NC_FLOAT', [node_dimid, time_dimid]);
0192 netcdf.putAtt(nc, slp_varid, 'long_name', 'Surface air pressure');
0193 netcdf.putAtt(nc, slp_varid, 'units', 'Pa');
0194 netcdf.putAtt(nc, slp_varid, 'grid', 'fvcom_grid');
0195 netcdf.putAtt(nc, slp_varid, 'coordinates', coordString);
0196 netcdf.putAtt(nc, slp_varid, 'type', 'data');
0197 
0198 % End definitions
0199 netcdf.endDef(nc);
0200 
0201 %--------------------------------------------------------------------------
0202 % Put the data in the NetCDF file.
0203 %--------------------------------------------------------------------------
0204 netcdf.putVar(nc, nv_varid, tri');
0205 netcdf.putVar(nc, time_varid, 0, ntimes, data.time);
0206 netcdf.putVar(nc, itime_varid, 0, ntimes, floor(data.time));
0207 netcdf.putVar(nc, itime2_varid, 0, ntimes, mod(data.time, 1) * 24 * 3600 * 1000);
0208 netcdf.putVar(nc, x_varid, x);
0209 netcdf.putVar(nc, y_varid, y);
0210 netcdf.putVar(nc, xc_varid, xc);
0211 netcdf.putVar(nc, yc_varid, yc);
0212 netcdf.putVar(nc, airt_varid, data.air.node);
0213 netcdf.putVar(nc, rhum_varid, data.rhum.node);
0214 try
0215     % NCEP
0216     netcdf.putVar(nc, dlwrf_varid, data.dlwrf.node);
0217 catch
0218     % Met Office Unified Model
0219     netcdf.putVar(nc, dlwrf_varid, data.nlwrf.node);
0220 end
0221 try
0222     % NCEP
0223     netcdf.putVar(nc, dswrf_varid, data.dswrf.node);
0224 catch
0225     % Met Office Unified Model
0226     netcdf.putVar(nc, dswrf_varid, data.nswrf.node);
0227 end
0228 try % work with both slp and pres data.
0229     netcdf.putVar(nc, slp_varid, data.slp.node);
0230 catch
0231     netcdf.putVar(nc, slp_varid, data.pres.node);
0232 end
0233 
0234 % Build the Times string and output to NetCDF.
0235 nStringOut = char();
0236 [nYr, nMon, nDay, nHour, nMin, nSec] = mjulian2greg(data.time);
0237 for tt=1:ntimes
0238     nDate = [nYr(tt), nMon(tt), nDay(tt), nHour(tt), nMin(tt), nSec(tt)];
0239     nStringOut = [nStringOut, sprintf('%04i/%02i/%02i %02i:%02i:%02i       ', nDate)];
0240 end
0241 netcdf.putVar(nc, times_varid, nStringOut);
0242 
0243 % Close the NetCDF file(s)
0244 netcdf.close(nc);
0245 
0246 if ftbverbose
0247     fprintf('end   : %s \n', subname)
0248 end

Generated on Tue 29-Jul-2014 15:11:16 by m2html © 2005