1 |
gforget |
1.1 |
function [dimOut]=write2nctiles(fileOut,fldIn,doCreate,varargin); |
2 |
|
|
%object : write gcmfaces object to netcdf files (tiled) |
3 |
|
|
%inputs : fileOut is the name of the file to be created |
4 |
|
|
% fldIn is the array to write to disk |
5 |
|
|
% doCreate is a 0/1 switch; 1 => create file ; 0 => append to file. |
6 |
|
|
%optional paramaters : |
7 |
|
|
% can be provided in the form {'name',value} |
8 |
|
|
% those that are currently active are |
9 |
|
|
% 'descr' is the file description (default ''). |
10 |
gforget |
1.2 |
% 'rdm' is the extended estimate description (default ''). |
11 |
gforget |
1.1 |
% 'fldName' is the nc variable name for fld (default : the outside name of fldIn). |
12 |
|
|
% 'longName' is the corresponding long name (default : ''). |
13 |
|
|
% 'units' is the unit of fld (default : '(unknown)'). |
14 |
|
|
% 'missval' is the missing value (default : NaN). |
15 |
|
|
% 'fillval' is the fill value (default : NaN). |
16 |
|
|
%netcdf dimensions : array dimensions are simply set to 'i1,i2,...' |
17 |
|
|
|
18 |
|
|
gcmfaces_global; |
19 |
|
|
|
20 |
|
|
doCheck=0;%set to one to print stuff to screen |
21 |
|
|
|
22 |
|
|
%set more optional paramaters to default values |
23 |
|
|
descr=''; |
24 |
gforget |
1.2 |
rdm=''; |
25 |
gforget |
1.1 |
fldName=inputname(2); longName=''; |
26 |
|
|
units='(unknown)'; missval=NaN; fillval=NaN; |
27 |
|
|
dimIn=[]; |
28 |
|
|
|
29 |
|
|
%set more optional paramaters to user defined values |
30 |
|
|
for ii=1:nargin-3; |
31 |
|
|
if ~iscell(varargin{ii}); |
32 |
|
|
warning('inputCheck:write2nctiles_1',... |
33 |
|
|
['write2nctiles expects \n'... |
34 |
|
|
' its optional parameters as cell arrays. \n'... |
35 |
|
|
' Argument no. ' num2str(ii+1) ' was ignored \n'... |
36 |
|
|
' Type ''help write2nctiles'' for details.']); |
37 |
|
|
elseif ~ischar(varargin{ii}{1}); |
38 |
|
|
warning('inputCheck:write2nctiles_2',... |
39 |
|
|
['write2nctiles expects \n'... |
40 |
|
|
' its optional parameters cell arrays \n'... |
41 |
|
|
' to start with character string. \n'... |
42 |
|
|
' Argument no. ' num2str(ii+1) ' was ignored \n'... |
43 |
|
|
' Type ''help write2nctiles'' for details.']); |
44 |
|
|
else; |
45 |
|
|
if strcmp(varargin{ii}{1},'descr')|... |
46 |
gforget |
1.2 |
strcmp(varargin{ii}{1},'rdm')|... |
47 |
gforget |
1.1 |
strcmp(varargin{ii}{1},'fldName')|... |
48 |
|
|
strcmp(varargin{ii}{1},'longName')|... |
49 |
|
|
strcmp(varargin{ii}{1},'units')|... |
50 |
|
|
strcmp(varargin{ii}{1},'missval')|... |
51 |
|
|
strcmp(varargin{ii}{1},'fillval')|... |
52 |
|
|
strcmp(varargin{ii}{1},'dimIn'); |
53 |
|
|
eval([varargin{ii}{1} '=varargin{ii}{2};']); |
54 |
|
|
else; |
55 |
|
|
warning('inputCheck:write2nctiles_3',... |
56 |
|
|
['unknown option ''' varargin{ii}{1} ''' was ignored']); |
57 |
|
|
end; |
58 |
|
|
end; |
59 |
|
|
end; |
60 |
|
|
|
61 |
|
|
for ff=1:mygrid.nFaces; |
62 |
|
|
|
63 |
|
|
if isa(fldIn,'gcmfaces'); |
64 |
|
|
fldTile=fldIn{ff}; |
65 |
|
|
%reverse order of dimensions |
66 |
|
|
nn=length(size(fldTile)); |
67 |
|
|
fldTile=permute(fldTile,[nn:-1:1]); |
68 |
|
|
else; |
69 |
|
|
fldTile=fldIn; |
70 |
|
|
end; |
71 |
|
|
fileTile=[fileOut sprintf('.%03d.nc',ff)]; |
72 |
|
|
|
73 |
|
|
%select dimensions of relevance: |
74 |
|
|
nDim=length(size(fldTile)); |
75 |
|
|
dimsize=size(fldTile); |
76 |
|
|
for iDim=1:nDim; |
77 |
|
|
if size(fldTile,iDim)~=1; |
78 |
|
|
dimlist{iDim}=['i' num2str(iDim)]; |
79 |
|
|
dimName{iDim}=['array index ' num2str(iDim)]; |
80 |
|
|
eval(['dimvec.i' num2str(iDim) '=[1:size(fldTile,iDim)];']); |
81 |
|
|
end; |
82 |
|
|
end; |
83 |
|
|
|
84 |
|
|
%omit singleton dimensions: |
85 |
|
|
ii=find(dimsize~=1); |
86 |
|
|
dimsize=dimsize(ii); |
87 |
|
|
dimlist={dimlist{ii}}; |
88 |
|
|
dimName={dimName{ii}}; |
89 |
|
|
|
90 |
|
|
%check : |
91 |
|
|
if doCheck; |
92 |
|
|
whos fldTile |
93 |
|
|
descr |
94 |
|
|
fldName |
95 |
|
|
longName |
96 |
|
|
units |
97 |
|
|
missval |
98 |
|
|
fillval |
99 |
|
|
dimlist |
100 |
|
|
dimName |
101 |
|
|
dimsize |
102 |
|
|
dimvec |
103 |
|
|
keyboard; |
104 |
|
|
end; |
105 |
|
|
|
106 |
|
|
%output dimension information |
107 |
|
|
dimOut{ff}=dimlist; |
108 |
|
|
|
109 |
|
|
if doCreate; |
110 |
|
|
%create netcdf file: |
111 |
|
|
%------------------- |
112 |
|
|
ncid=nccreate(fileTile,'NETCDF4'); |
113 |
|
|
|
114 |
gforget |
1.3 |
if ~isempty(rdm); |
115 |
|
|
descr2=[descr ' -- ' rdm{1}]; |
116 |
|
|
else; |
117 |
|
|
descr2=descr; |
118 |
|
|
end; |
119 |
gforget |
1.2 |
ncputAtt(ncid,'','description',descr); |
120 |
|
|
for pp=2:length(rdm); |
121 |
|
|
tmp1=char(pp+63); |
122 |
|
|
netcdf.putAtt(ncid,nc_global,tmp1,rdm{pp}); |
123 |
|
|
end; |
124 |
|
|
%append readme |
125 |
|
|
pp=length(rdm)+1; |
126 |
|
|
netcdf.putAtt(ncid,nc_global,tmp1,'file created using gcmfaces_IO/write2nctiles.m'); |
127 |
gforget |
1.1 |
ncputAtt(ncid,'','date',date); |
128 |
|
|
|
129 |
|
|
ncdefDim(ncid,'itxt',30); |
130 |
|
|
for dd=1:length(dimlist); ncdefDim(ncid,dimlist{dd},dimsize(dd)); end; |
131 |
|
|
|
132 |
|
|
for dd=1:length(dimlist); |
133 |
|
|
ncdefVar(ncid,dimlist{dd},'double',{dimlist{dd}}); |
134 |
|
|
ncputAtt(ncid,dimlist{dd},'long_name',dimName{dd}); |
135 |
|
|
end; |
136 |
|
|
ncclose(ncid); |
137 |
|
|
|
138 |
|
|
%fill in the dimensions dimensions values vectors: |
139 |
|
|
%------------------------------------------------- |
140 |
|
|
ncid=ncopen(fileTile,'write'); |
141 |
|
|
for dd=1:length(dimlist); |
142 |
|
|
ncputvar(ncid,dimlist{dd},getfield(dimvec,dimlist{dd})); |
143 |
|
|
end; |
144 |
|
|
ncclose(ncid); |
145 |
|
|
end; |
146 |
|
|
|
147 |
|
|
%use dimentsion specified by user |
148 |
|
|
if ~isempty(dimIn); dimlist=dimIn{ff}; end; |
149 |
|
|
|
150 |
|
|
%define and fill field: |
151 |
|
|
%---------------------- |
152 |
|
|
ncid=ncopen(fileTile,'write'); |
153 |
|
|
if (myenv.useNativeMatlabNetcdf); netcdf.reDef(ncid); end; |
154 |
|
|
ncdefVar(ncid,fldName,'double',flipdim(dimlist,2));%note the direction flip |
155 |
|
|
if ~isempty(longName); ncputAtt(ncid,fldName,'long_name',longName); end; |
156 |
|
|
if ~isempty(units); ncputAtt(ncid,fldName,'units',units); end; |
157 |
|
|
ncputAtt(ncid,fldName,'missing_value',missval); |
158 |
|
|
if (myenv.useNativeMatlabNetcdf); netcdf.endDef(ncid); end; |
159 |
|
|
ncputvar(ncid,fldName,fldTile); |
160 |
|
|
ncclose(ncid); |
161 |
|
|
|
162 |
|
|
end;%for ff=1:mygrid.nFaces; |
163 |
|
|
|