


Read temperature and salinity from NetCDF model output files and
interpolate onto the open boundaries in Mobj.
function struct = get_POLCOMS_netCDF(Mobj, files, varlist)
DESCRIPTION:
Extract variables in varlist to a struct from the files given in
files.
INPUT:
files = Cell array of NetCDF file(s).
varlist = Cell array of variables names.
OUTPUT:
Struct in which the field names are the variable names supplied in var
list. Each field has a data and units array which contain the data and
units variables from the NetCDF. If no units are found, it is left
blank for that variable.
EXAMPLE USAGE
S = get_POLCOMS_netCDF({'/tmp/2000.nc', '/tmp/2001.nc', {'temp', 'salt'})
NOTES:
- If you supply multiple files, there are a few assumptions:
- Variables are only appended if there are 3 or 4 dimensions; fewer
than that, and the values are assumed to be static across all the
given files (e.g. longitude, latitude etc.). The last dimension
is assumed to be time.
- The order of the files given should be chronological.
- This has been tested on NetCDF files generated from the PML
POLCOMS-ERSEM daily mean model output
Author(s):
Pierre Cazenave (Plymouth Marine Laboratory)
Revision history
2013-02-08 First version based on restart_FVCOM_AMM.m and
get_AMM_tsobc.m.
==========================================================================

0001 function ncdata = get_POLCOMS_netCDF(files, varlist) 0002 % Read temperature and salinity from NetCDF model output files and 0003 % interpolate onto the open boundaries in Mobj. 0004 % 0005 % function struct = get_POLCOMS_netCDF(Mobj, files, varlist) 0006 % 0007 % DESCRIPTION: 0008 % Extract variables in varlist to a struct from the files given in 0009 % files. 0010 % 0011 % INPUT: 0012 % files = Cell array of NetCDF file(s). 0013 % varlist = Cell array of variables names. 0014 % 0015 % OUTPUT: 0016 % Struct in which the field names are the variable names supplied in var 0017 % list. Each field has a data and units array which contain the data and 0018 % units variables from the NetCDF. If no units are found, it is left 0019 % blank for that variable. 0020 % 0021 % EXAMPLE USAGE 0022 % S = get_POLCOMS_netCDF({'/tmp/2000.nc', '/tmp/2001.nc', {'temp', 'salt'}) 0023 % 0024 % NOTES: 0025 % 0026 % - If you supply multiple files, there are a few assumptions: 0027 % 0028 % - Variables are only appended if there are 3 or 4 dimensions; fewer 0029 % than that, and the values are assumed to be static across all the 0030 % given files (e.g. longitude, latitude etc.). The last dimension 0031 % is assumed to be time. 0032 % - The order of the files given should be chronological. 0033 % 0034 % - This has been tested on NetCDF files generated from the PML 0035 % POLCOMS-ERSEM daily mean model output 0036 % 0037 % Author(s): 0038 % Pierre Cazenave (Plymouth Marine Laboratory) 0039 % 0040 % Revision history 0041 % 2013-02-08 First version based on restart_FVCOM_AMM.m and 0042 % get_AMM_tsobc.m. 0043 % 0044 %========================================================================== 0045 0046 subname = 'get_POLCOMS_netCDF'; 0047 0048 global ftbverbose; 0049 if ftbverbose 0050 fprintf('\nbegin : %s\n', subname) 0051 end 0052 0053 % Get the results. Check we have a cell array, and if we don't, assume it's 0054 % a file name. 0055 if iscell(files) 0056 todo = length(files); 0057 else 0058 todo = 1; 0059 end 0060 0061 for ii = 1:todo 0062 0063 if iscell(files) 0064 ftn = files{ii}; 0065 else 0066 ftn = files; 0067 end 0068 0069 if ftbverbose 0070 % Strip path from filename for the verbose output. 0071 [~, basename, ext] = fileparts(ftn); 0072 tmp_fn = [basename, ext]; 0073 0074 if todo == 1 0075 fprintf('%s: extracting file %s... ', subname, tmp_fn) 0076 else 0077 fprintf('%s: extracting file %s (%i of %i)... ', subname, tmp_fn, ii, todo) 0078 end 0079 end 0080 0081 nc = netcdf.open(ftn, 'NOWRITE'); 0082 0083 for var = 1:numel(varlist) 0084 0085 getVar = varlist{var}; 0086 varid = netcdf.inqVarID(nc, getVar); 0087 0088 data = double(netcdf.getVar(nc, varid, 'single')); 0089 if ii == 1 0090 ncdata.(getVar).data = data; 0091 else 0092 if ndims(data) < 3 0093 if strcmpi(getVar, 'time') 0094 % If the dimension is time, we need to be a bit more 0095 % clever since we'll need a concatenated time series 0096 % (in which values are continuous and from which we 0097 % can extract a sensible time). As such, we need to add 0098 % the maximum of the existing time. On the first 0099 % iteration, we should save ourselves the base time 0100 % (from the units of the time variable). 0101 ncdata.(getVar).data = [ncdata.(getVar).data; data + max(ncdata.(getVar).data)]; 0102 else 0103 % This should be a fixed set of values (probably lon or 0104 % lat) in which case we don't need to append them, so 0105 % just replace the existing values with those in the 0106 % current NetCDF file. 0107 ncdata.(getVar).data = data; 0108 end 0109 elseif ndims(data) == 3 || ndims(data) == 4 0110 % Concatenate along the last dimension and hope/assume it's 0111 % a time dimension. 0112 ncdata.(getVar).data = cat(ndims(data), ncdata.(getVar).data, data); 0113 else 0114 error('Unsupported number of dimensions in PML POLCOMS-ERSEM data') 0115 end 0116 end 0117 % Try to get some units (important for the calculation of MJD). 0118 try 0119 if ii == 1 0120 units = netcdf.getAtt(nc, varid, 'units'); 0121 else 0122 % Leave the units values alone so we always use the values 0123 % from the first file. This is particularly important for 0124 % the time calculation later on which is dependent on 0125 % knowing the time origin of the first file. 0126 continue 0127 end 0128 catch 0129 units = []; 0130 end 0131 ncdata.(getVar).units = units; 0132 end 0133 0134 netcdf.close(nc) 0135 0136 if ftbverbose 0137 fprintf('done.\n') 0138 end 0139 0140 end 0141 0142 if ftbverbose 0143 fprintf(['end : ' subname '\n']) 0144 end