Skip to content

Commit 23eaf79

Browse files
committed
Sample analysis code
1 parent 5eacb77 commit 23eaf79

9 files changed

+705
-0
lines changed

sample_analysis/example_IO.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function []=example_IO();
2+
%
3+
% EXAMPLE_IO computes and displays a standard deviation field
4+
%
5+
% stand-alone call: addpath gcmfaces/sample_analysis/; example_IO;
6+
%
7+
% needed input files:
8+
% mkdir release1
9+
% wget --recursive ftp://mit.ecco-group.org/ecco_for_las/version_4/release1/nctiles_climatology/ETAN
10+
% mv mit.ecco-group.org/ecco_for_las/version_4/release1/nctiles_climatology release1/.
11+
12+
gcmfaces_global;
13+
14+
input_list_check('example_IO',nargin);
15+
16+
%expected location:
17+
myenv.nctilesdir=fullfile(pwd,'/release1/nctiles_climatology/ETAN/');
18+
%if ETAN is not found then try old location:
19+
if ~isdir(myenv.nctilesdir);
20+
%if not found then try old location:
21+
tmpdir=fullfile(pwd,'/gcmfaces/sample_input/nctiles_climatology/ETAN/');
22+
if isdir(tmpdir); myenv.nctilesdir=tmpdir; end;
23+
end;
24+
%if ETAN is still not found then issue warning and skip example_IO
25+
if ~isdir(myenv.nctilesdir);
26+
help example_IO;
27+
warning('example_IO requires release1/nctiles_climatology/ETAN that was not found ---> abort!');
28+
return;
29+
end;
30+
31+
%%%%%%%%%%%%%%%%%
32+
%load grid:
33+
%%%%%%%%%%%%%%%%%
34+
35+
if isempty(mygrid);
36+
grid_load;
37+
end;
38+
39+
%%%%%%%%%%%
40+
%get field:
41+
%%%%%%%%%%%
42+
fld=read_nctiles([myenv.nctilesdir 'ETAN'],'ETAN');
43+
fld=std(fld,[],3); fld(find(fld==0))=NaN;
44+
cc=[0:0.1:1]*0.10;
45+
46+
%%%%%%%%%%%%
47+
%plot field:
48+
%%%%%%%%%%%%
49+
if ~myenv.lessplot;
50+
figureL; gcmfaces_sphere(fld,cc,[],[],1);
51+
end;
52+
53+

