0001 function write_WRF_forcing(WRF, 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
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051 assert(nargin == 2, 'Incorrect number of arguments')
0052
0053 subname = 'write_WRF_heating';
0054
0055 global ftbverbose
0056 if ftbverbose
0057 fprintf('\nbegin : %s \n', subname)
0058 end
0059
0060 ntimes = numel(WRF.time);
0061 [sgYr, sgMon, sgDay, sgHr, sgMin, sgSec] = mjulian2greg(WRF.time(1));
0062 [egYr, egMon, egDay, egHr, egMin, egSec] = mjulian2greg(WRF.time(end));
0063
0064 [x, y] = meshgrid(WRF.lon, WRF.lat);
0065
0066 if max(x) > 180
0067 x(x > 180) = x(x > 180) - 360;
0068 end
0069
0070
0071 if max(y) > 90
0072 y(y > 90) = y(y > 90) - 180;
0073 end
0074 [nsouth_north, nwest_east] = size(x);
0075
0076
0077
0078
0079
0080 nc = netcdf.create(filename, 'clobber');
0081
0082 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'type', 'FVCOM METEO FORCING FILE')
0083 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'title', [filename, ' forcing'])
0084 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'gauge', 'Met Office Unified Model forcing')
0085 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'history', sprintf('File with %s from the MATLAB fvcom-toolbox', subname))
0086 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'source', 'wrf grid (structured) surface forcing')
0087 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'START_DATE', datestr(datenum(sgYr, sgMon, sgDay, sgHr, sgMin, sgSec), 'yyyy-mm-dd HH:MM:SS'))
0088 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'END_DATE', datestr(datenum(egYr, egMon, egDay, egHr, egMin, egSec), 'yyyy-mm-dd HH:MM:SS'))
0089 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'file', filename)
0090 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'institution', 'Plymouth Marine Laboratory')
0091 netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'Conventions', 'CF-1.0')
0092
0093
0094 sn_dimid=netcdf.defDim(nc, 'south_north', nsouth_north);
0095 we_dimid=netcdf.defDim(nc, 'west_east', nwest_east);
0096 bu_dimid=netcdf.defDim(nc, 'bottom_up', 1);
0097 datestrlen_dimid=netcdf.defDim(nc, 'DateStrLen', 19);
0098 time_dimid=netcdf.defDim(nc, 'time', netcdf.getConstant('NC_UNLIMITED'));
0099
0100
0101 y_varid = netcdf.defVar(nc, 'XLAT', 'NC_FLOAT', [we_dimid, sn_dimid]);
0102 netcdf.putAtt(nc, y_varid, 'long_name', 'latitude');
0103 netcdf.putAtt(nc, y_varid, 'description', 'LATITUDE, SOUTH IS NEGATIVE');
0104 netcdf.putAtt(nc, y_varid, 'units', 'degrees_north');
0105 netcdf.putAtt(nc, y_varid, 'type', 'data');
0106
0107 x_varid=netcdf.defVar(nc, 'XLONG', 'NC_FLOAT', [we_dimid, sn_dimid]);
0108 netcdf.putAtt(nc, x_varid, 'long_name', 'longitude');
0109 netcdf.putAtt(nc, x_varid, 'description', 'LONGITUDE, WEST IS NEGATIVE');
0110 netcdf.putAtt(nc, x_varid, 'units', 'degrees_east');
0111 netcdf.putAtt(nc, x_varid, 'type', 'data');
0112
0113
0114 times_varid=netcdf.defVar(nc, 'Times', 'NC_CHAR', [datestrlen_dimid, time_dimid]);
0115 netcdf.putAtt(nc, times_varid, 'description', 'GMT time');
0116
0117
0118
0119 nswrs_varid = netcdf.defVar(nc, 'Shortwave', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0120 netcdf.putAtt(nc, nswrs_varid, 'long_name', 'Shortwave, upward is negative');
0121 netcdf.putAtt(nc, nswrs_varid, 'units', 'W m-2');
0122 netcdf.putAtt(nc, nswrs_varid, 'grid', 'wrf_grid');
0123 netcdf.putAtt(nc, nswrs_varid, 'coordinates', 'lat lon');
0124 netcdf.putAtt(nc, nswrs_varid, 'type', 'data');
0125
0126 nlwrs_varid = netcdf.defVar(nc, 'long_wave', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0127 netcdf.putAtt(nc, nlwrs_varid, 'long_name', 'Longwave, upward is negative');
0128 netcdf.putAtt(nc, nlwrs_varid, 'units', 'W m-2');
0129 netcdf.putAtt(nc, nlwrs_varid, 'grid', 'wrf_grid');
0130 netcdf.putAtt(nc, nlwrs_varid, 'coordinates', 'lat lon');
0131 netcdf.putAtt(nc, nlwrs_varid, 'type', 'data');
0132
0133 nshf_varid = netcdf.defVar(nc, 'net_heat_flux', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0134 netcdf.putAtt(nc, nshf_varid, 'long_name', 'Sum of shortwave, longwave, sensible and latent heat fluxes, ocean lose heat is negative');
0135 netcdf.putAtt(nc, nshf_varid, 'units', 'W m-2');
0136 netcdf.putAtt(nc, nshf_varid, 'grid', 'wrf_grid');
0137 netcdf.putAtt(nc, nshf_varid, 'coordinates', 'lat lon');
0138 netcdf.putAtt(nc, nshf_varid, 'type', 'data');
0139
0140 u10_varid = netcdf.defVar(nc, 'U10', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0141 netcdf.putAtt(nc, u10_varid, 'long_name', 'Eastward Wind Velocity');
0142 netcdf.putAtt(nc, u10_varid, 'description', 'U at 10 M');
0143 netcdf.putAtt(nc, u10_varid, 'units', 'm s-1');
0144 netcdf.putAtt(nc, u10_varid, 'grid', 'wrf_grid');
0145 netcdf.putAtt(nc, u10_varid, 'type', 'data');
0146
0147 v10_varid = netcdf.defVar(nc, 'V10', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0148 netcdf.putAtt(nc, v10_varid, 'long_name', 'Northward Wind Velocity');
0149 netcdf.putAtt(nc, v10_varid, 'description', 'V at 10 M');
0150 netcdf.putAtt(nc, v10_varid, 'units', 'm s-1');
0151 netcdf.putAtt(nc, v10_varid, 'grid', 'wrf_grid');
0152 netcdf.putAtt(nc, v10_varid, 'type', 'data');
0153
0154 prate_varid = netcdf.defVar(nc, 'Precipitation', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0155 netcdf.putAtt(nc, prate_varid, 'long_name', 'Precipitation');
0156 netcdf.putAtt(nc, prate_varid, 'description', 'Precipitation, ocean lose water is negative');
0157 netcdf.putAtt(nc, prate_varid, 'units', 'm s-1');
0158 netcdf.putAtt(nc, prate_varid, 'grid', 'wrf_grid');
0159 netcdf.putAtt(nc, prate_varid, 'coordinates', 'lat lon');
0160 netcdf.putAtt(nc, prate_varid, 'type', 'data');
0161
0162 evap_varid = netcdf.defVar(nc, 'Evaporation', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0163 netcdf.putAtt(nc, evap_varid, 'long_name', 'Evaporation');
0164 netcdf.putAtt(nc, evap_varid, 'description', 'Evaporation, ocean lose water is negative');
0165 netcdf.putAtt(nc, evap_varid, 'units', 'm s-1');
0166 netcdf.putAtt(nc, evap_varid, 'grid', 'wrf_grid');
0167 netcdf.putAtt(nc, evap_varid, 'coordinates', 'lat lon');
0168 netcdf.putAtt(nc, evap_varid, 'type', 'data');
0169
0170 pres_varid = netcdf.defVar(nc, 'air_pressure', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0171 netcdf.putAtt(nc, pres_varid, 'long_name', 'Air Pressure');
0172 netcdf.putAtt(nc, pres_varid, 'description', 'Sea surface airpressure');
0173 netcdf.putAtt(nc, pres_varid, 'units', 'Pa');
0174 netcdf.putAtt(nc, pres_varid, 'grid', 'wrf_grid');
0175 netcdf.putAtt(nc, pres_varid, 'coordinates', 'lat lon');
0176 netcdf.putAtt(nc, pres_varid, 'type', 'data');
0177
0178 rh_varid = netcdf.defVar(nc, 'relative_humidity', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0179 netcdf.putAtt(nc, rh_varid, 'long_name', 'Relative humidity');
0180 netcdf.putAtt(nc, rh_varid, 'description', 'Relative humidity');
0181 netcdf.putAtt(nc, rh_varid, 'units', '%');
0182 netcdf.putAtt(nc, rh_varid, 'grid', 'wrf_grid');
0183 netcdf.putAtt(nc, rh_varid, 'coordinates', 'lat lon');
0184 netcdf.putAtt(nc, rh_varid, 'type', 'data');
0185
0186 air_varid = netcdf.defVar(nc, 'air_temperature', 'NC_FLOAT', [we_dimid, sn_dimid, time_dimid]);
0187 netcdf.putAtt(nc, air_varid, 'long_name', 'Air Temperature');
0188 netcdf.putAtt(nc, air_varid, 'description', 'Bulk air temperature');
0189 netcdf.putAtt(nc, air_varid, 'units', 'Celsius');
0190 netcdf.putAtt(nc, air_varid, 'grid', 'wrf_grid');
0191 netcdf.putAtt(nc, air_varid, 'coordinates', 'lat lon');
0192 netcdf.putAtt(nc, air_varid, 'type', 'data');
0193
0194
0195
0196 netcdf.endDef(nc);
0197
0198
0199
0200
0201
0202
0203 nStringOut = char();
0204 [nYr, nMon, nDay, nHour, nMin, nSec] = mjulian2greg(WRF.time);
0205 for tt = 1:ntimes
0206 nDate = [nYr(tt), nMon(tt), nDay(tt), nHour(tt), nMin(tt), nSec(tt)];
0207 nStringOut = [nStringOut, sprintf('%04i/%02i/%02i %02i:%02i:%02i', nDate)];
0208 end
0209 netcdf.putVar(nc, times_varid, [0, 0], [19, ntimes], nStringOut);
0210
0211 netcdf.putVar(nc, x_varid, x');
0212 netcdf.putVar(nc, y_varid, flipdim(y', 2));
0213 netcdf.putVar(nc, nswrs_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.nswrs, 2));
0214 netcdf.putVar(nc, nlwrs_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.nlwrs, 2));
0215 netcdf.putVar(nc, nshf_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.nshf, 2));
0216 netcdf.putVar(nc, u10_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.u10, 2));
0217 netcdf.putVar(nc, v10_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.v10, 2));
0218 netcdf.putVar(nc, prate_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.P_E, 2));
0219 netcdf.putVar(nc, evap_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.evap, 2));
0220 netcdf.putVar(nc, pres_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.pres, 2));
0221 netcdf.putVar(nc, rh_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.rhum, 2));
0222 netcdf.putVar(nc, air_varid, [0, 0, 0], [nwest_east, nsouth_north, ntimes], flipdim(WRF.air, 2));
0223
0224
0225
0226 netcdf.close(nc);
0227
0228 if ftbverbose
0229 fprintf('end : %s \n', subname)
0230 end