0001 function write_FVCOM_Gridded_nc(Mobj, fileprefix, data)
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
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 assert(nargin == 3, 'Incorrect number of arguments')
0049
0050 subname = 'write_FVCOM_heating';
0051
0052 global ftbverbose
0053 if ftbverbose
0054 fprintf('\nbegin : %s \n', subname)
0055 end
0056
0057 tri = Mobj.tri;
0058 nNodes = Mobj.nVerts;
0059 nElems = Mobj.nElems;
0060 ntimes = numel(data.time);
0061
0062 if strcmpi(Mobj.nativeCoords, 'cartesian')
0063 x = Mobj.x;
0064 y = Mobj.y;
0065 else
0066 x = Mobj.lon;
0067 y = Mobj.lat;
0068
0069 x(x < 0) = x(x < 0) + 360;
0070 end
0071
0072 coordString = sprintf('FVCOM %s coordinates', Mobj.nativeCoords);
0073
0074
0075 xc = nodes2elems(x, Mobj);
0076 yc = nodes2elems(y, Mobj);
0077
0078
0079
0080
0081
0082 nc = netcdf.create([fileprefix, '_hfx.nc'], 'clobber');
0083
0084 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'title', 'FVCOM Forcing File')
0085 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'institution', 'Plymouth Marine Laboratory')
0086 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'source', 'FVCOM grid (unstructured) surface forcing')
0087 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'history', ['File created on ', datestr(now, 'yyyy-mm-dd HH:MM:SS'), ' with write_FVCOM_forcing.m from the MATLAB fvcom-toolbox (https://github.com/pwcazenave/fvcom-toolbox)'])
0088 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'references', 'http://fvcom.smast.umassd.edu, http://codfish.smast.umassd.edu')
0089 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'Conventions', 'CF-1.0')
0090 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'CoordinateSystem', Mobj.nativeCoords)
0091 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'CoordinateProjection', 'init=epsg:4326')
0092
0093
0094 nele_dimid=netcdf.defDim(nc, 'nele', nElems);
0095 node_dimid=netcdf.defDim(nc, 'node', nNodes);
0096 three_dimid=netcdf.defDim(nc, 'three', 3);
0097 time_dimid=netcdf.defDim(nc, 'time', netcdf.getConstant('NC_UNLIMITED'));
0098 datestrlen_dimid=netcdf.defDim(nc, 'DateStrLen', 26);
0099
0100
0101 if strcmpi(Mobj.nativeCoords, 'cartesian')
0102 x_varid=netcdf.defVar(nc, 'x', 'NC_FLOAT', node_dimid);
0103 netcdf.putAtt(nc, x_varid, 'long_name', 'nodal x-coordinate');
0104 netcdf.putAtt(nc, x_varid, 'units', 'meters');
0105
0106 y_varid=netcdf.defVar(nc, 'y', 'NC_FLOAT', node_dimid);
0107 netcdf.putAtt(nc, y_varid, 'long_name', 'nodal y-coordinate');
0108 netcdf.putAtt(nc, y_varid, 'units', 'meters');
0109
0110 xc_varid=netcdf.defVar(nc, 'xc','NC_FLOAT', nele_dimid);
0111 netcdf.putAtt(nc, xc_varid, 'long_name', 'zonal x-coordinate');
0112 netcdf.putAtt(nc, xc_varid, 'units', 'meters');
0113
0114 yc_varid=netcdf.defVar(nc, 'yc','NC_FLOAT', nele_dimid);
0115 netcdf.putAtt(nc, yc_varid, 'long_name', 'zonal y-coordinate');
0116 netcdf.putAtt(nc, yc_varid, 'units', 'meters');
0117
0118 elseif strcmpi(Mobj.nativeCoords, 'spherical')
0119 x_varid=netcdf.defVar(nc, 'lon','NC_FLOAT', node_dimid);
0120 netcdf.putAtt(nc, x_varid, 'long_name', 'nodal longitude');
0121 netcdf.putAtt(nc, x_varid, 'units', 'degrees_east');
0122
0123 y_varid = netcdf.defVar(nc, 'lat', 'NC_FLOAT', node_dimid);
0124 netcdf.putAtt(nc, y_varid, 'long_name', 'nodal latitude');
0125 netcdf.putAtt(nc, y_varid, 'units', 'degrees_north');
0126
0127 xc_varid = netcdf.defVar(nc, 'lonc', 'NC_FLOAT', nele_dimid);
0128 netcdf.putAtt(nc, xc_varid, 'long_name', 'zonal longitude');
0129 netcdf.putAtt(nc, xc_varid, 'units', 'degrees_east');
0130
0131 yc_varid = netcdf.defVar(nc, 'latc', 'NC_FLOAT', nele_dimid);
0132 netcdf.putAtt(nc, yc_varid, 'long_name', 'zonal latitude');
0133 netcdf.putAtt(nc, yc_varid, 'units', 'degrees_north');
0134 else
0135 error('Unknown coordinate type (%s)', Mobj.nativeCoords)
0136 end
0137
0138 nv_varid=netcdf.defVar(nc, 'nv', 'NC_INT', [nele_dimid, three_dimid]);
0139 netcdf.putAtt(nc, nv_varid, 'long_name', 'nodes surrounding element');
0140
0141
0142 time_varid=netcdf.defVar(nc, 'time', 'NC_FLOAT', time_dimid);
0143 netcdf.putAtt(nc, time_varid, 'long_name', 'time');
0144 netcdf.putAtt(nc, time_varid, 'units', 'days since 1858-11-17 00:00:00');
0145 netcdf.putAtt(nc, time_varid, 'format', 'modified julian day (MJD)');
0146 netcdf.putAtt(nc, time_varid, 'time_zone', 'UTC');
0147
0148 itime_varid=netcdf.defVar(nc, 'Itime', 'NC_INT', time_dimid);
0149 netcdf.putAtt(nc, itime_varid, 'units', 'days since 1858-11-17 00:00:00');
0150 netcdf.putAtt(nc, itime_varid, 'format', 'modified julian day (MJD)');
0151 netcdf.putAtt(nc, itime_varid, 'time_zone', 'UTC');
0152
0153 itime2_varid=netcdf.defVar(nc, 'Itime2', 'NC_INT', time_dimid);
0154 netcdf.putAtt(nc, itime2_varid, 'units', 'msec since 00:00:00');
0155 netcdf.putAtt(nc, itime2_varid, 'time_zone', 'UTC');
0156 netcdf.putAtt(nc, itime2_varid, 'long_name', 'time');
0157
0158 times_varid=netcdf.defVar(nc, 'Times', 'NC_CHAR', [datestrlen_dimid, time_dimid]);
0159 netcdf.putAtt(nc, times_varid, 'long_name', 'Calendar Date');
0160 netcdf.putAtt(nc, times_varid, 'format', 'String: Calendar Time');
0161 netcdf.putAtt(nc, times_varid, 'time_zone', 'UTC');
0162
0163 airt_varid = netcdf.defVar(nc, 'air_temperature', 'NC_FLOAT', [node_dimid, time_dimid]);
0164 netcdf.putAtt(nc, airt_varid, 'long_name', 'Surface air temperature');
0165 netcdf.putAtt(nc, airt_varid, 'units', 'Celsius Degree');
0166 netcdf.putAtt(nc, airt_varid, 'grid', 'fvcom_grid');
0167 netcdf.putAtt(nc, airt_varid, 'coordinates', coordString);
0168 netcdf.putAtt(nc, airt_varid, 'type', 'data');
0169
0170 rhum_varid = netcdf.defVar(nc, 'relative_humidity', 'NC_FLOAT', [node_dimid, time_dimid]);
0171 netcdf.putAtt(nc, rhum_varid, 'long_name', 'surface air relative humidity');
0172 netcdf.putAtt(nc, rhum_varid, 'units', 'percentage');
0173 netcdf.putAtt(nc, rhum_varid, 'grid', 'fvcom_grid');
0174 netcdf.putAtt(nc, rhum_varid, 'coordinates', coordString);
0175 netcdf.putAtt(nc, rhum_varid, 'type', 'data');
0176
0177 dlwrf_varid = netcdf.defVar(nc, 'long_wave', 'NC_FLOAT', [node_dimid, time_dimid]);
0178 netcdf.putAtt(nc, dlwrf_varid, 'long_name', 'Downward solar longwave radiation flux');
0179 netcdf.putAtt(nc, dlwrf_varid, 'units', 'Watts meter-2');
0180 netcdf.putAtt(nc, dlwrf_varid, 'grid', 'fvcom_grid');
0181 netcdf.putAtt(nc, dlwrf_varid, 'coordinates', coordString);
0182 netcdf.putAtt(nc, dlwrf_varid, 'type', 'data');
0183
0184 dswrf_varid = netcdf.defVar(nc, 'short_wave', 'NC_FLOAT', [node_dimid, time_dimid]);
0185 netcdf.putAtt(nc, dswrf_varid, 'long_name', 'Downward solar shortwave radiation flux');
0186 netcdf.putAtt(nc, dswrf_varid, 'units', 'Watts meter-2');
0187 netcdf.putAtt(nc, dswrf_varid, 'grid', 'fvcom_grid');
0188 netcdf.putAtt(nc, dswrf_varid, 'coordinates', coordString);
0189 netcdf.putAtt(nc, dswrf_varid, 'type', 'data');
0190
0191 slp_varid = netcdf.defVar(nc, 'air_pressure', 'NC_FLOAT', [node_dimid, time_dimid]);
0192 netcdf.putAtt(nc, slp_varid, 'long_name', 'Surface air pressure');
0193 netcdf.putAtt(nc, slp_varid, 'units', 'Pa');
0194 netcdf.putAtt(nc, slp_varid, 'grid', 'fvcom_grid');
0195 netcdf.putAtt(nc, slp_varid, 'coordinates', coordString);
0196 netcdf.putAtt(nc, slp_varid, 'type', 'data');
0197
0198
0199 netcdf.endDef(nc);
0200
0201
0202
0203
0204 netcdf.putVar(nc, nv_varid, tri');
0205 netcdf.putVar(nc, time_varid, 0, ntimes, data.time);
0206 netcdf.putVar(nc, itime_varid, 0, ntimes, floor(data.time));
0207 netcdf.putVar(nc, itime2_varid, 0, ntimes, mod(data.time, 1) * 24 * 3600 * 1000);
0208 netcdf.putVar(nc, x_varid, x);
0209 netcdf.putVar(nc, y_varid, y);
0210 netcdf.putVar(nc, xc_varid, xc);
0211 netcdf.putVar(nc, yc_varid, yc);
0212 netcdf.putVar(nc, airt_varid, data.air.node);
0213 netcdf.putVar(nc, rhum_varid, data.rhum.node);
0214 try
0215
0216 netcdf.putVar(nc, dlwrf_varid, data.dlwrf.node);
0217 catch
0218
0219 netcdf.putVar(nc, dlwrf_varid, data.nlwrf.node);
0220 end
0221 try
0222
0223 netcdf.putVar(nc, dswrf_varid, data.dswrf.node);
0224 catch
0225
0226 netcdf.putVar(nc, dswrf_varid, data.nswrf.node);
0227 end
0228 try
0229 netcdf.putVar(nc, slp_varid, data.slp.node);
0230 catch
0231 netcdf.putVar(nc, slp_varid, data.pres.node);
0232 end
0233
0234
0235 nStringOut = char();
0236 [nYr, nMon, nDay, nHour, nMin, nSec] = mjulian2greg(data.time);
0237 for tt=1:ntimes
0238 nDate = [nYr(tt), nMon(tt), nDay(tt), nHour(tt), nMin(tt), nSec(tt)];
0239 nStringOut = [nStringOut, sprintf('%04i/%02i/%02i %02i:%02i:%02i ', nDate)];
0240 end
0241 netcdf.putVar(nc, times_varid, nStringOut);
0242
0243
0244 netcdf.close(nc);
0245
0246 if ftbverbose
0247 fprintf('end : %s \n', subname)
0248 end