sample_analysis/example_budget.m

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
function []=example_budget();
2+
%EXAMPLE_TRANSPORTS illustrates ECCO v4 budgets
3+
%
4+
%stand-alone call: addpath gcmfaces/sample_analysis/; example_budget;
5+
%
6+
%needed input files:
7+
% wget --recursive ftp://mit.ecco-group.org/gforget/nctiles_budget_2d
8+
% mv mit.ecco-group.org/gforget/nctiles_budget_2d sample_input/.
9+
% rm -rf mit.ecco-group.org
10+
%
11+
%note: in the nctiles_budget_2d files from 03-Feb-2015 the sign convention
12+
% for trWtop and trWbot was positive downward; this sign convention was
13+
% reversed in later versions for consistency with MITgcm convention.
14+
15+
gcmfaces_global;
16+
17+
if myenv.verbose>0;
18+
gcmfaces_msg('===============================================');
19+
gcmfaces_msg(['*** entering example_budget ' ...
20+
'load budget terms from nctiles files ' ...
21+
'and compute hemispheric budgets'],'');
22+
end;
23+
24+
%%%%%%%%%%%%%%%%%
25+
%load grid:
26+
%%%%%%%%%%%%%%%%%
27+
28+
%expected location:
29+
myenv.nctilesdir=fullfile('sample_input',filesep,'nctiles_budget_2d',filesep);
30+
%if nctiles_budget_2d is not found then try old location:
31+
if ~isdir(myenv.nctilesdir);
32+
%if not found then try old location:
33+
tmpdir=fullfile(myenv.gcmfaces_dir,'/sample_input/nctiles_budget_2d/');
34+
if isdir(tmpdir); myenv.nctilesdir=tmpdir; end;
35+
end;
36+
%if nctiles_budget_2d is still not found then issue warning and skip example_budget
37+
if ~isdir(myenv.nctilesdir);
38+
diags=[];
39+
help example_budget;
40+
warning(['skipping example_budget (missing ' myenv.nctilesdir ')']);
41+
return;
42+
end;
43+
44+
dirName=fullfile(myenv.nctilesdir,filesep,'budgMo',filesep);
45+
46+
if ~isdir(dirName);
47+
help gcmfaces_demo;
48+
warning(['skipping example_budget (missing ' dirName ')']);
49+
return;
50+
end;
51+
52+
if isempty(which('ncload'));
53+
warning(['skipping example_budget (missing ncload that is part of MITprof)']);
54+
return;
55+
end;
56+
57+
%%%%%%%%%%%%%%%%%%
58+
%main computation:
59+
%%%%%%%%%%%%%%%%%%
60+
61+
%load grid:
62+
if isempty(mygrid);
63+
grid_load;
64+
end;
65+
66+
%select budget of interest
67+
nameBudg='budgMo';
68+
69+
%load budget terms
70+
fileName=[myenv.nctilesdir nameBudg filesep nameBudg(1:6)];
71+
tend=read_nctiles(fileName,'tend');
72+
trU=read_nctiles(fileName,'trU');
73+
trV=read_nctiles(fileName,'trV');
74+
trWtop=read_nctiles(fileName,'trWtop');
75+
trWbot=read_nctiles(fileName,'trWbot');
76+
77+
%load dt (time increments) vector (not a gcmfaces object)
78+
ncload([fileName '.0001.nc'],'dt');
79+
80+
%get budget descitption and units
81+
ncid = netcdf.open([fileName '.0001.nc'],'NC_NOWRITE');
82+
descr = netcdf.getAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'description');
83+
varid = netcdf.inqVarID(ncid,'tend');
84+
units = netcdf.getAtt(ncid,varid,'units');
85+
netcdf.close(ncid);
86+
87+
if myenv.verbose>0; gcmfaces_msg(['* display Northern Hemisphere budget for ' nameBudg ' (' descr ')']);end;
88+
89+
%define northern hemisphere as domain of integration
90+
nameMask='Northern Hemisphere';
91+
mask=mygrid.mskC(:,:,1).*(mygrid.YC>0);
92+
areaMask=mygrid.RAC.*mask;
93+
94+
%edit plot title accordingly
95+
tmp1=strfind(descr,'-- ECCO v4');
96+
descr=[descr(1:tmp1-1) 'for: ' nameMask];
97+
98+
%compute northern hemisphere integrals
99+
budg.tend=NaN*dt;
100+
budg.hconv=NaN*dt;
101+
budg.zconv=NaN*dt;
102+
for tt=1:length(dt);
103+
%compute flux convergence
104+
hconv=calc_UV_conv(trU(:,:,tt),trV(:,:,tt));
105+
zconv=trWtop(:,:,tt)-trWbot(:,:,tt);
106+
%compute sum over domain
107+
budg.tend(tt)=nansum(tend(:,:,tt).*mask)/nansum(areaMask);
108+
budg.hconv(tt)=nansum(hconv.*mask)/nansum(areaMask);
109+
budg.zconv(tt)=nansum(zconv.*mask)/nansum(areaMask);
110+
end;
111+
112+
%display result
113+
figureL;
114+
subplot(3,1,1); set(gca,'FontSize',12);
115+
plot(cumsum(dt.*budg.tend));
116+
grid on; xlabel('month'); ylabel([units ' x s']);
117+
legend('content anomaly');
118+
subplot(3,1,2); set(gca,'FontSize',12);
119+
plot(cumsum(dt.*budg.tend)); hold on;
120+
plot(cumsum(dt.*budg.hconv),'r');
121+
plot(cumsum(dt.*budg.zconv),'g');
122+
grid on; xlabel('month'); ylabel([units ' x s']);
123+
legend('content anomaly','horizontal convergence','vertical convergence');
124+
subplot(3,1,3); set(gca,'FontSize',12);
125+
plot(cumsum(dt.*(budg.tend-budg.hconv-budg.zconv)));
126+
grid on; xlabel('month'); ylabel([units ' x s']);
127+
legend('budget residual');
128+
129+
if myenv.verbose>0;
130+
gcmfaces_msg('*** leaving example_budget ','');
131+
gcmfaces_msg('===============================================');
132+
end;
133+

