/[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.10 - (hide annotations) (download)
Tue Apr 7 01:53:03 2015 UTC (10 years, 3 months ago) by gforget
Branch: MAIN
Changes since 1.9: +5 -0 lines
- make sure vectors are columns for DelaunayTri/nearestNeighbor

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.10 if size(lon,2)>1; lon=lon'; end;
63     if size(lat,2)>1; lat=lat'; end;
64 gforget 1.6 if myenv.useDelaunayTri;
65     kk = mytri.TRI.nearestNeighbor(lon,lat);
66     else;
67     kk=dsearch(mytri.XC,mytri.YC,mytri.TRI,lon,lat,mytri.Stri);
68     end;
69 gforget 1.5 if nargout==1;
70     varargout={mytri.kk(kk)};
71     elseif nargout==2;
72     ii=mytri.ii(kk); jj=mytri.jj(kk);
73     varargout={ii}; varargout(2)={jj};
74     else;
75     error('wrong output choice');
76     end;
77 gforget 1.1 elseif nargin==3;
78 gforget 1.5 %do the bin average (if nargout==1) or the bin sum+count (if nargout==2)
79     lon=varargin{1}; lat=varargin{2}; obs=varargin{3};
80 gforget 1.10 if size(lon,2)>1; lon=lon'; end;
81     if size(lat,2)>1; lat=lat'; end;
82     if size(obs,2)>1; obs=obs'; end;
83 gforget 1.5 ii=find(~isnan(obs)); lon=lon(ii); lat=lat(ii); obs=obs(ii);
84 gforget 1.6 if myenv.useDelaunayTri;
85     kk = mytri.TRI.nearestNeighbor(lon,lat);
86     else;
87     kk=dsearch(mytri.XC,mytri.YC,mytri.TRI,lon,lat,mytri.Stri);
88     end;
89 gforget 1.5 kk=mytri.kk(kk);
90    
91 gforget 1.6 OBS=convert2array(mygrid.XC);
92 gforget 1.5 OBS(:)=0; NOBS=OBS;
93     for k=1:length(kk)
94     NOBS(kk(k))=NOBS(kk(k))+1;
95     OBS(kk(k))=OBS(kk(k))+obs(k);
96     end % k=1:length(kk)
97    
98     if nargout==1;%output bin average
99     in=find(NOBS); OBS(in)=OBS(in)./NOBS(in);
100     in=find(~NOBS); OBS(in)=NaN; NOBS(in)=NaN;
101     varargout={convert2array(OBS)};
102     elseif nargout==2;%output bin sum+count
103     OBS=convert2array(OBS);
104     NOBS=convert2array(NOBS);
105     varargout={OBS}; varargout(2)={NOBS};
106     else;
107     error('wrong output choice');
108     end;
109    
110 gforget 1.1 else;
111 gforget 1.5 error('wrong input choice');
112 gforget 1.1 end;
113    
114 gforget 1.2 warning('on','MATLAB:dsearch:DeprecatedFunction');
115 gforget 1.1
116    
117    

  ViewVC Help
Powered by ViewVC 1.1.22