| 1 | /*\file Xy2ll.c | 
|---|
| 2 | *\brief: x/y to lat/long coordinate mex module. | 
|---|
| 3 | */ | 
|---|
| 4 | #include "./Xy2ll.h" | 
|---|
| 5 |  | 
|---|
| 6 | void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){ | 
|---|
| 7 |  | 
|---|
| 8 | int i,verbose=1; | 
|---|
| 9 |  | 
|---|
| 10 | /*input: */ | 
|---|
| 11 | double  *x=NULL,*y=NULL; | 
|---|
| 12 | int     nx,ny,ncoord; | 
|---|
| 13 | int     sgn; | 
|---|
| 14 |  | 
|---|
| 15 | Options* options=NULL; | 
|---|
| 16 | double   cm=0.,sp=0.; | 
|---|
| 17 |  | 
|---|
| 18 | /* output: */ | 
|---|
| 19 | double  *lat=NULL,*lon=NULL; | 
|---|
| 20 | int     iret=0; | 
|---|
| 21 |  | 
|---|
| 22 | /*Boot module: */ | 
|---|
| 23 | MODULEBOOT(); | 
|---|
| 24 |  | 
|---|
| 25 | /*checks on arguments on the matlab side: */ | 
|---|
| 26 | if (nlhs > NLHS) { | 
|---|
| 27 | Xy2llUsage(); | 
|---|
| 28 | _error_("Xy2ll usage error"); | 
|---|
| 29 | } | 
|---|
| 30 | if (nrhs < NRHS) { | 
|---|
| 31 | Xy2llUsage(); | 
|---|
| 32 | _error_("Xy2ll usage error"); | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | /*Input datasets: */ | 
|---|
| 36 | if (verbose) printf("Fetching inputs:\n"); | 
|---|
| 37 | FetchData(&x,&nx,X_IN); | 
|---|
| 38 | if (verbose) | 
|---|
| 39 | if   (nx == 1) printf("  x=%g\n",x[0]); | 
|---|
| 40 | else           printf("  x=[%d values]\n",nx); | 
|---|
| 41 | //      for (i=0; i<nx; i++) printf("  x[%d]=%g\n",i,x[i]); | 
|---|
| 42 | FetchData(&y,&ny,Y_IN); | 
|---|
| 43 | if (verbose) | 
|---|
| 44 | if   (ny == 1) printf("  y=%g\n",y[0]); | 
|---|
| 45 | else           printf("  y=[%d values]\n",ny); | 
|---|
| 46 | //      for (i=0; i<ny; i++) printf("  y[%d]=%g\n",i,y[i]); | 
|---|
| 47 | FetchData(&sgn,SGN_IN); | 
|---|
| 48 | if (verbose) printf("  sgn=%d\n",sgn); | 
|---|
| 49 |  | 
|---|
| 50 | if (verbose) printf("Parsing options:\n"); | 
|---|
| 51 | options=new Options(NRHS,nrhs,prhs); | 
|---|
| 52 | if (options->Size()) for(i=0;i<options->Size();i++) ((Option*)options->GetObjectByOffset(i))->DeepEcho(); | 
|---|
| 53 | /*  defaults are in Xy2lldef, so don't duplicate them here, and only use user values if both have been specified  */ | 
|---|
| 54 | if (options->GetOption("central_meridian") || options->GetOption("standard_parallel")) { | 
|---|
| 55 | options->Get(&cm,"central_meridian"); | 
|---|
| 56 | if (verbose) printf("  cm=%g\n",cm); | 
|---|
| 57 | options->Get(&sp,"standard_parallel"); | 
|---|
| 58 | if (verbose) printf("  sp=%g\n",sp); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | /*some checks*/ | 
|---|
| 62 | if (verbose) printf("Checking inputs:\n"); | 
|---|
| 63 |  | 
|---|
| 64 | if   (nx != ny) _error_("Must have same number of x[%d] and y[%d] coordinates.",nx,ny); | 
|---|
| 65 | else            ncoord=nx; | 
|---|
| 66 | if (sgn != +1 && sgn != -1) _error_("Hemisphere sgn=%d must be +1 (north) or -1 (south).",sgn); | 
|---|
| 67 | if (fabs(cm)      > 180.) _error_("Central meridian cm=%g must be between -180 (west) and +180 (east) degrees.",cm); | 
|---|
| 68 | if (sp < 0. || sp >  90.) _error_("Standard parallel sp=%g must be between 0 and 90 degrees (in specified hemisphere).",sp); | 
|---|
| 69 |  | 
|---|
| 70 | lat=(double *)xmalloc(ncoord*sizeof(double)); | 
|---|
| 71 | lon=(double *)xmalloc(ncoord*sizeof(double)); | 
|---|
| 72 |  | 
|---|
| 73 | /* Run core computations: */ | 
|---|
| 74 | if (verbose) printf("Calling core:\n"); | 
|---|
| 75 | if (options->GetOption("central_meridian") && options->GetOption("standard_parallel")) | 
|---|
| 76 | iret=Xy2llx(lat,lon, | 
|---|
| 77 | x,y,ncoord, | 
|---|
| 78 | sgn,cm,sp); | 
|---|
| 79 | else | 
|---|
| 80 | iret=Xy2llx(lat,lon, | 
|---|
| 81 | x,y,ncoord, | 
|---|
| 82 | sgn); | 
|---|
| 83 | if (verbose) printf("  iret=%d\n",iret); | 
|---|
| 84 |  | 
|---|
| 85 | /*Write data: */ | 
|---|
| 86 | WriteData(LAT_OUT,lat,ncoord); | 
|---|
| 87 | WriteData(LON_OUT,lon,ncoord); | 
|---|
| 88 |  | 
|---|
| 89 | /*Clean-up*/ | 
|---|
| 90 | delete options; | 
|---|
| 91 |  | 
|---|
| 92 | /*end module: */ | 
|---|
| 93 | MODULEEND(); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void Xy2llUsage(void) | 
|---|
| 97 | { | 
|---|
| 98 | _printf_(true,"Xy2ll - x/y to lat/long coordinate transformation module:\n"); | 
|---|
| 99 | _printf_(true,"\n"); | 
|---|
| 100 | _printf_(true,"   This module transforms x/y to lat/long coordinates.\n"); | 
|---|
| 101 | _printf_(true,"\n"); | 
|---|
| 102 | _printf_(true,"   Usage:\n"); | 
|---|
| 103 | _printf_(true,"      [lat,lon]=Xy2ll(x,y,sgn,'param name',param,...);\n"); | 
|---|
| 104 | _printf_(true,"\n"); | 
|---|
| 105 | _printf_(true,"      x           x coordinates (double vector)\n"); | 
|---|
| 106 | _printf_(true,"      y           y coordinates (double vector)\n"); | 
|---|
| 107 | _printf_(true,"      sgn         sign for hemisphere (double, +1 (north) or -1 (south))\n"); | 
|---|
| 108 | _printf_(true,"\n"); | 
|---|
| 109 | _printf_(true,"      central_meridian     central meridian (double, optional, but must specify with sp)\n"); | 
|---|
| 110 | _printf_(true,"      standard_parallel    standard parallel (double, optional, but must specify with cm)\n"); | 
|---|
| 111 | _printf_(true,"\n"); | 
|---|
| 112 | _printf_(true,"      lat         latitude coordinates (double vector)\n"); | 
|---|
| 113 | _printf_(true,"      lon         longitude coordinates (double vector)\n"); | 
|---|
| 114 | _printf_(true,"\n"); | 
|---|
| 115 | _printf_(true,"   Examples:\n"); | 
|---|
| 116 | _printf_(true,"      [lat,lon]=Xy2ll(x,y, 1);\n"); | 
|---|
| 117 | _printf_(true,"      [lat,lon]=Xy2ll(x,y, 1,'central_meridian',45,'standard_parallel',70);\n"); | 
|---|
| 118 | _printf_(true,"      [lat,lon]=Xy2ll(x,y,-1,'central_meridian', 0,'standard_parallel',71);\n"); | 
|---|
| 119 | _printf_(true,"\n"); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|