Home > fvcom_prepro > get_MetUM_forcing.m

get_MetUM_forcing

PURPOSE ^

Get the required parameters from the Met Office Unified Model (TM)

SYNOPSIS ^

function data = get_MetUM_forcing(Mobj, modelTime, credentials)

DESCRIPTION ^

 Get the required parameters from the Met Office Unified Model (TM)
 (hereafter MetUM) to use in FVCOM surface forcing.

 data = get_MetUM_forcing(Mobj, modelTime, credentials)

 DESCRIPTION:
   Using FTP access, extract the necessary parameters to create an FVCOM
   forcing file. Requires the air_sea toolbox (see below for where to get
   it). Data are sampled four times daily.

 INPUT:
   Mobj - MATLAB mesh object
   modelTime - Modified Julian Date start and end times
   credentials - struct with fields username and password to access the
   FTP server.

 OUTPUT:
   data - struct of the data necessary to force FVCOM. These can be
   interpolated onto an unstructured grid in Mobj using grid2fvcom.m.

 The required parameters which can be obtained are:
     - surface_net_downward_shortwave_flux (W m-2)
     - surface_downwelling_shortwave_flux_in_air (W m-2)
     - surface_net_downward_longwave_flux (W m-2)
     - surface_downwelling_longwave_flux_in_air (W m-2)
     - surface_upward_sensible_heat_flux (W m-2)
     - eastward_wind / x_wind (m s-1)
     - northward_wind / y_wind (m s-1)
     - surface_upward_latent_heat_flux  (W m-2)
     - air_temperature (K)
     - relative_humidity (%)
     - precipitation_flux (kg m-2 s-1)
     - air_pressure_at_sea_level (Pa)

 In addition to these, the momentum flux is calculated from wind data.
 Precipitation is converted from kg/m^2/s to m/s. Evaporation is
 calculated from the mean daily latent heat net flux (lhtfl) at the
 surface.

 REQUIRES:
   The air_sea toolbox:
       http://woodshole.er.usgs.gov/operations/sea-mat/air_sea-html/index.html

 Author(s)
   Pierre Cazenave (Plymouth Marine Laboratory)

 Revision history:
   2013-05-07 First version.

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function data = get_MetUM_forcing(Mobj, modelTime, credentials)
0002 % Get the required parameters from the Met Office Unified Model (TM)
0003 % (hereafter MetUM) to use in FVCOM surface forcing.
0004 %
0005 % data = get_MetUM_forcing(Mobj, modelTime, credentials)
0006 %
0007 % DESCRIPTION:
0008 %   Using FTP access, extract the necessary parameters to create an FVCOM
0009 %   forcing file. Requires the air_sea toolbox (see below for where to get
0010 %   it). Data are sampled four times daily.
0011 %
0012 % INPUT:
0013 %   Mobj - MATLAB mesh object
0014 %   modelTime - Modified Julian Date start and end times
0015 %   credentials - struct with fields username and password to access the
0016 %   FTP server.
0017 %
0018 % OUTPUT:
0019 %   data - struct of the data necessary to force FVCOM. These can be
0020 %   interpolated onto an unstructured grid in Mobj using grid2fvcom.m.
0021 %
0022 % The required parameters which can be obtained are:
0023 %     - surface_net_downward_shortwave_flux (W m-2)
0024 %     - surface_downwelling_shortwave_flux_in_air (W m-2)
0025 %     - surface_net_downward_longwave_flux (W m-2)
0026 %     - surface_downwelling_longwave_flux_in_air (W m-2)
0027 %     - surface_upward_sensible_heat_flux (W m-2)
0028 %     - eastward_wind / x_wind (m s-1)
0029 %     - northward_wind / y_wind (m s-1)
0030 %     - surface_upward_latent_heat_flux  (W m-2)
0031 %     - air_temperature (K)
0032 %     - relative_humidity (%)
0033 %     - precipitation_flux (kg m-2 s-1)
0034 %     - air_pressure_at_sea_level (Pa)
0035 %
0036 % In addition to these, the momentum flux is calculated from wind data.
0037 % Precipitation is converted from kg/m^2/s to m/s. Evaporation is
0038 % calculated from the mean daily latent heat net flux (lhtfl) at the
0039 % surface.
0040 %
0041 % REQUIRES:
0042 %   The air_sea toolbox:
0043 %       http://woodshole.er.usgs.gov/operations/sea-mat/air_sea-html/index.html
0044 %
0045 % Author(s)
0046 %   Pierre Cazenave (Plymouth Marine Laboratory)
0047 %
0048 % Revision history:
0049 %   2013-05-07 First version.
0050 %
0051 %==========================================================================
0052 subname = 'get_MetUM_forcing';
0053 
0054 global ftbverbose;
0055 if ftbverbose;
0056     fprintf('\nbegin : %s \n', subname)
0057 end
0058 
0059 % Get the extent of the model domain (in spherical)
0060 if ~Mobj.have_lonlat
0061     error('Need spherical coordinates to extract the forcing data')
0062 else
0063     % Add a 1 degree buffer to make sure the model domain is fully covered
0064     % by the extracted data.
0065     [dx, dy] = deal(1, 1);
0066     extents = [min(Mobj.lon(:))-(2*dx), max(Mobj.lon(:))+(2*dx), min(Mobj.lat(:))-dy, max(Mobj.lat(:))+dy];
0067 end
0068 
0069 nt = modelTime(end) - modelTime(1);
0070 if nt > 365
0071     error('Can''t (yet) process more than a year at a time.')
0072 end
0073 
0074 [yearStart, monthStart, dayStart] = mjulian2greg(modelTime(1));
0075 [yearEnd, monthEnd, dayEnd] = mjulian2greg(modelTime(end));
0076 t = modelTime(1):1/4:modelTime(end);
0077 
0078 if yearEnd ~= yearStart
0079     error('Can''t (yet) process across a year boundary.')
0080 end
0081 
0082 if yearStart < 2006 || yearEnd > 2012
0083     error('The MetUM repository does not contain data earlier than 2006 and later than 2012')
0084 end
0085 
0086 % For the pre-2006 data, we need to download several files with unique
0087 % names. The names are based on the STASH numbers and the date:
0088 %   naamYYYYMMDDHH_STASH#_00.pp
0089 % The numbers we're interested in are stored in stash.
0090 stash = [2, 3, 407, 408, 409, 4222, 9229, 16004];
0091 vars = {'uwnd', 'uwnd', 'vwnd', 'vwnd', 'slp_rho', 'slp_theta', ...
0092     'surface_air_pressure', 'air_sw', 'air_lw', ...
0093     'rhum', 'prate', 'temp_model', 'temp_press'};
0094 
0095 ns = length(stash);
0096 
0097 % From where will we be downloading the data?
0098 site = 'ftp.ceda.ac.uk';
0099 basePath = 'badc/ukmo-um/data/nae/';
0100 
0101 % Open a remote connection to the FTP site
0102 remote = ftp(site, credentials(1), credentials(2));
0103 
0104 % Depending on the year we're extracting, we need to append different
0105 % directories to get the data.
0106 for i = 1:nt * 4 % four files per day (at 0000, 0600, 1200 and 1800).
0107 
0108     [year, month, day, hour] = mjulian2greg(t(i));
0109 
0110     % Cell array for the files to download.
0111     files = cell(0);
0112 
0113     % Do 2010 first because it straddles the two directories.
0114     if year == 2010
0115         if month < 11 && day < 4
0116             % Use the am data
0117             prefix = 'am';
0118             URL = sprintf('%sna/%s/%04d/%02d/%02d', basePath, ...
0119                 prefix, ...
0120                 year, ...
0121                 month, ...
0122                 day);
0123             for f = 1:ns
0124                 files{f} = sprintf('na%s%04d%02d%02d%02d_%05d_00.pp', ...
0125                     prefix, ...
0126                     year, ...
0127                     month, ...
0128                     day, ...
0129                     hour, ...
0130                     stash(f));
0131             end
0132         elseif month > 11 && day > 3
0133             % Use the mn data
0134             prefix = 'mn';
0135             URL = sprintf('%sna/%s/%04d/%02d/%02d', basePath, ...
0136                 prefix, ...
0137                 year, ...
0138                 month, ...
0139                 day);
0140             files = sprintf('%s_%04d%02d%02d%02d_s00.pp', ...
0141                 prefix, ...
0142                 year, ...
0143                 month, ...
0144                 day, ...
0145                 hour);
0146         end
0147 
0148     % Check the 2006 data are from the 7th November onwards.
0149     elseif year == 2006
0150         if month < 11 
0151             if day < 7
0152                 error('The MetUM repository does not contain data earlier than 7th November, 2006')
0153             else
0154                 prefix = 'am';
0155                 URL = sprintf('%sna/%s/%04d/%02d/%02d', basePath, ...
0156                     prefix, ...
0157                     year, ...
0158                     month, ...
0159                     day);
0160                 for f = 1:ns
0161                     files{f} = sprintf('na%s%04d%02d%02d%02d_%05d_00.pp', ...
0162                         prefix, ...
0163                         year, ...
0164                         month, ...
0165                         day, ...
0166                         hour, ...
0167                         stash(f));
0168                 end
0169             end
0170         end
0171 
0172     % Check the 2012 data are from before the 17th January, 2012.
0173     elseif year == 2012
0174         if month > 1
0175             error('The MetUM repository does not contain data later than 17th January, 2012')
0176         elseif month == 1
0177             if day > 17
0178                 error('The MetUM repository does not contain data later than 17th January, 2012')
0179             else
0180                 prefix = 'mn';
0181             URL = sprintf('%sna/%s/%04d/%02d/%02d', basePath, ...
0182                     prefix, ...
0183                     year, ...
0184                     month, ...
0185                     day);
0186                 files = sprintf('%s_%04d%02d%02d%02d_s00.pp', ...
0187                     prefix, ...
0188                     year, ...
0189                     month, ...
0190                     day, ...
0191                     hour);
0192             end
0193         end
0194 
0195     % Pre-2010 files.
0196     elseif year < 2010
0197         % Use the am data.
0198         prefix = 'am';
0199             URL = sprintf('%sna/%s/%04d/%02d/%02d', basePath, ...
0200             prefix, ...
0201             year, ...
0202             month, ...
0203             day);
0204             for f = 1:ns
0205                 files{f} = sprintf('na%s%04d%02d%02d%02d_%05d_00.pp', ...
0206                     prefix, ...
0207                     year, ...
0208                     month, ...
0209                     day, ...
0210                     hour, ...
0211                     stash(f));
0212             end
0213 
0214     % Post-2010 files.
0215     elseif year > 2010
0216         % Use the mn data.
0217         prefix = 'mn';
0218             URL = sprintf('%sna/%s/%04d/%02d/%02d', basePath, ...
0219             prefix, ...
0220             year, ...
0221             month, ...
0222             day);
0223         files = sprintf('%s_%04d%02d%02d%02d_s00.pp', ...
0224             prefix, ...
0225             year, ...
0226             month, ...
0227             day, ...
0228             hour);
0229     end
0230 
0231     fprintf('%s: %s\n', URL, files{1})
0232 end
0233 
0234 % Close the connection to the FTP server.
0235 close(remote)
0236 
0237 if ftbverbose
0238     fprintf('end   : %s \n', subname)
0239 end
0240 
0241 
0242 function ftpdata = get_badc_data(remote, URL, files)
0243 % Child function to do the actual downloading from the BADC site via FTP.
0244 %
0245 % Inputs:
0246 %
0247 %   remote - FTP object
0248 %   URL - path to the files to download
0249 %   files - cell array of a file or files to download
0250 %
0251 % Outputs:
0252 %
0253 %   noidea...
0254 
0255 if ~iscell(files)
0256     error('Provide a cell array of files to download')
0257 end
0258 
0259 cd(remote, URL);
0260 nf = length(files);
0261 for i = 1:nf
0262     tmpdata = mget(remote, files{i});
0263     ftpdata.(vars{i}).data = 
0264 end
0265 
0266 
0267 function pp2nc(file, convsh)
0268 % Child function to call the convsh program to convert the obscure pp
0269 % format to a sensible NetCDF which we can more easily read.
0270 
0271 % Assume convsh is in /usr/local unless otherwise told.
0272 if nargin == 1
0273     convsh = '/usr/local/bin/convsh';
0274 end
0275 
0276 if exist(file, 'file') ~= 2
0277     error('File %s not found', file)
0278 end
0279 
0280 [path, name, ext] = fileparts(file);
0281 out = fullfile(path, [name, '.nc']);
0282 
0283 system([convsh, '-i ',

Generated on Tue 04-Jun-2013 12:12:57 by m2html © 2005