/[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.7 - (show annotations) (download)
Wed Mar 16 15:02:19 2016 UTC (9 years, 3 months ago) by gforget
Branch: MAIN
CVS Tags: checkpoint65v, checkpoint65u
Changes since 1.6: +12 -4 lines
- process2nctiles.m: rename 'step' netcdf variable as 'timstep' to avoid
  conflict with existing function; add hard coded 'diagsDir' param;
  omit grid variables in netcdf file when RAC has not been defined;
  use repmat to avoid loop over size(myDiag{1},4).
- write2file.m: add omitNaNs option (2nd optional input parameter; 1 by default)
- write2meta.m: add fldList option (2nd optional input parameter; [] by default)
- write2nctiles.m: switch to 'NETCDF4' if array size exceed 1.5G; clear
  temporary variables once they are no longer needed.

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

  ViewVC Help
Powered by ViewVC 1.1.22