sample_analysis/example_display.m

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
function []=example_display(method);
2+
% EXAMPLE_DISPLAY(method) illustrate various options to display 2D fields.
3+
%
4+
% method (optional) selects the plotting method
5+
% 1: plot a gcmfaces object face by face (as in Fig. C1) of doi:10.5194/gmd-8-3071-2015).
6+
% 2: use convert2array via qwckplot (a quick but crude display option).
7+
% 3: use convert2pcol then pcolor (for a display in lat-lon coordinates).
8+
% 4: use the m_map_gcmfaces front end to m_map (https://www.eoas.ubc.ca/~rich/map.html).
9+
% 5: use gcmfaces_sphere to display fields on a sphere.
10+
% 6: use stretched vertical coordinate.
11+
% If method is not specified then it is set to [3:5] by default.
12+
%
13+
% Example: addpath gcmfaces/sample_analysis/; example_display;
14+
15+
gcmfaces_global;
16+
17+
input_list_check('example_display',nargin);
18+
19+
if isempty(whos('method')); method=[3:5]; end;
20+
21+
if myenv.verbose>0;
22+
gcmfaces_msg('===============================================');
23+
gcmfaces_msg(['*** entering example_display: displays a gridded ' ...
24+
'field of ocean depth (gcmfaces object) in geographic coordinates ' ...
25+
'using pcolor, in various projections (if m_map is in Matlab path), ' ...
26+
'and on a sphere.'],'');
27+
end;
28+
29+
%%%%%%%%%%%%%%%%%
30+
%load grid:
31+
%%%%%%%%%%%%%%%%%
32+
33+
if isempty(mygrid);
34+
grid_load;
35+
end;
36+
nF=mygrid.nFaces;
37+
38+
%%%%%%%%%%%
39+
%get field:
40+
%%%%%%%%%%%
41+
42+
fld=mygrid.Depth; fld(fld==0)=NaN;
43+
cc=[[0:0.05:0.5] [0.6 0.75 1 1.25]]*1e4; myCmap='gray';
44+
45+
%%%%%%%%%%%%
46+
%plot field:
47+
%%%%%%%%%%%%
48+
49+
if sum(ismember(method,1));
50+
if myenv.verbose>0; gcmfaces_msg('* gcmfaces format display -- face by face.'); end;
51+
if nF==1;
52+
figureL; imagescnan(fld{1}','nancolor',[1 1 1]*0.8); axis xy; cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); delete(cb);
53+
elseif nF==5;
54+
figureL;
55+
subplot(3,3,7); imagescnan(fld{1}','nancolor',[1 1 1]*0.8); axis xy; cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); delete(cb);
56+
tmp1=axis; tmp2=text(tmp1(2)/2,tmp1(4)/2,'1','FontSize',32,'Color','r','Rotation',0);
57+
subplot(3,3,8); imagescnan(fld{2}','nancolor',[1 1 1]*0.8); axis xy; cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); delete(cb);
58+
tmp1=axis; tmp2=text(tmp1(2)/2,tmp1(4)/2,'2','FontSize',32,'Color','r','Rotation',0);
59+
subplot(3,3,5); imagescnan(fld{3}','nancolor',[1 1 1]*0.8); axis xy; cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); delete(cb);
60+
tmp1=axis; tmp2=text(tmp1(2)/2,tmp1(4)/2,'3','FontSize',32,'Color','r','Rotation',0);
61+
subplot(3,3,6); imagescnan(fld{4}','nancolor',[1 1 1]*0.8); axis xy; cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); delete(cb);
62+
tmp1=axis; tmp2=text(tmp1(2)/2,tmp1(4)/2,'4','FontSize',32,'Color','r','Rotation',0);
63+
subplot(3,3,3); imagescnan(fld{5}','nancolor',[1 1 1]*0.8); axis xy; cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); delete(cb);
64+
tmp1=axis; tmp2=text(tmp1(2)/2,tmp1(4)/2,'5','FontSize',32,'Color','r','Rotation',0);
65+
title('display face by face');
66+
elseif nF==6;
67+
error('face by face plot for cude : not yet implemented');
68+
end;
69+
end;
70+
71+
if sum(ismember(method,2));
72+
if myenv.verbose>0; gcmfaces_msg('* array format display -- all faces concatenated.'); end;
73+
figureL; qwckplot(fld); cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); %delete(cb);
74+
title('display using qwckplot');
75+
for ff=1:mygrid.nFaces;
76+
tmp0=0*mygrid.XC;
77+
tmp1=round(size(tmp0{ff})/2);
78+
tmp0{ff}(tmp1(1),tmp1(2))=1;
79+
tmp0=convert2array(tmp0); [ii,jj]=find(tmp0==1);
80+
if ff==3; ang=90; elseif ff>3; ang=-90; else; ang=0; end;
81+
hold on; text(ii,jj,num2str(ff),'FontSize',32,'Color','r','Rotation',ang);
82+
end;
83+
end;
84+
85+
if sum(ismember(method,3));
86+
if myenv.verbose>0; gcmfaces_msg('* geographical display -- using pcolor directly.'); end;
87+
figureL;
88+
[X,Y,FLD]=convert2pcol(mygrid.XC,mygrid.YC,fld); pcolor(X,Y,FLD);
89+
if ~isempty(find(X>359)); axis([0 360 -90 90]); else; axis([-180 180 -90 90]); end;
90+
shading flat; cb=gcmfaces_cmap_cbar(cc,{'myCmap',myCmap}); delete(cb);
91+
xlabel('longitude'); ylabel('latitude');
92+
title('display using convert2pcol');
93+
end;
94+
95+
if sum(ismember(method,4));
96+
if myenv.verbose>0; gcmfaces_msg('* geographical display -- using m_map_gcmfaces.'); end;
97+
if ~isempty(which('m_proj'));
98+
figureL; m_map_gcmfaces(fld,0,{'myCaxis',cc},{'myCmap',myCmap});
99+
aa=get(gcf,'Children'); axes(aa(4));
100+
title('display using m_map_gcmfaces','Interpreter','none');
101+
elseif myenv.verbose;
102+
fprintf(' > To use m_map_gcmfaces, please add m_map to your Matlab path\n');
103+
end;
104+
end;
105+
106+
if sum(ismember(method,5));
107+
if myenv.verbose>0; gcmfaces_msg('* geographical display -- on a sphere.'); end;
108+
figureL; gcmfaces_sphere(fld,cc,[],'N',3);
109+
title('display using gcmfaces_sphere','Interpreter','none');
110+
end;
111+
112+
%test case for depthStretchPlot:
113+
if sum(ismember(method,6));
114+
if myenv.verbose>0; gcmfaces_msg('* section display -- using strecthed vertical coord.'); end;
115+
x=ones(length(mygrid.RC),1)*[1:200]; z=mygrid.RC*ones(1,200); c=sin(z/2000*pi).*cos(x/50*pi);
116+
figureL;
117+
subplot(1,2,1); set(gca,'FontSize',16);
118+
pcolor(x,z,c); shading flat; title('standard depth display');
119+
subplot(1,2,2); set(gca,'FontSize',16);
120+
depthStretchPlot('pcolor',{x,z,c}); shading flat;
121+
title('stretched depth display');
122+
end;
123+
124+
if myenv.verbose>0;
125+
gcmfaces_msg('*** leaving example_display');
126+
gcmfaces_msg('===============================================');
127+
end;
128+

0 commit comments

Comments
 (0)