-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnc_add.m
72 lines (67 loc) · 2.38 KB
/
nc_add.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
function [] = nc_add(nc,shortName,longName,units,grid,var,varargin)
% nc_add(nc,shortName,longName,units,grid,var)
% nc_add(nc,shortName,longName,units,grid,var,record_number)
%
% Adds variables (with data) to a NETCDF file with handle "nc"
% nc is the netcdf file handle
% shortName is the name used to refer to the variable 'theta'
% longName is a more descriptive name e.g. 'Potential temperature'
% units is the units of the variable e.g. 'Degrees Celsius'
% grid is a collection cell array of dimensino names e.g. {'Y' 'X'}
% var is the scalar/vector/array of data to be written
% record_number is the record slot to write when there is a "recordable"
% dimension in the grid specification
%
% If "shortName" is identical to "grid" then the variable is written to the
% netcdf file as dimension data and can subsequently be used in other grid
% specifications. The size of the dimension is the length of the vector of
% data. To create a recordable dimension, supply an empty vector, [].
%
% e.g.
% >> nc=netcdf('test.nc','clobber');
%
% Add a dimension (use grid==shortName)
% >> nc_add(nc,'lon','Longitude','Degrees East','lon',xc)
% >> nc_add(nc,'lat','Latitude','Degrees North','lat',yc)
%
% Add a record dimension (use grid==shortName and no values)
% >> nc_add(nc,'time','Time','Years','time',[])
%
% Add a variable on a grid
% >> nc_add(nc,'H','Orography','m',{'lat' 'lon'},H)
%
% Add a variable on a grid with recordable dimension
% >> nc_add(nc,'time','Time','Years','time',1978,1)
% >> nc_add(nc,'time','Time','Years','time',1978,2)
% >> nc_add(nc,'theta','Pot. Temp','Degrees K',{'time' 'lat' 'lon'},theta,1)
% >> nc_add(nc,'theta','Pot. Temp','Degrees K',{'time' 'lat' 'lon'},theta,2)
%
% >> close(nc);
%
% Written by [email protected], 2004.
% $Header: /u/gcmpack/MITgcm/utils/matlab/nc_add.m,v 1.2 2007/02/17 23:49:43 jmc Exp $
% $Name: $
if strcmp(shortName,grid{1})
nc(shortName) = length(var);
end
nc{shortName} = grid;
%nc{shortName}.uniquename = longName;
nc{shortName}.long_name = longName;
nc{shortName}.units = units;
nc{shortName}.missing_value = ncdouble(NaN);
nvargs=nargin-6;
rcn=0;
if nvargs==1
rcn=varargin{1};
end
if ~isempty(var)
if prod(size(var))==1
nc{shortName}(rcn) = var;
else
if rcn ~= 0
nc{shortName}(rcn,:) = permute(var,ndims(var):-1:1);
else
nc{shortName}(:) = permute(var,ndims(var):-1:1);
end
end
end