Home > fvcom_prepro > write_WRF_forcing.m

write_WRF_forcing

PURPOSE ^

Write data out to WRF format netCDF forcing file.

SYNOPSIS ^

function write_WRF_forcing(WRF, filename)

DESCRIPTION ^

 Write data out to WRF format netCDF forcing file.

 write_WRF_forcing(WRF, filename)

 DESCRIPTION:
   Takes the given regularly gridded forcing data and writes out to a WRF
   format netCDF file.

 INPUT:
   WRF - struct with the following fields:
       lon   : longitude, rectangular array (see MESHGRID).
       lat   : latitude, rectangular array (see MESHGRID).
       time  : Modified Julian Day times.
       pres  : surface pressure [mb]
       nswrs : shortwave radiation (upward = negative) [W/m^{2}]
       nlwrs : longwave radiation (upward = negative) [W/m^{2}]
       nshf  : net surface heat flux (ocean losing = negative) [W/m^{2}]
       u10   : eastward wind velocity [m/s]
       v10   : northward wind velocity [m/s]
       P_E   : precipitation (ocean losing = negative) [m/s]
       evap  : evaporation (ocean losing = negative) [m/s]
       rhum  : relative humidity [%]
       air   : temperature [Celsius]
       Net surface heat flux is defined as the sum of shortwave, longwave,
       sensible and latent heat fluxes (ocean losing heat = negative).
   filename - Output netCDF file name.

 OUTPUT:
   WRF format heating netCDF file.

 EXAMPLE USAGE:
   wrf_file = '/path/to/output/casename_wnd.nc';
   write_WRF_forcing(WRF, wrf_file);

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

 Revision history:
   2013-08-29 - First version based on write_FVCOM_heating.m.
   2013-09-09 - FVCOM version 3.1.6 (official) appears to require a
   net_heat_flux variable (rather than Net_heat). Adjusted accordingly.
   2013-10-24 - Add full suite of variables so that HEATING_CALCULATED can
   be used instead.
   2013-12-05 - Fix names of the fields for long and shortwave radiation
   to match the NCEP ones (from n{l,s}wrf to n{l,s}wrs i.e. change last
   letter from f to s).

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_WRF_forcing(WRF, filename)
0002 % Write data out to WRF format netCDF forcing file.
0003 %
0004 % write_WRF_forcing(WRF, filename)
0005 %
0006 % DESCRIPTION:
0007 %   Takes the given regularly gridded forcing data and writes out to a WRF
0008 %   format netCDF file.
0009 %
0010 % INPUT:
0011 %   WRF - struct with the following fields:
0012 %       lon   : longitude, rectangular array (see MESHGRID).
0013 %       lat   : latitude, rectangular array (see MESHGRID).
0014 %       time  : Modified Julian Day times.
0015 %       pres  : surface pressure [mb]
0016 %       nswrs : shortwave radiation (upward = negative) [W/m^{2}]
0017 %       nlwrs : longwave radiation (upward = negative) [W/m^{2}]
0018 %       nshf  : net surface heat flux (ocean losing = negative) [W/m^{2}]
0019 %       u10   : eastward wind velocity [m/s]
0020 %       v10   : northward wind velocity [m/s]
0021 %       P_E   : precipitation (ocean losing = negative) [m/s]
0022 %       evap  : evaporation (ocean losing = negative) [m/s]
0023 %       rhum  : relative humidity [%]
0024 %       air   : temperature [Celsius]
0025 %       Net surface heat flux is defined as the sum of shortwave, longwave,
0026 %       sensible and latent heat fluxes (ocean losing heat = negative).
0027 %   filename - Output netCDF file name.
0028 %
0029 % OUTPUT:
0030 %   WRF format heating netCDF file.
0031 %
0032 % EXAMPLE USAGE:
0033 %   wrf_file = '/path/to/output/casename_wnd.nc';
0034 %   write_WRF_forcing(WRF, wrf_file);
0035 %
0036 % Author(s):
0037 %   Pierre Cazenave (Plymouth Marine Laboratory)
0038 %
0039 % Revision history:
0040 %   2013-08-29 - First version based on write_FVCOM_heating.m.
0041 %   2013-09-09 - FVCOM version 3.1.6 (official) appears to require a
0042 %   net_heat_flux variable (rather than Net_heat). Adjusted accordingly.
0043 %   2013-10-24 - Add full suite of variables so that HEATING_CALCULATED can
0044 %   be used instead.
0045 %   2013-12-05 - Fix names of the fields for long and shortwave radiation
0046 %   to match the NCEP ones (from n{l,s}wrf to n{l,s}wrs i.e. change last
0047 %   letter from f to s).
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 % Make the range of lon -180 - 180.
0066 if max(x) > 180
0067     x(x > 180) = x(x > 180) - 360;
0068 end
0069 % I've yet to come across a latitude range that isn't -90 - 90, but just in
0070 % case.
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 % Create the netCDF header for the FVCOM forcing file
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 % Dimensions
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); % not sure if this is necessary...
0097 datestrlen_dimid=netcdf.defDim(nc, 'DateStrLen', 19);
0098 time_dimid=netcdf.defDim(nc, 'time', netcdf.getConstant('NC_UNLIMITED'));
0099 
0100 % Space variables
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 % Time variables
0114 times_varid=netcdf.defVar(nc, 'Times', 'NC_CHAR', [datestrlen_dimid, time_dimid]);
0115 netcdf.putAtt(nc, times_varid, 'description', 'GMT time');
0116 % netcdf.putAtt(nc, times_varid, 'format', 'String: Calendar Time');
0117 % netcdf.putAtt(nc, times_varid, 'time_zone', 'UTC');
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 % End definitions
0196 netcdf.endDef(nc);
0197 
0198 %--------------------------------------------------------------------------
0199 % Put the data in the netCDF file.
0200 %--------------------------------------------------------------------------
0201 
0202 % Build the Times string and output to netCDF.
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 % And the rest...
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 % Close the netCDF file
0226 netcdf.close(nc);
0227 
0228 if ftbverbose
0229     fprintf('end   : %s \n', subname)
0230 end

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