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

Annotation 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.11 - (hide annotations) (download)
Fri Mar 17 17:41:56 2017 UTC (8 years, 3 months ago) by gforget
Branch: MAIN
CVS Tags: checkpoint66f, checkpoint66e, checkpoint66o, HEAD
Changes since 1.10: +1 -1 lines
o grid_load_native.m
 - revise help section
 - add option to initialize hFacC etc. to 1 when called in stand-alone mode (i.e., with 3 arguments)
 - set gcm2facesFast,memoryLimit,facesExpand to false,0,[].
 - assign ioSize based on convert2gcmfaces(mygrid.XC) size (convert2gcmfaces now calls
   convert2array when fileFormat is 'native ') and facesSize based on mygrid.XC face sizes.
o rdmds2gcmfaces.m
 - allow for secondary output from rdmds.m
o write2nctiles.m
 - comment out definition of never-used itxt dimension (was copied from MITprof?)
o convert2gcmfaces.m
 - use convert2array convert2gcmfaces when mygrid.fileFormat is 'native'

1 gforget 1.1 function [dimOut]=write2nctiles(fileOut,fldIn,doCreate,varargin);
2 gforget 1.10 % 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 gforget 1.1
36     gcmfaces_global;
37 gforget 1.5 if ~(myenv.useNativeMatlabNetcdf);
38     error('only native matlab nectdf is supported in write2nctiles');
39     end;
40 gforget 1.1
41     doCheck=0;%set to one to print stuff to screen
42    
43 gforget 1.10 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 gforget 1.7 fldInIsaGcmfaces=isa(fldIn,'gcmfaces');
48 gforget 1.8 if fldInIsaGcmfaces;
49     fldInIsEmpty=isempty(fldIn{1});
50     else;
51     fldInIsEmpty=isempty(fldIn);
52     end;
53 gforget 1.10
54 gforget 1.1 %set more optional paramaters to default values
55 gforget 1.10 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 gforget 1.4 tileNo=mygrid.XC; for ff=1:mygrid.nFaces; tileNo{ff}(:)=ff; end;
61 gforget 1.10 coord=''; dimsize=[]; xtype='double'; descr=''; rdm='';
62 gforget 1.1
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 gforget 1.2 strcmp(varargin{ii}{1},'rdm')|...
81 gforget 1.1 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 gforget 1.4 strcmp(varargin{ii}{1},'tileNo')|...
87 gforget 1.5 strcmp(varargin{ii}{1},'coord')|...
88 gforget 1.8 strcmp(varargin{ii}{1},'dimsize')|...
89     strcmp(varargin{ii}{1},'xtype')|...
90 gforget 1.1 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 gforget 1.4 %split fldIn (if isa gcmfaces) into tiles
100     tileList=unique(convert2vector(tileNo));
101     tileList=tileList(~isnan(tileList));
102     ntile=length(tileList);
103     %
104 gforget 1.8 if fldInIsaGcmfaces&~fldInIsEmpty;
105 gforget 1.4 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 gforget 1.7 clear fldIn;
118 gforget 1.8 elseif fldInIsaGcmfaces;
119     for ff=1:ntile;
120     fldTiles{ff}=[];
121     end;
122 gforget 1.4 end;
123    
124     %start processing loop
125     for ff=1:ntile;
126 gforget 1.1
127 gforget 1.7 if fldInIsaGcmfaces;
128 gforget 1.4 fldTile=fldTiles{ff};
129 gforget 1.1 %reverse order of dimensions
130     nn=length(size(fldTile));
131     fldTile=permute(fldTile,[nn:-1:1]);
132 gforget 1.7 if ntile==1; clear fldTiles; end;
133 gforget 1.1 else;
134     fldTile=fldIn;
135     end;
136 gforget 1.4 fileTile=[fileOut sprintf('.%04d.nc',ff)];
137 gforget 1.1
138     %select dimensions of relevance:
139 gforget 1.8 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 gforget 1.1 for iDim=1:nDim;
149 gforget 1.8 if dimsize(iDim)~=1;
150 gforget 1.1 dimlist{iDim}=['i' num2str(iDim)];
151     dimName{iDim}=['array index ' num2str(iDim)];
152 gforget 1.8 eval(['dimvec.i' num2str(iDim) '=[1:dimsize(iDim)];']);
153 gforget 1.1 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 gforget 1.8 if prod(dimsize)*4/1e9<1.5;%use (always available) basic netcdf:
185 gforget 1.7 mode='clobber';
186     else;%to allow for large file:
187     mode='NETCDF4';
188     end;
189 gforget 1.9 ncid=nccreateFile(fileTile,mode);
190 gforget 1.6 nc_global=netcdf.getConstant('NC_GLOBAL');
191 gforget 1.1
192 gforget 1.3 if ~isempty(rdm);
193     descr2=[descr ' -- ' rdm{1}];
194     else;
195     descr2=descr;
196     end;
197 gforget 1.4 ncputAtt(ncid,'','description',descr2);
198 gforget 1.2 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 gforget 1.10 if length(rdm)>0; pp=length(rdm)+1; tmp1=char(pp+63); else; tmp1='A'; end;
204 gforget 1.2 netcdf.putAtt(ncid,nc_global,tmp1,'file created using gcmfaces_IO/write2nctiles.m');
205 gforget 1.1 ncputAtt(ncid,'','date',date);
206 gforget 1.5 netcdf.putAtt(ncid,nc_global,'Conventions','CF-1.6')
207    
208     ncputAtt(ncid,'','_FillValue',fillval);
209     ncputAtt(ncid,'','missing_value',missval);
210 gforget 1.1
211 gforget 1.11 %ncdefDim(ncid,'itxt',30);
212 gforget 1.1 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 gforget 1.5 ncputAtt(ncid,dimlist{dd},'units','1');
218 gforget 1.1 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 gforget 1.5 %
237     netcdf.reDef(ncid);
238 gforget 1.8 ncdefVar(ncid,fldName,xtype,flipdim(dimlist,2));%note the direction flip
239 gforget 1.1 if ~isempty(longName); ncputAtt(ncid,fldName,'long_name',longName); end;
240     if ~isempty(units); ncputAtt(ncid,fldName,'units',units); end;
241 gforget 1.5 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 gforget 1.8 if ~fldInIsEmpty; ncputvar(ncid,fldName,fldTile); end;
252     %
253 gforget 1.1 ncclose(ncid);
254    
255 gforget 1.4 end;%for ff=1:ntile;
256 gforget 1.1

  ViewVC Help
Powered by ViewVC 1.1.22