Home > fvcom_prepro > read_ERA_wind.m

read_ERA_wind

PURPOSE ^

Reads in ERA Interim files and outputs a struct containing the requested

SYNOPSIS ^

function era = read_ERA_wind(year, datadir, varlist)

DESCRIPTION ^

 Reads in ERA Interim files and outputs a struct containing the requested
 variables for a given year. 
 
 ERA = read_ERA_wind(YEAR, DATADIR, VARLIST)
 
 DESCRIPTION:
   For the given YEAR, find all the ERA Interim NetCDF files and aggregate
   them into a single MATLAB struct, which contains the variables
   specified in VARLIST. In addition to the specified variables, time and
   longitude and latitude will also be extracted. 
 
 INPUT:
   YEAR - year to extract
   DATADIR - path to the directory which contains the ERA NetCDF files
   VARLIST - list of the particular variables to extract from the NetCDF
   files.
 
 OUTPUT:
   era - struct containing the variables specified in VARLIST.
 
 Author(s)
   Pierre Cazenave (Plymouth Marine Laboratory)
 
 Revision history:
   2012-10-19 First version based loosely on read_NCEP_wind.m in the
   fvcom-toolbox.
 
==========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function era = read_ERA_wind(year, datadir, varlist)
0002 % Reads in ERA Interim files and outputs a struct containing the requested
0003 % variables for a given year.
0004 %
0005 % ERA = read_ERA_wind(YEAR, DATADIR, VARLIST)
0006 %
0007 % DESCRIPTION:
0008 %   For the given YEAR, find all the ERA Interim NetCDF files and aggregate
0009 %   them into a single MATLAB struct, which contains the variables
0010 %   specified in VARLIST. In addition to the specified variables, time and
0011 %   longitude and latitude will also be extracted.
0012 %
0013 % INPUT:
0014 %   YEAR - year to extract
0015 %   DATADIR - path to the directory which contains the ERA NetCDF files
0016 %   VARLIST - list of the particular variables to extract from the NetCDF
0017 %   files.
0018 %
0019 % OUTPUT:
0020 %   era - struct containing the variables specified in VARLIST.
0021 %
0022 % Author(s)
0023 %   Pierre Cazenave (Plymouth Marine Laboratory)
0024 %
0025 % Revision history:
0026 %   2012-10-19 First version based loosely on read_NCEP_wind.m in the
0027 %   fvcom-toolbox.
0028 %
0029 %==========================================================================
0030 
0031 % year = 2006;
0032 % % datadir = '/data/modellers/to_archive/momm-ERA40-interim/';
0033 % datadir = '/users/modellers/pica/Data/ECMWF/2006';
0034 % varlist = {'u10', 'v10'};
0035 
0036 warning off
0037 
0038 if nargin ~= 3
0039     error('Incorrect number of arguments')
0040 end
0041 
0042 subname = 'read_ERA_wind';
0043 
0044 global ftbverbose;
0045 if(ftbverbose);
0046   fprintf('\n')
0047   fprintf(['begin : ' subname '\n'])
0048 end;
0049 
0050 if exist(datadir, 'dir') ~= 7
0051    error(['file: ' datadir ' does not exist']);
0052 end
0053 
0054 %--------------------------------------------------------------------------
0055 % Open ERA Interim data and check for time range
0056 %--------------------------------------------------------------------------
0057 
0058 % Get the time.
0059 ncERA = netcdf.open(fullfile(datadir, [num2str(year), '_U10V10.nc']), 'NOWRITE');
0060 time_varid = netcdf.inqVarID(ncERA, 'time');
0061 eratimehours = netcdf.getVar(ncERA, time_varid);
0062 
0063 % ERA Interim times are stored as hours since 1900-01-01 00:00:0.0.
0064 % MATLAB's dates days since 0000/00/00 00:00:00.
0065 eratimehours = datevec((double(eratimehours)/24) + datenum('1900-01-01 00:00:0.0'));
0066 % Convert the ERA times to Modified Julian Date.
0067 era.time = greg2mjulian(eratimehours(:,1), eratimehours(:,2),...
0068     eratimehours(:,3), eratimehours(:,4), eratimehours(:,5),...
0069     eratimehours(:,6));
0070 
0071 if(ftbverbose);
0072     fprintf('beg time of ERA Interim data %04i/%02i/%02i %02i:%02i:%02i\n', eratimehours(1,:));
0073     fprintf('end time of ERA Interim data %04i/%02i/%02i %02i:%02i:%02i\n', eratimehours(end,:));
0074 end
0075 
0076 % Get the geographical information from the ERA Interim data. Again, use
0077 % the U10 file only (we're assuming they're both global).
0078 lat_varid = netcdf.inqVarID(ncERA, 'latitude');
0079 lon_varid = netcdf.inqVarID(ncERA, 'longitude');
0080 eralatvector = netcdf.getVar(ncERA, lat_varid);
0081 eralonvector = netcdf.getVar(ncERA, lon_varid);
0082 [era.lon, era.lat] = meshgrid(eralonvector, eralatvector);
0083 
0084 % Find the necessary variables
0085 for var=1:numel(varlist)
0086 
0087     getVar = varlist{var};
0088     varid_ERA = netcdf.inqVarID(ncERA, getVar);
0089 
0090     % Get the data
0091     data = netcdf.getVar(ncERA, varid_ERA, 'single');
0092     
0093     if strcmpi(getVar, 'u10') || strcmpi(getVar, 'v10')
0094         % The ERA Interim wind component data are packed as integers. The
0095         % following equation describes how to unpack them:
0096         %     unpacked value = add_offset + ((packed value)*scale_factor)
0097         % (from
0098         % http://www.ecmwf.int/products/data/archive/data_faq.html#netcdfintegers).
0099         % ERA wind scale_factor and add_offset are doubles (the NCEP ones
0100         % are singles).
0101         scale_factor = netcdf.getAtt(ncERA,varid_ERA,'scale_factor','double');
0102         add_offset = netcdf.getAtt(ncERA,varid_ERA,'add_offset','double');
0103 
0104         % Unpack the values. In general, the data for U10 and V10 should be
0105         % doubles for griddata to work. Fix the order of the dimensions to
0106         % match the coordinates in eralon and eralat.
0107         era.(getVar) = permute(double(add_offset + (data.*scale_factor)), [2,1,3]);
0108     else
0109         % We're assuming they're not packed and so we just return the data
0110         % as is (but as doubles).
0111         era.(getVar) = double(data);
0112     end
0113 end
0114 
0115 netcdf.close(ncERA)
0116 
0117 if ftbverbose;
0118     fprintf(['end   : ' subname '\n'])
0119 end

Generated on Mon 04-Feb-2013 14:22:28 by m2html © 2005