Home > fvcom_prepro > write_FVCOM_heating.m

write_FVCOM_heating

PURPOSE ^

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

SYNOPSIS ^

function write_FVCOM_heating(Mobj, fileprefix, data)

DESCRIPTION ^

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

 write_FVCOM_heating(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_forcing.m.

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_FVCOM_heating(Mobj, fileprefix, data)
0002 % Write data out to FVCOM NetCDF heating file (for HEATING_CALCULATED).
0003 %
0004 % write_FVCOM_heating(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_forcing.m.
0044 %
0045 %==========================================================================
0046 
0047 assert(nargin == 3, 'Incorrect number of arguments')
0048 
0049 subname = 'write_FVCOM_heating';
0050 
0051 global ftbverbose
0052 if ftbverbose
0053     fprintf('\nbegin : %s \n', subname)
0054 end
0055 
0056 tri = Mobj.tri;
0057 nNodes = Mobj.nVerts;
0058 nElems = Mobj.nElems;
0059 ntimes = numel(data.time);
0060 
0061 if strcmpi(Mobj.nativeCoords, 'cartesian')
0062     x = Mobj.x;
0063     y = Mobj.y;
0064 else
0065     x = Mobj.lon;
0066     y = Mobj.lat;
0067     % Make the range of lon 0-360
0068     x(x < 0) = x(x < 0) + 360;
0069 end
0070 % Create a string for each variable's coordinate attribute
0071 coordString = sprintf('FVCOM %s coordinates', Mobj.nativeCoords);
0072 
0073 % Create element vertices positions
0074 xc = nodes2elems(x, Mobj);
0075 yc = nodes2elems(y, Mobj);
0076 
0077 %--------------------------------------------------------------------------
0078 % Create the NetCDF header for the FVCOM forcing file
0079 %--------------------------------------------------------------------------
0080 
0081 nc = netcdf.create([fileprefix, '_hfx.nc'], 'clobber');
0082 
0083 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'title', 'FVCOM Forcing File')
0084 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'institution', 'Plymouth Marine Laboratory')
0085 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'source', 'FVCOM grid (unstructured) surface forcing')
0086 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'history', sprintf('File created with %s from the MATLAB fvcom-toolbox', subname))
0087 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'references', 'http://fvcom.smast.umassd.edu, http://codfish.smast.umassd.edu')
0088 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'Conventions', 'CF-1.0')
0089 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'CoordinateSystem', Mobj.nativeCoords)
0090 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'CoordinateProjection', 'init=epsg:4326') % WGS84?
0091 
0092 % Dimensions
0093 nele_dimid=netcdf.defDim(nc, 'nele', nElems);
0094 node_dimid=netcdf.defDim(nc, 'node', nNodes);
0095 three_dimid=netcdf.defDim(nc, 'three', 3);
0096 time_dimid=netcdf.defDim(nc, 'time', netcdf.getConstant('NC_UNLIMITED'));
0097 datestrlen_dimid=netcdf.defDim(nc, 'DateStrLen', 26);
0098 
0099 % Space variables
0100 if strcmpi(Mobj.nativeCoords, 'cartesian')
0101     x_varid=netcdf.defVar(nc, 'x', 'NC_FLOAT', node_dimid);
0102     netcdf.putAtt(nc, x_varid, 'long_name', 'nodal x-coordinate');
0103     netcdf.putAtt(nc, x_varid, 'units', 'meters');
0104 
0105     y_varid=netcdf.defVar(nc, 'y', 'NC_FLOAT', node_dimid);
0106     netcdf.putAtt(nc, y_varid, 'long_name', 'nodal y-coordinate');
0107     netcdf.putAtt(nc, y_varid, 'units', 'meters');
0108 
0109     xc_varid=netcdf.defVar(nc, 'xc','NC_FLOAT', nele_dimid);
0110     netcdf.putAtt(nc, xc_varid, 'long_name', 'zonal x-coordinate');
0111     netcdf.putAtt(nc, xc_varid, 'units', 'meters');
0112 
0113     yc_varid=netcdf.defVar(nc, 'yc','NC_FLOAT', nele_dimid);
0114     netcdf.putAtt(nc, yc_varid, 'long_name', 'zonal y-coordinate');
0115     netcdf.putAtt(nc, yc_varid, 'units', 'meters');
0116 
0117 elseif strcmpi(Mobj.nativeCoords, 'spherical')
0118     x_varid=netcdf.defVar(nc, 'lon','NC_FLOAT', node_dimid);
0119     netcdf.putAtt(nc, x_varid, 'long_name', 'nodal longitude');
0120     netcdf.putAtt(nc, x_varid, 'units', 'degrees_east');
0121 
0122     y_varid = netcdf.defVar(nc, 'lat', 'NC_FLOAT', node_dimid);
0123     netcdf.putAtt(nc, y_varid, 'long_name', 'nodal latitude');
0124     netcdf.putAtt(nc, y_varid, 'units', 'degrees_north');
0125 
0126     xc_varid = netcdf.defVar(nc, 'lonc', 'NC_FLOAT', nele_dimid);
0127     netcdf.putAtt(nc, xc_varid, 'long_name', 'zonal longitude');
0128     netcdf.putAtt(nc, xc_varid, 'units', 'degrees_east');
0129 
0130     yc_varid = netcdf.defVar(nc, 'latc', 'NC_FLOAT', nele_dimid);
0131     netcdf.putAtt(nc, yc_varid, 'long_name', 'zonal latitude');
0132     netcdf.putAtt(nc, yc_varid, 'units', 'degrees_north');   
0133 else
0134     error('Unknown coordinate type (%s)', Mobj.nativeCoords)
0135 end
0136 
0137 nv_varid=netcdf.defVar(nc, 'nv', 'NC_INT', [nele_dimid, three_dimid]);
0138 netcdf.putAtt(nc, nv_varid, 'long_name', 'nodes surrounding element');
0139 
0140 % Time variables
0141 time_varid=netcdf.defVar(nc, 'time', 'NC_FLOAT', time_dimid);
0142 netcdf.putAtt(nc, time_varid, 'long_name', 'time');
0143 netcdf.putAtt(nc, time_varid, 'units', 'days since 1858-11-17 00:00:00');
0144 netcdf.putAtt(nc, time_varid, 'format', 'modified julian day (MJD)');
0145 netcdf.putAtt(nc, time_varid, 'time_zone', 'UTC');
0146 
0147 itime_varid=netcdf.defVar(nc, 'Itime', 'NC_INT', time_dimid);
0148 netcdf.putAtt(nc, itime_varid, 'units', 'days since 1858-11-17 00:00:00');
0149 netcdf.putAtt(nc, itime_varid, 'format', 'modified julian day (MJD)');
0150 netcdf.putAtt(nc, itime_varid, 'time_zone', 'UTC');
0151 
0152 itime2_varid=netcdf.defVar(nc, 'Itime2', 'NC_INT', time_dimid);
0153 netcdf.putAtt(nc, itime2_varid, 'units', 'msec since 00:00:00');
0154 netcdf.putAtt(nc, itime2_varid, 'time_zone', 'UTC');
0155 netcdf.putAtt(nc, itime2_varid, 'long_name', 'time');
0156 
0157 times_varid=netcdf.defVar(nc, 'Times', 'NC_CHAR', [datestrlen_dimid, time_dimid]);
0158 netcdf.putAtt(nc, times_varid, 'long_name', 'Calendar Date');
0159 netcdf.putAtt(nc, times_varid, 'format', 'String: Calendar Time');
0160 netcdf.putAtt(nc, times_varid, 'time_zone', 'UTC');
0161 
0162 airt_varid = netcdf.defVar(nc, 'air_temperature', 'NC_FLOAT', [node_dimid, time_dimid]);
0163 netcdf.putAtt(nc, airt_varid, 'long_name', 'Surface air temperature');
0164 netcdf.putAtt(nc, airt_varid, 'units', 'Celsius Degree');
0165 netcdf.putAtt(nc, airt_varid, 'grid', 'fvcom_grid');
0166 netcdf.putAtt(nc, airt_varid, 'coordinates', coordString);
0167 netcdf.putAtt(nc, airt_varid, 'type', 'data');
0168 
0169 rhum_varid = netcdf.defVar(nc, 'relative_humidity', 'NC_FLOAT', [node_dimid, time_dimid]);
0170 netcdf.putAtt(nc, rhum_varid, 'long_name', 'surface air relative humidity');
0171 netcdf.putAtt(nc, rhum_varid, 'units', 'percentage');
0172 netcdf.putAtt(nc, rhum_varid, 'grid', 'fvcom_grid');
0173 netcdf.putAtt(nc, rhum_varid, 'coordinates', coordString);
0174 netcdf.putAtt(nc, rhum_varid, 'type', 'data');
0175 
0176 dlwrf_varid = netcdf.defVar(nc, 'long_wave', 'NC_FLOAT', [node_dimid, time_dimid]);
0177 netcdf.putAtt(nc, dlwrf_varid, 'long_name', 'Downward solar longwave radiation flux');
0178 netcdf.putAtt(nc, dlwrf_varid, 'units', 'Watts meter-2');
0179 netcdf.putAtt(nc, dlwrf_varid, 'grid', 'fvcom_grid');
0180 netcdf.putAtt(nc, dlwrf_varid, 'coordinates', coordString);
0181 netcdf.putAtt(nc, dlwrf_varid, 'type', 'data');
0182 
0183 dswrf_varid = netcdf.defVar(nc, 'short_wave', 'NC_FLOAT', [node_dimid, time_dimid]);
0184 netcdf.putAtt(nc, dswrf_varid, 'long_name', 'Downward solar shortwave radiation flux');
0185 netcdf.putAtt(nc, dswrf_varid, 'units', 'Watts meter-2');
0186 netcdf.putAtt(nc, dswrf_varid, 'grid', 'fvcom_grid');
0187 netcdf.putAtt(nc, dswrf_varid, 'coordinates', coordString);
0188 netcdf.putAtt(nc, dswrf_varid, 'type', 'data');
0189 
0190 slp_varid = netcdf.defVar(nc, 'air_pressure', 'NC_FLOAT', [node_dimid, time_dimid]);
0191 netcdf.putAtt(nc, slp_varid, 'long_name', 'Surface air pressure');
0192 netcdf.putAtt(nc, slp_varid, 'units', 'Pa');
0193 netcdf.putAtt(nc, slp_varid, 'grid', 'fvcom_grid');
0194 netcdf.putAtt(nc, slp_varid, 'coordinates', coordString);
0195 netcdf.putAtt(nc, slp_varid, 'type', 'data');
0196 
0197 % End definitions
0198 netcdf.endDef(nc);
0199 
0200 %--------------------------------------------------------------------------
0201 % Put the data in the NetCDF file.
0202 %--------------------------------------------------------------------------
0203 netcdf.putVar(nc, nv_varid, tri');
0204 netcdf.putVar(nc, time_varid, 0, ntimes, data.time);
0205 netcdf.putVar(nc, itime_varid, 0, ntimes, floor(data.time));
0206 netcdf.putVar(nc, itime2_varid, 0, ntimes, mod(data.time, 1) * 24 * 3600 * 1000);
0207 netcdf.putVar(nc, x_varid, x);
0208 netcdf.putVar(nc, y_varid, y);
0209 netcdf.putVar(nc, xc_varid, xc);
0210 netcdf.putVar(nc, yc_varid, yc);
0211 netcdf.putVar(nc, airt_varid, data.air.node);
0212 netcdf.putVar(nc, rhum_varid, data.rhum.node);
0213 try
0214     % NCEP
0215     netcdf.putVar(nc, dlwrf_varid, data.dlwrf.node);
0216 catch
0217     % Met Office Unified Model
0218     netcdf.putVar(nc, dlwrf_varid, data.nlwrf.node);
0219 end
0220 try
0221     % NCEP
0222     netcdf.putVar(nc, dswrf_varid, data.dswrf.node);
0223 catch
0224     % Met Office Unified Model
0225     netcdf.putVar(nc, dswrf_varid, data.nswrf.node);
0226 end
0227 try % work with both slp and pres data.
0228     netcdf.putVar(nc, slp_varid, data.slp.node);
0229 catch
0230     netcdf.putVar(nc, slp_varid, data.pres.node);
0231 end
0232 
0233 % Build the Times string and output to NetCDF.
0234 nStringOut = char();
0235 [nYr, nMon, nDay, nHour, nMin, nSec] = mjulian2greg(data.time);
0236 for tt=1:ntimes
0237     nDate = [nYr(tt), nMon(tt), nDay(tt), nHour(tt), nMin(tt), nSec(tt)];
0238     nStringOut = [nStringOut, sprintf('%04i/%02i/%02i %02i:%02i:%02i       ', nDate)];
0239 end
0240 netcdf.putVar(nc, times_varid, nStringOut);
0241 
0242 % Close the NetCDF file(s)
0243 netcdf.close(nc);
0244 
0245 if ftbverbose
0246     fprintf('end   : %s \n', subname)
0247 end

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005