write_FVCOM_z0.m 3.35 KB
Newer Older
1
function write_FVCOM_z0(z0,filename,mytitle,cbcmin)
2 3
% Dump spatially-variable or uniform bottom roughness (z0) to FVCOM forcing
% file.
Pierre Cazenave's avatar
Pierre Cazenave committed
4
%
5
% function write_FVCOM_z0(z0, filename, mytitle)
Pierre Cazenave's avatar
Pierre Cazenave committed
6 7
%
% DESCRIPTION:
8
%    Generate a NetCDF file containing spatially variable z0 for FVCOM
Pierre Cazenave's avatar
Pierre Cazenave committed
9
%
10 11
% INPUT
%   z0        = user defined roughness field (m)
Pierre Cazenave's avatar
Pierre Cazenave committed
12
%               roughness is defined on the elements
Ricardo Torres's avatar
Ricardo Torres committed
13
%               expect values between 3 10^-3 (gravel) and .2 10^-3 (i.e. 0.0002) for mud
Pierre Cazenave's avatar
Pierre Cazenave committed
14
%   filename  = filename to dump to
15
%   mytitle   = title of the case (set as global attribute)
16 17
%   cbcmin    = minimum value of CBC (optional, defaults to 0.0018 if
%               omitted).
Pierre Cazenave's avatar
Pierre Cazenave committed
18 19
%
% OUTPUT:
20
%    netCDF file called `filename'
Pierre Cazenave's avatar
Pierre Cazenave committed
21 22 23 24
%
% EXAMPLE USAGE
%    write_FVCOM_z0(z0field, 'tst_z0.nc', 'z0 tst domain')
%
25
% Author(s):
Pierre Cazenave's avatar
Pierre Cazenave committed
26 27 28 29 30 31
%    Geoff Cowles (University of Massachusetts Dartmouth)
%    Pierre Cazenave (Plymouth Marine Laboratory)
%
% Revision history
%    2012-06-15 Added support for native MATLAB NetCDF routines. Requires
%    MATLAB 2010a or higher.
32 33
%    2016-08-09 Added new variable (cbcmin) to support FVCOM version 4.
%    Also tidied up the code a bit.
34
%
Pierre Cazenave's avatar
Pierre Cazenave committed
35
%==============================================================================
36 37

[~, subname] = fileparts(mfilename('fullpath'));
Pierre Cazenave's avatar
Pierre Cazenave committed
38
global ftbverbose;
39 40 41
if ftbverbose
    fprintf('\nbegin : %s\n', subname);
end
Pierre Cazenave's avatar
Pierre Cazenave committed
42 43 44 45

%------------------------------------------------------------------------------
% Parse input arguments
%------------------------------------------------------------------------------
46
if ~exist('z0', 'var')
Pierre Cazenave's avatar
Pierre Cazenave committed
47
    error('incorrect usage of write_FVCOM_z0, must provide z0 field')
48 49
end
if ~exist('filename', 'var')
Pierre Cazenave's avatar
Pierre Cazenave committed
50
    error('incorrect usage of write_FVCOM_z0, must provide filename')
51 52
end
if ~exist('mytitle', 'var')
Pierre Cazenave's avatar
Pierre Cazenave committed
53
    error('incorrect usage of write_FVCOM_z0, must provide title field')
54
end
Pierre Cazenave's avatar
Pierre Cazenave committed
55 56 57

% check dimensions
nElems = numel(z0);
58 59 60 61 62 63 64 65 66
if nElems == 0
    error('Number of elements in z0 is 0.')
end

% If we haven't been given a value of cbc min, set it to the example from
% Jianzhong Ge.
if nargin == 3
    cbcmin = repmat(0.0018, nElems, 1);
end
Pierre Cazenave's avatar
Pierre Cazenave committed
67 68

%------------------------------------------------------------------------------
69
% Dump to variables to the netCDF file
Pierre Cazenave's avatar
Pierre Cazenave committed
70
%------------------------------------------------------------------------------
71 72 73 74 75
if ftbverbose
  fprintf('Dumping to z0 NetCDF file: %s\n', filename);
  fprintf('Size of z0 array: %i\n', nElems);
end
nc = netcdf.create(filename, 'clobber');
Pierre Cazenave's avatar
Pierre Cazenave committed
76

77 78 79
netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'title', mytitle)
netcdf.putAtt(nc, netcdf.getConstant('NC_GLOBAL'), 'history', ...
    sprintf('File created with %s from the MATLAB fvcom-toolbox', subname))
Pierre Cazenave's avatar
Pierre Cazenave committed
80 81

% dimensions
82
nele_dimid = netcdf.defDim(nc, 'nele', nElems);
Pierre Cazenave's avatar
Pierre Cazenave committed
83 84

% variables and attributes
85 86 87 88 89 90 91 92 93
z0b_varid = netcdf.defVar(nc, 'z0b', 'NC_FLOAT', nele_dimid);
netcdf.putAtt(nc, z0b_varid, 'long_name', 'bottom roughness');
netcdf.putAtt(nc, z0b_varid, 'units', 'm');
netcdf.putAtt(nc, z0b_varid, 'type', 'data');

cbcmin_varid=netcdf.defVar(nc, 'cbcmin', 'NC_FLOAT', nele_dimid);
netcdf.putAtt(nc, cbcmin_varid, 'long_name', 'bottom roughness minimum');
netcdf.putAtt(nc, cbcmin_varid, 'units', 'None');
netcdf.putAtt(nc, cbcmin_varid, 'type', 'data');
Pierre Cazenave's avatar
Pierre Cazenave committed
94 95 96 97 98

% end definitions
netcdf.endDef(nc);

% write data
99 100
netcdf.putVar(nc, z0b_varid, z0);
netcdf.putVar(nc, cbcmin_varid, cbcmin);
Pierre Cazenave's avatar
Pierre Cazenave committed
101 102 103 104

% close file
netcdf.close(nc);

105 106 107
if ftbverbose
  fprintf('end   : %s\n', subname)
end