/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_calc/gcmfaces_bindata.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_calc/gcmfaces_bindata.m

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


Revision 1.9 - (hide annotations) (download)
Tue Oct 2 00:11:28 2012 UTC (12 years, 9 months ago) by gforget
Branch: MAIN
Changes since 1.8: +5 -2 lines
- fix logic (bug from previous revision).

1 gforget 1.1 function [varargout]=gcmfaces_bindata(varargin);
2 gforget 1.4 %object: compute delaunay triangulation, then bin averaging
3     %inputs: are all optional, triggering different sections of the code
4     % if none then generate the very triangulation (myTri)
5     % if lon,lat vectors, then compute closest neighbor index(ices)
6     % if lon,lat,obs vectors, then further do the bin average
7     %outputs: are all optional, triggering different sections of the code
8     % if OBS then return the bin aberage
9     % if OBS,NOBS then return the bin sum and count
10     %
11 gforget 1.8 %pre-requisite : the grid of interest must have been loaded with grid_load
12     %
13     %note to self: should mytri become part of mygrid ?
14 gforget 1.1
15 gforget 1.2 warning('off','MATLAB:dsearch:DeprecatedFunction');
16    
17 gforget 1.8 gcmfaces_global; global mytri;
18 gforget 1.6
19     if ~isfield(myenv,'useDelaunayTri');
20     myenv.useDelaunayTri=~isempty(which('DelaunayTri'));
21     end;
22 gforget 1.1
23 gforget 1.8 %1) check that triangulation is up to date
24     if isempty(mytri);
25     mytri.dirGrid=mygrid.dirGrid; mytri.nFaces=mygrid.nFaces;
26     mytri.fileFormat=mygrid.fileFormat; mytri.ioSize=mygrid.ioSize;
27     end;
28    
29     test1=strcmp(mytri.dirGrid,mygrid.dirGrid)&(mytri.nFaces==mygrid.nFaces)&...
30     strcmp(mytri.fileFormat,mygrid.fileFormat)&(sum(mytri.ioSize~=mygrid.ioSize)==0);
31     test2=isfield(mytri,'TRI');
32    
33 gforget 1.9 %2) update triangulation if not up to date
34     if (~test1|~test2)&(nargin~=0); gcmfaces_bindata; end;
35    
36     %3) carry on depending on inputs list
37     if (nargin==0);
38 gforget 1.8 %
39     mytri=[];
40     mytri.dirGrid=mygrid.dirGrid; mytri.nFaces=mygrid.nFaces;
41     mytri.fileFormat=mygrid.fileFormat; mytri.ioSize=mygrid.ioSize;
42 gforget 1.5 %generate delaunay triangulation:
43     XC=convert2array(mygrid.XC);
44     YC=convert2array(mygrid.YC);
45 gforget 1.7 kk=find(~isnan(XC)); mytri.kk=kk;
46     [mytri.ii,mytri.jj]=find(~isnan(XC));
47 gforget 1.6 if myenv.useDelaunayTri;
48     %the new way, using DelaunayTri&nearestNeighbor
49     mytri.TRI=DelaunayTri(XC(kk),YC(kk));
50     else;
51     %the old way, using delaunay&dsearch
52 gforget 1.7 TRI=delaunay(XC(kk),YC(kk)); nxy = length(kk);
53 gforget 1.6 Stri=sparse(TRI(:,[1 1 2 2 3 3]),TRI(:,[2 3 1 3 1 2]),1,nxy,nxy);
54 gforget 1.7 mytri.XC=XC(mytri.kk); mytri.YC=YC(mytri.kk);
55     mytri.TRI=TRI; mytri.Stri=Stri;
56 gforget 1.6 %usage: kk=dsearch(mytri.XC,mytri.YC,mytri.TRI,lon,lat,mytri.Stri);
57     % where lon and lat are vector of position
58     end
59 gforget 1.1 elseif nargin==2;
60 gforget 1.5 %compute grid point vector associated with lon/lat vectors
61     lon=varargin{1}; lat=varargin{2};
62 gforget 1.6 if myenv.useDelaunayTri;
63     kk = mytri.TRI.nearestNeighbor(lon,lat);
64     else;
65     kk=dsearch(mytri.XC,mytri.YC,mytri.TRI,lon,lat,mytri.Stri);
66     end;
67 gforget 1.5 if nargout==1;
68     varargout={mytri.kk(kk)};
69     elseif nargout==2;
70     ii=mytri.ii(kk); jj=mytri.jj(kk);
71     varargout={ii}; varargout(2)={jj};
72     else;
73     error('wrong output choice');
74     end;
75 gforget 1.1 elseif nargin==3;
76 gforget 1.5 %do the bin average (if nargout==1) or the bin sum+count (if nargout==2)
77     lon=varargin{1}; lat=varargin{2}; obs=varargin{3};
78     ii=find(~isnan(obs)); lon=lon(ii); lat=lat(ii); obs=obs(ii);
79 gforget 1.6 if myenv.useDelaunayTri;
80     kk = mytri.TRI.nearestNeighbor(lon,lat);
81     else;
82     kk=dsearch(mytri.XC,mytri.YC,mytri.TRI,lon,lat,mytri.Stri);
83     end;
84 gforget 1.5 kk=mytri.kk(kk);
85    
86 gforget 1.6 OBS=convert2array(mygrid.XC);
87 gforget 1.5 OBS(:)=0; NOBS=OBS;
88     for k=1:length(kk)
89     NOBS(kk(k))=NOBS(kk(k))+1;
90     OBS(kk(k))=OBS(kk(k))+obs(k);
91     end % k=1:length(kk)
92    
93     if nargout==1;%output bin average
94     in=find(NOBS); OBS(in)=OBS(in)./NOBS(in);
95     in=find(~NOBS); OBS(in)=NaN; NOBS(in)=NaN;
96     varargout={convert2array(OBS)};
97     elseif nargout==2;%output bin sum+count
98     OBS=convert2array(OBS);
99     NOBS=convert2array(NOBS);
100     varargout={OBS}; varargout(2)={NOBS};
101     else;
102     error('wrong output choice');
103     end;
104    
105 gforget 1.1 else;
106 gforget 1.5 error('wrong input choice');
107 gforget 1.1 end;
108    
109 gforget 1.2 warning('on','MATLAB:dsearch:DeprecatedFunction');
110 gforget 1.1
111    
112    

  ViewVC Help
Powered by ViewVC 1.1.22