-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_netcdf_biogem_3d.m
More file actions
217 lines (209 loc) · 7.85 KB
/
Copy pathread_netcdf_biogem_3d.m
File metadata and controls
217 lines (209 loc) · 7.85 KB
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
function [] = read_netcdf_biogem_3d(PEXP,PVAR,PT)
% read_netcdf_biogem_3d
%
% ***********************************************************************
% *** GENIE netCDF results processing ***********************************
% ***********************************************************************
%
% read_netcdf_biogem_3d(PEXP,PVAR,PT)
% processes BIOGEM 3-D netCDF data file 'fields_biogem_2d.nc' and takes 3 argument2:
%
% PEXP [STRING] (e.g. 'preindustrial_spinup')
% --> the experiment name
% PVAR [STRING] (e.g. 'ocn_PO4')
% --> id the name of 1st variable to be plotted
% --> all valid valiable names will be listed if an invalid name is given
% PT [REAL] (e.g. 1999.5)
% --> the (first) time-slice year
% --> all valid years will be listed if an invalid year is given
%
% Example
% read_netcdf_biogem_3d('genie_output','experiment','ocn_PO4',1765.5)
% will read in a GENIE netcdf field
%
% ******************************************************************* %
% ******************************************************************* %
% *** HISTORY ******************************************************* %
% ******************************************************************* %
%
% 13/10/15: created
% 14/01/06: fixed grids
% 14/11/28: split into 2d and 3d varients
% much further development
% added to svn
%
% ******************************************************************* %
% *********************************************************************** %
% *** INITIALIZE PARAMETERS & VARIABLES ********************************* %
% *********************************************************************** %
%
% close open windows
% NOTE: don't clear variable space here ...
close all;
% load plotting options
if isempty(POPT), POPT='plot_fields_settings'; end
eval(POPT);
% set passed parameters
expid = PEXP;
dataid = PVAR;
timesliceid = PT;
data_n_i = PI;
data_n_j = PJ;
data_n_k = PK; % NOTE: not used -- for completeness (consistency with read_netcdf_biogem_3d)
% set date
str_date = [datestr(date,11), datestr(date,5), datestr(date,7)];
% set function name
str_function = 'read_netcdf_biogem_2d';
% plot format
if ~isempty(plot_format), plot_format_old='n'; end
% plotting paths
addpath(library_path1);
if (plot_format_old == 'n'),
addpath(library_path2);
addpath(library_path3);
end
%
% *********************************************************************** %
% *********************************************************************** %
% *** OPEN netCDF FILE ************************************************** %
% *********************************************************************** %
%
% open netCDF file
ncid=netcdf.open([data_path '/' expid '/biogem/fields_biogem_3d.nc'],'nowrite');
% read netCDf information
[ndims,nvars,ngatts,unlimdimid] = netcdf.inq(ncid);
%
% *********************************************************************** %
% *********************************************************************** %
% *** SET UP GRID ******************************************************* %
% *********************************************************************** %
%
% load grid data
varid = netcdf.inqVarID(ncid,'grid_level');
grid_k1 = netcdf.getVar(ncid,varid);
% flip array around diagonal to give (j,i) array orientation
grid_k1 = grid_k1';
% determine grid axes dimensions
varid = netcdf.inqVarID(ncid,'lat');
[dimname, dimlen] = netcdf.inqDim(ncid,varid);
jmax = dimlen;
varid = netcdf.inqVarID(ncid,'lon');
[dimname, dimlen] = netcdf.inqDim(ncid,varid);
imax = dimlen;
varid = netcdf.inqVarID(ncid,'zt');
[dimname, dimlen] = netcdf.inqDim(ncid,varid);
kmax = dimlen;
% load grid axes
% NOTE: invert z-axis
varid = netcdf.inqVarID(ncid,'lat');
grid_lat = netcdf.getVar(ncid,varid);
varid = netcdf.inqVarID(ncid,'lon');
grid_lon = netcdf.getVar(ncid,varid);
varid = netcdf.inqVarID(ncid,'lat_edges');
grid_lat_edges = netcdf.getVar(ncid,varid);
varid = netcdf.inqVarID(ncid,'lon_edges');
grid_lon_edges = netcdf.getVar(ncid,varid);
varid = netcdf.inqVarID(ncid,'zt');
grid_zt = netcdf.getVar(ncid,varid);
grid_zt = flipud(grid_zt);
varid = netcdf.inqVarID(ncid,'zt_edges');
grid_zt_edges = netcdf.getVar(ncid,varid);
grid_zt_edges = flipud(grid_zt_edges);
% create 2D arrays of grid boundaries
[lats layb] = meshgrid(grid_lat_edges(1:jmax),-grid_zt_edges(1:kmax));
[latn layt] = meshgrid(grid_lat_edges(2:jmax+1),-grid_zt_edges(2:kmax+1));
[lonw lats] = meshgrid(grid_lon_edges(1:imax),grid_lat_edges(1:jmax));
[lone latn] = meshgrid(grid_lon_edges(2:imax+1),grid_lat_edges(2:jmax+1));
[latm laym] = meshgrid(grid_lat,-grid_zt);
%
grid_lon_origin = grid_lon_edges(1);
%
% *********************************************************************** %
% *********************************************************************** %
% *** LOAD DATASET ****************************************************** %
% *********************************************************************** %
%
% check that the year exists
varid = netcdf.inqVarID(ncid,'time');
timeslices = netcdf.getVar(ncid,varid);
[dimname, dimlen] = netcdf.inqDim(ncid,varid);
clear time;
while exist('time','var') == 0
for n = 1:dimlen,
if double(int32(100*timeslices(n)))/100 == timesliceid
time = timesliceid;
tid = n;
end
end
if exist('time','var') == 0
disp(' > WARNING: Year #1 must be one of the following;');
format long g;
double(int32(100*timeslices(:)))/100
format;
timesliceid = input(' > Time-slice year: ');
end
end
% check that the variable name exists
varid = [];
while isempty(varid)
for n = 0:nvars-1,
[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,n);
if strcmp(varname,dataid)
varid = n;
end
end
if isempty(varid)
disp(' > WARNING: Variable #1 name must be one of the following;');
for n = 0:nvars-1,
[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,n);
varname
end
dataid = input(' > Variable name: ','s');
end
end
% load data
% flip array around diagonal to give (j,i) array orientation
[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,varid);
rawdata = netcdf.getVar(ncid,varid);
if length(dimids) == 4
rawdata(1:imax,1:jmax,1:kmax) = rawdata(1:imax,1:jmax,1:kmax,tid);
for n = 1:kmax,
data(kmax - n + 1,1:jmax,1:imax) = rawdata(1:imax,1:jmax,n)';
end
elseif length(dimids) == 3
rawdata(1:imax,1:jmax,1:kmax) = rawdata(1:imax,1:jmax,1:kmax);
for n = 1:kmax,
data(kmax - n + 1,1:jmax,1:imax) = rawdata(1:imax,1:jmax,n)';
end
else
data(:,:,:) = NaN;
end
%
% *********************************************************************** %
% *********************************************************************** &
% *** PLOTTING ********************************************************** &
% *********************************************************************** &
%
% *** QUICK DIAGNOSTIC PLOT ********************************************* %
%
if (plot_secondary == 'y'),
% plot surface field
zm = [];
zm(:,:) = data(kmax,:,:);
% plot
plot_2dgridded(zm,0.9E19,'',[expid '.' dataid],strrep([dataid ' from: ' expid],'_',' '));
end
%
% *********************************************************************** %
% *********************************************************************** %
% *** END and OPEN netCDF FILE ****************************************** %
% *********************************************************************** %
%
% export data
data_ALL = zm;
data_i = 0.0;
data_j = 0.0;
% close netCDF file
netcdf.close(ncid);
%
% *********************************************************************** %