Home > fvcom_prepro > interp_sst_assimilation.m

interp_sst_assimilation

PURPOSE ^

Interpolate SST values to a given FVCOM mesh object.

SYNOPSIS ^

function Mobj = interp_sst_assimilation(Mobj, conf, output_file)

DESCRIPTION ^

 Interpolate SST values to a given FVCOM mesh object.

 Mobj = interp_sst_assimilation(Mobj, year, sst_dir, file_pattern)

 DESCRIPTION:
   Interpolate SST data from remote sensing data onto the supplied model
   grid.

 INPUT:
   Mobj - MATLAB mesh object containing fields:
       lon, lat - node coordinates (spherical)
       lonc, latc - element coordinates (spherical)
       tri - element triangulation table
   conf - struct with fields:
       sst_dir - directory containing the SST data
       sst_pattern - file name pattern for the SST data
       year - year for which to generate SST data
   output_file - path to which to output the netCDF

 OUTPUT:
   FVCOM data assimilation SST netCDF file.
   Mobj - input MATLAB mesh object with added 'assim.sst' field, with
   fields:
       data - the SST data
       time - the SST time series

 EXAMPLE USAGE:
   Mobj = read_fvcom_mesh('casename.grd');
   conf.sst_dir = '/home/user/GHRSST/';
   conf.sst_pattern = '-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc';
   interp_sst_assimilation(Mobj, conf, 'casename_sstgrd.nc');

 Author(s)
   Ricardo Torres (Plymouth Marine Laboratory)
   Pierre Cazenave (Plymouth Marine Laboratory)

 History:
   2017-08-02 Turned the script into a function.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function Mobj = interp_sst_assimilation(Mobj, conf, output_file)
