| 1 |
function [] = nc_add(nc,shortName,longName,units,grid,var,varargin) |
| 2 |
% nc_add(nc,shortName,longName,units,grid,var) |
| 3 |
% nc_add(nc,shortName,longName,units,grid,var,record_number) |
| 4 |
% |
| 5 |
% Adds variables (with data) to a NETCDF file with handle "nc" |
| 6 |
% nc is the netcdf file handle |
| 7 |
% shortName is the name used to refer to the variable 'theta' |
| 8 |
% longName is a more descriptive name e.g. 'Potential temperature' |
| 9 |
% units is the units of the variable e.g. 'Degrees Celsius' |
| 10 |
% grid is a collection cell array of dimensino names e.g. {'Y' 'X'} |
| 11 |
% var is the scalar/vector/array of data to be written |
| 12 |
% record_number is the record slot to write when there is a "recordable" |
| 13 |
% dimension in the grid specification |
| 14 |
% |
| 15 |
% If "shortName" is identical to "grid" then the variable is written to the |
| 16 |
% netcdf file as dimension data and can subsequently be used in other grid |
| 17 |
% specifications. The size of the dimension is the length of the vector of |
| 18 |
% data. To create a recordable dimension, supply an empty vector, []. |
| 19 |
% |
| 20 |
% e.g. |
| 21 |
% >> nc=netcdf('test.nc','clobber'); |
| 22 |
% |
| 23 |
% Add a dimension (use grid==shortName) |
| 24 |
% >> nc_add(nc,'lon','Longitude','Degrees East','lon',xc) |
| 25 |
% >> nc_add(nc,'lat','Latitude','Degrees North','lat',yc) |
| 26 |
% |
| 27 |
% Add a record dimension (use grid==shortName and no values) |
| 28 |
% >> nc_add(nc,'time','Time','Years','time',[]) |
| 29 |
% |
| 30 |
% Add a variable on a grid |
| 31 |
% >> nc_add(nc,'H','Orography','m',{'lat' 'lon'},H) |
| 32 |
% |
| 33 |
% Add a variable on a grid with recordable dimension |
| 34 |
% >> nc_add(nc,'time','Time','Years','time',1978,1) |
| 35 |
% >> nc_add(nc,'time','Time','Years','time',1978,2) |
| 36 |
% >> nc_add(nc,'theta','Pot. Temp','Degrees K',{'time' 'lat' 'lon'},theta,1) |
| 37 |
% >> nc_add(nc,'theta','Pot. Temp','Degrees K',{'time' 'lat' 'lon'},theta,2) |
| 38 |
% |
| 39 |
% >> close(nc); |
| 40 |
% |
| 41 |
% Written by adcroft@mit.edu, 2004. |
| 42 |
% $Header: /u/gcmpack/MITgcm/utils/matlab/nc_add.m,v 1.1 2004/06/04 15:50:52 adcroft Exp $ |
| 43 |
|
| 44 |
if strcmp(shortName,grid{1}) |
| 45 |
nc(shortName) = length(var); |
| 46 |
end |
| 47 |
|
| 48 |
nc{shortName} = grid; |
| 49 |
%nc{shortName}.uniquename = longName; |
| 50 |
nc{shortName}.long_name = longName; |
| 51 |
nc{shortName}.units = units; |
| 52 |
nc{shortName}.missing_value = ncdouble(NaN); |
| 53 |
|
| 54 |
nvargs=nargin-6; |
| 55 |
rcn=0; |
| 56 |
if nvargs==1 |
| 57 |
rcn=varargin{1}; |
| 58 |
end |
| 59 |
|
| 60 |
if ~isempty(var) |
| 61 |
if prod(size(var))==1 |
| 62 |
nc{shortName}(rcn) = var; |
| 63 |
else |
| 64 |
if rcn ~= 0 |
| 65 |
nc{shortName}(rcn,:) = permute(var,ndims(var):-1:1); |
| 66 |
else |
| 67 |
nc{shortName}(:) = permute(var,ndims(var):-1:1); |
| 68 |
end |
| 69 |
end |
| 70 |
end |
| 71 |
|