NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/opnml/MEX/isopmex5.c

Revision 495 (checked in by cbc, 11 years ago)

Initial import of Stark code.

Line 
1 #include <math.h>
2 #include <stdio.h>
3 #include "mex.h"
4 #include "opnml_mex5_allocs.c"
5
6 /* PROTOTYPES */
7 void isophase(int,
8               double *,
9               double *,
10               int *,
11               double *,
12               double,
13               double **,
14               int *,
15               double);
16
17 /************************************************************
18
19   ####     ##     #####  ######  #    #    ##     #   #
20  #    #   #  #      #    #       #    #   #  #     # #
21  #       #    #     #    #####   #    #  #    #     #
22  #  ###  ######     #    #       # ## #  ######     #
23  #    #  #    #     #    #       ##  ##  #    #     #
24   ####   #    #     #    ######  #    #  #    #     #
25
26 ************************************************************/
27
28
29 void mexFunction(int            nlhs,
30                  mxArray       *plhs[],
31                  int            nrhs,
32                  const mxArray *prhs[])
33 {
34
35 /* ---- isopmex will be called as :
36         mat=isopmex(x,y,e,q,cval);
37 [e,x,y,z,b]=loadgrid('nsea2ll');                                     
38 [data,gname]=read_s2r;
39 q=data(:,2);                       
40                                       ------------------------------- */
41    int cnt,*ele,i,j,nn,ne;
42    double *x, *y, *q;
43    double *cval,*dele;
44    double **cmat,*newcmat;
45    int nrl,nrh,ncl,nch;
46    double NaN=mxGetNaN();
47      
48 /* ---- check I/O arguments ----------------------------------------- */
49    if (nrhs != 5)
50       mexErrMsgTxt("isophase_mex requires 5 input arguments.");
51    else if (nlhs !=1)
52       mexErrMsgTxt("isophase_mex requires 1 output arguments.");
53
54 /* ---- dereference input arrays ------------------------------------ */
55    x=mxGetPr(prhs[0]);
56    y=mxGetPr(prhs[1]);
57    dele=mxGetPr(prhs[2]);
58    q=mxGetPr(prhs[3]);
59    cval=mxGetPr(prhs[4]);
60    nn=mxGetM(prhs[0]);
61    ne=mxGetM(prhs[2]); 
62    
63    
64 /* ---- allocate space for int representation of dele &
65         convert double element representation to int  &
66         shift node numbers toward 0 by 1 for proper indexing -------- */
67    ele=(int *)mxIvector(0,3*ne-1);
68    for (i=0;i<3*ne;i++)
69       ele[i]=((int)dele[i]-1);
70    
71 /* ---- allocate space for divergence list dv following
72         NRC allocation style                           
73    cmat= mxDmatrix(     0,     ne-1,    0,      3)     -------------- */
74
75    nrl=0;
76    nrh=6*ne;
77    ncl=0;
78    nch=3;
79    cmat=(double **) mxDmatrix(nrl,nrh,ncl,nch);
80        
81    isophase(ne,x,y,ele,q,cval[0],cmat,&cnt,NaN);
82    
83    if (cnt<1){
84       plhs[0]=mxCreateDoubleMatrix(0,0,mxREAL);
85       return;
86    }     
87    
88    newcmat=(double *) mxDvector(0,2*cnt-1);
89    for (j=0;j<2;j++)
90       for (i=0;i<cnt;i++)
91          newcmat[cnt*j+i]=cmat[i][j];
92
93    /*
94    Set elements of return matrix, pointed to by plhs[0]
95    */
96    plhs[0]=mxCreateDoubleMatrix(cnt,2,mxREAL);
97    mxSetPr(plhs[0],newcmat);
98
99    /*
100    No need to free memory allocated with "mxCalloc"; MATLAB does
101    this auotmatically.  The CMEX allocation functions in
102    "opnml_allocs.c" use mxCalloc.   
103    */
104    
105    return;   
106 }
107
108 /****************************************************************
109
110   ###    #####  ####### ######  #     #    #     #####  #######
111    #    #     # #     # #     # #     #   # #   #     # #
112    #    #       #     # #     # #     #  #   #  #       #
113    #     #####  #     # ######  ####### #     #  #####  #####
114    #          # #     # #       #     # #######       # #
115    #    #     # #     # #       #     # #     # #     # #
116   ###    #####  ####### #       #     # #     #  #####  #######
117
118 ****************************************************************/
119    
120 #ifdef __STDC__
121    void isophase(int ne,
122                  double *x,
123                  double *y,
124                  int *ele,
125                  double *pha,
126                  double cval,
127                  double **cmat,
128                  int *cnt,
129                  double NaN)
130 #else
131    void isophase(ne,x,y,ele,pha,cval,cmat,cnt,NaN)
132    int ne;
133    double *x, *y, *pha,cval,**cmat,NaN;
134    int *ele,*cnt;
135 #endif
136 #define ELE(i,j,m) ele[i+m*j]
137 {
138    double var[9],xx[9],yy[9];
139    double plmt=150.,p0lmt=260.,xp[6],yp[6];
140    int i,icnt,k,k2,l,n,count,nsw,nvert=3;
141    double vlmt,v1,v3,xcon,ycon;
142    
143    count=0;
144    
145    for(l=0;l<ne;l++){                  /* begin 651 */
146       nsw=0;
147       for(k=0;k<nvert;k++){            /* begin 641 */
148          n=ELE(l,k,ne);
149          xx[k]=x[n];
150          yy[k]=y[n];
151          var[k]=pha[n];
152          vlmt=plmt;
153          if(cval<1.){
154             vlmt=p0lmt;
155             if(var[k]>180.) var[k]=var[k]-360.;
156          }
157       }                                /* end 641 */
158    
159       if(cval==var[0]) {
160          nsw=1;
161          icnt=1;
162          xp[icnt]=xx[0];
163          yp[icnt]=yy[0];
164       }
165       for(k=0;k<nvert;k++){                /* begin 649 */
166          k2=k+1;
167          if(k2>nvert-1) k2=0;
168          if(var[k]>var[k2]) goto L610;
169          if(cval<var[k]||cval>var[k2]) goto L649;
170          goto L611;
171  L610:   if(cval<var[k2]||cval>var[k]) goto L649;
172  L611:   v3=var[k2]-var[k];
173          if(fabs(v3)>vlmt) goto L649;
174          v1=1.;
175          if(fabs(v3)>1.e-7) v1=(cval-var[k])/v3;
176          xcon=v1*(xx[k2]-xx[k])+xx[k];
177          ycon=v1*(yy[k2]-yy[k])+yy[k];
178          if(nsw==1) {
179             icnt++;
180             xp[icnt]=xcon;
181             yp[icnt]=ycon;
182          }
183          else{
184             nsw=1;
185             icnt=1;
186             xp[icnt]=xcon;
187             yp[icnt]=ycon;
188          }
189  L649:continue;
190       }                                  /* end 649 */
191       if(nsw==1&&icnt>1) {
192          for(i=1;i<=icnt;i++){
193             cmat[count][0]=xp[i];
194             cmat[count][1]=yp[i];
195             count++;
196          }
197          cmat[count][0]=NaN;
198          cmat[count][1]=NaN;
199          count++;       
200       }
201    }                                    /* end 651 */
202    *cnt=count;
203    return;
204 }
Note: See TracBrowser for help on using the browser.