Home > fvcom_prepro > add_weights_FVCOM_nested_forcing.m

add_weights_FVCOM_nested_forcing

PURPOSE ^

Creates an FVCOM nesting file.

SYNOPSIS ^

function add_weights_FVCOM_nested_forcing(conf, ncfile2read)

DESCRIPTION ^

 Creates an FVCOM nesting file.

 function add_weights_FVCOM_nested_forcing(conf, ncfile)

 DESCRIPTION:
   Modifies in place the nested file to add weights for the number of levels 
   suitable for types 3. 


 INPUT:
   conf        = with either new_weights field or conf.nest.levels
   ncfile2read = full path to the nesting file to be created.

 OUTPUT:
   FVCOM nesting file.

 EXAMPLE USAGE:
   conf.new_weight_cell = weights see manual for explanation [0-1]. It
           only requires nlevel values, not spatial or temporal dimension is
           expected.
   conf.new_weight_node = weights see manual for explanation [0-1]. It
           only requires nlevel values, not spatial or temporal dimension is
           expected.
   conf.nest.levels = [1]; The value reflect the inner most level to keep.
           1 will keep the most external boundary, 4 will keep 1-4 levels.

   modify_FVCOM_nested_forcing(conf, '/tmp/fvcom_nested.nc', 1)

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

 Revision history:
   2017-01-17 First version based on Pierre's write_FVCOM_nested_forcing.m
   script. Compress the time series data to save space. Requires
   netCDF4 in FVCOM.

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function add_weights_FVCOM_nested_forcing(conf, ncfile2read)
0002 % Creates an FVCOM nesting file.
0003 %
0004 % function add_weights_FVCOM_nested_forcing(conf, ncfile)
0005 %
0006 % DESCRIPTION:
0007 %   Modifies in place the nested file to add weights for the number of levels
0008 %   suitable for types 3.
0009 %
0010 %
0011 % INPUT:
0012 %   conf        = with either new_weights field or conf.nest.levels
0013 %   ncfile2read = full path to the nesting file to be created.
0014 %
0015 % OUTPUT:
0016 %   FVCOM nesting file.
0017 %
0018 % EXAMPLE USAGE:
0019 %   conf.new_weight_cell = weights see manual for explanation [0-1]. It
0020 %           only requires nlevel values, not spatial or temporal dimension is
0021 %           expected.
0022 %   conf.new_weight_node = weights see manual for explanation [0-1]. It
0023 %           only requires nlevel values, not spatial or temporal dimension is
0024 %           expected.
0025 %   conf.nest.levels = [1]; The value reflect the inner most level to keep.
0026 %           1 will keep the most external boundary, 4 will keep 1-4 levels.
0027 %
0028 %   modify_FVCOM_nested_forcing(conf, '/tmp/fvcom_nested.nc', 1)
0029 %
0030 % Author(s):
0031 %   Ricardo Torres (Plymouth Marine Laboratory)
0032 %   Pierre Cazenave (Plymouth Marine Laboratory)
0033 %
0034 % Revision history:
0035 %   2017-01-17 First version based on Pierre's write_FVCOM_nested_forcing.m
0036 %   script. Compress the time series data to save space. Requires
0037 %   netCDF4 in FVCOM.
0038 %
0039 %==========================================================================
0040 
0041 % The following variables are read from the file:
0042 %
0043 % lon, lat:     Grid node positions             [node]
0044 % lonc, latc:   Grid element positions          [nele]
0045 % h:            Grid node depths                [node]
0046 % hc:           Grid element depth              [nele]
0047 % nv:           Triangulation table             [nele, 3]
0048 % zeta:         Sea surface elevation           [node, time]
0049 % ua:           Vertically averaged x velocity  [node, time]
0050 % va:           Vertically averaged y velocity  [nele, time]
0051 % u:            Eastward Water Velocity         [nele, siglay, time]
0052 % v:            Northward Water Velocity        [nele, siglay, time]
0053 % temp:         Temperature                     [node, siglay, time]
0054 % salinity:     Salinity                        [node, siglay, time]
0055 % hyw:          Hydrostatic vertical velocity   [node, siglev, time]
0056 % weight_cell:  Weighting for elements          [nele]
0057 % weight_node:  Weighting for nodes             [node]
0058 % Itime:        Days since 1858-11-17 00:00:00  [time]
0059 % Itime2:       msec since 00:00:00             [time]
0060 %
0061 % We include these optional ones for humans:
0062 % time:         Modified Julian Day             [time]
0063 % Times:        Gregorian dates                 [time, datestrlen]
0064 
0065 [~, subname] = fileparts(mfilename('fullpath'));
0066 
0067 global ftbverbose
0068 if ftbverbose
0069     fprintf('\nbegin : %s\n', subname)
0070 end
0071 
0072 % Can't use CLOBBER and NETCDF4 at the same time (the bitwise or didn't
0073 % work). Fall back to a horrible delete and then create instead.
0074 if exist(ncfile2read, 'file')
0075     nc2read = netcdf.open(ncfile2read,'WRITE');
0076     [PATHSTR,NAME,EXT] = fileparts(ncfile2read);
0077     if ftbverbose
0078         fprintf('\nProcessing file : %s\n', ncfile2read)
0079     end
0080 else
0081     sprintf('I am stoping. The nesting file %s doesn''t exist',ncfile2read);
0082     error(['Aborting the script ',subname])
0083 end
0084 % List of data we need.
0085 required = {'weight_cell', 'weight_node'};
0086 for f = required
0087     nest.(f{1})=[];
0088 end
0089 % Read all variables from the existing file
0090 if ~isfield (conf,'nest')
0091     error('Missing conf.nest, aborting... ')
0092 end
0093 if ~isfield (conf.nest,'levels')
0094     error('Missing conf.nest.levels, aborting... ')
0095 end
0096 % define dimensions
0097 
0098 timeid = netcdf.inqDimID(nc2read, 'time');
0099 siglayid = netcdf.inqDimID(nc2read, 'siglay');
0100 siglevid = netcdf.inqDimID(nc2read, 'siglev');
0101 nodeid = netcdf.inqDimID(nc2read,'node');
0102 neleid = netcdf.inqDimID(nc2read,'nele');
0103        
0104 [~,nsiglay] = netcdf.inqDim(nc2read, siglayid);
0105 [~,nsiglev] = netcdf.inqDim(nc2read, siglevid);
0106 [~,ntimes] = netcdf.inqDim(nc2read, timeid);
0107 [~,nodes] = netcdf.inqDim(nc2read, nodeid);
0108 [~,elems] = netcdf.inqDim(nc2read, neleid);
0109 
0110 % Replicate weights in time
0111 
0112 nest.weight_cell = repmat([conf.new_weight_cell{:}]',1,ntimes);
0113 nest.weight_node = repmat([conf.new_weight_node{:}]',1,ntimes)
0114 
0115 [elem_nest] =size(nest.weight_cell,1);
0116 
0117 if elem_nest ~= elems
0118     error('We have different elements in FVCOM nesting output file and our calculations')
0119 end
0120 try
0121     netcdf.reDef(nc2read);
0122 
0123     cweights_varid = netcdf.defVar(nc2read, 'weight_cell', 'NC_FLOAT', ...
0124         [neleid, timeid]);
0125     netcdf.putAtt(nc2read, cweights_varid, 'long_name', ...
0126         'Weights for elements in relaxation zone');
0127     netcdf.putAtt(nc2read, cweights_varid, 'units', 'no units');
0128     netcdf.putAtt(nc2read, cweights_varid, 'grid', 'fvcom_grid');
0129     netcdf.putAtt(nc2read, cweights_varid, 'type', 'data');
0130     
0131     nweights_varid = netcdf.defVar(nc2read, 'weight_node', 'NC_FLOAT', ...
0132         [nodeid, timeid]);
0133     netcdf.putAtt(nc2read, nweights_varid, 'long_name', ...
0134         'Weights for nodes in relaxation zone');
0135     netcdf.putAtt(nc2read, nweights_varid, 'units', 'no units');
0136     netcdf.putAtt(nc2read, nweights_varid, 'grid', 'fvcom_grid');
0137     netcdf.putAtt(nc2read, nweights_varid, 'type', 'data');
0138 
0139 % end definitions
0140 netcdf.endDef(nc2read);
0141 
0142 
0143     netcdf.putVar(nc2read, cweights_varid, nest.weight_cell);
0144     netcdf.putVar(nc2read, nweights_varid, nest.weight_node);
0145 catch e
0146     fprintf(e.message)
0147     error('Adding variable %s failed - does the variable already exist?', 'weight_cell')
0148 end
0149 
0150 
0151 % close file
0152 netcdf.close(nc2read)
0153 
0154 if ftbverbose
0155     fprintf('end   : %s\n', subname)
0156 end

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