1 |
function []=process2nctiles(dirModel,fileModel,fldModel); |
function []=process2nctiles(dirModel,fileModel,fldModel,tileSize); |
2 |
%process2nctiles(dirModel); |
%process2nctiles(dirModel); |
3 |
%object : convert MITgcm binary output to netcdf files (tiled) |
%object : convert MITgcm binary output to netcdf files (tiled) |
4 |
%inputs : dirModel is the MITgcm run directory |
%inputs : dirModel is the MITgcm run directory |
9 |
% By default : all variables in e.g. 'state_2d_set1*' |
% By default : all variables in e.g. 'state_2d_set1*' |
10 |
% files will be processed, and writen individually to |
% files will be processed, and writen individually to |
11 |
% nctiles (tiled netcdf) that will be located in 'nctiles/' |
% nctiles (tiled netcdf) that will be located in 'nctiles/' |
12 |
% fldModel (optional) can be specifiec as e.g. 'ETAN' |
% fldModel (by default []) can be specified (as e.g. 'ETAN') |
13 |
|
% when fldModel is empty, all fields are processed |
14 |
|
% tileSize (optional) is e.g. [90 90] (by default tiles=faces) |
15 |
%output : (netcdf files) |
%output : (netcdf files) |
16 |
|
|
17 |
gcmfaces_global; |
gcmfaces_global; |
18 |
|
|
19 |
|
%listFiles={'state_2d_set1','state_2d_set2','state_3d_set1','state_3d_set2'}; |
20 |
|
%listFiles={'trsp_3d_set1','trsp_3d_set2','trsp_3d_set3'}; |
21 |
|
%for ff=1:length(listFiles); process2nctiles('iter12/',listFiles{ff},[],[90 90]); end; |
22 |
|
|
23 |
|
%replace time series with monthly climatology? |
24 |
|
doClim=0; |
25 |
|
|
26 |
%directory names |
%directory names |
27 |
listDirs={'STATE/','TRSP/'}; |
listDirs={'STATE/','TRSP/'}; |
28 |
filAvailDiag=[dirModel 'available_diagnostics.log']; |
filAvailDiag=[dirModel 'available_diagnostics.log']; |
29 |
filReadme=[dirModel 'README']; |
filReadme=[dirModel 'README']; |
30 |
dirOut=[dirModel 'nctiles/']; |
dirOut=[dirModel 'nctiles_tmp/']; |
31 |
if ~isdir(dirOut); mkdir(dirOut); end; |
if ~isdir(dirOut); mkdir(dirOut); end; |
32 |
|
|
33 |
%seacrh in subdirectories |
%search in subdirectories |
34 |
subDir=[]; |
subDir=[]; |
35 |
for ff=1:length(listDirs); |
for ff=1:length(listDirs); |
36 |
tmp1=dir([dirModel 'diags/' listDirs{ff} fileModel '*']); |
tmp1=dir([dirModel 'diags/' listDirs{ff} fileModel '*']); |
46 |
end; |
end; |
47 |
|
|
48 |
%set list of variables to process |
%set list of variables to process |
49 |
if ~isempty(who('fldModel')); |
if ~isempty(fldModel); |
50 |
if ischar(fldModel); listFlds={fldModel}; |
if ischar(fldModel); listFlds={fldModel}; |
51 |
else; listFlds=fldModel; |
else; listFlds=fldModel; |
52 |
end; |
end; |
55 |
listFlds=meta.fldList; |
listFlds=meta.fldList; |
56 |
end; |
end; |
57 |
|
|
58 |
|
%determine map of tile indices (by default tiles=faces) |
59 |
|
if isempty(whos('tileSize')); |
60 |
|
tileNo=mygrid.XC; |
61 |
|
for ff=1:mygrid.nFaces; tileNo{ff}(:)=ff; end; |
62 |
|
else; |
63 |
|
tileNo=gcmfaces_loc_tile(tileSize(1),tileSize(2)); |
64 |
|
end; |
65 |
|
|
66 |
%now do the actual processing |
%now do the actual processing |
67 |
for vv=1:length(listFlds); |
for vv=1:length(listFlds); |
68 |
nameDiag=deblank(listFlds{vv}) |
nameDiag=deblank(listFlds{vv}) |
75 |
%read time series |
%read time series |
76 |
myDiag=rdmds2gcmfaces([dirIn fileModel '*'],NaN,'rec',irec); |
myDiag=rdmds2gcmfaces([dirIn fileModel '*'],NaN,'rec',irec); |
77 |
|
|
78 |
%ancilliary fields for netcdf file |
%replace time series with monthly climatology |
79 |
tim=[1:size(myDiag{1},3)]; |
if doClim; myDiag=compClim(myDiag); end; |
80 |
descr=nameDiag; |
|
81 |
|
%set ancilliary time variable |
82 |
|
nn=length(size(myDiag{1})); |
83 |
|
nn=size(myDiag{1},nn); |
84 |
|
%tim=[1:nn]; |
85 |
|
tim=[1992*ones(nn,1) [1:nn]' 15*ones(nn,1)]; |
86 |
|
tim=datenum(tim)-datenum([1992 1 0]); |
87 |
|
timUnits='days since 1992-1-1 0:0:0'; |
88 |
|
|
89 |
|
%get time step axis |
90 |
|
[listTimes]=diags_list_times({[dirModel 'diags/STATE/']},{'state_2d_set1'}); |
91 |
|
|
92 |
%get units and long name from available_diagnostics.log |
%get units and long name from available_diagnostics.log |
93 |
[avail_diag]=read_avail_diag(filAvailDiag,nameDiag); |
[avail_diag]=read_avail_diag(filAvailDiag,nameDiag); |
97 |
disp(rdm'); |
disp(rdm'); |
98 |
|
|
99 |
%convert to MITgcm format (90x1170 array) |
%convert to MITgcm format (90x1170 array) |
100 |
myFile=[dirOut nameDiag]; |
myFile=[dirOut nameDiag];%first instance is for subdirectory name |
101 |
|
if ~isdir(myFile); mkdir(myFile); end; |
102 |
|
myFile=[myFile '/' nameDiag];%second instance is for file name base |
103 |
|
|
104 |
%get grid params |
%get grid params |
105 |
[grid_diag]=set_grid_diag(avail_diag); |
[grid_diag]=set_grid_diag(avail_diag); |
106 |
|
|
107 |
|
%apply mask, and convert to land mask |
108 |
|
msk=grid_diag.msk; |
109 |
|
if length(size(myDiag{1}))==3; |
110 |
|
msk=repmat(msk,[1 1 size(myDiag{1},3)]); |
111 |
|
end; |
112 |
|
for kk=1:size(myDiag{1},4); |
113 |
|
myDiag(:,:,:,kk)=myDiag(:,:,:,kk).*msk; |
114 |
|
end; |
115 |
|
clear msk; |
116 |
|
% |
117 |
|
land=isnan(grid_diag.msk); |
118 |
|
|
119 |
|
%set 'coord' attribute |
120 |
|
if avail_diag.nr~=1; |
121 |
|
coord='lon lat dep tim'; |
122 |
|
else; |
123 |
|
coord='lon lat tim'; |
124 |
|
end; |
125 |
|
|
126 |
|
%replace time series with monthly climatology |
127 |
|
if doClim; |
128 |
|
listTimes=listTimes(1:12); |
129 |
|
timUnits='days since year-1-1 0:0:0'; |
130 |
|
avail_diag.longNameDiag=[avail_diag.longNameDiag ' (climatology) ']; |
131 |
|
end; |
132 |
|
|
133 |
%create netcdf file using write2nctiles (works only with old matlab, thus far ...) |
%create netcdf file using write2nctiles (works only with old matlab, thus far ...) |
134 |
doCreate=1; |
doCreate=1; |
135 |
dimOut=write2nctiles(myFile,myDiag,doCreate,... |
dimlist=write2nctiles(myFile,myDiag,doCreate,{'tileNo',tileNo},... |
136 |
{'fldName',nameDiag},{'longName',avail_diag.longNameDiag},... |
{'fldName',nameDiag},{'longName',avail_diag.longNameDiag},... |
137 |
{'units',avail_diag.units},{'descr',descr},{'rdm',rdm}); |
{'units',avail_diag.units},{'descr',nameDiag},{'coord',coord},{'rdm',rdm}); |
|
|
|
|
%prepare to add fields |
|
|
doCreate=0; |
|
138 |
|
|
139 |
%determine relevant dimensions (note the reverse order) |
%determine relevant dimensions |
140 |
for ff=1:mygrid.nFaces; |
for ff=1:length(dimlist); |
141 |
dimtim{ff}={dimOut{ff}{1}}; |
dim.tim{ff}={dimlist{ff}{1}}; |
142 |
dim2d{ff}={dimOut{ff}{end-1:end}}; |
dim.twoD{ff}={dimlist{ff}{end-1:end}}; |
143 |
if avail_diag.nr~=1; |
if avail_diag.nr~=1; |
144 |
dimmsk{ff}={dimOut{ff}{end-2:end}}; |
dim.threeD{ff}={dimlist{ff}{end-2:end}}; |
145 |
dimdep{ff}={dimOut{ff}{end-2}}; |
dim.dep{ff}={dimlist{ff}{end-2}}; |
146 |
else; |
else; |
147 |
dimmsk{ff}=dim2d{ff}; |
dim.threeD{ff}=dim.twoD{ff}; |
148 |
dimdep{ff}=[]; |
dim.dep{ff}=[]; |
149 |
end; |
end; |
150 |
end; |
end; |
151 |
|
|
152 |
|
%prepare to add fields |
153 |
|
doCreate=0; |
154 |
|
|
155 |
%now add fields |
%now add fields |
156 |
write2nctiles(myFile,tim,doCreate,{'fldName','month'},... |
write2nctiles(myFile,tim,doCreate,{'tileNo',tileNo},... |
157 |
{'longName','month index starting Jan. 1992'},... |
{'fldName','tim'},{'longName','time'},... |
158 |
{'units',''},{'dimIn',dimtim}); |
{'units',timUnits},{'dimIn',dim.tim}); |
159 |
write2nctiles(myFile,grid_diag.lon,doCreate,... |
write2nctiles(myFile,listTimes,doCreate,{'tileNo',tileNo},... |
160 |
{'fldName','longitude'},{'units',''},{'dimIn',dim2d}); |
{'fldName','step'},{'longName','final time step number'},... |
161 |
write2nctiles(myFile,grid_diag.lat,doCreate,... |
{'units','1'},{'dimIn',dim.tim}); |
162 |
{'fldName','latitude'},{'units',''},{'dimIn',dim2d}); |
write2nctiles(myFile,grid_diag.lon,doCreate,{'tileNo',tileNo},... |
163 |
write2nctiles(myFile,grid_diag.msk,doCreate,... |
{'fldName','lon'},{'units','degrees_east'},{'dimIn',dim.twoD}); |
164 |
{'fldName','landmask'},{'units',''},... |
write2nctiles(myFile,grid_diag.lat,doCreate,{'tileNo',tileNo},... |
165 |
{'longName','land mask'},{'dimIn',dimmsk}); |
{'fldName','lat'},{'units','degrees_north'},{'dimIn',dim.twoD}); |
166 |
write2nctiles(myFile,grid_diag.RAC,doCreate,... |
write2nctiles(myFile,grid_diag.msk,doCreate,{'tileNo',tileNo},... |
167 |
{'fldName','cellarea'},{'units','m^2'},... |
{'fldName','land'},{'units','1'},{'longName','land mask'},{'dimIn',dim.threeD}); |
168 |
{'longName','grid cell area'},{'dimIn',dim2d}); |
write2nctiles(myFile,grid_diag.RAC,doCreate,{'tileNo',tileNo},... |
169 |
|
{'fldName','area'},{'units','m^2'},{'longName','grid cell area'},{'dimIn',dim.twoD}); |
170 |
if isfield(grid_diag,'dep'); |
if isfield(grid_diag,'dep'); |
171 |
write2nctiles(myFile,grid_diag.dep,doCreate,... |
write2nctiles(myFile,grid_diag.dep,doCreate,{'tileNo',tileNo},... |
172 |
{'fldName','depth'},{'units','m'},{'dimIn',dimdep}); |
{'fldName','dep'},{'units','m'},{'dimIn',dim.dep}); |
173 |
write2nctiles(myFile,grid_diag.dz,doCreate,... |
write2nctiles(myFile,grid_diag.dz,doCreate,{'tileNo',tileNo},... |
174 |
{'fldName','cellthick'},{'units','m'},{'dimIn',dimdep}); |
{'fldName','thic'},{'units','m'},{'dimIn',dim.dep}); |
175 |
end; |
end; |
176 |
|
|
177 |
end;%for vv=1:length(listFlds); |
end;%for vv=1:length(listFlds); |
180 |
|
|
181 |
%read meta file |
%read meta file |
182 |
tmp1=dir([fileName '*.meta']); tmp1=tmp1(1).name; |
tmp1=dir([fileName '*.meta']); tmp1=tmp1(1).name; |
183 |
tmp2=strfind(fileName,'/'); |
tmp2=strfind(fileName,filesep); |
184 |
if ~isempty(tmp2); tmp2=tmp2(end); else; tmp2=0; end; |
if ~isempty(tmp2); tmp2=tmp2(end); else; tmp2=0; end; |
185 |
tmp1=[fileName(1:tmp2) tmp1]; fid=fopen(tmp1); |
tmp1=[fileName(1:tmp2) tmp1]; fid=fopen(tmp1); |
186 |
while 1; |
while 1; |
291 |
grid_diag.dep=reshape(grid_diag.dep,[1 1 avail_diag.nr]); |
grid_diag.dep=reshape(grid_diag.dep,[1 1 avail_diag.nr]); |
292 |
grid_diag.dz=reshape(grid_diag.dz,[1 1 avail_diag.nr]); |
grid_diag.dz=reshape(grid_diag.dz,[1 1 avail_diag.nr]); |
293 |
end; |
end; |
294 |
|
|
295 |
|
%%replace time series with monthly climatology |
296 |
|
function [FLD]=compClim(fld); |
297 |
|
|
298 |
|
gcmfaces_global; |
299 |
|
|
300 |
|
ndim=length(size(fld{1})); |
301 |
|
nyear=size(fld{1},ndim)/12; |
302 |
|
|
303 |
|
if ndim==3; FLD=NaN*fld(:,:,1:12); end; |
304 |
|
if ndim==4; FLD=NaN*fld(:,:,:,1:12); end; |
305 |
|
|
306 |
|
for mm=1:12; |
307 |
|
if ndim==3; FLD(:,:,mm)=mean(fld(:,:,mm:12:12*nyear),ndim); end; |
308 |
|
if ndim==4; FLD(:,:,:,mm)=mean(fld(:,:,:,mm:12:12*nyear),ndim); end; |
309 |
|
end; |
310 |
|
|