7 |
% can be provided in the form {'name',value} |
% can be provided in the form {'name',value} |
8 |
% those that are currently active are |
% those that are currently active are |
9 |
% 'descr' is the file description (default ''). |
% '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). |
% 'fldName' is the nc variable name for fld (default : the outside name of fldIn). |
12 |
% 'longName' is the corresponding long name (default : ''). |
% 'longName' is the corresponding long name (default : ''). |
13 |
% 'units' is the unit of fld (default : '(unknown)'). |
% 'units' is the unit of fld (default : '(unknown)'). |
14 |
% 'missval' is the missing value (default : NaN). |
% 'missval' is the missing value (default : NaN). |
15 |
% 'fillval' is the fill value (default : NaN). |
% '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,...' |
%netcdf dimensions : array dimensions are simply set to 'i1,i2,...' |
19 |
|
|
20 |
gcmfaces_global; |
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 |
doCheck=0;%set to one to print stuff to screen |
26 |
|
|
27 |
%set more optional paramaters to default values |
%set more optional paramaters to default values |
28 |
descr=''; |
descr=''; |
29 |
|
rdm=''; |
30 |
fldName=inputname(2); longName=''; |
fldName=inputname(2); longName=''; |
31 |
units='(unknown)'; missval=NaN; fillval=NaN; |
units='(unknown)'; missval=NaN; fillval=NaN; dimIn=[]; |
32 |
dimIn=[]; |
tileNo=mygrid.XC; for ff=1:mygrid.nFaces; tileNo{ff}(:)=ff; end; |
33 |
|
coord=''; |
34 |
|
|
35 |
%set more optional paramaters to user defined values |
%set more optional paramaters to user defined values |
36 |
for ii=1:nargin-3; |
for ii=1:nargin-3; |
49 |
' Type ''help write2nctiles'' for details.']); |
' Type ''help write2nctiles'' for details.']); |
50 |
else; |
else; |
51 |
if strcmp(varargin{ii}{1},'descr')|... |
if strcmp(varargin{ii}{1},'descr')|... |
52 |
|
strcmp(varargin{ii}{1},'rdm')|... |
53 |
strcmp(varargin{ii}{1},'fldName')|... |
strcmp(varargin{ii}{1},'fldName')|... |
54 |
strcmp(varargin{ii}{1},'longName')|... |
strcmp(varargin{ii}{1},'longName')|... |
55 |
strcmp(varargin{ii}{1},'units')|... |
strcmp(varargin{ii}{1},'units')|... |
56 |
strcmp(varargin{ii}{1},'missval')|... |
strcmp(varargin{ii}{1},'missval')|... |
57 |
strcmp(varargin{ii}{1},'fillval')|... |
strcmp(varargin{ii}{1},'fillval')|... |
58 |
|
strcmp(varargin{ii}{1},'tileNo')|... |
59 |
|
strcmp(varargin{ii}{1},'coord')|... |
60 |
strcmp(varargin{ii}{1},'dimIn'); |
strcmp(varargin{ii}{1},'dimIn'); |
61 |
eval([varargin{ii}{1} '=varargin{ii}{2};']); |
eval([varargin{ii}{1} '=varargin{ii}{2};']); |
62 |
else; |
else; |
66 |
end; |
end; |
67 |
end; |
end; |
68 |
|
|
69 |
for ff=1:mygrid.nFaces; |
%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'); |
if isa(fldIn,'gcmfaces'); |
93 |
fldTile=fldIn{ff}; |
fldTile=fldTiles{ff}; |
94 |
%reverse order of dimensions |
%reverse order of dimensions |
95 |
nn=length(size(fldTile)); |
nn=length(size(fldTile)); |
96 |
fldTile=permute(fldTile,[nn:-1:1]); |
fldTile=permute(fldTile,[nn:-1:1]); |
97 |
else; |
else; |
98 |
fldTile=fldIn; |
fldTile=fldIn; |
99 |
end; |
end; |
100 |
fileTile=[fileOut sprintf('.%03d.nc',ff)]; |
fileTile=[fileOut sprintf('.%04d.nc',ff)]; |
101 |
|
|
102 |
%select dimensions of relevance: |
%select dimensions of relevance: |
103 |
nDim=length(size(fldTile)); |
nDim=length(size(fldTile)); |
138 |
if doCreate; |
if doCreate; |
139 |
%create netcdf file: |
%create netcdf file: |
140 |
%------------------- |
%------------------- |
141 |
ncid=nccreate(fileTile,'NETCDF4'); |
% ncid=nccreate(fileTile,'NETCDF4');%to allow for big files |
142 |
|
ncid=nccreate(fileTile,'clobber'); |
143 |
aa=sprintf([descr ' [file created with gcmfaces_IO/write2nctiles.m]']); |
nc_global=netcdf.getConstant('NC_GLOBAL'); |
144 |
ncputAtt(ncid,'','description',aa); |
|
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); |
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); |
ncdefDim(ncid,'itxt',30); |
165 |
for dd=1:length(dimlist); ncdefDim(ncid,dimlist{dd},dimsize(dd)); end; |
for dd=1:length(dimlist); ncdefDim(ncid,dimlist{dd},dimsize(dd)); end; |
167 |
for dd=1:length(dimlist); |
for dd=1:length(dimlist); |
168 |
ncdefVar(ncid,dimlist{dd},'double',{dimlist{dd}}); |
ncdefVar(ncid,dimlist{dd},'double',{dimlist{dd}}); |
169 |
ncputAtt(ncid,dimlist{dd},'long_name',dimName{dd}); |
ncputAtt(ncid,dimlist{dd},'long_name',dimName{dd}); |
170 |
|
ncputAtt(ncid,dimlist{dd},'units','1'); |
171 |
end; |
end; |
172 |
ncclose(ncid); |
ncclose(ncid); |
173 |
|
|
186 |
%define and fill field: |
%define and fill field: |
187 |
%---------------------- |
%---------------------- |
188 |
ncid=ncopen(fileTile,'write'); |
ncid=ncopen(fileTile,'write'); |
189 |
if (myenv.useNativeMatlabNetcdf); netcdf.reDef(ncid); end; |
% |
190 |
|
netcdf.reDef(ncid); |
191 |
ncdefVar(ncid,fldName,'double',flipdim(dimlist,2));%note the direction flip |
ncdefVar(ncid,fldName,'double',flipdim(dimlist,2));%note the direction flip |
192 |
if ~isempty(longName); ncputAtt(ncid,fldName,'long_name',longName); end; |
if ~isempty(longName); ncputAtt(ncid,fldName,'long_name',longName); end; |
193 |
if ~isempty(units); ncputAtt(ncid,fldName,'units',units); end; |
if ~isempty(units); ncputAtt(ncid,fldName,'units',units); end; |
194 |
ncputAtt(ncid,fldName,'missing_value',missval); |
if ~isempty(coord); ncputAtt(ncid,fldName,'coordinates',coord); end; |
195 |
if (myenv.useNativeMatlabNetcdf); netcdf.endDef(ncid); end; |
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); |
ncputvar(ncid,fldName,fldTile); |
205 |
ncclose(ncid); |
ncclose(ncid); |
206 |
|
|
207 |
end;%for ff=1:mygrid.nFaces; |
end;%for ff=1:ntile; |
208 |
|
|