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

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

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

revision 1.3 by gforget, Sun Feb 2 01:47:38 2014 UTC revision 1.10 by gforget, Thu Mar 16 13:15:39 2017 UTC
# Line 1  Line 1 
1  function [dimOut]=write2nctiles(fileOut,fldIn,doCreate,varargin);  function [dimOut]=write2nctiles(fileOut,fldIn,doCreate,varargin);
2  %object : write gcmfaces object to netcdf files (tiled)  % WRITE2NCTILES writes a gcmfaces object to nctiles, tiled netcdf, files
3  %inputs : fileOut is the name of the file to be created  %
4  %         fldIn is the array to write to disk  % inputs :
5  %         doCreate is a 0/1 switch; 1 => create file ; 0 => append to file.  %    - fileOut is the name of the file to be created
6  %optional paramaters :  %    - fldIn is the array to write to disk
7  %         can be provided in the form {'name',value}  %    - doCreate: if 1 (default) then create file ; if 0 then append to file.
8  %         those that are currently active are  %
9  %               'descr' is the file description (default '').  % additional paramaters can be provided afterwards in the {'name',value} form:
10  %               'rdm' is the extended estimate description (default '').  %         'fldName' is the nc variable name for fld (see "notes" regarding default)
11  %               'fldName' is the nc variable name for fld (default : the outside name of fldIn).  %         'longName' is the corresponding long name ('' by default).
12  %               'longName' is the corresponding long name (default : '').  %         'units' is the unit of fld ('(unknown)' by default).
13  %               'units' is the unit of fld (default : '(unknown)').  %         'missval' is the missing value (NaN by default).
14  %               'missval' is the missing value (default : NaN).  %         'fillval' is the fill value (NaN by default).
15  %               'fillval' is the fill value (default : NaN).  %         'tileNo' is a map of tile indices (face # by default)
16  %netcdf dimensions : array dimensions are simply set to 'i1,i2,...'  %         '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;  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  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  %set more optional paramaters to default values
55  descr='';  fldName=inputname(2);
56  rdm='';  if isempty(fldName);
57  fldName=inputname(2); longName='';      [tmp1,fldName,tmp2] = fileparts(fileOut);
58  units='(unknown)'; missval=NaN; fillval=NaN;  end;
59  dimIn=[];  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  %set more optional paramaters to user defined values
64  for ii=1:nargin-3;  for ii=1:nargin-3;
# Line 49  for ii=1:nargin-3; Line 83  for ii=1:nargin-3;
83                  strcmp(varargin{ii}{1},'units')|...                  strcmp(varargin{ii}{1},'units')|...
84                  strcmp(varargin{ii}{1},'missval')|...                  strcmp(varargin{ii}{1},'missval')|...
85                  strcmp(varargin{ii}{1},'fillval')|...                  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');                  strcmp(varargin{ii}{1},'dimIn');
91              eval([varargin{ii}{1} '=varargin{ii}{2};']);              eval([varargin{ii}{1} '=varargin{ii}{2};']);
92          else;          else;
# Line 58  for ii=1:nargin-3; Line 96  for ii=1:nargin-3;
96      end;      end;
97  end;  end;
98    
99  for ff=1:mygrid.nFaces;  %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 isa(fldIn,'gcmfaces');  if fldInIsaGcmfaces;
128    fldTile=fldIn{ff};    fldTile=fldTiles{ff};
129    %reverse order of dimensions    %reverse order of dimensions
130    nn=length(size(fldTile));    nn=length(size(fldTile));
131    fldTile=permute(fldTile,[nn:-1:1]);    fldTile=permute(fldTile,[nn:-1:1]);
132      if ntile==1; clear fldTiles; end;
133  else;  else;
134    fldTile=fldIn;    fldTile=fldIn;
135  end;  end;
136  fileTile=[fileOut sprintf('.%03d.nc',ff)];  fileTile=[fileOut sprintf('.%04d.nc',ff)];
137    
138  %select dimensions of relevance:  %select dimensions of relevance:
139  nDim=length(size(fldTile));  if ~fldInIsEmpty;
140  dimsize=size(fldTile);    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;  for iDim=1:nDim;
149  if size(fldTile,iDim)~=1;  if dimsize(iDim)~=1;
150      dimlist{iDim}=['i' num2str(iDim)];      dimlist{iDim}=['i' num2str(iDim)];
151      dimName{iDim}=['array index ' num2str(iDim)];      dimName{iDim}=['array index ' num2str(iDim)];
152      eval(['dimvec.i' num2str(iDim) '=[1:size(fldTile,iDim)];']);      eval(['dimvec.i' num2str(iDim) '=[1:dimsize(iDim)];']);
153  end;  end;
154  end;  end;
155    
# Line 109  dimOut{ff}=dimlist; Line 181  dimOut{ff}=dimlist;
181  if doCreate;  if doCreate;
182    %create netcdf file:    %create netcdf file:
183    %-------------------    %-------------------
184    ncid=nccreate(fileTile,'NETCDF4');    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);    if ~isempty(rdm);
193      descr2=[descr ' -- ' rdm{1}];      descr2=[descr ' -- ' rdm{1}];
194    else;    else;
195      descr2=descr;      descr2=descr;
196    end;    end;
197    ncputAtt(ncid,'','description',descr);    ncputAtt(ncid,'','description',descr2);
198    for pp=2:length(rdm);    for pp=2:length(rdm);
199    tmp1=char(pp+63);    tmp1=char(pp+63);
200    netcdf.putAtt(ncid,nc_global,tmp1,rdm{pp});    netcdf.putAtt(ncid,nc_global,tmp1,rdm{pp});
201    end;    end;
202    %append readme    %append readme
203    pp=length(rdm)+1;    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');    netcdf.putAtt(ncid,nc_global,tmp1,'file created using gcmfaces_IO/write2nctiles.m');
205    ncputAtt(ncid,'','date',date);    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);    ncdefDim(ncid,'itxt',30);
212    for dd=1:length(dimlist); ncdefDim(ncid,dimlist{dd},dimsize(dd)); end;    for dd=1:length(dimlist); ncdefDim(ncid,dimlist{dd},dimsize(dd)); end;
# Line 132  if doCreate; Line 214  if doCreate;
214    for dd=1:length(dimlist);    for dd=1:length(dimlist);
215      ncdefVar(ncid,dimlist{dd},'double',{dimlist{dd}});      ncdefVar(ncid,dimlist{dd},'double',{dimlist{dd}});
216      ncputAtt(ncid,dimlist{dd},'long_name',dimName{dd});      ncputAtt(ncid,dimlist{dd},'long_name',dimName{dd});
217        ncputAtt(ncid,dimlist{dd},'units','1');
218    end;    end;
219    ncclose(ncid);    ncclose(ncid);
220    
# Line 150  if ~isempty(dimIn); dimlist=dimIn{ff}; e Line 233  if ~isempty(dimIn); dimlist=dimIn{ff}; e
233  %define and fill field:  %define and fill field:
234  %----------------------  %----------------------
235  ncid=ncopen(fileTile,'write');  ncid=ncopen(fileTile,'write');
236  if (myenv.useNativeMatlabNetcdf); netcdf.reDef(ncid); end;  %
237  ncdefVar(ncid,fldName,'double',flipdim(dimlist,2));%note the direction flip  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;  if ~isempty(longName); ncputAtt(ncid,fldName,'long_name',longName); end;
240  if ~isempty(units); ncputAtt(ncid,fldName,'units',units); end;  if ~isempty(units); ncputAtt(ncid,fldName,'units',units); end;
241  ncputAtt(ncid,fldName,'missing_value',missval);  if ~isempty(coord); ncputAtt(ncid,fldName,'coordinates',coord); end;
242  if (myenv.useNativeMatlabNetcdf); netcdf.endDef(ncid); end;  if strcmp(fldName,'lon'); ncputAtt(ncid,fldName,'standard_name','longitude'); end;
243  ncputvar(ncid,fldName,fldTile);  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);  ncclose(ncid);
254    
255  end;%for ff=1:mygrid.nFaces;  end;%for ff=1:ntile;
256    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.22