forked from emmomp/write-gcmfaces-adjoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_adj2netcdf.m
319 lines (262 loc) · 11.8 KB
/
write_adj2netcdf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
function write_adj2netcdf(expt,var,mygrid,mode)
% Writes adjoint experiment sensitivity fields to netcdf format
%INPUTS:
% expt- Location of var files
% var - Variable name(s) (from file name) to convert to netcdf format
% mygrid - the mygrid object created by grid_load
% mode - 'nctiles','interp' or 'both' for output in nctiles format,
% interpolated to 1/2 degree grid, or both.
% ### Define variable attributes etc in *adj_netcdf_options.m* ###
nvars = length(var);
for v = 1:nvars
% Load options
display('Loading Options')
adj_netcdf_options;
fieldname=var{v};
longname=['Sensitivity of Objective Function to ' longn(fieldname)];
units = [unit_of ' per ' unit_v(fieldname)];
tileList=unique(convert2vector(tileno));
display('Loading Variable')
fin = [expt var{v}];
fout = [expt var{v}];
if strcmp(var{v}(1:4),'adxx')
varin = rdmds2gcmfaces(fin,12);
elseif strcmp(var{v}(1:3),'ADJ')
varin = rdmds2gcmfaces(fin,nan);
else
display('Expecting variables of the type adxx or ADJ')
end
its = load([expt 'its_ad.txt']);
nt = length(its);
nd = ndims(varin.f1);
time = date0_num+its*timestep/60/60/24;
time_lag = time-date_lag0;
if nd ==3
display('Found 3D variable')
varin = varin(:,:,1:nt);
coords = 'lon lat tim';
elseif nd ==4
display('Found 4D variable')
varin = varin(:,:,:,1:nt);
coords = 'lon lat dep tim';
else
display('Expecting 3 or 4d variable input')
end
if strcmp(mode,'nctiles')||strcmp(mode,'both')
readme{length(readme)+1}='Experiment run on LLC grid';
display('===========================================================================================')
display('***The following options have been selected, please edit adj_netcdf_options.m to change:***')
display('===========================================================================================')
display(sprintf('Writing var to %s.[ind].nc, %.0f tiles',fout,max(tileList)))
display(sprintf('Variable %s:%s',fieldname,longname))
display(sprintf('Units %s; Coords, %s; Miss val %s; Fill val %s',units,coords,missval,fillval))
display(descr(:)')
for r = 1:length(readme)
display(readme{r})
end
% Write main variable
overwrite = 1;
dimlist=mywrite2nctiles(fout,varin,overwrite,{'missval',missval},{'fillval',fillval},{'descr',descr},{'tileNo',tileno},{'rdm',readme},{'fldName',fieldname},{'longName',longname},{'units',units},{'coord',coords});
% Get the right dimensions for other vars
dimIn2D = cell(1,length(dimlist));
dimInT = dimIn2D;
dimIn3D = dimIn2D;
dimInZ = dimIn2D;
for ff=1:length(dimlist)
dimIn2D{ff} = {dimlist{ff}{end-1:end}};
dimInT{ff} = {dimlist{ff}{1}};
if nd == 4
dimIn3D{ff} = {dimlist{ff}{end-2:end}};
dimInZ{ff}={dimlist{ff}{end-2}};
end
end
% Add other variables from grid, time
overwrite = 0;
fieldname = 'lon';
units = 'degrees east';
mywrite2nctiles(fout,mygrid.XC,overwrite,{'missval',missval},{'fillval',fillval},{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'dimIn',dimIn2D});
fieldname = 'lat';
units = 'degrees north';
mywrite2nctiles(fout,mygrid.YC,overwrite,{'missval',missval},{'fillval',fillval},{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'dimIn',dimIn2D});
fieldname = 'tim';
units = 'Days since January 0, 0000';
mywrite2nctiles(fout,time,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'dimIn',dimInT});
fieldname = 'tim_lag';
units = 'Lag in Days';
mywrite2nctiles(fout,time_lag,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'dimIn',dimInT});
fieldname = 'date0';
longname = 'Start of Simulation';
units = 'Days since January 0, 0000';
mywrite2nctiles(fout,date0_num,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'longName',longname},{'dimIn',[]});
fieldname = 'date_lag0';
longname = 'Start of Objective Function : Lag 0';
units = 'Days since January 0, 0000';
mywrite2nctiles(fout,date_lag0,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'longName',longname},{'dimIn',[]});
area = mygrid.RAC;
fieldname = 'area';
units = 'm^2';
longname='grid cell area';
mywrite2nctiles(fout,area,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'longName',longname},{'dimIn',dimIn2D});
mask = mygrid.mskC;
dep = -mygrid.RC;
dz = mygrid.DRF;
if nd == 3
fieldname = 'land';
units = '1';
longname='land mask';
mywrite2nctiles(fout,squeeze(mask(:,:,1)),overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'longName',longname},{'dimIn',dimIn2D});
elseif nd == 4
fieldname = 'land';
units = '1';
longname='land mask';
mywrite2nctiles(fout,mask,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'longName',longname},{'dimIn',dimIn3D});
fieldname = 'thic';
units = 'm';
mywrite2nctiles(fout,dz,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'dimIn', dimInZ});
fieldname = 'dep';
units = 'm';
mywrite2nctiles(fout,dep,overwrite,{'tileNo',tileno},{'fldName',fieldname},{'units',units},{'dimIn', dimInZ});
end
end
if strcmp(mode,'interp')||strcmp(mode,'both')
adj_netcdf_options;
fieldname=var{v};
longname=['Sensitivity of Objective Function to ' longn(fieldname)];
units = [unit_of ' per ' unit_v(fieldname)];
readme{length(readme)+1}='Interpolated from LLC grid to 1/2 degree grid using gcmfaces_interp_coeffs.m';
if v == 1
lon=-179.75:0.5:179.75; lat=-89.75:0.5:89.75;
[lat2,lon2] = meshgrid(lat,lon);
display('Calculating Interpolation Coeffs')
interp=gcmfaces_interp_coeffs(lon2(:),lat2(:));
dep = -mygrid.RC;
end
display('===========================================================================================')
display('***The following options have been selected, please edit adj_netcdf_options.m to change:***')
display('===========================================================================================')
display(sprintf('Writing interpolated var to %s.05deg.nc',fout))
display(sprintf('Variable %s: %s',fieldname,longname))
display(sprintf('Units %s; Coords, %s; Miss val %s; Fill val %s',units,coords,missval,fillval))
display(descr(:)')
for r = 1:length(readme)
display(readme{r})
end
% Make Netcdf file
fout = [fout '.05deg.nc'];
ncid=nccreateFile(fout,'CLOBBER');
nc_global=netcdf.getConstant('NC_GLOBAL');
% Add readme
if ~isempty(readme);
descr2=[descr ' -- ' readme{1}];
else
descr2=descr;
end
ncputAtt(ncid,'','description',descr2);
for pp=2:length(readme);
tmp1=char(pp+63);
netcdf.putAtt(ncid,nc_global,tmp1,readme{pp});
end;
ncputAtt(ncid,'','date',date);
netcdf.putAtt(ncid,nc_global,'Conventions','CF-1.6')
ncputAtt(ncid,'','_FillValue',fillval);
ncputAtt(ncid,'','missing_value',missval);
ncdefDim(ncid,'lon',720)
ncdefDim(ncid,'lat',360)
ncdefDim(ncid,'tim',nt)
if nd ==4
ncdefDim(ncid,'dep',50)
end
myncdefVar(ncid,'lon','double',{'lon'});
ncputAtt(ncid,'lon','long_name','longitude');
ncputAtt(ncid,'lon','units','degrees_east');
myncdefVar(ncid,'lat','double',{'lat'});
ncputAtt(ncid,'lat','long_name','latitude');
ncputAtt(ncid,'lat','units','degrees_north');
myncdefVar(ncid,'tim','double',{'tim'});
ncputAtt(ncid,'tim','long_name','time');
ncputAtt(ncid,'tim','units','Days since January 0, 0000');
if nd == 4
myncdefVar(ncid,'dep','double',{'dep'});
ncputAtt(ncid,'dep','long_name','depth');
ncputAtt(ncid,'dep','units','m');
myncdefVar(ncid,'thic','double',{'dep'});
ncputAtt(ncid,'thic','long_name','cell thickness');
ncputAtt(ncid,'thic','units','m');
end
ncclose(ncid);
ncid=ncopen(fout,'write');
myncputvar(ncid,'lon',lon)
myncputvar(ncid,'lat',lat)
myncputvar(ncid,'tim',time)
if nd == 4
myncputvar(ncid,'dep',dep)
myncputvar(ncid,'dep',dz)
end
display('Writing time variables')
fieldname = 'tim_lag';
units = 'Lag in Days';
longname = 'time_lag';
coords = {'tim'};
netcdf.reDef(ncid);
myncdefVar(ncid,fieldname,'double',coords);
ncputAtt(ncid,fieldname,'long_name',longname);
ncputAtt(ncid,fieldname,'units',units);
netcdf.endDef(ncid);
myncputvar(ncid,fieldname,time_lag)
fieldname = 'date0';
longname = 'Start of Simulation';
units = 'Days since January 0, 0000';
coords = [];
netcdf.reDef(ncid);
myncdefVar(ncid,fieldname,'double',coords);
ncputAtt(ncid,fieldname,'long_name',longname);
ncputAtt(ncid,fieldname,'units',units);
netcdf.endDef(ncid);
myncputvar(ncid,fieldname,date0_num)
fieldname = 'date_lag0';
longname = 'Start of Objective Function : Lag 0';
units = 'Days since January 0, 0000';
coords = [];
netcdf.reDef(ncid);
myncdefVar(ncid,fieldname,'double',coords);
ncputAtt(ncid,fieldname,'long_name',longname);
ncputAtt(ncid,fieldname,'units',units);
netcdf.endDef(ncid);
myncputvar(ncid,fieldname,date_lag0)
display('Writing main variable')
fieldname=var{v};
longname=['Sensitivity of Objective Function to ' longn(fieldname)];
units = [unit_of ' per ' unit_v(fieldname)];
netcdf.reDef(ncid);
if nd == 3
coords={'lon','lat','tim'};
else
coords={'lon','lat','dep','tim'};
end
myncdefVar(ncid,fieldname,'double',coords);
ncputAtt(ncid,fieldname,'long_name',longname);
ncputAtt(ncid,fieldname,'units',units);
netcdf.endDef(ncid);
vv = netcdf.inqVarID(ncid,fieldname);
for t = 1:nt
if nd == 3
var_t1 = convert2vector(squeeze(varin(:,:,t)));
else
var_t1 = convert2vector(squeeze(varin(:,:,:,t)));
end
var_t0 = 1*~isnan(var_t1);
var_t1=interp.SPM*var_t1;
var_t0=interp.SPM*var_t0;
if nd == 3
varout = reshape(var_t1./var_t0,[720 360]);
varout(isnan(varout))=0;
netcdf.putVar(ncid,vv,[0 0 t-1],[720 360 1],varout);
else
varout = reshape(var_t1./var_t0,[720 360 50]);
varout(isnan(varout))=0;
netcdf.putVar(ncid,vv,[0 0 0 t-1],[720 360 50 1],varout);
end
end
ncclose(ncid);
end
end