0001 function write_FVCOM_sediment(sediment, filename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
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
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
0049 nVerts = numel(sediment.tau_cd);
0050 assert(nVerts ~= 0, 'dimension of sediment data is 0, something is wrong')
0051
0052
0053
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
0067 node_dimid = netcdf.defDim(nc, 'node', nVerts);
0068
0069
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
0095 netcdf.endDef(nc);
0096
0097
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
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
0112 netcdf.close(nc)
0113
0114 if ftbverbose
0115 fprintf('end : %s\n', subname)
0116 end