Home > fvcom_prepro > get_cs3x.m

get_cs3x

PURPOSE ^

Extract boundary forcing information from NOC Operational Surge Model

SYNOPSIS ^

function [Mobj] = get_cs3x(Mobj,inputConf,SetupFile,DataFile)

DESCRIPTION ^

 Extract boundary forcing information from NOC Operational Surge Model
 output.

 function get_cs3x(Mobj,inputConf,SetupFile,DataFile)

 DESCRIPTION:
    Extract boundary forcing information from NOC Operational Surge
    Model output and interpolate to FVCOM open boundary nodes.

 INPUT
    Mobj          = Matlab mesh object
    inputConf     = Structure containing start and end date and time for
                    FVCOM run
    SetupFile     = Location of surge model setup file
    DataFile      = Location of surge model data file
 
 OUTPUT:
    Mobj.surfaceElevation = Addition to Matlab mesh object. Timeseries of 
                            surface elevation at each open boundary point

 EXAMPLE USAGE
    function get_cs3x(Mobj,inputConf,SetupFile,DataFile)

 Author(s):  
    Karen Amoudry (National Oceanography Centre Liverpool)

 Revision history
    2014-01-09 First version.
   
==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Mobj] = get_cs3x(Mobj,inputConf,SetupFile,DataFile)
0002 
0003 % Extract boundary forcing information from NOC Operational Surge Model
0004 % output.
0005 %
0006 % function get_cs3x(Mobj,inputConf,SetupFile,DataFile)
0007 %
0008 % DESCRIPTION:
0009 %    Extract boundary forcing information from NOC Operational Surge
0010 %    Model output and interpolate to FVCOM open boundary nodes.
0011 %
0012 % INPUT
0013 %    Mobj          = Matlab mesh object
0014 %    inputConf     = Structure containing start and end date and time for
0015 %                    FVCOM run
0016 %    SetupFile     = Location of surge model setup file
0017 %    DataFile      = Location of surge model data file
0018 %
0019 % OUTPUT:
0020 %    Mobj.surfaceElevation = Addition to Matlab mesh object. Timeseries of
0021 %                            surface elevation at each open boundary point
0022 %
0023 % EXAMPLE USAGE
0024 %    function get_cs3x(Mobj,inputConf,SetupFile,DataFile)
0025 %
0026 % Author(s):
0027 %    Karen Amoudry (National Oceanography Centre Liverpool)
0028 %
0029 % Revision history
0030 %    2014-01-09 First version.
0031 %
0032 %==============================================================================
0033 subname = 'get_cs3x';
0034 global ftbverbose;
0035 if(ftbverbose);
0036   fprintf('\n')
0037   fprintf(['begin : ' subname '\n'])
0038 end;
0039 
0040 %% Put the open boundary nodes into a single array for convenience
0041 count = 0;
0042 ObcNodes = nan(1,sum(Mobj.nObcNodes));
0043 for ob=1:Mobj.nObs
0044     nObcs = Mobj.nObcNodes(ob);
0045     for j=1:nObcs
0046         count = count + 1;
0047         ObcNodes(count) = Mobj.obc_nodes(ob,j);  % set the open boundary nodes
0048     end
0049 end
0050 
0051 % Open the compression info file
0052 fid=fopen(SetupFile,'r','b');
0053 temp=fread(fid,inf,'*int32','b');
0054 fclose(fid);
0055 
0056 NRR = temp(2);  % Number of rows in full rectangle
0057 NCC = temp(3);  % Number of columns in full rectangle
0058 ITOT = temp(4); % Total number of points in compact arrays
0059 
0060 NTRNS = temp(8:NRR+7);  % Transformation matrix for compact addressing
0061 NTRNT = temp(NRR+10:2*NRR+9);   % Transformation matrix for compact addressing
0062 
0063 clear temp
0064 
0065 % Define the parameters of the cs3x grid
0066 cs3x_lonstart = -19-(5/6); % longitude of the bottom left corner
0067 cs3x_latstart = 40+(1/9);   % latitude of the bottom left corner
0068 cs3x_loninc = 1/6;   % grid resolution in the x direction (degrees)
0069 cs3x_latinc = 1/9;   % grid resolution in the y direction (degrees)
0070 cs3x_lonfin = double(cs3x_lonstart+(cs3x_loninc*(NCC-1)));  % longitude of the top right corner
0071 cs3x_latfin = double(cs3x_latstart+(cs3x_latinc*(NRR-1)));  % latitude of the top right corner
0072 cs3x_lon = cs3x_lonstart:cs3x_loninc:cs3x_lonfin;   % array of grid lon points
0073 cs3x_lat = cs3x_latstart:cs3x_latinc:cs3x_latfin;   % array of grid lat points
0074 % cs3x_area = cs3x_loninc*cs3x_latinc;  % I'll need this if I do velocity
0075 
0076 % Sanity check. Does our FVCOM grid fit within the cs3x domain?
0077 if min(Mobj.lon) < cs3x_lonstart || max(Mobj.lon) > cs3x_lonfin || ...
0078         min(Mobj.lat) < cs3x_latstart || max(Mobj.lat) > cs3x_latfin
0079     error('Your FVCOM grid is bigger than the available cs3x grid. Choose another met forcing option or crop your FVCOM grid.')
0080 end
0081 
0082 E = zeros(NCC,NRR);     % Initialise the elevation array
0083 % U = zeros(NCC,NRR);     % Initialise the u-velocity array
0084 % V = zeros(NCC,NRR);     % Initialise the v-velocity array
0085 
0086 % How many files do we need to open? One per year
0087 timesteps = datevec(datenum(inputConf.startDate):1/24:datenum(inputConf.endDate));
0088 
0089 % Find the number of years in the timeseries
0090 [years,~] = unique(timesteps(:,1),'rows');
0091 %%
0092 for p = 1:size(years,1)
0093     % Construct the data filename
0094     dfile = [DataFile,num2str(years(p))];
0095     
0096     % Open the surge model data file
0097     fid = fopen(dfile,'r','b');
0098     
0099     PASTIT = false;
0100     
0101     while PASTIT == false
0102         
0103         % Read the data file
0104         dump=fread(fid,1,'*int32','b');   % Don't need this bit
0105         
0106         if feof(fid)
0107             PASTIT = true;
0108         else
0109             datem = double(fread(fid,5,'*int32','b'));  % start date of data file
0110             
0111             % Start date of data file in MJD format
0112             mjd4 = greg2mjulian(datem(4),datem(3),datem(2),datem(1),0,0)+(datem(5)/24);
0113             
0114             if p<size(years,1) && (mjd4 >= greg2mjulian(years(p+1),1,1,0,0,0))
0115                 PASTIT = true;
0116                 
0117                 % If the date is in the range we want
0118             elseif (mjd4 >= inputConf.startDateMJD) && (mjd4 <= inputConf.endDateMJD)
0119                 
0120                 dump=fread(fid,2,'*int32','b');   % Don't need this bit
0121                 elev = fread(fid,ITOT,'*single','b');    % read in elevation
0122                 dump=fread(fid,2,'*int32','b');   % Don't need this bit
0123                 u = fread(fid,ITOT,'*single','b');    % read in u velocity
0124                 dump=fread(fid,2,'*int32','b');   % Don't need this bit
0125                 v = fread(fid,ITOT,'*single','b');    % read in v velocity
0126                 dump=fread(fid,1,'*int32','b');   % Don't need this bit
0127                 
0128                 k = 0;
0129                 
0130                 % Reshape data into x,y array
0131                 for j = 0:NRR-1
0132                     I1 = NTRNT(j+1);
0133                     I2 = NTRNS(j+1);
0134                     for i = I1+1:I2
0135                         k = k+1;
0136                         E(i,NRR-j) = elev(k);
0137                     end
0138                 end
0139                 
0140                 % Interpolate the elevation onto the open boundary nodes
0141                 if ~exist('SurfaceElevation','var')
0142                     SurfaceElevation(:,1) = interp2(cs3x_lon,cs3x_lat,E',...
0143                         Mobj.lon(ObcNodes),Mobj.lat(ObcNodes));
0144                 else
0145                     SurfaceElevation(:,end+1) = interp2(cs3x_lon,cs3x_lat,E',...
0146                         Mobj.lon(ObcNodes),Mobj.lat(ObcNodes));
0147                 end
0148             elseif (mjd4 < inputConf.startDateMJD)
0149                 dump = fread(fid,7+(3*ITOT),'*int32','b');
0150             elseif (mjd4 > inputConf.endDateMJD)
0151                 PASTIT = true;
0152             end
0153         end
0154     end
0155     
0156     fclose(fid);
0157 end
0158 
0159 %% Output elevation to Mobj
0160 Mobj.surfaceElevation = SurfaceElevation;
0161 
0162 if(ftbverbose);
0163     fprintf(['end   : ' subname '\n']);
0164 end

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