/[MITgcm]/MITgcm_contrib/gael/profilesMatlabProcessing/netcdf_ecco_GenericgridMatFirstStep_2points.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/profilesMatlabProcessing/netcdf_ecco_GenericgridMatFirstStep_2points.m

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


Revision 1.2 - (hide annotations) (download)
Thu May 13 19:42:33 2010 UTC (15 years, 2 months ago) by gforget
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +0 -0 lines
FILE REMOVED
moving to profiles_genericgrid directory

1 gforget 1.1 %function: netcdf_ecco_GenericgridMatFirstStep
2     %object: compute the additional information to use an "ECCO format" data set with a non latlon grid
3     %author: Gael Forget (gforget@mit.edu)
4     %date: June 12th, 2007
5     %
6     %inputs:
7     % - an "ECCO format" data set and grid files (profilesXCincl1PointOverlap* etc)
8     % that the model should generate when you first try to run pkg/profiles
9     % with a non latlon grid
10     % - directories for I/O
11     % - lat_polar (see below)
12     %output: mat files that are then fed to netcdf_ecco_GenericgridMat2Nc (step 2)
13     %
14     %lat_polar: positive value smaller than 90
15     % [in Cube Sphere/Polar Cap case]
16     % lat_polar should be set to the latitude beyond which lies the cube polar tiles (all of the tile grid points)
17     % -> there we start by project coordinates on the polar tangent plane because the lat/lon system collapes at the pole
18     % -> we refer to this operation as "polar switch" below
19     % [in other grids that do not include the poles]
20     % set it to 85
21     %
22     %rk: below you will find a parameter [choice_plot] do do some display
23     %
24     function []=netcdf_ecco_GenericgridMatFirstStep(file_data,rep_data,rep_grid,rep_out,lat_polar);
25    
26     warning off MATLAB:mir_warning_variable_used_as_function;
27    
28     fprintf('\n WARNING from netcdf_ecco_GenericgridMatFirstStep.m : \n');
29     fprintf('please have a look at netcdf_ecco_GenericgridNOTEONOVERLAPCORNERS \n\n');
30    
31    
32     %choice to do some visual chek or not
33     choice_plot=0; if choice_plot==1; figure; end; fprintf('visualization off \n\n');
34    
35    
36     %longitude systems: 1 <-> degree east from Greenwich [0 360] // 2 <-> degree east and west [-180 180]
37     lonSystModel=2;
38     if lonSystModel==1; fprintf('the script below assumes [-180 +180] longitudes => please do not change \n'); return; end
39     %-> both model and observations will first be switched to this system
40    
41    
42    
43    
44     eval(['ncload ' rep_data file_data ' prof_lon prof_lat;']);
45    
46     %set the longitude system for observations:
47     [prof_lon]=netcdf_ecco_GenericgridRotateLon(prof_lon,0,0,lonSystModel);
48     prof_lonM2=prof_lon; prof_latM2=prof_lat;
49    
50     list_subsets=[1:4];
51     %list_subsets=3;
52    
53     for subset_cur=list_subsets
54     switch subset_cur
55     case 1
56     switch_to_polar=0;
57     list_profilesM2=find((prof_lonM2>=-90&prof_lonM2<90)&abs(prof_latM2)<lat_polar);
58     suff_cur='latlon1'; fprintf('computing mid latitudes, part 1 \n');
59     case 2
60     switch_to_polar=0;
61     list_profilesM2=find((prof_lonM2<-90|prof_lonM2>=90)&abs(prof_latM2)<lat_polar);
62     suff_cur='latlon2'; fprintf('computing mid latitudes, part 2 \n');
63     case 3
64     switch_to_polar=1;
65     list_profilesM2=find(prof_latM2>=switch_to_polar*lat_polar);
66     suff_cur='northpole'; fprintf('computing high latitude, northern hemisphere \n');
67     case 4
68     switch_to_polar=-1;
69     list_profilesM2=find(prof_latM2<=switch_to_polar*lat_polar);
70     suff_cur='southpole'; fprintf('computing high latitude, southern hemisphere \n');
71     end
72     prof_lonM1=prof_lonM2(list_profilesM2); prof_latM1=prof_latM2(list_profilesM2);
73    
74     %do the "polar switch" for the data
75     [prof_lonM1,prof_latM1]=netcdf_ecco_GenericgridPolarPlaneCoords(prof_lonM1,prof_latM1,switch_to_polar);
76    
77    
78     eval(['files_list=ls('' -1 ' rep_grid 'profilesXC*.data '');']);
79     tmp1=files_list; tmp2=strfind(files_list,rep_grid)+length(rep_grid);
80     tmp3=strfind(files_list,'.data')+4;
81     files_list='';
82     for tmp4=1:length(tmp2);
83     files_list=strvcat(files_list, tmp1(tmp2(tmp4):tmp3(tmp4)) );
84     end
85    
86    
87     for tcur=1:size(files_list,1)
88    
89     prof_interp_XC11=NaN*zeros(length(prof_lonM2),1); prof_interp_YC11=prof_interp_XC11;
90     prof_interp_XCNINJ=prof_interp_XC11; prof_interp_YCNINJ=prof_interp_XC11;
91     prof_interp_i=NaN*zeros(length(prof_lonM2),2); prof_interp_j=prof_interp_i; prof_interp_weights=prof_interp_i;
92     prof_interp_lon=prof_interp_i; prof_interp_lat=prof_interp_i;
93    
94     file_cur=deblank(files_list(tcur,:));
95     tmp3=strfind(file_cur,'.');
96     ni=str2num(file_cur(tmp3(end-2)+1:tmp3(end-1)-1));
97     nj=str2num(file_cur(tmp3(end-1)+1:tmp3(end)-1));
98     fid=fopen([rep_grid file_cur],'r','b'); XC_cur=fread(fid,[ni+2 nj+2],'float64'); fclose(fid);
99     tmp3=strfind(file_cur,'XC'); file_cur(tmp3)='Y';
100     fid=fopen([rep_grid file_cur],'r','b'); YC_cur=fread(fid,[ni+2 nj+2],'float64'); fclose(fid);
101     XC_ref=XC_cur; YC_ref=YC_cur;
102     %set the longitude system for model:
103     [XC_cur(:)]=netcdf_ecco_GenericgridRotateLon(XC_cur(:),0,0,lonSystModel);
104    
105    
106     %tests to avoid bug particualr cases (test1&test2) and useless loops
107     %-------------------------------------------------------------------
108     %test1=1; if we are to do "polar switch", only tiles that have grid points in the "polar region" can be relevant!
109     test1=1; if switch_to_polar~=0; test1=~isempty(find(( YC_cur(:)-switch_to_polar*(lat_polar-10) )*switch_to_polar > 0)); end;
110     %test2: if no switch_to_polar do not address the polar tiles (the ones that go though all latitudes)
111     test2=1; if switch_to_polar==0; test2=isempty(find(XC_cur(:)>175))|isempty(find(XC_cur(:)<-175))|isempty(find(abs(XC_cur(:))<5)); end;
112     %test3: if no data don't bother!
113     test3=~isempty(list_profilesM2);
114    
115     %fprintf([num2str([subset_cur tcur test1 test2 test3]) '\n']);
116    
117     %%%%%%%%%%%%%%%%%%%%
118     if test1&test2&test3
119     %%%%%%%%%%%%%%%%%%%%
120    
121    
122     if choice_plot==1; subplot(2,1,1); plot(XC_cur(:),YC_cur(:),'yx'); hold on; plot(XC_cur(1,1),YC_cur(1,1),'b.'); plot(XC_cur(end,1),YC_cur(end,1),'m.');
123     tmp_lonM1=prof_lonM2(list_profilesM2); tmp_latM1=prof_latM2(list_profilesM2); plot(tmp_lonM1,tmp_latM1,'go'); axis([-180 180 -90 90]); end;
124    
125     if switch_to_polar~=0;
126     %switch to polar plane coordinates
127     tmp1=find(( YC_cur(:)-switch_to_polar*(lat_polar-10) )*switch_to_polar <= 0); XC_cur(tmp1)=NaN; YC_cur(tmp1)=NaN;
128     [XC_cur,YC_cur]=netcdf_ecco_GenericgridPolarPlaneCoords(XC_cur,YC_cur,switch_to_polar);
129     [XC_cur,YC_cur]=netcdf_ecco_GenericgridFixFieldsOverlapCorners(XC_cur,YC_cur);
130     else
131     lon0global=0;
132     if ~isempty(find(abs(XC_cur(:))>175)); lon0tile=180; else; lon0tile=0; end;
133     prof_lonM1ref=prof_lonM1;
134     %do the "model flip" for the data ... or not
135     [prof_lonM1]=netcdf_ecco_GenericgridRotateLon(prof_lonM1,lon0global,lon0tile,lonSystModel);
136     %do the "model flip" for the model ... or not
137     [XC_cur]=netcdf_ecco_GenericgridRotateLon(XC_cur,lon0global,lon0tile,lonSystModel);
138     [XC_cur,YC_cur]=netcdf_ecco_GenericgridFixFieldsOverlapCorners(XC_cur,YC_cur);
139     end
140    
141     if choice_plot==1; subplot(2,1,2); plot(XC_cur(:),YC_cur(:),'yx'); hold on; plot(XC_cur(1,1),YC_cur(1,1),'b.'); plot(XC_cur(end,1),YC_cur(end,1),'m.');
142     plot(prof_lonM1,prof_latM1,'go'); if ~switch_to_polar; axis([-180 180 -90 90]); else; axis([-90 90 -90 90]); end; end;
143    
144     x0=zeros((ni+1)*(nj+1),4); y0=x0; i0=x0; j0=x0;
145    
146     tmp_count=0;
147     for icur=1:ni+1; for jcur=1:nj+1;
148     tmp_count=tmp_count+1;
149    
150     x0(tmp_count,:)=[XC_cur(icur,jcur) XC_cur(icur+1,jcur) XC_cur(icur+1,jcur+1) XC_cur(icur,jcur+1)];
151     y0(tmp_count,:)=[YC_cur(icur,jcur) YC_cur(icur+1,jcur) YC_cur(icur+1,jcur+1) YC_cur(icur,jcur+1)];
152     i0(tmp_count,:)=[icur icur+1 icur+1 icur]; j0(tmp_count,:)=[jcur jcur jcur+1 jcur+1];
153    
154     end; end;
155    
156     tmp1=find(~isnan(sum(x0,2)));
157     x0=x0(tmp1,:); y0=y0(tmp1,:); i0=i0(tmp1,:); j0=j0(tmp1,:);
158    
159    
160     %first step: restrict our attention to profiles that are within the area
161     %-----------
162     %will work in any case when
163     % 1) the cell is non polar, so we do not cross the longitude limit
164     % or
165     % 2) we use polar coordinates, which is well defined, for both model and data
166     %=> if this is not the case: I have put some NaNs in XC_cur
167     if isempty(find(isnan(XC_cur)))
168     x00=[XC_cur(:,1)' XC_cur(end,2:end-1) flipdim(XC_cur(:,end)',2) XC_cur(1,end-1:-1:2)];
169     y00=[YC_cur(:,1)' YC_cur(end,2:end-1) flipdim(YC_cur(:,end)',2) YC_cur(1,end-1:-1:2)];
170     list_profilesM1=zeros(size(prof_lonM1));
171     length_div=1e3;
172     num_div=ceil(length(prof_lonM1)/length_div);
173     for cur_div=1:num_div
174     list_profiles0=[(cur_div-1)*length_div+1:min(cur_div*length_div,length(prof_lonM1))];
175     [tmp1]=netcdf_ecco_GenericgridLocateCell(x00,y00,prof_lonM1(list_profiles0),prof_latM1(list_profiles0));
176     list_profilesM1(list_profiles0(find(tmp1>0)))=1;
177     end
178     list_profilesM1=find(list_profilesM1>0);
179     prof_lon=prof_lonM1(list_profilesM1); prof_lat=prof_latM1(list_profilesM1);
180     else
181     list_profilesM1=[1:length(prof_lonM1)]'; prof_lon=prof_lonM1; prof_lat=prof_latM1;
182     end
183    
184     %second step: the main computation
185     %---------------------------------
186     if ~isempty(list_profilesM1)
187    
188     length_div=1e2;
189     num_div=ceil(length(prof_lon)/length_div);
190     for cur_div=1:num_div
191     list_profiles0=[(cur_div-1)*length_div+1:min(cur_div*length_div,length(prof_lon))];
192    
193     %if mod(cur_div,20)==1;
194     %tmp1=length(find(~isnan(prof_interp_i(:,1)))); tmp2=length(prof_lon);
195     %fprintf([num2str(tcur) ' -- ' num2str(list_profiles0(1)) ' / ' num2str(length(prof_lon)) ' -- ' num2str(tmp1) '\n']);
196     %end
197    
198     [list_profiles2]=netcdf_ecco_GenericgridLocateCell(x0,y0,prof_lon(list_profiles0),prof_lat(list_profiles0));
199    
200     [coeffs2]=netcdf_ecco_GenericgridCoeffs(x0,y0,prof_lon(list_profiles0),prof_lat(list_profiles0));
201    
202     list_profiles3=find(sum(list_profiles2,2)>0);
203    
204     for kkcur=1:length(list_profiles3)
205     %index in small data vector:
206     kcur0=list_profiles3(kkcur);
207     %more than one 1 can happen when close to cell edges; we pick the first;
208     tmp1=find(list_profiles2(kcur0,:)>0); tmp1=tmp1(1);
209     tmp2=squeeze(coeffs2(kcur0,tmp1,:)); tmp3=find(tmp2>0);
210     %index in the full data vector:
211     kcur=list_profilesM2(list_profilesM1(list_profiles0(kcur0)));
212     %fill arrays:
213     if length(tmp3)==2
214     prof_interp_XC11(kcur)=XC_ref(2,2); prof_interp_YC11(kcur)=YC_ref(2,2);
215     prof_interp_XCNINJ(kcur)=XC_ref(end-1,end-1); prof_interp_YCNINJ(kcur)=YC_ref(end-1,end-1);
216     prof_interp_i(kcur,:)=i0(tmp1,tmp3)-1;
217     prof_interp_j(kcur,:)=j0(tmp1,tmp3)-1;
218     prof_interp_weights(kcur,:)=tmp2(tmp3);
219     elseif length(tmp3)==1
220     prof_interp_XC11(kcur)=XC_ref(2,2); prof_interp_YC11(kcur)=YC_ref(2,2);
221     prof_interp_XCNINJ(kcur)=XC_ref(end-1,end-1); prof_interp_YCNINJ(kcur)=YC_ref(end-1,end-1);
222     prof_interp_i(kcur,:)=i0(tmp1,tmp3)-1;
223     prof_interp_j(kcur,:)=j0(tmp1,tmp3)-1;
224     prof_interp_weights(kcur,:)=[tmp2(tmp3) 0];
225     end
226     end
227    
228     end%for cur_div=1:num_div
229    
230     end%if ~isempty(list_profilesM1)
231    
232     if switch_to_polar==0;
233     %undo the "model flip" for the data
234     [prof_lonM1]=netcdf_ecco_GenericgridRotateLon(prof_lonM1,lon0tile,lon0global,lonSystModel); %needed for computation
235     [XC_cur]=netcdf_ecco_GenericgridRotateLon(XC_cur,lon0tile,lon0global,lonSystModel); %needed for plot only
236     % if ~isempty(find(mod(prof_lonM1,360)~=mod(prof_lonM1ref,360)));
237     % fprintf('pb due to change in longitudes => stop \n'); return;
238     % end
239     end
240    
241     if choice_plot==1; tmp1=find(~isnan(prof_interp_i(:,1)));
242     tmp_i=prof_interp_i(tmp1,1); tmp_j=prof_interp_j(tmp1,1); tmp2=(tmp_j+1-1)*(ni+2)+(tmp_i+1); plot(XC_cur(tmp2),YC_cur(tmp2),'ro');
243     tmp_i=prof_interp_i(tmp1,2); tmp_j=prof_interp_j(tmp1,2); tmp2=(tmp_j+1-1)*(ni+2)+(tmp_i+1); plot(XC_cur(tmp2),YC_cur(tmp2),'bo');
244     if ~switch_to_polar;
245     plot(prof_lonM2(tmp1),prof_latM2(tmp1),'k.');
246     else;
247     tmp_x=prof_lonM2(tmp1); tmp_y=prof_latM2(tmp1);
248     [tmp_x,tmp_y]=netcdf_ecco_GenericgridPolarPlaneCoords(tmp_x,tmp_y,switch_to_polar); plot(tmp_x,tmp_y,'k.');
249     end;
250     clf;
251     end;
252    
253    
254     %third step : we do not use overlap corner points in interpolation
255     %------------ -> we average the two neighboring overlap points instead
256     tmp1=( prof_interp_i==0&prof_interp_j==0 ); tmp1=find(sum(tmp1,2)>0);
257     if ~isempty(tmp1);
258     prof_interp_i(tmp1,:)=ones(length(tmp1),1)*[0 1];
259     prof_interp_j(tmp1,:)=ones(length(tmp1),1)*[1 0];
260     prof_interp_weights(tmp1,:)=ones(length(tmp1),1)*[1/2 1/2];
261     end
262     tmp1=( prof_interp_i==0&prof_interp_j==nj+1 ); tmp1=find(sum(tmp1,2)>0);
263     if ~isempty(tmp1);
264     prof_interp_i(tmp1,:)=ones(length(tmp1),1)*[0 1];
265     prof_interp_j(tmp1,:)=ones(length(tmp1),1)*[nj nj+1];
266     prof_interp_weights(tmp1,:)=ones(length(tmp1),1)*[1/2 1/2];
267     end
268     tmp1=( prof_interp_i==ni+1&prof_interp_j==0 ); tmp1=find(sum(tmp1,2)>0);
269     if ~isempty(tmp1);
270     prof_interp_i(tmp1,:)=ones(length(tmp1),1)*[ni+1 ni];
271     prof_interp_j(tmp1,:)=ones(length(tmp1),1)*[1 0];
272     prof_interp_weights(tmp1,:)=ones(length(tmp1),1)*[1/2 1/2];
273     end
274     tmp1=( prof_interp_i==ni+1&prof_interp_j==nj+1 ); tmp1=find(sum(tmp1,2)>0);
275     if ~isempty(tmp1);
276     prof_interp_i(tmp1,:)=ones(length(tmp1),1)*[ni ni+1];
277     prof_interp_j(tmp1,:)=ones(length(tmp1),1)*[nj+1 nj];
278     prof_interp_weights(tmp1,:)=ones(length(tmp1),1)*[1/2 1/2];
279     end
280    
281    
282     %fourth step: store the lon/lat of the interpolation points for checks
283     %------------
284     tmp1=find(~isnan(prof_interp_i));
285     tmp2=(ni+2)*prof_interp_j(tmp1)+prof_interp_i(tmp1)+1;
286     %rk: the computation of the previous line accounts for the fact that i/j go from 0 to ni+1/nj+1
287     prof_interp_lon(tmp1)=XC_ref(tmp2); prof_interp_lat(tmp1)=YC_ref(tmp2);
288    
289     %fifth step: save in .mat file
290     %------------------------------
291     tmp1=findstr(file_data,'.nc');
292     eval(['save ' rep_out file_data(1:tmp1(end)-1) '_4mygrid.' suff_cur '.' num2str(tcur) '.mat prof_interp_*;']);
293    
294     %%%%%%%%%%%%%%%%%%%%%%%%
295     end%if test1&test2&test3
296     %%%%%%%%%%%%%%%%%%%%%%%%
297    
298     end%for tcur=...
299    
300     end%for subset_cur=1:3
301    
302    

  ViewVC Help
Powered by ViewVC 1.1.22