write_FVCOM_tsobc.m 7.86 KB
Newer Older
1
function write_FVCOM_tsobc(basename,time,nSiglay,in_temp,in_salt)
Pierre Cazenave's avatar
Pierre Cazenave committed
2
% example file for dumping a file to force temperature and salinity at the open b.
3
%
4
% function write_FVCOM_tsobc(basename,time,nSiglay,in_temp,in_salt)
5 6
%
% DESCRIPTION:
7 8 9
%    Setup an FVCOM hydrographic open boundary forcing file. Supply either
%    uniform values for temperature and salinity or 3D arrays (node, sigma,
%    time).
10 11
%
% INPUT
12
%    Model case name (to find the bathymetry and open boundary .dat files).
13 14
%    Time
%    Number of sigma layers
15 16
%    Boundary temperature (Celcius)
%    Boundary salinity (psu)
17 18 19 20
%
% OUTPUT:
%    FVCOM hydrographic open boundary file
%
21
% Author(s):
22 23
%    Geoff Cowles (University of Massachusetts Dartmouth)
%    Pierre Cazenave (Plymouth Marine Laboratory)
24
%
25 26 27 28 29
% Revision history
%    2012-06-15 Added support for native MATLAB NetCDF routines. Requires
%    MATLAB 2010a or higher.
%    2012-07-16 Removed hard-coded nSiglay and nSiglev and instead moved to
%    arguments list.
30 31
%    2012-10-08 Updated help to reflect the fact nSiglev is calculated as
%    nSiglay+1.
32 33
%    2012-11-09 Add new arguments to use user defined temperature and
%    salinity.
34 35
%    2013-01-09 Add support for 3D input temperature and salinity (such as
%    might be generated with get_POLCOMS_tsobc.m.
36
%
37 38 39 40 41
%==============================================================================

warning off;


42
subname = 'write_FVCOM_tsobc';
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
global ftbverbose;
if(ftbverbose);
  fprintf('\n')
  fprintf(['begin : ' subname '\n'])
end;

fvcom_bathy = [basename, '_dep.dat'];
fvcom_obc   = [basename, '_obc.dat'];
tsOBCFile = [basename, '_tsobc.nc'];

%------------------------------------------------------------------------------
% read in the FVCOM open boundary node data (need node numbers and dimension)
%------------------------------------------------------------------------------
fid = fopen(fvcom_obc,'r');
if(fid  < 0)
  error(['file: ' fvcom_obc ' does not exist']);
end;
C = textscan(fid, '%s %s %s %s %d', 1);
nObc = C{5};
obc_nodes = zeros(nObc,1);
if(ftbverbose); fprintf('reading obc file\n'); end;
if(ftbverbose); fprintf('# nodes %d\n',nObc); end;
for i=1:nObc
  C = textscan(fid, '%d %d %d', 1);
  obc_nodes(i) = C{2};
end;

if(ftbverbose); fprintf('obc reading complete\n');end;

%------------------------------------------------------------------------------
% read in the FVCOM bathymetry data (need bathymetry on open boundary nodes)
%------------------------------------------------------------------------------
fid = fopen(fvcom_bathy,'r');
if(fid  < 0)
  error(['file: ' fvcom_bathy ' does not exist']);
end;
C = textscan(fid, '%s %s %s %d', 1);
Nverts = C{4};
h = zeros(Nverts,1);
if(ftbverbose); fprintf('reading bathymetry file\n');end;
if(ftbverbose); fprintf('# nodes %d\n',Nverts);end;
for i=1:Nverts
  C = textscan(fid, '%f %f %f', 1);
  h(i) = C{3};
end;
if(ftbverbose); fprintf('min depth %f max depth %f\n',min(h),max(h));end;
if(ftbverbose); fprintf('bathymetry reading complete\n');end;
fclose(fid);

%--------------------------------------------------------------
93
% Generate the requisite data
94 95 96 97 98 99 100 101 102
%--------------------------------------------------------------

% extract bathymetry at open boundary nodes
obc_h = h(obc_nodes);

% time
% time = 0:1:31.;
nTimes = numel(time);

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
% Create or process the temperature and salinity arrays.
if ndim(in_temp) == 1
    nSiglev = nSiglay + 1;
    inc = 1./real(nSiglay);
    siglev = 0:-inc:-1;
    siglay = nan(1, nSiglay);
    for i=1:nSiglay
        siglay(i) = mean(siglev(i:i+1));
    end
    % initialize temperature/salinity arrays
    temp = zeros(nObc,nSiglay,nTimes);
    salt = zeros(nObc,nSiglay,nTimes);

    % set a constant temperature and salinity
    obc_temp = ones(1,nTimes)*in_temp;
    obc_salt = ones(1,nTimes)*in_salt;

    % set variable temperature and salinity
    % for i=1:nTimes
    % 	obc_temp(i) = 18. + 2.*real(i-1)/nTimes;
    % 	obc_salt(i) = 30. - 5.*real(i-1)/nTimes;
    % end

    % Create 3D array from three 1D arrays
    % temp = repmat(obc_temp, [nObc, nSiglay, 1]);
    % salt = repmat(obc_salt, [nObc, nSiglay, 1]);
    for i=1:nObc
        for j=1:nSiglay
            temp(i,j,:) = obc_temp;
            salt(i,j,:) = obc_salt;
        end
    end
else
    % We have a 3D array already so we just need a couple of stats.
    temp = in_temp;
    salt = in_salt;
    nSiglev = size(in_temp, 3);
    nSiglay = nSiglev - 1;
end
142 143

%--------------------------------------------------------------
144
% set NetCDF variables and dump to file
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
%--------------------------------------------------------------

% open boundary forcing
nc = netcdf.create(tsOBCFile, 'clobber');

% define global attributes
netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'type','FVCOM RIVER FORCING FILE')
netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'title','simple open boundary hydrography test')
netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'type','FVCOM TIME SERIES OBC TS FILE')
netcdf.putAtt(nc,netcdf.getConstant('NC_GLOBAL'),'history','generated using the fvcom-toolbox')


