/[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.7 by gforget, Wed Mar 16 15:02:19 2016 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  %               'tileNo' is a map of tile indices (default is face number)  %         'coord' is auxilliary coordinates attribute (e.g. 'lon lat dep')
17  %               'coord' is auxilliary coordinates attribute (e.g. 'lon lat dep')  %         'dimsize' is the array size associated with 'coord'
18  %netcdf dimensions : array dimensions are simply set to 'i1,i2,...'  %         '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);  if ~(myenv.useNativeMatlabNetcdf);
# Line 24  end; Line 40  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  fldInIsaGcmfaces=isa(fldIn,'gcmfaces');  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; dimIn=[];  end;
59    longName=''; units='(unknown)'; missval=NaN; fillval=NaN; dimIn=[];
60  tileNo=mygrid.XC; for ff=1:mygrid.nFaces; tileNo{ff}(:)=ff; end;  tileNo=mygrid.XC; for ff=1:mygrid.nFaces; tileNo{ff}(:)=ff; end;
61  coord='';  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 59  for ii=1:nargin-3; Line 85  for ii=1:nargin-3;
85                  strcmp(varargin{ii}{1},'fillval')|...                  strcmp(varargin{ii}{1},'fillval')|...
86                  strcmp(varargin{ii}{1},'tileNo')|...                  strcmp(varargin{ii}{1},'tileNo')|...
87                  strcmp(varargin{ii}{1},'coord')|...                  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 73  tileList=unique(convert2vector(tileNo)); Line 101  tileList=unique(convert2vector(tileNo));
101  tileList=tileList(~isnan(tileList));  tileList=tileList(~isnan(tileList));
102  ntile=length(tileList);  ntile=length(tileList);
103  %  %
104  if fldInIsaGcmfaces;  if fldInIsaGcmfaces&~fldInIsEmpty;
105   for ff=1:ntile;   for ff=1:ntile;
106     tmp1=[];     tmp1=[];
107     for gg=1:mygrid.nFaces;     for gg=1:mygrid.nFaces;
# Line 87  if fldInIsaGcmfaces; Line 115  if fldInIsaGcmfaces;
115     fldTiles{ff}=tmp1;     fldTiles{ff}=tmp1;
116   end;   end;
117   clear fldIn;   clear fldIn;
118    elseif fldInIsaGcmfaces;
119     for ff=1:ntile;
120       fldTiles{ff}=[];
121     end;
122  end;  end;
123    
124  %start processing loop  %start processing loop
# Line 104  end; Line 136  end;
136  fileTile=[fileOut sprintf('.%04d.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 142  dimOut{ff}=dimlist; Line 181  dimOut{ff}=dimlist;
181  if doCreate;  if doCreate;
182    %create netcdf file:    %create netcdf file:
183    %-------------------    %-------------------
184    if prod(size(fldTile))*4/1e9<1.5;%use (always available) basic ncetcf:    if prod(dimsize)*4/1e9<1.5;%use (always available) basic netcdf:
185      mode='clobber';      mode='clobber';
186    else;%to allow for large file:    else;%to allow for large file:
187      mode='NETCDF4';      mode='NETCDF4';
188    end;    end;
189    ncid=nccreate(fileTile,mode);    ncid=nccreateFile(fileTile,mode);
190    nc_global=netcdf.getConstant('NC_GLOBAL');    nc_global=netcdf.getConstant('NC_GLOBAL');
191    
192    if ~isempty(rdm);    if ~isempty(rdm);
# Line 161  if doCreate; Line 200  if doCreate;
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')      netcdf.putAtt(ncid,nc_global,'Conventions','CF-1.6')  
# Line 196  if ~isempty(dimIn); dimlist=dimIn{ff}; e Line 235  if ~isempty(dimIn); dimlist=dimIn{ff}; e
235  ncid=ncopen(fileTile,'write');  ncid=ncopen(fileTile,'write');
236  %  %
237  netcdf.reDef(ncid);  netcdf.reDef(ncid);
238  ncdefVar(ncid,fldName,'double',flipdim(dimlist,2));%note the direction flip  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  if ~isempty(coord); ncputAtt(ncid,fldName,'coordinates',coord); end;  if ~isempty(coord); ncputAtt(ncid,fldName,'coordinates',coord); end;
# Line 209  if strcmp(fldName,'area'); ncputAtt(ncid Line 248  if strcmp(fldName,'area'); ncputAtt(ncid
248  if strcmp(fldName,'thic'); ncputAtt(ncid,fldName,'standard_name','cell_thickness'); end;  if strcmp(fldName,'thic'); ncputAtt(ncid,fldName,'standard_name','cell_thickness'); end;
249  netcdf.endDef(ncid);  netcdf.endDef(ncid);
250  %  %
251  ncputvar(ncid,fldName,fldTile);  if ~fldInIsEmpty; ncputvar(ncid,fldName,fldTile); end;
252    %
253  ncclose(ncid);  ncclose(ncid);
254    
255  end;%for ff=1:ntile;  end;%for ff=1:ntile;

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

  ViewVC Help
Powered by ViewVC 1.1.22