/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_IO/write2nctiles.m
ViewVC logotype

Contents of /MITgcm_contrib/gael/matlab_class/gcmfaces_IO/write2nctiles.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download)
Sat May 7 15:21:57 2016 UTC (9 years, 2 months ago) by gforget
Branch: MAIN
CVS Tags: checkpoint65x, checkpoint65w, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a
Changes since 1.7: +31 -9 lines
- add options:
    'dimsize' is the array size associated with 'coord'
    'xtype' is 'double' by default

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

  ViewVC Help
Powered by ViewVC 1.1.22