Home > fvcom_prepro > write_FVCOM_sediment.m

write_FVCOM_sediment

PURPOSE ^

Write spatially-varying sediment threshold data.

SYNOPSIS ^

function write_FVCOM_sediment(sediment, filename)

DESCRIPTION ^

 Write spatially-varying sediment threshold data.

 function write_FVCOM_sediment(sediment, filename)

 DESCRIPTION:
    Generate a netCDF file containing spatially variable sediment propoerties
    for FVCOM

 INPUT
   sediment  = struct with nodal data in the following mandatory fields:
               - tau_ce
               - tau_cd (this appears to be unused but required at launch)
               - erate
               and the following optional field(s):
               - <sediment_class>_bedfrac
                If the optional field(s) is included, the namelist option
                SEDIMENT_PARAMETER_TYPE = "non-uniform" can be used.
   filename  = filename to which to write the outputs.

 OUTPUT:
    netCDF file: filename of the output netCDF file to generate.

 EXAMPLE USAGE
    write_FVCOM_sediment(sediment, 'tst_sediment.nc')

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

 Revision history
    2017-03-27 New function to create a netCDF for the SEDIMENT_PARAMETER_FILE
    section in the model namelist.

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_FVCOM_sediment(sediment, filename)
0002 % Write spatially-varying sediment threshold data.
0003 %
0004 % function write_FVCOM_sediment(sediment, filename)
0005 %
0006 % DESCRIPTION:
0007 %    Generate a netCDF file containing spatially variable sediment propoerties
0008 %    for FVCOM
0009 %
0010 % INPUT
0011 %   sediment  = struct with nodal data in the following mandatory fields:
0012 %               - tau_ce
0013 %               - tau_cd (this appears to be unused but required at launch)
0014 %               - erate
0015 %               and the following optional field(s):
0016 %               - <sediment_class>_bedfrac
0017 %                If the optional field(s) is included, the namelist option
0018 %                SEDIMENT_PARAMETER_TYPE = "non-uniform" can be used.
0019 %   filename  = filename to which to write the outputs.
0020 %
0021 % OUTPUT:
0022 %    netCDF file: filename of the output netCDF file to generate.
0023 %
0024 % EXAMPLE USAGE
0025 %    write_FVCOM_sediment(sediment, 'tst_sediment.nc')
0026 %
0027 % Author(s):
0028 %    Pierre Cazenave (Plymouth Marine Laboratory)
0029 %
0030 % Revision history
0031 %    2017-03-27 New function to create a netCDF for the SEDIMENT_PARAMETER_FILE
0032 %    section in the model namelist.
0033 %
0034 %==========================================================================
0035 
0036 global ftbverbose
0037 [~, subname] = fileparts(mfilename('fullpath'));
0038 if ftbverbose
0039     fprintf('\nbegin : %s\n', subname)
0040 end
0041 
0042 %--------------------------------------------------------------------------
0043 % Check inputs.
0044 %--------------------------------------------------------------------------
0045 assert(exist('sediment', 'var') == 1, 'incorrect usage of %s, must provide sediment struct', subname)
0046 assert(exist('filename', 'var') == 1, 'incorrect usage of %s, must provide ''filename''', subname)
0047 
0048 % Check dimensions
0049 nVerts = numel(sediment.tau_cd);
0050 assert(nVerts ~= 0, 'dimension of sediment data is 0, something is wrong')
0051 
0052 %--------------------------------------------------------------------------
0053 % Dump to netCDF file
0054 %--------------------------------------------------------------------------
0055 if ftbverbose
0056     fprintf('Writing sediment data to netCDF file: %s\n', filename);
0057     fprintf('nodes dimension: %d\n', nVerts);
0058 end
0059 
0060 nc = netcdf.create(filename, 'clobber');
0061 
0062 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'), 'history', ...
0063     sprintf('File created using %s from the MATLAB fvcom-toolbox', subname))
0064 
0065 
0066 % define dimensions
0067 node_dimid = netcdf.defDim(nc, 'node', nVerts);
0068 
0069 % define variables and attributes
0070 erate_varid = netcdf.defVar(nc, 'erate', 'NC_DOUBLE', node_dimid);
0071 netcdf.putAtt(nc, erate_varid, 'long_name', 'bed erodibility constant');
0072 netcdf.putAtt(nc, erate_varid, 'units', '-');
0073 
0074 tau_ce_varid = netcdf.defVar(nc, 'tau_ce', 'NC_DOUBLE', node_dimid);
0075 netcdf.putAtt(nc, tau_ce_varid, 'long_name', 'critical shear stress for erosion');
0076 netcdf.putAtt(nc, tau_ce_varid, 'units', 'N/m^2');
0077 
0078 tau_cd_varid = netcdf.defVar(nc, 'tau_cd', 'NC_DOUBLE', node_dimid);
0079 netcdf.putAtt(nc, tau_cd_varid, 'long_name', 'critical shear stress for deposition');
0080 netcdf.putAtt(nc, tau_cd_varid, 'units', 'N/m^2');
0081 
0082 extra_fields = setdiff(fieldnames(sediment), {'tau_ce', 'tau_cd', 'erate'});
0083 for f = 1:length(extra_fields)
0084     sediment_name = extra_fields{f};
0085     if ftbverbose
0086         fprintf('Defining extra variable: %s\n', sediment_name)
0087     end
0088     eval(sprintf('%s_varid = netcdf.defVar(nc, ''%s'', ''NC_DOUBLE'', node_dimid);\n', sediment_name, sediment_name))
0089     eval(sprintf('netcdf.putAtt(nc, %s_varid, ''long_name'', ''Fraction of %s in surface layer'');\n', sediment_name, sediment_name))
0090     eval(sprintf('netcdf.putAtt(nc, %s_varid, ''units'', ''-'')\n', sediment_name))
0091     eval(sprintf('netcdf.putAtt(nc, %s_varid, ''grid'', ''fvcom_grid'')\n', sediment_name))
0092 end
0093 
0094 % end definitions
0095 netcdf.endDef(nc);
0096 
0097 % dump data
0098 netcdf.putVar(nc, erate_varid, sediment.erate);
0099 netcdf.putVar(nc, tau_ce_varid, sediment.tau_ce);
0100 netcdf.putVar(nc, tau_cd_varid, sediment.tau_cd);
0101 
0102 % Add the extra data.
0103 for f = 1:length(extra_fields)
0104     sediment_name = extra_fields{f};
0105     if ftbverbose
0106         fprintf('Adding extra variable: %s\n', sediment_name)
0107     end
0108     eval(sprintf('netcdf.putVar(nc, %s_varid, sediment.(sediment_name));', sediment_name))
0109 end
0110 
0111 % close netCDF
0112 netcdf.close(nc)
0113 
0114 if ftbverbose
0115     fprintf('end   : %s\n', subname)
0116 end

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