


Interpolate regularly gridded surface forcing data onto a given FVCOM
grid.
grid2fvcom(Mobj,vars,data)
DESCRIPTION:
Takes a given NCEP reanalysis grid file and interpolates the U10 and
V10 values onto the specified FVCOM grid file.
INPUT:
Mobj - MATLAB mesh object
vars - a cell array of the variables to be interpolated on the FVCOM
grid in Mobj (e.g. uwnd, U10, vwnd, V10 etc.).
data - a struct which contains the following arrays:
x - x data (probably best in cartesian for the interpolation)
y - y data (probably best in cartesian for the interpolation)
The struct must also contain all the variables defined in vars.
time - time vector (in Modified Julian Days)
OUTPUT:
fvcom - struct of the interpolated data values at the model nodes and
element centres. Also includes any variables which were in the input
struct but which have not been interpolated (e.g. time).
NOTE:
The shape of the returned arrays for rhum and slp (via
get_NCEP_forcing.m) have sometimes differed from the other vairables
(they appear to be projected onto a different grid). Unless you
desperately need them, I would suggest omitting them from the
interpolation here as this assumes the arrays are all the same size.
Author(s):
Pierre Cazenave (Plymouth Marine Laboratory)
Revision history:
2012-10-15 First version based on ncep2fvcom_U10V10.m in the
fvcom-toolbox.
2012-10-16 Removed the code to read the NCEP file. Instead, farmed that
out to a new function (read_NCEP_wind) so that the relevant section can
be more readily extracted (rather than using the entire globe's data:
it's easier to subsample and provide the subsampled data here).
2012-10-17 Add outputs to the function for use in visualisation.
2012-10-19 Add wind struct as input rather than separate u, v, time and
lat/long arrays. Makes invocation a bit cleaner.
2012-11-01 Farmed out the creation of the NetCDF file to
write_FVCOM_forcing.m and made this purely an interpolation script.
==========================================================================

0001 function fvcom = grid2fvcom(Mobj,vars,data) 0002 % Interpolate regularly gridded surface forcing data onto a given FVCOM 0003 % grid. 0004 % 0005 % grid2fvcom(Mobj,vars,data) 0006 % 0007 % DESCRIPTION: 0008 % Takes a given NCEP reanalysis grid file and interpolates the U10 and 0009 % V10 values onto the specified FVCOM grid file. 0010 % 0011 % INPUT: 0012 % Mobj - MATLAB mesh object 0013 % vars - a cell array of the variables to be interpolated on the FVCOM 0014 % grid in Mobj (e.g. uwnd, U10, vwnd, V10 etc.). 0015 % data - a struct which contains the following arrays: 0016 % x - x data (probably best in cartesian for the interpolation) 0017 % y - y data (probably best in cartesian for the interpolation) 0018 % The struct must also contain all the variables defined in vars. 0019 % time - time vector (in Modified Julian Days) 0020 % 0021 % OUTPUT: 0022 % fvcom - struct of the interpolated data values at the model nodes and 0023 % element centres. Also includes any variables which were in the input 0024 % struct but which have not been interpolated (e.g. time). 0025 % 0026 % NOTE: 0027 % The shape of the returned arrays for rhum and slp (via 0028 % get_NCEP_forcing.m) have sometimes differed from the other vairables 0029 % (they appear to be projected onto a different grid). Unless you 0030 % desperately need them, I would suggest omitting them from the 0031 % interpolation here as this assumes the arrays are all the same size. 0032 % 0033 % Author(s): 0034 % Pierre Cazenave (Plymouth Marine Laboratory) 0035 % 0036 % Revision history: 0037 % 2012-10-15 First version based on ncep2fvcom_U10V10.m in the 0038 % fvcom-toolbox. 0039 % 2012-10-16 Removed the code to read the NCEP file. Instead, farmed that 0040 % out to a new function (read_NCEP_wind) so that the relevant section can 0041 % be more readily extracted (rather than using the entire globe's data: 0042 % it's easier to subsample and provide the subsampled data here). 0043 % 2012-10-17 Add outputs to the function for use in visualisation. 0044 % 2012-10-19 Add wind struct as input rather than separate u, v, time and 0045 % lat/long arrays. Makes invocation a bit cleaner. 0046 % 2012-11-01 Farmed out the creation of the NetCDF file to 0047 % write_FVCOM_forcing.m and made this purely an interpolation script. 0048 % 0049 %========================================================================== 0050 0051 if nargin ~= 3 0052 error('Incorrect number of arguments') 0053 end 0054 0055 subname = 'grid2fvcom'; 0056 0057 global ftbverbose; 0058 if(ftbverbose) 0059 fprintf('\n') 0060 fprintf(['begin : ' subname '\n']) 0061 end 0062 0063 %-------------------------------------------------------------------------- 0064 % Get the relevant bits from the FVCOM mesh object 0065 %-------------------------------------------------------------------------- 0066 x = Mobj.x; 0067 y = Mobj.y; 0068 nVerts = Mobj.nVerts; 0069 nElems = Mobj.nElems; 0070 if(ftbverbose); 0071 fprintf('info for FVCOM domain\n'); 0072 fprintf('number of nodes: %d\n',nVerts); 0073 fprintf('number of elems: %d\n',nElems); 0074 end 0075 0076 xc = nodes2elems(x, Mobj); 0077 yc = nodes2elems(y, Mobj); 0078 0079 try 0080 ntimes = numel(data.time); 0081 catch 0082 ntimes = numel(data.(vars{1}).time); 0083 end 0084 0085 % Interpolate supplied regularly gridded data to FVCOM mesh. Use 0086 % TriScatteredInterp to do the interpolation instead of griddata (should be 0087 % faster). 0088 for vv=1:length(vars) 0089 if strcmpi(vars{vv}, 'time') 0090 fprintf('transferring variable %s as is\n', vars{vv}) 0091 fvcom.(vars{vv}) = data.(vars{vv}); 0092 continue 0093 elseif strcmpi(vars{vv}, 'lat') || strcmpi(vars{vv}, 'lon') || strcmpi(vars{vv}, 'x') || strcmpi(vars{vv}, 'y') 0094 fprintf('reassigning variable %s from unstructured grid\n', vars{vv}) 0095 fvcom.(vars{vv}) = Mobj.(vars{vv}); 0096 else 0097 % Preallocate the output arrays 0098 fvcom.(vars{vv}).data = zeros(nElems,ntimes); 0099 fvcom.(vars{vv}).node = zeros(nVerts,ntimes); 0100 0101 for i=1:ntimes 0102 fprintf('interpolating %s, frame %d of %d\n', vars{vv}, i, ntimes); 0103 currvar = data.(vars{vv}).data(:, :, i); 0104 % griddata way (cubic interpolation) 0105 %fvcom.(vars{vv}).node(:,i) = griddata(wind.x,wind.y,currvar,x,y,'cubic'); 0106 %fvcom.(vars{vv}).data(:,i) = griddata(wind.x,wind.y,currvar,xc,yc,'cubic'); 0107 % TriScatteredInterp way (with natural neighbour interpolation) 0108 ftsin = TriScatteredInterp(data.x(:), data.y(:), currvar(:), 'natural'); 0109 fvcom.(vars{vv}).node(:,i) = ftsin(x,y); 0110 fvcom.(vars{vv}).data(:,i) = ftsin(xc,yc); 0111 nnans(1) = sum(isnan(fvcom.(vars{vv}).node(:,i))); 0112 nnans(2) = sum(isnan(fvcom.(vars{vv}).data(:,i))); 0113 if nnans(1) > 0 0114 warning('%i NaNs in the interpolated node data. This won''t work with FVCOM.', nnans(1)) 0115 end 0116 if nnans(2) > 0 0117 warning('%i NaNs in the interpolated element data. This won''t work with FVCOM.', nnans(2)) 0118 end 0119 end 0120 fprintf('interpolation of %s complete\n', vars{vv}); 0121 end 0122 end 0123 0124 if ftbverbose; 0125 fprintf(['end : ' subname '\n']) 0126 end