% define dimensions
nobc_dimid=netcdf.defDim(nc,'nobc',nObc);
datestrlen_dimid=netcdf.defDim(nc,'Datestrln',26);
time_dimid=netcdf.defDim(nc,'time',netcdf.getConstant('NC_UNLIMITED'));
siglay_dimid=netcdf.defDim(nc,'siglay',nSiglay);
siglev_dimid=netcdf.defDim(nc,'siglev',nSiglev);

% variables
% nc{'river_names'} = ncchar('rivers', 'namelen');

time_varid=netcdf.defVar(nc,'time','NC_FLOAT',time_dimid);
netcdf.putAtt(nc,time_varid,'long_name','time');
169 170
netcdf.putAtt(nc,time_varid,'units','days since 1858-11-17 00:00:00');
netcdf.putAtt(nc,time_varid,'time_zone','UTC');
171 172

itime_varid=netcdf.defVar(nc,'Itime','NC_INT',time_dimid);
173 174 175
netcdf.putAtt(nc,itime_varid,'units','days since 1858-11-17 00:00:00');
netcdf.putAtt(nc,itime_varid,'format','modified julian day (MJD)');
netcdf.putAtt(nc,itime_varid,'time_zone','UTC');
176 177 178

itime2_varid=netcdf.defVar(nc,'Itime2','NC_INT',time_dimid);
netcdf.putAtt(nc,itime2_varid,'units','msec since 00:00:00');
179
netcdf.putAtt(nc,itime2_varid,'time_zone','UTC');
180 181 182 183

nobc_varid=netcdf.defVar(nc,'obc_nodes','NC_INT',nobc_dimid);
netcdf.putAtt(nc,nobc_varid,'long_name','Open Boundary Node Number');
netcdf.putAtt(nc,nobc_varid,'grid','obc_grid');
184
netcdf.putAtt(nc,nobc_varid,'type','data');
185 186

obc_h_varid=netcdf.defVar(nc,'obc_h','NC_FLOAT',nobc_dimid);
187
netcdf.putAtt(nc,obc_h_varid,'long_name','Open Boundary Depth');
188 189
netcdf.putAtt(nc,obc_h_varid,'units','m');
netcdf.putAtt(nc,obc_h_varid,'grid','obc_grid');
190
netcdf.putAtt(nc,obc_h_varid,'type','data');
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

obc_siglev_varid=netcdf.defVar(nc,'siglev','NC_FLOAT',siglev_dimid);
netcdf.putAtt(nc,obc_siglev_varid,'long_name','ocean_sigma/general_coordinate');
netcdf.putAtt(nc,obc_siglev_varid,'grid','obc_grid');

obc_siglay_varid=netcdf.defVar(nc,'siglay','NC_FLOAT',siglay_dimid);
netcdf.putAtt(nc,obc_siglay_varid,'long_name','ocean_sigma/general_coordinate');
netcdf.putAtt(nc,obc_siglay_varid,'grid','obc_grid');

obc_temp_varid=netcdf.defVar(nc,'obc_temp','NC_FLOAT',[nobc_dimid,siglay_dimid,time_dimid]);
netcdf.putAtt(nc,obc_temp_varid,'long_name','sea_water_temperature');
netcdf.putAtt(nc,obc_temp_varid,'units','Celcius');
netcdf.putAtt(nc,obc_temp_varid,'grid','obc_grid');

obc_salinity_varid=netcdf.defVar(nc,'obc_salinity','NC_FLOAT',[nobc_dimid,siglay_dimid,time_dimid]);
netcdf.putAtt(nc,obc_salinity_varid,'long_name','sea_water_salinity');
netcdf.putAtt(nc,obc_salinity_varid,'units','PSU');
netcdf.putAtt(nc,obc_salinity_varid,'grid','obc_grid');

% end definitions
netcdf.endDef(nc);

% write data
netcdf.putVar(nc,nobc_varid,obc_nodes);
netcdf.putVar(nc,obc_h_varid,obc_h);
netcdf.putVar(nc,obc_siglev_varid,siglev);
netcdf.putVar(nc,obc_siglay_varid,siglay);
netcdf.putVar(nc,time_varid,0,numel(time),time);
netcdf.putVar(nc,itime_varid,floor(time));
netcdf.putVar(nc,itime2_varid,0,numel(time),mod(time,1)*24*3600*1000);

netcdf.putVar(nc,obc_temp_varid,temp);
netcdf.putVar(nc,obc_salinity_varid,salt);

% close file
netcdf.close(nc);

if(ftbverbose); fprintf(['end   : ' subname '\n']);end;