/[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.11 - (hide annotations) (download)
Mon Oct 5 21:58:49 2015 UTC (9 years, 9 months ago) by gforget
Branch: MAIN
CVS Tags: checkpoint65x, checkpoint65r, checkpoint65p, checkpoint65q, checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a, checkpoint66o, HEAD
Changes since 1.10: +1 -0 lines
- fix dimensions if needed.

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

  ViewVC Help
Powered by ViewVC 1.1.22