1 |
gforget |
1.13 |
function []=grid_load(dirGrid,nFaces,fileFormat,memoryLimit,omitNativeGrid); |
2 |
gforget |
1.5 |
%object: load grid information, convert it to gcmfaces format |
3 |
|
|
% and encapsulate it in the global mygrid structure. |
4 |
|
|
%inputs: dirGrid is the directory where the grid files (gcm output) can be found. |
5 |
|
|
% nFaces is the number of faces in this gcm set-up of current interest. |
6 |
|
|
% fileFormat is the file format ('straight','cube','compact') |
7 |
gforget |
1.9 |
%optional: memoryLimit is a flag that allows the user to omit secondary |
8 |
|
|
% grid fields in case memory/storage become an issue. It |
9 |
|
|
% takes 3 values : (0; the default) includes everything |
10 |
|
|
% (1) omits all 3D fields but hFacC (2) only loads XC & YC. |
11 |
gforget |
1.13 |
% omitNativeGrid is a flag (0 by default) to bypass (when flag 1) |
12 |
|
|
% grid_load_native and grid_load_native_RAZ calls (that can |
13 |
|
|
% complement mygrid based upon e.g. tile*.mitgrid files) |
14 |
gforget |
1.1 |
|
15 |
gforget |
1.9 |
if isempty(whos('memoryLimit')); memoryLimit=0; end; |
16 |
gforget |
1.13 |
if isempty(whos('omitNativeGrid')); omitNativeGrid=0; end; |
17 |
gforget |
1.9 |
|
18 |
gforget |
1.7 |
gcmfaces_global; mygrid=[]; |
19 |
|
|
|
20 |
gforget |
1.5 |
mygrid.dirGrid=dirGrid; |
21 |
|
|
mygrid.nFaces=nFaces; |
22 |
|
|
mygrid.fileFormat=fileFormat; |
23 |
gforget |
1.6 |
mygrid.gcm2facesFast=false; |
24 |
gforget |
1.9 |
mygrid.memoryLimit=memoryLimit; |
25 |
|
|
|
26 |
|
|
if mygrid.memoryLimit>0; |
27 |
|
|
gcmfaces_msg(['* Warning from grid_load : memoryLimit>0 ' ... |
28 |
|
|
'may precludes advanced gmcfaces functions.'],''); |
29 |
|
|
end; |
30 |
|
|
if mygrid.memoryLimit>1; |
31 |
|
|
gcmfaces_msg(['* Warning from grid_load : memoryLimit>1 ' ... |
32 |
|
|
'may only allow for basic fields displays.'],''); |
33 |
|
|
end; |
34 |
|
|
|
35 |
|
|
if ~isempty(dir([dirGrid 'grid.specs.mat'])); |
36 |
|
|
specs=open([dirGrid 'grid.specs.mat']); |
37 |
|
|
mygrid.ioSize=specs.ioSize; |
38 |
|
|
mygrid.facesSize=specs.facesSize; |
39 |
|
|
mygrid.facesExpand=specs.facesExpand; |
40 |
|
|
%example for creating grid.specs.mat, to put in dirGrid : |
41 |
|
|
%ioSize=[364500 1]; |
42 |
|
|
%facesSize=[[270 450];[0 0];[270 270];[180 270];[450 270]]; |
43 |
|
|
%facesExpand=[270 450]; |
44 |
|
|
%save grid.specs.mat ioSize facesSize facesExpand; |
45 |
gforget |
1.10 |
elseif strcmp(fileFormat,'compact'); |
46 |
gforget |
1.9 |
v0=rdmds([dirGrid 'XC']); |
47 |
|
|
mygrid.ioSize=size(v0); |
48 |
|
|
nn=size(v0,1); pp=size(v0,2)/nn; |
49 |
|
|
mm=(pp+4-mygrid.nFaces)/4*nn; |
50 |
gforget |
1.10 |
mygrid.facesSize=[[nn mm];[nn mm];[nn nn];[mm nn];[mm nn];[nn nn]]; |
51 |
|
|
mygrid.facesExpand=[]; |
52 |
|
|
elseif strcmp(fileFormat,'cube'); |
53 |
|
|
v0=rdmds([dirGrid 'XC']); |
54 |
|
|
mygrid.ioSize=size(v0); |
55 |
|
|
nn=size(v0,2); |
56 |
|
|
mygrid.facesSize=[[nn nn];[nn nn];[nn nn];[nn nn];[nn nn];[nn nn]]; |
57 |
|
|
mygrid.facesExpand=[]; |
58 |
|
|
elseif strcmp(fileFormat,'straight'); |
59 |
|
|
v0=rdmds([dirGrid 'XC']); |
60 |
|
|
mygrid.ioSize=size(v0); |
61 |
|
|
mygrid.facesSize=mygrid.ioSize; |
62 |
gforget |
1.9 |
mygrid.facesExpand=[]; |
63 |
|
|
end; |
64 |
gforget |
1.12 |
mygrid.missVal=NaN;%will be set to 0 once the grid has been loaded. |
65 |
gforget |
1.1 |
|
66 |
gforget |
1.5 |
if ~(nFaces==1&strcmp(fileFormat,'straight'))&... |
67 |
|
|
~(nFaces==6&strcmp(fileFormat,'cube'))&... |
68 |
|
|
~(nFaces==6&strcmp(fileFormat,'compact'))&... |
69 |
|
|
~(nFaces==5&strcmp(fileFormat,'compact')); |
70 |
gforget |
1.8 |
% if myenv.verbose; |
71 |
|
|
% fprintf('\nconvert2gcmfaces.m init: there are several supported file conventions. \n'); |
72 |
|
|
% fprintf(' By default gcmfaces assumes MITgcm type binary formats as follows: \n') |
73 |
|
|
% fprintf(' (1 face) straight global format; (4 or 5 faces) compact global format\n'); |
74 |
|
|
% fprintf(' (6 faces) cube format with one face after the other. \n'); |
75 |
|
|
% fprintf(' If this is inadequate, you can change the format below.\n\n'); |
76 |
|
|
% end; |
77 |
gforget |
1.5 |
error('non-tested topology/fileFormat'); |
78 |
gforget |
1.1 |
end; |
79 |
|
|
|
80 |
gforget |
1.6 |
%the various grid fields: |
81 |
gforget |
1.9 |
if mygrid.memoryLimit==0; |
82 |
|
|
list0={'XC','XG','YC','YG','RAC','RAZ','DXC','DXG','DYC','DYG','hFacC','hFacS','hFacW','Depth'}; |
83 |
|
|
elseif mygrid.memoryLimit==1; |
84 |
|
|
list0={'XC','XG','YC','YG','RAC','RAZ','DXC','DXG','DYC','DYG','hFacC','Depth'}; |
85 |
|
|
elseif mygrid.memoryLimit==2; |
86 |
|
|
list0={'XC','YC'}; |
87 |
|
|
end; |
88 |
|
|
|
89 |
gforget |
1.1 |
for iFld=1:length(list0); |
90 |
gforget |
1.8 |
eval(['mygrid.' list0{iFld} '=rdmds2gcmfaces([dirGrid ''' list0{iFld} '*'']);']); |
91 |
gforget |
1.1 |
end; |
92 |
|
|
|
93 |
gforget |
1.9 |
%the vertical grid |
94 |
|
|
list0={'RC','RF','DRC','DRF'}; |
95 |
|
|
for iFld=1:length(list0); |
96 |
|
|
eval(['mygrid.' list0{iFld} '=squeeze(rdmds([dirGrid ''' list0{iFld} '*'']));']); |
97 |
|
|
end; |
98 |
|
|
|
99 |
|
|
%grid orientation |
100 |
|
|
if mygrid.memoryLimit<2; |
101 |
|
|
list0={'AngleCS','AngleSN'}; |
102 |
|
|
test0=~isempty(dir([dirGrid 'AngleCS*'])); |
103 |
|
|
if test0; |
104 |
|
|
for iFld=1:length(list0); |
105 |
|
|
eval(['mygrid.' list0{iFld} '=rdmds2gcmfaces([dirGrid ''' list0{iFld} '*'']);']); |
106 |
|
|
end; |
107 |
|
|
else; |
108 |
|
|
warning('\n AngleCS/AngleSN not found; set to 1/0 assuming lat/lon grid.\n'); |
109 |
|
|
mygrid.AngleCS=mygrid.XC; mygrid.AngleCS(:)=1; |
110 |
|
|
mygrid.AngleSN=mygrid.XC; mygrid.AngleSN(:)=0; |
111 |
gforget |
1.8 |
end; |
112 |
gforget |
1.4 |
end; |
113 |
|
|
|
114 |
gforget |
1.12 |
%if grid is incomplete (blank tiles) then try to get |
115 |
|
|
%additional info from native grid, or apply missing value. |
116 |
|
|
test0=sum(isnan(mygrid.XC))>0; |
117 |
|
|
test1=prod(mygrid.ioSize)~=sum(isnan(NaN*mygrid.XC(:))); |
118 |
gforget |
1.13 |
if (test0|test1)&~omitNativeGrid; |
119 |
gforget |
1.12 |
%treat fields that are part of the native grid |
120 |
|
|
mygrid1=mygrid; mygrid=[]; |
121 |
|
|
grid_load_native(dirGrid,nFaces,0); |
122 |
|
|
mygrid1.XC=mygrid.XC; mygrid1.YC=mygrid.YC; |
123 |
|
|
mygrid1.XC=mygrid.XG; mygrid1.YG=mygrid.YG; |
124 |
|
|
mygrid1.RAC=mygrid.RAC; |
125 |
|
|
mygrid=mygrid1; |
126 |
|
|
%apply missing value for fields that aren't |
127 |
|
|
list0={'hFacC','hFacS','hFacW','Depth','AngleCS','AngleSN'}; |
128 |
|
|
for ii=1:length(list0); |
129 |
|
|
eval(['tmp1=mygrid.' list0{ii} ';']); |
130 |
|
|
tmp1(isnan(tmp1))=0; |
131 |
|
|
eval(['mygrid.' list0{ii} '=tmp1;']); |
132 |
|
|
end; |
133 |
|
|
%and fix angles if needed |
134 |
|
|
tmp1=mygrid.AngleCS.^2+mygrid.AngleSN.^2; |
135 |
|
|
tmp1=1*(tmp1>0.999&tmp1<1.001); |
136 |
|
|
mygrid.AngleCS(tmp1==0)=1; |
137 |
|
|
mygrid.AngleSN(tmp1==0)=0; |
138 |
|
|
end; |
139 |
|
|
|
140 |
gforget |
1.11 |
%get full RAZ (incl. 'extra line and column') needed for e.g. rotational computations |
141 |
gforget |
1.13 |
if mygrid.memoryLimit<2&~omitNativeGrid; |
142 |
gforget |
1.11 |
grid_load_native_RAZ; |
143 |
|
|
end; |
144 |
|
|
|
145 |
gforget |
1.9 |
%grid masks |
146 |
|
|
if mygrid.memoryLimit<1; |
147 |
|
|
mygrid.hFacCsurf=mygrid.hFacC; |
148 |
|
|
for ff=1:mygrid.hFacC.nFaces; mygrid.hFacCsurf{ff}=mygrid.hFacC{ff}(:,:,1); end; |
149 |
|
|
|
150 |
|
|
mskC=mygrid.hFacC; mskC(mskC==0)=NaN; mskC(mskC>0)=1; mygrid.mskC=mskC; |
151 |
|
|
mskW=mygrid.hFacW; mskW(mskW==0)=NaN; mskW(mskW>0)=1; mygrid.mskW=mskW; |
152 |
|
|
mskS=mygrid.hFacS; mskS(mskS==0)=NaN; mskS(mskS>0)=1; mygrid.mskS=mskS; |
153 |
gforget |
1.1 |
end; |
154 |
|
|
|
155 |
gforget |
1.9 |
%zonal mean and sections needed for transport computations |
156 |
|
|
if mygrid.memoryLimit<1; |
157 |
|
|
if ~isfield(mygrid,'mygrid.LATS_MASKS'); |
158 |
|
|
gcmfaces_lines_zonal; |
159 |
|
|
mygrid.LATS=[mygrid.LATS_MASKS.lat]'; |
160 |
|
|
end; |
161 |
|
|
if ~isfield(mygrid,'LINES_MASKS'); |
162 |
|
|
[lonPairs,latPairs,names]=line_greatC_TUV_MASKS_v4; |
163 |
|
|
gcmfaces_lines_transp(lonPairs,latPairs,names); |
164 |
|
|
end; |
165 |
|
|
end; |
166 |
gforget |
1.2 |
|
167 |
gforget |
1.8 |
%to allow convert2gcmfaces/doFast: |
168 |
gforget |
1.9 |
if isempty(mygrid.facesExpand)&mygrid.memoryLimit<2; |
169 |
|
|
tmp1=convert2gcmfaces(mygrid.XC); |
170 |
|
|
tmp1(:)=[1:length(tmp1(:))]; |
171 |
|
|
nn=length(tmp1(:)); |
172 |
|
|
mygrid.gcm2faces=convert2gcmfaces(tmp1); |
173 |
|
|
mygrid.faces2gcmSize=size(tmp1); |
174 |
|
|
mygrid.faces2gcm=convert2gcmfaces(tmp1); |
175 |
|
|
for iFace=1:mygrid.nFaces; |
176 |
|
|
n=length(mygrid.gcm2faces{iFace}(:)); |
177 |
|
|
mygrid.faces2gcm{iFace}=mygrid.gcm2faces{iFace}(:); |
178 |
|
|
mygrid.gcm2faces{iFace}=sparse([1:n],mygrid.gcm2faces{iFace}(:),ones(1,n),n,nn); |
179 |
|
|
end; |
180 |
|
|
mygrid.gcm2facesFast=true; |
181 |
gforget |
1.6 |
end; |
182 |
|
|
|
183 |
gforget |
1.12 |
%reset missVal parameter to 0. |
184 |
|
|
%Note : this is only used by convert2widefaces, for runs with cropped grids. |
185 |
|
|
%Note : 0 should not be used as a fill for the grid itself (NaN was used). |
186 |
|
|
mygrid.missVal=0; |
187 |
gforget |
1.1 |
|