/[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.6 - (show annotations) (download)
Sun Jan 18 14:07:15 2015 UTC (10 years, 5 months ago) by gforget
Branch: MAIN
CVS Tags: checkpoint65r, checkpoint65p, checkpoint65q, checkpoint65t
Changes since 1.5: +1 -0 lines
- get nc_global from netcdf.getConstant('NC_GLOBAL')
- remove out of date comment

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

  ViewVC Help
Powered by ViewVC 1.1.22