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

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_IO/write2nc.m

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


Revision 1.1 - (hide annotations) (download)
Thu Apr 26 23:33:59 2012 UTC (13 years, 2 months ago) by gforget
Branch: MAIN
- write array (not a gcmfaces object) to netcdf file.

1 gforget 1.1 function []=write2nc(fileOut,fldIn,varargin);
2     %object : write array (not a gcmfaces object) to netcdf file
3     %inputs : fileOut is the name of the file to be created
4     % fldIn is the array to write to disk
5     %optional paramaters :
6     % can be provided in the form {'name',value}
7     % those that are currently active are
8     % 'descr' is the file description (default '').
9     % 'fldName' is the nc variable name for fld (default : the outside name of fldIn).
10     % 'longName' is the corresponding long name (default : '').
11     % 'units' is the unit of fld (default : '(unknown)').
12     % 'missval' is the missing value (default : NaN).
13     % 'fillval' is the fill value (default : NaN).
14     % 'tim' is a time vector (default : []) if fld has one such dimension.
15     % 'timName' is the long_name for that dimension (default : 'time index')
16     % 'lon' is a 2D longitude field consistent with the first two dimensions of fld.
17     % 'lat' is a 2D latitude field consistent with the first two dimensions of fld.
18     %netcdf dimensions : for unspecified (non-singleton) dimensions we will try to make a guess based on
19     % the length of mygrid.RC, mygrid.RF and mygrid.LATS; in case that guess fails
20     % we will use 'i1','i2' etc.
21    
22     gcmfaces_global;
23    
24     doCheck=1;%set to one to print stuff to screen
25    
26     %set more optional paramaters to default values
27     descr=''; fldName=inputname(2); longName=''; units='(unknown)'; missval=NaN; fillval=NaN;
28     tim=[]; timName='time index'; lon=[]; lat=[];
29    
30     %set more optional paramaters to user defined values
31     for ii=1:nargin-2;
32     if ~iscell(varargin{ii});
33     warning('inputCheck:write2nc_1',...
34     ['write2nc expects \n'...
35     ' its optional parameters as cell arrays. \n'...
36     ' Argument no. ' num2str(ii+1) ' was ignored \n'...
37     ' Type ''help write2nc'' for details.']);
38     elseif ~ischar(varargin{ii}{1});
39     warning('inputCheck:write2nc_2',...
40     ['write2nc expects \n'...
41     ' its optional parameters cell arrays \n'...
42     ' to start with character string. \n'...
43     ' Argument no. ' num2str(ii+1) ' was ignored \n'...
44     ' Type ''help write2nc'' for details.']);
45     else;
46     if strcmp(varargin{ii}{1},'descr')|...
47     strcmp(varargin{ii}{1},'fldName')|...
48     strcmp(varargin{ii}{1},'longName')|...
49     strcmp(varargin{ii}{1},'units')|...
50     strcmp(varargin{ii}{1},'missval')|...
51     strcmp(varargin{ii}{1},'fillval')|...
52     strcmp(varargin{ii}{1},'tim')|...
53     strcmp(varargin{ii}{1},'timName')|...
54     strcmp(varargin{ii}{1},'lon')|...
55     strcmp(varargin{ii}{1},'lat');
56     eval([varargin{ii}{1} '=varargin{ii}{2};']);
57     else;
58     warning('inputCheck:write2nc_3',...
59     ['unknown option ''' varargin{ii}{1} ''' was ignored']);
60     end;
61     end;
62     end;
63    
64     %select dimensions of relevance:
65     nDim=length(size(fldIn));
66     dimsize=size(fldIn);
67     for iDim=1:nDim;
68     if isfield(mygrid,'LATS'); nLATS=length(mygrid.LATS); else; nLATS=0; end;
69     if size(fldIn,iDim)==nLATS;
70     dimlist{iDim}='ilat';
71     dimName{iDim}='latitude';
72     dimvec.ilat=mygrid.LATS;
73     elseif size(fldIn,iDim)==length(mygrid.RC);
74     dimlist{iDim}='idept';
75     dimName{iDim}='depth';
76     dimvec.idept=-mygrid.RC;
77     elseif size(fldIn,iDim)==length(mygrid.RF);
78     dimlist{iDim}='idepw';
79     dimName{iDim}='depth';
80     dimvec.idepw=-mygrid.RF;
81     elseif size(fldIn,iDim)==length(tim);
82     dimlist{iDim}='itim';
83     dimName{iDim}=timName;
84     dimvec.itim=tim;
85     elseif size(fldIn,iDim)~=1;
86     dimlist{iDim}=['i' num2str(iDim)];
87     dimName{iDim}=['array index ' num2str(iDim)];
88     dimvec.igrid=[1:size(fldIn,iDim)];
89     end;
90     end;
91    
92     %omit singleton dimensions:
93     ii=find(dimsize~=1);
94     dimsize=dimsize(ii);
95     dimlist={dimlist{ii}};
96     dimName={dimName{ii}};
97    
98     %check :
99     if doCheck;
100     whos fldIn tim lon lat
101     descr
102     fldName
103     longName
104     units
105     missval
106     fillval
107     timName
108     dimlist
109     dimName
110     dimsize
111     dimvec
112     keyboard;
113     end;
114    
115     %create netcdf file:
116     %-------------------
117     ncid=nccreate(fileOut,'clobber');
118    
119     aa=sprintf([descr ' [file created with gcmfaces_IO/write2nc.m]']);
120     ncputAtt(ncid,'','description',aa);
121     ncputAtt(ncid,'','date',date);
122    
123     ncdefDim(ncid,'itxt',30);
124     for dd=1:length(dimlist); ncdefDim(ncid,dimlist{dd},dimsize(dd)); end;
125    
126     for dd=1:length(dimlist)1;
127     ncdefVar(ncid,dimlist{dd},'double',{dimlist{dd}});
128     ncputAtt(ncid,dimlist{dd},'long_name',dimName{dd});
129     end;
130    
131     ncdefVar(ncid,fldName,'double',flipdim(dimlist,2));%note the direction flip
132     ncputAtt(ncid,fldName,'long_name',longName);
133     ncputAtt(ncid,fldName,'units',units);
134     ncputAtt(ncid,fldName,'missing_value',missval);
135     ncputAtt(ncid,fldName,'_FillValue',fillval);
136    
137     ncclose(ncid);
138    
139     %fill diag field:
140     %---------------
141     ncid=ncopen(fileOut,'write');
142     ncputvar(ncid,fldName,fldIn);
143     for dd=1:length(dimlist);
144     ncputvar(ncid,dimlist{dd},getfield(dimvec,dimlist{dd}));
145     end;
146     ncclose(ncid);
147    
148    

  ViewVC Help
Powered by ViewVC 1.1.22