1 |
function [dimOut]=write2nctiles(fileOut,fldIn,doCreate,varargin); |
2 |
% WRITE2NCTILES writes a gcmfaces object to nctiles, tiled netcdf, files |
3 |
% |
4 |
% inputs : |
5 |
% - fileOut is the name of the file to be created |
6 |
% - fldIn is the array to write to disk |
7 |
% - doCreate: if 1 (default) then create file ; if 0 then append to file. |
8 |
% |
9 |
% additional paramaters can be provided afterwards in the {'name',value} form: |
10 |
% 'fldName' is the nc variable name for fld (see "notes" regarding default) |
11 |
% 'longName' is the corresponding long name ('' by default). |
12 |
% 'units' is the unit of fld ('(unknown)' by default). |
13 |
% 'missval' is the missing value (NaN by default). |
14 |
% 'fillval' is the fill value (NaN by default). |
15 |
% 'tileNo' is a map of tile indices (face # by default) |
16 |
% 'coord' is auxilliary coordinates attribute (e.g. 'lon lat dep') |
17 |
% 'dimsize' is the array size associated with 'coord' |
18 |
% 'xtype' is the variable ('double' by default) |
19 |
% 'rdm' is the extended estimate description ('' by default). |
20 |
% 'descr' is the file description ('' by default). |
21 |
% |
22 |
% notes: |
23 |
% - if fldName is not explicitly specified then the input variable |
24 |
% name (if ok) or file name (otherwise) is used as fldName. |
25 |
% - netcdf dimensions are simply set to 'i1,i2,...' |
26 |
% |
27 |
% examples: |
28 |
% |
29 |
% write2nctiles('test1',mygrid.RAC,1,{'fldName','RAC'}); |
30 |
% RAC=read_nctiles('test1','RAC'); |
31 |
% |
32 |
% write2nctiles('test2',mygrid.RAC); |
33 |
% RAC=read_nctiles('test2','test2'); |
34 |
% RAC=read_nctiles('test2'); |
35 |
|
36 |
gcmfaces_global; |
37 |
if ~(myenv.useNativeMatlabNetcdf); |
38 |
error('only native matlab nectdf is supported in write2nctiles'); |
39 |
end; |
40 |
|
41 |
doCheck=0;%set to one to print stuff to screen |
42 |
|
43 |
if isempty(whos('fileOut')); error('fileOut must be specified'); end; |
44 |
if isempty(whos('fldIn')); error('fldIn must be specified'); end; |
45 |
if isempty(whos('doCreate')); doCreate=1; end; |
46 |
|
47 |
fldInIsaGcmfaces=isa(fldIn,'gcmfaces'); |
48 |
if fldInIsaGcmfaces; |
49 |
fldInIsEmpty=isempty(fldIn{1}); |
50 |
else; |
51 |
fldInIsEmpty=isempty(fldIn); |
52 |
end; |
53 |
|
54 |
%set more optional paramaters to default values |
55 |
fldName=inputname(2); |
56 |
if isempty(fldName); |
57 |
[tmp1,fldName,tmp2] = fileparts(fileOut); |
58 |
end; |
59 |
longName=''; units='(unknown)'; missval=NaN; fillval=NaN; dimIn=[]; |
60 |
tileNo=mygrid.XC; for ff=1:mygrid.nFaces; tileNo{ff}(:)=ff; end; |
61 |
coord=''; dimsize=[]; xtype='double'; descr=''; rdm=''; |
62 |
|
63 |
%set more optional paramaters to user defined values |
64 |
for ii=1:nargin-3; |
65 |
if ~iscell(varargin{ii}); |
66 |
warning('inputCheck:write2nctiles_1',... |
67 |
['write2nctiles expects \n'... |
68 |
' its optional parameters as cell arrays. \n'... |
69 |
' Argument no. ' num2str(ii+1) ' was ignored \n'... |
70 |
' Type ''help write2nctiles'' for details.']); |
71 |
elseif ~ischar(varargin{ii}{1}); |
72 |
warning('inputCheck:write2nctiles_2',... |
73 |
['write2nctiles expects \n'... |
74 |
' its optional parameters cell arrays \n'... |
75 |
' to start with character string. \n'... |
76 |
' Argument no. ' num2str(ii+1) ' was ignored \n'... |
77 |
' Type ''help write2nctiles'' for details.']); |
78 |
else; |
79 |
if strcmp(varargin{ii}{1},'descr')|... |
80 |
strcmp(varargin{ii}{1},'rdm')|... |
81 |
strcmp(varargin{ii}{1},'fldName')|... |
82 |
strcmp(varargin{ii}{1},'longName')|... |
83 |
strcmp(varargin{ii}{1},'units')|... |
84 |
strcmp(varargin{ii}{1},'missval')|... |
85 |
strcmp(varargin{ii}{1},'fillval')|... |
86 |
strcmp(varargin{ii}{1},'tileNo')|... |
87 |
strcmp(varargin{ii}{1},'coord')|... |
88 |
strcmp(varargin{ii}{1},'dimsize')|... |
89 |
strcmp(varargin{ii}{1},'xtype')|... |
90 |
strcmp(varargin{ii}{1},'dimIn'); |
91 |
eval([varargin{ii}{1} '=varargin{ii}{2};']); |
92 |
else; |
93 |
warning('inputCheck:write2nctiles_3',... |
94 |
['unknown option ''' varargin{ii}{1} ''' was ignored']); |
95 |
end; |
96 |
end; |
97 |
end; |
98 |
|
99 |
%split fldIn (if isa gcmfaces) into tiles |
100 |
tileList=unique(convert2vector(tileNo)); |
101 |
tileList=tileList(~isnan(tileList)); |
102 |
ntile=length(tileList); |
103 |
% |
104 |
if fldInIsaGcmfaces&~fldInIsEmpty; |
105 |
for ff=1:ntile; |
106 |
tmp1=[]; |
107 |
for gg=1:mygrid.nFaces; |
108 |
[tmpi,tmpj]=find(tileNo{gg}==ff); |
109 |
if ~isempty(tmpi); |
110 |
tmpi=[min(tmpi(:)):max(tmpi(:))]; |
111 |
tmpj=[min(tmpj(:)):max(tmpj(:))]; |
112 |
tmp1=fldIn{gg}(tmpi,tmpj,:,:); |
113 |
end; |
114 |
end; |
115 |
fldTiles{ff}=tmp1; |
116 |
end; |
117 |
clear fldIn; |
118 |
elseif fldInIsaGcmfaces; |
119 |
for ff=1:ntile; |
120 |
fldTiles{ff}=[]; |
121 |
end; |
122 |
end; |
123 |
|
124 |
%start processing loop |
125 |
for ff=1:ntile; |
126 |
|
127 |
if fldInIsaGcmfaces; |
128 |
fldTile=fldTiles{ff}; |
129 |
%reverse order of dimensions |
130 |
nn=length(size(fldTile)); |
131 |
fldTile=permute(fldTile,[nn:-1:1]); |
132 |
if ntile==1; clear fldTiles; end; |
133 |
else; |
134 |
fldTile=fldIn; |
135 |
end; |
136 |
fileTile=[fileOut sprintf('.%04d.nc',ff)]; |
137 |
|
138 |
%select dimensions of relevance: |
139 |
if ~fldInIsEmpty; |
140 |
nDim=length(size(fldTile)); |
141 |
dimsize=size(fldTile); |
142 |
elseif ~isempty(dimsize); |
143 |
nDim=length(dimsize); |
144 |
else; |
145 |
error('undertermined array size'); |
146 |
end; |
147 |
|
148 |
for iDim=1:nDim; |
149 |
if dimsize(iDim)~=1; |
150 |
dimlist{iDim}=['i' num2str(iDim)]; |
151 |
dimName{iDim}=['array index ' num2str(iDim)]; |
152 |
eval(['dimvec.i' num2str(iDim) '=[1:dimsize(iDim)];']); |
153 |
end; |
154 |
end; |
155 |
|
156 |
%omit singleton dimensions: |
157 |
ii=find(dimsize~=1); |
158 |
dimsize=dimsize(ii); |
159 |
dimlist={dimlist{ii}}; |
160 |
dimName={dimName{ii}}; |
161 |
|
162 |
%check : |
163 |
if doCheck; |
164 |
whos fldTile |
165 |
descr |
166 |
fldName |
167 |
longName |
168 |
units |
169 |
missval |
170 |
fillval |
171 |
dimlist |
172 |
dimName |
173 |
dimsize |
174 |
dimvec |
175 |
keyboard; |
176 |
end; |
177 |
|
178 |
%output dimension information |
179 |
dimOut{ff}=dimlist; |
180 |
|
181 |
if doCreate; |
182 |
%create netcdf file: |
183 |
%------------------- |
184 |
if prod(dimsize)*4/1e9<1.5;%use (always available) basic netcdf: |
185 |
mode='clobber'; |
186 |
else;%to allow for large file: |
187 |
mode='NETCDF4'; |
188 |
end; |
189 |
ncid=nccreateFile(fileTile,mode); |
190 |
nc_global=netcdf.getConstant('NC_GLOBAL'); |
191 |
|
192 |
if ~isempty(rdm); |
193 |
descr2=[descr ' -- ' rdm{1}]; |
194 |
else; |
195 |
descr2=descr; |
196 |
end; |
197 |
ncputAtt(ncid,'','description',descr2); |
198 |
for pp=2:length(rdm); |
199 |
tmp1=char(pp+63); |
200 |
netcdf.putAtt(ncid,nc_global,tmp1,rdm{pp}); |
201 |
end; |
202 |
%append readme |
203 |
if length(rdm)>0; pp=length(rdm)+1; tmp1=char(pp+63); else; tmp1='A'; end; |
204 |
netcdf.putAtt(ncid,nc_global,tmp1,'file created using gcmfaces_IO/write2nctiles.m'); |
205 |
ncputAtt(ncid,'','date',date); |
206 |
netcdf.putAtt(ncid,nc_global,'Conventions','CF-1.6') |
207 |
|
208 |
ncputAtt(ncid,'','_FillValue',fillval); |
209 |
ncputAtt(ncid,'','missing_value',missval); |
210 |
|
211 |
%ncdefDim(ncid,'itxt',30); |
212 |
for dd=1:length(dimlist); ncdefDim(ncid,dimlist{dd},dimsize(dd)); end; |
213 |
|
214 |
for dd=1:length(dimlist); |
215 |
ncdefVar(ncid,dimlist{dd},'double',{dimlist{dd}}); |
216 |
ncputAtt(ncid,dimlist{dd},'long_name',dimName{dd}); |
217 |
ncputAtt(ncid,dimlist{dd},'units','1'); |
218 |
end; |
219 |
ncclose(ncid); |
220 |
|
221 |
%fill in the dimensions dimensions values vectors: |
222 |
%------------------------------------------------- |
223 |
ncid=ncopen(fileTile,'write'); |
224 |
for dd=1:length(dimlist); |
225 |
ncputvar(ncid,dimlist{dd},getfield(dimvec,dimlist{dd})); |
226 |
end; |
227 |
ncclose(ncid); |
228 |
end; |
229 |
|
230 |
%use dimentsion specified by user |
231 |
if ~isempty(dimIn); dimlist=dimIn{ff}; end; |
232 |
|
233 |
%define and fill field: |
234 |
%---------------------- |
235 |
ncid=ncopen(fileTile,'write'); |
236 |
% |
237 |
netcdf.reDef(ncid); |
238 |
ncdefVar(ncid,fldName,xtype,flipdim(dimlist,2));%note the direction flip |
239 |
if ~isempty(longName); ncputAtt(ncid,fldName,'long_name',longName); end; |
240 |
if ~isempty(units); ncputAtt(ncid,fldName,'units',units); end; |
241 |
if ~isempty(coord); ncputAtt(ncid,fldName,'coordinates',coord); end; |
242 |
if strcmp(fldName,'lon'); ncputAtt(ncid,fldName,'standard_name','longitude'); end; |
243 |
if strcmp(fldName,'lat'); ncputAtt(ncid,fldName,'standard_name','latitude'); end; |
244 |
if strcmp(fldName,'dep'); ncputAtt(ncid,fldName,'standard_name','depth'); end; |
245 |
if strcmp(fldName,'tim'); ncputAtt(ncid,fldName,'standard_name','time'); end; |
246 |
if strcmp(fldName,'land'); ncputAtt(ncid,fldName,'standard_name','land_binary_mask'); end; |
247 |
if strcmp(fldName,'area'); ncputAtt(ncid,fldName,'standard_name','cell_area'); end; |
248 |
if strcmp(fldName,'thic'); ncputAtt(ncid,fldName,'standard_name','cell_thickness'); end; |
249 |
netcdf.endDef(ncid); |
250 |
% |
251 |
if ~fldInIsEmpty; ncputvar(ncid,fldName,fldTile); end; |
252 |
% |
253 |
ncclose(ncid); |
254 |
|
255 |
end;%for ff=1:ntile; |
256 |
|