Warning, /utils/matlab/nc_add.m is written in an unsupported language. File is not indexed.
view on githubraw file Latest commit add29e06 on 2018-01-31 20:35:05 UTC
a33039f58e Alis*0001 function [] = nc_add(nc,shortName,longName,units,grid,var,varargin)
0002 % nc_add(nc,shortName,longName,units,grid,var)
0003 % nc_add(nc,shortName,longName,units,grid,var,record_number)
0004 %
0005 % Adds variables (with data) to a NETCDF file with handle "nc"
0006 % nc is the netcdf file handle
0007 % shortName is the name used to refer to the variable 'theta'
0008 % longName is a more descriptive name e.g. 'Potential temperature'
0009 % units is the units of the variable e.g. 'Degrees Celsius'
0010 % grid is a collection cell array of dimensino names e.g. {'Y' 'X'}
0011 % var is the scalar/vector/array of data to be written
0012 % record_number is the record slot to write when there is a "recordable"
0013 % dimension in the grid specification
0014 %
0015 % If "shortName" is identical to "grid" then the variable is written to the
0016 % netcdf file as dimension data and can subsequently be used in other grid
0017 % specifications. The size of the dimension is the length of the vector of
0018 % data. To create a recordable dimension, supply an empty vector, [].
0019 %
0020 % e.g.
0021 % >> nc=netcdf('test.nc','clobber');
0022 %
0023 % Add a dimension (use grid==shortName)
0024 % >> nc_add(nc,'lon','Longitude','Degrees East','lon',xc)
0025 % >> nc_add(nc,'lat','Latitude','Degrees North','lat',yc)
0026 %
0027 % Add a record dimension (use grid==shortName and no values)
0028 % >> nc_add(nc,'time','Time','Years','time',[])
0029 %
0030 % Add a variable on a grid
0031 % >> nc_add(nc,'H','Orography','m',{'lat' 'lon'},H)
0032 %
0033 % Add a variable on a grid with recordable dimension
0034 % >> nc_add(nc,'time','Time','Years','time',1978,1)
0035 % >> nc_add(nc,'time','Time','Years','time',1978,2)
0036 % >> nc_add(nc,'theta','Pot. Temp','Degrees K',{'time' 'lat' 'lon'},theta,1)
0037 % >> nc_add(nc,'theta','Pot. Temp','Degrees K',{'time' 'lat' 'lon'},theta,2)
0038 %
0039 % >> close(nc);
0040 %
0041 % Written by adcroft@mit.edu, 2004.
0042 if strcmp(shortName,grid{1})
0043 nc(shortName) = length(var);
0044 end
0045
0046 nc{shortName} = grid;
0047 %nc{shortName}.uniquename = longName;
0048 nc{shortName}.long_name = longName;
0049 nc{shortName}.units = units;
0050 nc{shortName}.missing_value = ncdouble(NaN);
0051
0052 nvargs=nargin-6;
0053 rcn=0;
0054 if nvargs==1
0055 rcn=varargin{1};
0056 end
0057
0058 if ~isempty(var)
0059 if prod(size(var))==1
0060 nc{shortName}(rcn) = var;
0061 else
0062 if rcn ~= 0
0063 nc{shortName}(rcn,:) = permute(var,ndims(var):-1:1);
0064 else
0065 nc{shortName}(:) = permute(var,ndims(var):-1:1);
0066 end
0067 end
0068 end
0069