Home > fvcom_prepro > write_FVCOM_groundwater.m

write_FVCOM_groundwater

PURPOSE ^

Write an FVCOM groundwater time series forcing file.

SYNOPSIS ^

function write_FVCOM_groundwater(Mobj, grndwtr_file, varargin)

DESCRIPTION ^

 Write an FVCOM groundwater time series forcing file.

 write_FVCOM_groundwater(Mobj, grndwtr_file, varargin)

 DESCRIPTION:
   Write an FVCOM groundwater time series forcing file.

 INPUT:
   Mobj = Matlab mesh object with fields:
       groundwater - struct with the following fields (sizes of arrays in
       square brackets):
           - times - Modified Julian Days of the groundwater input.
           - flux - Ground water Volume flux (m^{3}s^{-1}) [nodes, time].
           - temp - Ground water temperature (Celsius) [nodes, time].
           - salt - Ground water salinity (PSU) [nodes, time].
   grndwtr_file = name of netCDF file.

   Optional keyword-argument pairs. These control the time variables. This
   script defaults to writing 'Times' only.
   FVCOM needs only one of:
       1. Times: character string of times
       2. Itime and Itime2: integer days and milliseconds since midnight
       3. time: float days.
   FVCOM checks for these in the order above and this script defaults to
   writing Times only. Adjust the keyword-argument pairs to your liking:

   'strtime' = set to true to output the 'Times' variable
   'inttime' = set to true to output the 'Itime' and 'Itime2' variables
   'floattime' = set to true to output the 'time' variable

 OUTPUT:
   grndwtr_file = a netCDF FVCOM surface elevations tide forcing file.

 EXAMPLE USAGE
   With default settings:
       write_FVCOM_groundwater(Mobj, '/tmp/grndwtr.nc)
   Enable the 'time' variable in the netCDF.
       write_FVCOM_groundwater(Mobj, '/tmp/grndwtr.nc, ...
           'floattime', true)

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

 Revision history
   2015-11-18 First version.
   2016-08-08 Added help which actually refers to this script's function
   rather than the template upon which it was based.

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_FVCOM_groundwater(Mobj, grndwtr_file, varargin)
0002 % Write an FVCOM groundwater time series forcing file.
0003 %
0004 % write_FVCOM_groundwater(Mobj, grndwtr_file, varargin)
0005 %
0006 % DESCRIPTION:
0007 %   Write an FVCOM groundwater time series forcing file.
0008 %
0009 % INPUT:
0010 %   Mobj = Matlab mesh object with fields:
0011 %       groundwater - struct with the following fields (sizes of arrays in
0012 %       square brackets):
0013 %           - times - Modified Julian Days of the groundwater input.
0014 %           - flux - Ground water Volume flux (m^{3}s^{-1}) [nodes, time].
0015 %           - temp - Ground water temperature (Celsius) [nodes, time].
0016 %           - salt - Ground water salinity (PSU) [nodes, time].
0017 %   grndwtr_file = name of netCDF file.
0018 %
0019 %   Optional keyword-argument pairs. These control the time variables. This
0020 %   script defaults to writing 'Times' only.
0021 %   FVCOM needs only one of:
0022 %       1. Times: character string of times
0023 %       2. Itime and Itime2: integer days and milliseconds since midnight
0024 %       3. time: float days.
0025 %   FVCOM checks for these in the order above and this script defaults to
0026 %   writing Times only. Adjust the keyword-argument pairs to your liking:
0027 %
0028 %   'strtime' = set to true to output the 'Times' variable
0029 %   'inttime' = set to true to output the 'Itime' and 'Itime2' variables
0030 %   'floattime' = set to true to output the 'time' variable
0031 %
0032 % OUTPUT:
0033 %   grndwtr_file = a netCDF FVCOM surface elevations tide forcing file.
0034 %
0035 % EXAMPLE USAGE
0036 %   With default settings:
0037 %       write_FVCOM_groundwater(Mobj, '/tmp/grndwtr.nc)
0038 %   Enable the 'time' variable in the netCDF.
0039 %       write_FVCOM_groundwater(Mobj, '/tmp/grndwtr.nc, ...
0040 %           'floattime', true)
0041 %
0042 % Author(s):
0043 %   Pierre Cazenave (Plymouth Marine Laboratory)
0044 %
0045 % Revision history
0046 %   2015-11-18 First version.
0047 %   2016-08-08 Added help which actually refers to this script's function
0048 %   rather than the template upon which it was based.
0049 %
0050 %==========================================================================
0051 
0052 global ftbverbose
0053 
0054 subname = 'write_FVCOM_groundwater';
0055 if ftbverbose
0056     fprintf('\nbegin : %s \n', subname);
0057 end
0058 
0059 % Default to string times as FVCOM looks for these first.
0060 strtime = true;
0061 inttime = false;
0062 floattime = false;
0063 for vv = 1:2:length(varargin)
0064     switch varargin{vv}
0065         case 'strtime'
0066             strtime = true;
0067         case 'inttime'
0068             inttime = true;
0069         case 'floattime'
0070             floattime = true;
0071     end
0072 end
0073 
0074 %%
0075 %--------------------------------------------------------------------------
0076 % Dump the file
0077 %--------------------------------------------------------------------------
0078 
0079 nc = netcdf.create(grndwtr_file, 'clobber');
0080 
0081 % Define global attributes
0082 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'type', 'FVCOM GROUNDWATER FORCING FILE')
0083 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'source', 'FVCOM grid (unstructured) surface forcing')
0084 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'history', sprintf('File created with %s from the MATLAB fvcom-toolbox', subname))
0085 
0086 % Dfine dimensions
0087 node_dimid = netcdf.defDim(nc, 'node', Mobj.nVerts);
0088 % Although the nele dimension isn't used, FVCOM checks for its existence
0089 % and FATAL_ERRORs if it's missing, so we need to add it here.
0090 nele_dimid = netcdf.defDim(nc, 'nele', Mobj.nElems); %#ok<NASGU>
0091 time_dimid = netcdf.defDim(nc, 'time', netcdf.getConstant('NC_UNLIMITED'));
0092 date_str_len_dimid = netcdf.defDim(nc, 'DateStrLen', 26);
0093 
0094 % Define variables and attributes
0095 gflux_varid = netcdf.defVar(nc, 'groundwater_flux', 'NC_FLOAT', [node_dimid, time_dimid]);
0096 netcdf.putAtt(nc, gflux_varid, 'long_name', 'groundwater volume flux');
0097 netcdf.putAtt(nc, gflux_varid, 'units', 'm3 s-1');
0098 netcdf.putAtt(nc, gflux_varid, 'grid', 'fvcom_grid');
0099 netcdf.putAtt(nc, gflux_varid, 'type', 'data');
0100 
0101 gtemp_varid = netcdf.defVar(nc, 'groundwater_temp', 'NC_FLOAT', [node_dimid, time_dimid]);
0102 netcdf.putAtt(nc, gtemp_varid, 'long_name', 'groundwater inflow temperature');
0103 netcdf.putAtt(nc, gtemp_varid, 'units', 'degrees_C');
0104 netcdf.putAtt(nc, gtemp_varid, 'grid', 'fvcom_grid');
0105 netcdf.putAtt(nc, gtemp_varid, 'type', 'data');
0106 
0107 gsalt_varid = netcdf.defVar(nc, 'groundwater_salt', 'NC_FLOAT', [node_dimid, time_dimid]);
0108 netcdf.putAtt(nc, gsalt_varid, 'long_name', 'groundwater inflow salinity');
0109 netcdf.putAtt(nc, gsalt_varid, 'units', '1e-3');
0110 netcdf.putAtt(nc, gsalt_varid, 'grid', 'fvcom_grid');
0111 netcdf.putAtt(nc, gsalt_varid, 'type', 'data');
0112 
0113 iint_varid = netcdf.defVar(nc, 'iint', 'NC_INT', time_dimid);
0114 netcdf.putAtt(nc, iint_varid, 'long_name', 'internal mode iteration number');
0115 
0116 if floattime
0117     time_varid = netcdf.defVar(nc, 'time', 'NC_FLOAT', time_dimid);
0118     netcdf.putAtt(nc, time_varid, 'long_name', 'time');
0119     netcdf.putAtt(nc, time_varid, 'units', 'days since 1858-11-17 00:00:00');
0120     netcdf.putAtt(nc, time_varid, 'format', 'modified julian day (MJD)');
0121     netcdf.putAtt(nc, time_varid, 'time_zone', 'UTC');
0122 end
0123 
0124 if inttime
0125     itime_varid = netcdf.defVar(nc, 'Itime', 'NC_INT', time_dimid);
0126     netcdf.putAtt(nc, itime_varid, 'units', 'days since 1858-11-17 00:00:00');
0127     netcdf.putAtt(nc, itime_varid, 'format', 'modified julian day (MJD)');
0128     netcdf.putAtt(nc, itime_varid, 'time_zone', 'UTC');
0129 
0130     itime2_varid = netcdf.defVar(nc, 'Itime2', 'NC_INT', time_dimid);
0131     netcdf.putAtt(nc, itime2_varid, 'units', 'msec since 00:00:00');
0132     netcdf.putAtt(nc, itime2_varid, 'time_zone', 'UTC');
0133 end
0134 
0135 if strtime
0136     Times_varid = netcdf.defVar(nc, 'Times', 'NC_CHAR', [date_str_len_dimid, time_dimid]);
0137     netcdf.putAtt(nc, Times_varid, 'time_zone', 'UTC');
0138 end
0139 
0140 % End definitions
0141 netcdf.endDef(nc);
0142 
0143 % Write data
0144 nTimes = length(Mobj.groundwater.times);
0145 netcdf.putVar(nc, iint_varid, 0, nTimes, 1:nTimes);
0146 if floattime
0147     netcdf.putVar(nc, time_varid, 0, nTimes, Mobj.groundwater.times);
0148 end
0149 if inttime
0150     netcdf.putVar(nc, itime_varid, floor(Mobj.groundwater.times));
0151     netcdf.putVar(nc, itime2_varid, 0, nTimes, round(mod(Mobj.groundwater.times, 1) * 24 * 60 * 60 * 1000));
0152 end
0153 if strtime
0154     nStringOut = char();
0155     [nYr, nMon, nDay, nHour, nMin, nSec] = mjulian2greg(Mobj.groundwater.times);
0156     for i = 1:nTimes
0157         nDate = [nYr(i),  nMon(i),  nDay(i),  nHour(i),  nMin(i),  nSec(i)];
0158         nStringOut = [nStringOut, sprintf('%04i/%02i/%02i %02i:%02i:%09.6f',  nDate)];
0159     end
0160     netcdf.putVar(nc, Times_varid, nStringOut);
0161 end
0162 netcdf.putVar(nc, gflux_varid, Mobj.groundwater.flux);
0163 netcdf.putVar(nc, gtemp_varid, Mobj.groundwater.temp);
0164 netcdf.putVar(nc, gsalt_varid, Mobj.groundwater.salt);
0165 
0166 % Close file
0167 netcdf.close(nc);
0168 
0169 if ftbverbose
0170     fprintf('end   : %s \n',  subname)
0171 end

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