Home > fvcom_prepro > write_FVCOM_river_ERSEM.m

write_FVCOM_river_ERSEM

PURPOSE ^

Write FVCOM 3.x netCDF river file

SYNOPSIS ^

function write_FVCOM_river_ERSEM(RiverFile,RiverName,time,flux,temp,salt,n1p,n3n,n4n,n5s,dic,o2,bioalk,alkalinity,RiverInfo1,RiverInfo2)

DESCRIPTION ^

 Write FVCOM 3.x netCDF river file

 function write_FVCOM_river_ERSEM(RiverFile,RiverName,time,flux,temp,salt,n1p,n3n,n4n,n5s,dic,alkalinity,RiverInfo1,RiverInfo2)

 DESCRIPTION:
    Write river flux, temperature, salinity and ERSEM nutrients to an
    FVCOM river file.

 INPUT
    RiverFile  : FVCOM 3.x netCDF river forcing file
    RiverName  : Name of the actual River
    time       : Timestamp array in modified Julian day
    flux       : Total river flux in m^3/s (dimensions [time, nRivernodes])
    temp       : Temperature in C (dimensions [time, nRivernodes])
    salt       : Salinity in PSU (dimensions [time, nRivernodes])
    n1p        : Phosphate (mmol P/m^3) (dimensions [time, nRivernodes])
    n3n        : Nitrate (mmol N/m^3) (dimensions [time, nRivernodes])
    n4n        : Ammonia (mmol N/m^3) (dimensions [time, nRivernodes])
    n5s        : Silicate (mmol Si/m^3) (dimensions [time, nRivernodes])
    dic        : Dissolved inorganic carbon (mmol C/m^3) (dimensions [time, nRivernodes])
    o2         : Dissolved Oxygen (mmol O_2/m^3) (dimensions [time, nRivernodes])
    bioalk     : Bio_alkalinity compartment carbon (mmol C/m^3) (dimensions [time, nRivernodes])
    Total_alk  : Total Alkalinity (umol C/m^3) (dimensions [time, nRivernodes])
    RiverInfo1 : Global attribute title of file
    RiverInfo2 : Global attribute info of file

 OUTPUT:
    FVCOM netCDF river file with flux, temperature, salinity and ERSEM
    nutrients.

 EXAMPLE USAGE
    write_FVCOM_river('tst_riv.nc', {'Penobscot'}, time, flux, ...
         temp, salt, n1p, n3n, n4n, n5s, dic, alkalinity, ...
         'Penobscot Flux', 'source: USGS')

 Author(s):
    Geoff Cowles (University of Massachusetts Dartmouth)
    Pierre Cazenave (Plymouth Marine Laboratory)
    Ricardo Torres (Plymouth Marine Laboratory)

 Revision history
   2016-03-14 New version to export nutrients alongside the physical
   parameters for FVCOM-ERSEM. Based on write_FVCOM_river.
   2017-01-12 Add missing ERSEM variables (oxygen and alkalinity).
   2017-11-03 Add conflict variables that causes FABM debug efforts. At
   the moment these are all the zooplankton variables. Set to a very low
   number by default. The one used in ERSEM to maintain low population
   backgrounds. (Riqui)

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_FVCOM_river_ERSEM(RiverFile,RiverName,time,flux,temp,salt,n1p,n3n,n4n,n5s,dic,o2,bioalk,alkalinity,RiverInfo1,RiverInfo2)
0002 % Write FVCOM 3.x netCDF river file
0003 %
0004 % function write_FVCOM_river_ERSEM(RiverFile,RiverName,time,flux,temp,salt,n1p,n3n,n4n,n5s,dic,alkalinity,RiverInfo1,RiverInfo2)
0005 %
0006 % DESCRIPTION:
0007 %    Write river flux, temperature, salinity and ERSEM nutrients to an
0008 %    FVCOM river file.
0009 %
0010 % INPUT
0011 %    RiverFile  : FVCOM 3.x netCDF river forcing file
0012 %    RiverName  : Name of the actual River
0013 %    time       : Timestamp array in modified Julian day
0014 %    flux       : Total river flux in m^3/s (dimensions [time, nRivernodes])
0015 %    temp       : Temperature in C (dimensions [time, nRivernodes])
0016 %    salt       : Salinity in PSU (dimensions [time, nRivernodes])
0017 %    n1p        : Phosphate (mmol P/m^3) (dimensions [time, nRivernodes])
0018 %    n3n        : Nitrate (mmol N/m^3) (dimensions [time, nRivernodes])
0019 %    n4n        : Ammonia (mmol N/m^3) (dimensions [time, nRivernodes])
0020 %    n5s        : Silicate (mmol Si/m^3) (dimensions [time, nRivernodes])
0021 %    dic        : Dissolved inorganic carbon (mmol C/m^3) (dimensions [time, nRivernodes])
0022 %    o2         : Dissolved Oxygen (mmol O_2/m^3) (dimensions [time, nRivernodes])
0023 %    bioalk     : Bio_alkalinity compartment carbon (mmol C/m^3) (dimensions [time, nRivernodes])
0024 %    Total_alk  : Total Alkalinity (umol C/m^3) (dimensions [time, nRivernodes])
0025 %    RiverInfo1 : Global attribute title of file
0026 %    RiverInfo2 : Global attribute info of file
0027 %
0028 % OUTPUT:
0029 %    FVCOM netCDF river file with flux, temperature, salinity and ERSEM
0030 %    nutrients.
0031 %
0032 % EXAMPLE USAGE
0033 %    write_FVCOM_river('tst_riv.nc', {'Penobscot'}, time, flux, ...
0034 %         temp, salt, n1p, n3n, n4n, n5s, dic, alkalinity, ...
0035 %         'Penobscot Flux', 'source: USGS')
0036 %
0037 % Author(s):
0038 %    Geoff Cowles (University of Massachusetts Dartmouth)
0039 %    Pierre Cazenave (Plymouth Marine Laboratory)
0040 %    Ricardo Torres (Plymouth Marine Laboratory)
0041 %
0042 % Revision history
0043 %   2016-03-14 New version to export nutrients alongside the physical
0044 %   parameters for FVCOM-ERSEM. Based on write_FVCOM_river.
0045 %   2017-01-12 Add missing ERSEM variables (oxygen and alkalinity).
0046 %   2017-11-03 Add conflict variables that causes FABM debug efforts. At
0047 %   the moment these are all the zooplankton variables. Set to a very low
0048 %   number by default. The one used in ERSEM to maintain low population
0049 %   backgrounds. (Riqui)
0050 %
0051 %==========================================================================
0052 
0053 [~, subname] = fileparts(mfilename('fullpath'));
0054 
0055 global ftbverbose
0056 if ftbverbose
0057     fprintf('\nbegin : %s\n', subname)
0058 end
0059 
0060 [nTimes, nRivnodes] = size(flux);
0061 if ftbverbose
0062     fprintf('# of river nodes: %d\n', nRivnodes);
0063     fprintf('# of time frames: %d\n', nTimes);
0064 end
0065 
0066 [year1, month1, day1, ~, ~, ~] = mjulian2greg(time(1));
0067 [year2, month2, day2, ~, ~, ~] = mjulian2greg(time(end));
0068 if ftbverbose
0069     fprintf('time series begins at:\t%04d %02d %02d\n', year1, month1, day1);
0070     fprintf('time series ends at:\t%04d %02d %02d\n', year2, month2, day2);
0071 end
0072 clear year? month? day?
0073 
0074 %--------------------------------------------------------------------------
0075 % dump to netcdf file
0076 %--------------------------------------------------------------------------
0077 
0078 % river node forcing
0079 nc = netcdf.create(RiverFile, 'clobber');
0080 
0081 % global variables
0082 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'type', 'FVCOM RIVER FORCING FILE')
0083 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'title', RiverInfo1)
0084 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'info', RiverInfo2)
0085 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'history', sprintf('File created using %s from the MATLAB fvcom-toolbox', subname))
0086 
0087 % dimensions
0088 namelen_dimid = netcdf.defDim(nc, 'namelen', 80);
0089 rivers_dimid = netcdf.defDim(nc, 'rivers', nRivnodes);
0090 time_dimid = netcdf.defDim(nc, 'time', netcdf.getConstant('NC_UNLIMITED'));
0091 date_str_len_dimid = netcdf.defDim(nc, 'DateStrLen', 26);
0092 
0093 % variables
0094 river_names_varid = netcdf.defVar(nc, 'river_names', 'NC_CHAR', [namelen_dimid, rivers_dimid]);
0095 
0096 time_varid = netcdf.defVar(nc, 'time', 'NC_FLOAT', time_dimid);
0097 netcdf.putAtt(nc, time_varid, 'long_name', 'time');
0098 netcdf.putAtt(nc, time_varid, 'units', 'days since 1858-11-17 00:00:00');
0099 netcdf.putAtt(nc, time_varid, 'format', 'modified julian day (MJD)');
0100 netcdf.putAtt(nc, time_varid, 'time_zone', 'UTC');
0101 
0102 itime_varid = netcdf.defVar(nc, 'Itime', 'NC_INT', time_dimid);
0103 netcdf.putAtt(nc, itime_varid, 'units', 'days since 1858-11-17 00:00:00');
0104 netcdf.putAtt(nc, itime_varid, 'format', 'modified julian day (MJD)');
0105 netcdf.putAtt(nc, itime_varid, 'time_zone', 'UTC');
0106 
0107 itime2_varid = netcdf.defVar(nc, 'Itime2', 'NC_INT', time_dimid);
0108 netcdf.putAtt(nc, itime2_varid, 'units', 'msec since 00:00:00');
0109 netcdf.putAtt(nc, itime2_varid, 'time_zone', 'UTC');
0110 
0111 times_varid = netcdf.defVar(nc,'Times','NC_CHAR',[date_str_len_dimid, time_dimid]);
0112 netcdf.putAtt(nc, times_varid, 'time_zone','UTC');
0113 
0114 river_flux_varid = netcdf.defVar(nc, 'river_flux', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0115 netcdf.putAtt(nc, river_flux_varid, 'long_name', 'river runoff volume flux');
0116 netcdf.putAtt(nc, river_flux_varid, 'units', 'm^3s^-1');
0117 
0118 river_temp_varid = netcdf.defVar(nc, 'river_temp', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0119 netcdf.putAtt(nc, river_temp_varid, 'long_name', 'river runoff temperature');
0120 netcdf.putAtt(nc, river_temp_varid, 'units', 'Celsius');
0121 
0122 river_salt_varid = netcdf.defVar(nc, 'river_salt', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0123 netcdf.putAtt(nc, river_salt_varid, 'long_name', 'river runoff salinity');
0124 netcdf.putAtt(nc, river_salt_varid, 'units', 'PSU');
0125 
0126 river_n1p_varid = netcdf.defVar(nc, 'N1_p', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0127 netcdf.putAtt(nc, river_n1p_varid, 'long_name', 'phosphate phosphorus');
0128 netcdf.putAtt(nc, river_n1p_varid, 'units', 'mmol P/m^3');
0129 
0130 river_n3n_varid = netcdf.defVar(nc, 'N3_n', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0131 netcdf.putAtt(nc, river_n3n_varid, 'long_name', 'nitrate nitrogen');
0132 netcdf.putAtt(nc, river_n3n_varid, 'units', 'mmol N/m^3');
0133 
0134 river_n4n_varid = netcdf.defVar(nc, 'N4_n', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0135 netcdf.putAtt(nc, river_n4n_varid, 'long_name', 'ammonium nitrogen');
0136 netcdf.putAtt(nc, river_n4n_varid, 'units', 'mmol N/m^3');
0137 
0138 river_n5s_varid = netcdf.defVar(nc, 'N5_s', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0139 netcdf.putAtt(nc, river_n5s_varid, 'long_name', 'silicate silicate');
0140 netcdf.putAtt(nc, river_n5s_varid, 'units', 'mmol Si/m^3');
0141 
0142 river_O2_varid = netcdf.defVar(nc, 'O2_o', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0143 netcdf.putAtt(nc, river_O2_varid, 'long_name', 'dissolved Oxygen');
0144 netcdf.putAtt(nc, river_O2_varid, 'units', 'mmol O_2/m^3');
0145 
0146 river_TA_varid = netcdf.defVar(nc, 'O3_TA', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0147 netcdf.putAtt(nc, river_TA_varid, 'long_name', 'carbonate total alkalinity');
0148 netcdf.putAtt(nc, river_TA_varid, 'units', 'mmol C/m^3');
0149 
0150 
0151 river_dic_varid = netcdf.defVar(nc, 'O3_c', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0152 netcdf.putAtt(nc, river_dic_varid, 'long_name', 'carbonate total dissolved inorganic carbon');
0153 netcdf.putAtt(nc, river_dic_varid, 'units', 'mmol C/m^3');
0154 
0155 river_bioalk_varid = netcdf.defVar(nc, 'O3_bioalk', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0156 netcdf.putAtt(nc, river_bioalk_varid, 'long_name', 'carbonate bioalkalinity');
0157 netcdf.putAtt(nc, river_bioalk_varid, 'units', 'umol/kg');
0158 
0159 % Additional zooplankton variables
0160 zoo_vars = {'Z5','Z6'};
0161 zuu_nuts = {'n','p','c'};
0162 zoo_long = {'microzooplankton','nanoflagellates'};
0163 nuts_long = {'nitrogen','phosphorus','nitrogen'};
0164 nuts_units = {'mmol N/m^3','mmol P/m^3','mg C/m^3'};
0165 
0166 river_Z4c_varid = netcdf.defVar(nc, 'Z4_c', 'NC_FLOAT', [rivers_dimid, time_dimid]);
0167 netcdf.putAtt(nc, river_Z4c_varid, 'long_name', 'mesozooplankton carbon');
0168 netcdf.putAtt(nc, river_Z4c_varid, 'units', 'mg C/m^3');
0169 
0170 for zz=1:length(zoo_vars)
0171     for nn=1:length(zuu_nuts)
0172         eval(['river_',zoo_vars{zz},zuu_nuts{nn},'_varid = netcdf.defVar(nc, ''',zoo_vars{zz},'_',zuu_nuts{nn},''', ''NC_FLOAT'', [rivers_dimid, time_dimid]);'])
0173         eval(['netcdf.putAtt(nc, river_',zoo_vars{zz},zuu_nuts{nn},'_varid, ''long_name'', ''',zoo_long{zz},' ',nuts_long{nn},''');'])
0174         eval(['netcdf.putAtt(nc, river_',zoo_vars{zz},zuu_nuts{nn},'_varid, ''units'', ''',nuts_units{nn},''');'])
0175     end
0176 end
0177 
0178 % end definitions
0179 netcdf.endDef(nc);
0180 
0181 % river names (must be 80 character strings)
0182 rString = char();
0183 for i = 1:nRivnodes
0184     % Left-aligned 80 character string.
0185     rString = [rString, sprintf('%-80s', RiverName{i})];
0186 end
0187 netcdf.putVar(nc, river_names_varid, rString);
0188 
0189 % dump dynamic data
0190 netcdf.putVar(nc, time_varid, 0, nTimes, time);
0191 netcdf.putVar(nc, itime_varid, 0, nTimes, floor(time));
0192 netcdf.putVar(nc, itime2_varid, 0, nTimes, mod(time, 1)*24*3600*1000);
0193 if any(isnan(flux(:)))
0194     error('NaNs in river flux varaible')
0195 end
0196 netcdf.putVar(nc, river_flux_varid, flux');
0197 if any(isnan(temp(:)))
0198     error('NaNs in river temp varaible')
0199 end
0200 netcdf.putVar(nc, river_temp_varid, temp');
0201 if any(isnan(salt(:)))
0202     error('NaNs in river salt varaible')
0203 end
0204 netcdf.putVar(nc, river_salt_varid, salt');
0205 if any(isnan(n1p(:)))
0206     error('NaNs in river n1p varaible')
0207 end
0208 netcdf.putVar(nc, river_n1p_varid, n1p');
0209 if any(isnan(n3n(:)))
0210     error('NaNs in river n3n varaible')
0211 end
0212 netcdf.putVar(nc, river_n3n_varid, n3n');
0213 if any(isnan(n4n(:)))
0214     error('NaNs in river n4n varaible')
0215 end
0216 netcdf.putVar(nc, river_n4n_varid, n4n');
0217 if any(isnan(n5s(:)))
0218     error('NaNs in river n5s varaible')
0219 end
0220 netcdf.putVar(nc, river_n5s_varid, n5s');
0221 if any(isnan(dic(:)))
0222     error('NaNs in river dic varaible')
0223 end
0224 netcdf.putVar(nc, river_dic_varid, dic');
0225 if any(isnan(o2(:)))
0226     error('NaNs in river o2 varaible')
0227 end
0228 netcdf.putVar(nc, river_O2_varid, o2');
0229 if any(isnan(alkalinity(:)))
0230     error('NaNs in river alkalinity varaible')
0231 end
0232 netcdf.putVar(nc, river_TA_varid, alkalinity');
0233 if any(isnan(bioalk(:)))
0234     error('NaNs in river bioalk varaible')
0235 end
0236 netcdf.putVar(nc, river_bioalk_varid,bioalk');
0237 % add small zooplankton values
0238 % taken to be 10^-6 of L4 initial conditions.
0239 fac = 10^-6;
0240 netcdf.putVar(nc, river_Z4c_varid,ones(size(bioalk')).*1.2*fac);
0241 netcdf.putVar(nc, river_Z5c_varid,ones(size(bioalk')).*7.2*fac);
0242 netcdf.putVar(nc, river_Z5n_varid,ones(size(bioalk')).*0.12*fac);
0243 netcdf.putVar(nc, river_Z5p_varid,ones(size(bioalk')).*0.0113*fac);
0244 netcdf.putVar(nc, river_Z6c_varid,ones(size(bioalk')).*2.4*fac);
0245 netcdf.putVar(nc, river_Z6n_varid,ones(size(bioalk')).*0.0505*fac);
0246 netcdf.putVar(nc, river_Z6p_varid,ones(size(bioalk')).*0.0047*fac); % not quite the initiall values in fabm.yaml... but they are not in carbon balance...
0247 
0248 % build the time string and output to netCDF.
0249 nStringOut = char();
0250 [nYr, nMon, nDay, nHour, nMin, nSec] = mjulian2greg(time);
0251 for tt = 1:nTimes
0252     nDate = [nYr(tt), nMon(tt), nDay(tt), nHour(tt), nMin(tt), nSec(tt)];
0253     nStringOut = [nStringOut, sprintf('%04i/%02i/%02i %02i:%02i:%02i       ', nDate)];
0254 end
0255 netcdf.putVar(nc, times_varid, nStringOut);
0256 
0257 netcdf.close(nc);
0258 
0259 if ftbverbose
0260     fprintf('end   : %s\n', subname)
0261 end
0262

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