0002 % Interpolate SST values to a given FVCOM mesh object.
0003 %
0004 % Mobj = interp_sst_assimilation(Mobj, year, sst_dir, file_pattern)
0005 %
0006 % DESCRIPTION:
0007 %   Interpolate SST data from remote sensing data onto the supplied model
0008 %   grid.
0009 %
0010 % INPUT:
0011 %   Mobj - MATLAB mesh object containing fields:
0012 %       lon, lat - node coordinates (spherical)
0013 %       lonc, latc - element coordinates (spherical)
0014 %       tri - element triangulation table
0015 %   conf - struct with fields:
0016 %       sst_dir - directory containing the SST data
0017 %       sst_pattern - file name pattern for the SST data
0018 %       year - year for which to generate SST data
0019 %   output_file - path to which to output the netCDF
0020 %
0021 % OUTPUT:
0022 %   FVCOM data assimilation SST netCDF file.
0023 %   Mobj - input MATLAB mesh object with added 'assim.sst' field, with
0024 %   fields:
0025 %       data - the SST data
0026 %       time - the SST time series
0027 %
0028 % EXAMPLE USAGE:
0029 %   Mobj = read_fvcom_mesh('casename.grd');
0030 %   conf.sst_dir = '/home/user/GHRSST/';
0031 %   conf.sst_pattern = '-JPL-L4_GHRSST-SSTfnd-MUR-GLOB-v02.0-fv04.1.nc';
0032 %   interp_sst_assimilation(Mobj, conf, 'casename_sstgrd.nc');
0033 %
0034 % Author(s)
0035 %   Ricardo Torres (Plymouth Marine Laboratory)
0036 %   Pierre Cazenave (Plymouth Marine Laboratory)
0037 %
0038 % History:
0039 %   2017-08-02 Turned the script into a function.
0040 
0041 [~, subname] = fileparts(mfilename('fullpath'));
0042 global ftbverbose
0043 if ftbverbose
0044     fprintf('\nbegin : %s \n', subname)
0045 end
0046 
0047 year = conf.year;
0048 % SST files.
0049 sst_dir = conf.sst_dir;
0050 sst_pattern = conf.sst_pattern;
0051 
0052 time_span = datenum(year-1,12,31):datenum(year+1,01,01);
0053 file_list = cell(length(time_span), 1);
0054 % Add last day from the previous year and the first day of the following
0055 % year.
0056 for aa = 1:length(time_span)
0057     file_list{aa} = fullfile(sst_dir, ...
0058         datestr(time_span(aa),'yyyy'), ...
0059         sprintf(sst_pattern, ...
0060         str2num(datestr(time_span(aa), 'yyyy')), ...
0061         str2num(datestr(time_span(aa), 'mm')), ...
0062         str2num(datestr(time_span(aa), 'dd'))));
0063     if ~exist(file_list{aa}, 'file')
0064         error('We are missing a file (%s) from this year (%04d)', ...
0065             file_list{aa}, datestr(time_span(aa),'yyyy'))
0066     end
0067 end
0068 
0069 % Read SST data files and interpolate each to the FVCOM mesh
0070 lon = ncread(file_list{1},'lon');
0071 lat = ncread(file_list{1},'lat');
0072 mask = ncread(file_list{1},'mask');
0073 [lonm,latm]=meshgrid(lon,lat);
0074 lonm=lonm';
0075 latm=latm';
0076 lonm = lonm(mask==1);
0077 latm = latm(mask==1);
0078 time = zeros(length(file_list),1);
0079 sst = zeros(Mobj.nVerts,length(file_list),1,'single');
0080 fvcomlon = Mobj.lon;
0081 fvcomlat = Mobj.lat;
0082 
0083 if license('test', 'Distrib_Computing_Toolbox')
0084     if isempty(gcp('nocreate'))
0085         % Force pool to be local in case we have remote pools available.
0086         parpool('local');
0087     end
0088 end
0089 
0090 if ftbverbose
0091     fprintf('Progress:\n');
0092     fprintf([repmat('.', 1, length(file_list)), '\n']);
0093 end
0094 parfor ff = 1:length(file_list)
0095     if ftbverbose
0096         fprintf('|');
0097     end
0098     sst_eo = ncread(file_list{ff}, 'analysed_sst') - 273.15;
0099     mask = ncread(file_list{ff}, 'mask');
0100     lon = ncread(file_list{ff}, 'lon');
0101     lat = ncread(file_list{ff}, 'lat');
0102     [lonm, latm] = meshgrid(lon, lat);
0103     lonm = lonm';
0104     latm = latm';
0105     lonm = lonm(mask == 1);
0106     latm = latm(mask == 1);
0107 
0108     time_eo = ncread(file_list{ff}, 'time');
0109     time_eo_units = ncreadatt(file_list{ff}, 'time', 'units');
0110     t0str = textscan(time_eo_units, 'seconds since %s%s');
0111 
0112     t0 = datenum([strtrim(t0str{1}{1}), strtrim(t0str{2}{1})], 'yyyy-mm-ddHH:MM:SS');
0113     time_out = (t0 + double(time_eo/(60*60*24)));
0114     sst_eo = sst_eo(mask == 1);
0115 
0116     % Build interpolant
0117     ft = scatteredInterpolant(double(lonm), double(latm), sst_eo, 'nearest', 'linear');
0118 
0119     sst(:,ff) = ft(fvcomlon, fvcomlat);
0120     time(ff) = time_out + 0.5; % fvcom expects these to be at mid-day
0121 end
0122 
0123 ntimes = length(time);
0124 
0125 % Do the times.
0126 [sYr, sMon, sDay, sHr, sMin, sSec] = datevec(time);
0127 MJDtime = greg2mjulian(sYr, sMon, sDay, sHr, sMin, sSec);
0128 
0129 % Create netCDF file
0130 nc = netcdf.create(output_file, 'clobber');
0131 
0132 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'year', num2str(year))
0133 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'title','FVCOM SST 1km merged product File')
0134 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'institution','Plymouth Marine Laboratory')
0135 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'source','FVCOM grid (unstructured) surface forcing')
0136 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'history', sprintf('File created with %s from the MATLAB fvcom-toolbox', subname))
0137 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'references','http://fvcom.smast.umassd.edu, http://codfish.smast.umassd.edu')
0138 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'Conventions','CF-1.0')
0139 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'CoordinateSystem',Mobj.nativeCoords)
0140 netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'CoordinateProjection','init=WGS84') % WGS84?
0141 
0142 % Dimensions
0143 nele_dimid=netcdf.defDim(nc,'nele',Mobj.nElems);
0144 node_dimid=netcdf.defDim(nc,'node',Mobj.nVerts);
0145 three_dimid=netcdf.defDim(nc,'three',3);
0146 time_dimid=netcdf.defDim(nc,'time',netcdf.getConstant('NC_UNLIMITED'));
0147 datestrlen_dimid=netcdf.defDim(nc,'DateStrLen',26);
0148 
0149 % Space variables
0150 lon_varid=netcdf.defVar(nc,'lon','NC_FLOAT',node_dimid);
0151 netcdf.putAtt(nc,lon_varid,'long_name','nodal longitude');
0152 netcdf.putAtt(nc,lon_varid,'units','degrees_easdt');
0153 
0154 lat_varid=netcdf.defVar(nc,'lat','NC_FLOAT',node_dimid);
0155 netcdf.putAtt(nc,lat_varid,'long_name','nodal latitude');
0156 netcdf.putAtt(nc,lat_varid,'units','degrees_north');
0157 
0158 lonc_varid=netcdf.defVar(nc,'lonc','NC_FLOAT',nele_dimid);
0159 netcdf.putAtt(nc,lonc_varid,'long_name','zonal longitude');
0160 netcdf.putAtt(nc,lonc_varid,'units','meters');
0161 
0162 latc_varid=netcdf.defVar(nc,'latc','NC_FLOAT',nele_dimid);
0163 netcdf.putAtt(nc,latc_varid,'long_name','zonal latitude');
0164 netcdf.putAtt(nc,latc_varid,'units','meters');
0165 
0166 nv_varid=netcdf.defVar(nc,'nv','NC_INT',[nele_dimid, three_dimid]);
0167 netcdf.putAtt(nc,nv_varid,'long_name','nodes surrounding element');
0168 
0169 % Time variables
0170 time_varid=netcdf.defVar(nc,'time','NC_FLOAT',time_dimid);
0171 netcdf.putAtt(nc,time_varid,'long_name','time');
0172 netcdf.putAtt(nc,time_varid,'units','days since 1858-11-17 00:00:00');
0173 netcdf.putAtt(nc,time_varid,'delta_t','0000-00-00 01:00:00')
0174 
0175 netcdf.putAtt(nc,time_varid,'format','modified julian day (MJD)');
0176 netcdf.putAtt(nc,time_varid,'time_zone','UTC');
0177 
0178 times_varid=netcdf.defVar(nc,'Times','NC_CHAR',[datestrlen_dimid,time_dimid]);
0179 netcdf.putAtt(nc,times_varid,'long_name','Calendar Date');
0180 netcdf.putAtt(nc,times_varid,'format','String: Calendar Time');
0181 netcdf.putAtt(nc,times_varid,'time_zone','UTC');
0182 
0183 sst_varid = netcdf.defVar(nc, 'sst', 'NC_FLOAT', [node_dimid, time_dimid]);
0184 netcdf.putAtt(nc, sst_varid, 'long_name', 'sea surface Temperature');
0185 netcdf.putAtt(nc, sst_varid, 'units', 'Celsius Degree');
0186 netcdf.putAtt(nc, sst_varid, 'grid', 'fvcom_grid');
0187 netcdf.putAtt(nc, sst_varid, 'coordinates', Mobj.nativeCoords);
0188 netcdf.putAtt(nc, sst_varid, 'type', 'data');
0189 
0190 % End definitions
0191 netcdf.endDef(nc);
0192 
0193 % Put the easy ones in first.
0194 netcdf.putVar(nc, nv_varid, Mobj.tri);
0195 netcdf.putVar(nc,lon_varid,Mobj.lon);
0196 netcdf.putVar(nc,lat_varid,Mobj.lat);
0197 netcdf.putVar(nc,lonc_varid,Mobj.lonc);
0198 netcdf.putVar(nc,latc_varid,Mobj.latc);
0199 netcdf.putVar(nc,time_varid,0,ntimes,MJDtime);
0200 
0201 nStringOut = char();
0202 [nYr, nMon, nDay, nHour, nMin, nSec] = mjulian2greg(MJDtime);
0203 for i=1:ntimes
0204     nDate = [nYr(i), nMon(i), nDay(i), nHour(i), nMin(i), nSec(i)];
0205     nStringOut = [nStringOut, sprintf('%04i/%02i/%02i %02i:%02i:%09.6f', nDate)];
0206 end
0207 netcdf.putVar(nc,times_varid,[0, 0], [26, ntimes], nStringOut);
0208 
0209 netcdf.putVar(nc, sst_varid, [0, 0], [Mobj.nVerts, ntimes], sst)
0210 fprintf('done.\n')
0211 
0212 % Close the netCDF file(s)
0213 netcdf.close(nc);
0214 
0215 Mobj.assim.sst.data = sst;
0216 Mobj.assim.sst.time = time;
0217 
0218 % Plot sst if needed.
0219 % for aa = 1:size(sst,2)
0220 %     plot_field(Mobj,sst(:,aa))
0221 %     pause
0222 % end
0223 
0224 if ftbverbose
0225     fprintf('end   : %s \n', subname)
0226 end

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