/[MITgcm]/MITgcm_contrib/gael/matlab_class/ecco_v4/gcmfaces_remap.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/ecco_v4/gcmfaces_remap.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (hide annotations) (download)
Wed Jul 13 18:12:23 2011 UTC (14 years ago) by gforget
Branch: MAIN
- introducing new routine that uses gcmfaces_bindata to
remap a lat-lon grid product to a gcmfaces grid.

1 gforget 1.1 function []=gcmfaces_remap(dirIn,fileIn,dirOut,fileOut);
2     %object: use bin average to remap a lat-lon grid product to a gcmfaces grid
3     %inputs: dirIn is the input directory
4     % fileIn is the input file name without the final four year characters
5     % (e.g. 'SST_monthly_r2_' to process 'SST_monthly_r2_1992' etc.)
6     % dirOut and filOut are the corresponding output names
7     %
8     %assumption: mygrid has been read using grid_load
9    
10     global mygrid mytri;
11     %create triangulation
12     gcmfaces_bindata;
13     %list files to be processed
14     listFiles=dir([dirIn fileIn '*']);
15     %related grid information (lon must be 0-360 for gcmfaces_remap_one)
16     if 0;
17     x=[0.5:359.5]'*ones(1,180);
18     y=ones(360,1)*[-89.5:89.5];
19     [nx,ny]=size(x);
20     mis=0;
21     else;
22     x=[0.5:359.5]'*ones(1,160);
23     y=ones(360,1)*[-79.5:79.5];
24     [nx,ny]=size(x);
25     mis=0;
26     end;
27    
28     for ii=1:length(listFiles);
29     yy=listFiles(ii).name(end-3:end); fprintf(['processing ' fileIn ' for year ' yy '\n']);
30     nt=listFiles(ii).bytes/nx/ny/4; if round(nt)~=nt; error('inconsistent sizes'); end;
31     %read data
32     fld=reshape(read2memory([dirIn listFiles(ii).name],[nx*ny*nt 1]),[nx ny nt]);
33     %mask land
34     fld(fld==mis)=NaN;
35     %map to v4 grid
36     FLD=convert2array(zeros(360,360,nt));
37     for tt=1:1; FLD(:,:,tt)=gcmfaces_remap_one(x,y,fld(:,:,tt),3); end;
38     %set missing value
39     FLD(find(isnan(FLD)))=mis;
40     %write data
41     write2file([dirOut fileOut yy],convert2gcmfaces(FLD));
42     end;
43    
44     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45    
46     function [fld]=gcmfaces_remap_one(lon,lat,fld,nDblRes,varargin);
47     %object: use bin average to remap ONE lat-lon grid FIELD to a gcmfaces grid
48     %input: lon,lat,fld are the gridded product arrays
49     % nDblRes is the number of times the input field
50     % resolution should be doubled before bin average.
51     %optional: mskExtrap is the mask to which to extrapolate after bin average
52     % If it is not specified we do no extrapolation
53     %notes: nDblRes>0 is useful if the starting resolution is coarse
54     % enough that the bin average would leave many empry points.
55     % fld should show NaN for missing values
56    
57     global mygrid;
58     if nargin>4; mskExtrap=varargin{1}; else; mskExtrap=[]; end;
59    
60     %refine grid before bin average:
61     for ii=1:nDblRes;
62     [lon,lat,fld]=dbl_res(lon,lat,fld,ii);
63     end;
64     %put in vector form:
65     tmp1=find(~isnan(fld));
66     lon=lon(tmp1); lat=lat(tmp1); fld=fld(tmp1);
67     %switch longitude range to -180+180
68     lon(find(lon>180))=lon(find(lon>180))-360;
69     %compute bin average:
70     fld=gcmfaces_bindata(lon,lat,fld);
71     %potential extrapolation:
72     if ~isempty(mskExtrap); fld=diffsmooth2D_extrap_inv(fld,mskExtrap); end;
73     %apply surface land mask:
74     fld=fld.*mygrid.mskC(:,:,1);
75    
76     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77    
78     function [lonOut,latOut,obsOut]=dbl_res(lon,lat,obs,extendNaN);
79     %object: use neighbor average to double the fields resolution
80     %inputs: lon,lat,fld are the gridded product arrays, with
81     % NaN for missing values in fld.
82     % extendNaN states whether between a real and a NaN
83     % we add a NaN (extendNaN==1) or a real (extendNaN~=1)
84     %outputs: lonOut,latOut,fldOut are the x2 resolution arrays
85     %
86     %assumptions: 0-360 longitudes and a consistent ordering of arrays
87    
88     %check longitude range:
89     if ~isempty(find(lon(:)<0))|~isempty(find(lon(:)>360)); fprintf('problem in longitude specs\n'); return; end;
90     %check simple ordering:
91     tmp1=diff(lon(:,1)); if ~isempty(find(tmp1<=0)); fprintf('problem in longitude specs\n'); return; end;
92    
93     %make sure that we are >=0 and <360:
94     if lon(1,1)>0&lon(end,1)==360; lon=[lon(end,:)-360;lon(1:end-1,:)];
95     lat=[lat(end,:);lat(1:end-1,:)]; obs=[obs(end,:);obs(1:end-1,:)]; end;
96     %make sure that the last point is not duplicated:
97     if lon(1,1)==0&lon(end,1)==360; lon=lon(1:end-1,:);
98     lat=lat(1:end-1,:); obs=obs(1:end-1,:); end;
99    
100     %what do we do with last longitude points:
101     if lon(1,1)>0&lon(end,1)<360;
102     addOnePt=1; %add point at the beginning
103     else;
104     addOnePt=2; %add point at the end
105     end;
106    
107     for ii=1:3;
108     %0) get field
109     if ii==1; tmpA=lon;
110     elseif ii==2; tmpA=lat;
111     elseif ii==3; tmpA=obs;
112     end;
113    
114     %1) zonal direction:
115     tmpB=nanmean2flds(tmpA(1:end-1,:),tmpA(2:end,:),extendNaN);
116     tmpC=ones(size(tmpA,1)*2-1,size(tmpA,2));
117     tmpC(1:2:end,:)=tmpA;
118     tmpC(2:2:end-1,:)=tmpB;
119     %treat zonal edge of the domain
120     if addOnePt==1; %add one point at the beginning
121     if ii==1; offset=-360; else; offset=0; end;
122     tmpB=nanmean2flds(tmpA(1,:),offset+tmpA(end,:),extendNaN); tmpC=[tmpB;tmpC];
123     end;
124     if addOnePt==2; %add one point at the end
125     if ii==1; offset=360; else; offset=0; end;
126     tmpB=nanmean2flds(offset+tmpA(1,:),tmpA(end,:),extendNaN); tmpC=[tmpC;tmpB];
127     end;
128     %check that we did not go too far (should not happen)
129     if ii==1; tmp1=~isempty(find(lon(:)<0))|~isempty(find(lon(:)>360));
130     if tmp1; fprintf('problem in longitude interp\n'); return; end;
131     end;
132    
133     %2) meridional direction:
134     tmpB=nanmean2flds(tmpC(:,1:end-1),tmpC(:,2:end),extendNaN);
135     tmpA=ones(size(tmpC,1),size(tmpC,2)*2-1);
136     tmpA(:,1:2:end)=tmpC;
137     tmpA(:,2:2:end-1)=tmpB;
138    
139     %3) store field:
140     if ii==1; lonOut=tmpA;
141     elseif ii==2; latOut=tmpA;
142     elseif ii==3; obsOut=tmpA;
143     end;
144     end;
145    
146     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147    
148     function [fld]=nanmean2flds(fld1,fld2,extendNaN);
149     %object: compute the average of two fields, accounting for NaNs
150     %inputs: fld1 and fld2 are the two fields
151     % if extendNaN==1 the result is NaN if either fld1 or fld2 is NaN.
152     % Otherwise the result is real if either fld1 or fld2 is real.
153    
154     if extendNaN==1;
155     fld=(fld1+fld2)/2;
156     else;
157     msk1=~isnan(fld1); fld1(isnan(fld1))=0;
158     msk2=~isnan(fld2); fld2(isnan(fld2))=0;
159     fld=fld1+fld2;
160     msk=msk1+msk2;
161     msk(msk==0)=NaN;
162     fld=fld./msk;
163     end;
164    

  ViewVC Help
Powered by ViewVC 1.1.22