1 |
function [fld fc ix jx]=quikread_llc(fnam,nx,kx,prec,gdir,minlat,maxlat,minlon,maxlon); |
2 |
|
3 |
% Function [fld fc ix jx]=quikread_llc(fnam,nx,kx,prec, ... |
4 |
% gdir,minlat,maxlat,minlon,maxlon); |
5 |
% Read lat-lon-cap field |
6 |
% If there is less than 5 input arguments: read the complete field. |
7 |
% If there is more than 5 input arguments: read a region. |
8 |
% |
9 |
% INPUTS |
10 |
% fnam input path and file name |
11 |
% nx tile dimension (default 270) |
12 |
% kx vertical indices to read, e.g., 1:50 (default 1) |
13 |
% prec numeric precision (see fread; default 'real*4') |
14 |
% gdir directory path name that contains grid files XC.data and YC.data |
15 |
% minlat minimum latitude of region to extract |
16 |
% maxlat maximum latitude of region to extract |
17 |
% minlon minimum longitude of region to extract |
18 |
% maxlon maximum longitude of region to extract |
19 |
% (valid longitude range is -180 to 180 or 0 to 360) |
20 |
% |
21 |
% OUTPUTS |
22 |
% fld output array |
23 |
% fc faces that contain requested region |
24 |
% ix i-indices for requested region |
25 |
% jx j-indices for requested region |
26 |
% (note that if length(fc)>1, fld, ix, and jx are matlab |
27 |
% cell arrays that can be accessed as fld{fc(1)}, etc.) |
28 |
% |
29 |
% NOTES |
30 |
% For faces 4 and 5, southwest velocity is: |
31 |
% u_East (i,j) = v_Model(i,j) |
32 |
% v_North(i,j) = - u_Model(i,j-1) |
33 |
% |
34 |
% EXAMPLES |
35 |
% |
36 |
% % read and plot complete field |
37 |
% fld=quikread_llc('Depth.data',270); |
38 |
% quikplot_llc(fld) |
39 |
% |
40 |
% % read and plot the region 120W to 40W and 80S and 60N |
41 |
% fld=quikread_llc('Depth.data',270,1,'real*4','',-80,60,-120,-40); |
42 |
% quikpcolor(fld') |
43 |
% |
44 |
% SEE ALSO |
45 |
% read_llc_fkij quilplot_llc quikpcolor |
46 |
|
47 |
if nargin < 9, maxlon=180; end |
48 |
if nargin < 8, minlon=-180; end |
49 |
if nargin < 7, maxlat=90; end |
50 |
if nargin < 6, minlat=-90; end |
51 |
if nargin < 5, gdir=''; end |
52 |
if nargin < 4, prec='real*4'; end |
53 |
if nargin < 3, kx=1; end |
54 |
if nargin < 2, nx=270; end |
55 |
if nargin < 1, error('please specify input file name'); end |
56 |
|
57 |
switch prec |
58 |
case {'integer*1'} |
59 |
prec='int8'; |
60 |
case {'integer*2'} |
61 |
prec='int16'; |
62 |
case {'integer*4'} |
63 |
prec='int32'; |
64 |
case {'real*4','float32'} |
65 |
prec='single'; |
66 |
case {'integer*8'} |
67 |
prec='int64'; |
68 |
case {'real*8','float64'} |
69 |
prec='double'; |
70 |
end |
71 |
|
72 |
switch prec |
73 |
case {'int8'} |
74 |
preclength=1; |
75 |
case {'int16','uint16'} |
76 |
preclength=2; |
77 |
case {'int32','uint32','single'} |
78 |
preclength=4; |
79 |
case {'int64','uint64','double'} |
80 |
preclength=8; |
81 |
end |
82 |
|
83 |
if nargin < 6 |
84 |
fld=zeros(nx,nx*13,length(kx),prec); |
85 |
fid=fopen(fnam,'r','ieee-be'); |
86 |
for k=1:length(kx) |
87 |
if kx(k) > 1 |
88 |
skip=(kx(k)-1)*nx*nx*13; |
89 |
if(fseek(fid,skip*preclength,'bof')<0) |
90 |
error('past end of file'); |
91 |
end |
92 |
end |
93 |
fld(:,:,k)=reshape(fread(fid,nx*nx*13,prec),nx,nx*13); |
94 |
end |
95 |
fid=fclose(fid); |
96 |
else |
97 |
if minlon < -180 |
98 |
error('minlon<-180 not yet implemented: please email menemenlis@jpl.nasa.gov') |
99 |
end |
100 |
if maxlon > 360 |
101 |
error('maxlon>360 not yet implemented: please email menemenlis@jpl.nasa.gov') |
102 |
end |
103 |
if minlat >= maxlat |
104 |
error('maxlat must be greater than minlat') |
105 |
end |
106 |
if minlon >= maxlon |
107 |
error('maxlon must be greater than minlon') |
108 |
end |
109 |
fld=[]; |
110 |
fc =[]; |
111 |
ix =[]; |
112 |
jx =[]; |
113 |
for f=1:5 |
114 |
yc=read_llc_fkij([gdir 'YC.data'],nx,f); |
115 |
xc=read_llc_fkij([gdir 'XC.data'],nx,f); |
116 |
if maxlon>180 |
117 |
in=find(xc<0); |
118 |
xc(in)=xc(in)+360; |
119 |
end |
120 |
[i j]=find(yc>=minlat&yc<=maxlat&xc>=minlon&xc<=maxlon); |
121 |
if ~isempty(i) |
122 |
fc=[fc f]; |
123 |
ix{f}=min(i):max(i); |
124 |
jx{f}=min(j):max(j); |
125 |
fld{f}=zeros(length(ix{f}),length(jx{f}),length(kx),prec); |
126 |
for k=1:length(kx) |
127 |
fld{f}(:,:,k)=read_llc_fkij(fnam,nx,f,kx(k),ix{f},jx{f},prec); |
128 |
end |
129 |
end |
130 |
end |
131 |
if length(fc) == 1 |
132 |
fld=fld{fc}; |
133 |
ix = ix{fc}; |
134 |
jx = jx{fc}; |
135 |
end |
136 |
end |