[8681] | 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 | Options* options=NULL;
|
---|
| 15 | double cm=0.,sp=0.;
|
---|
| 16 |
|
---|
| 17 | /* output: */
|
---|
| 18 | double *lat=NULL,*lon=NULL;
|
---|
[8689] | 19 | int iret=0;
|
---|
[8681] | 20 |
|
---|
| 21 | /*Boot module: */
|
---|
| 22 | MODULEBOOT();
|
---|
| 23 |
|
---|
| 24 | /*checks on arguments on the matlab side: */
|
---|
| 25 | if (nlhs > NLHS) {
|
---|
[12049] | 26 | Xy2llUsage(); _error_("Xy2ll usage error");
|
---|
[8681] | 27 | }
|
---|
| 28 | if (nrhs < NRHS) {
|
---|
[12049] | 29 | Xy2llUsage(); _error_("Xy2ll usage error");
|
---|
[8681] | 30 | }
|
---|
| 31 |
|
---|
| 32 | /*Input datasets: */
|
---|
[11933] | 33 | FetchData(&x,&nx,X_IN);
|
---|
| 34 | FetchData(&y,&ny,Y_IN);
|
---|
| 35 | FetchData(&sgn,SGN_IN);
|
---|
[12049] | 36 | FetchData(&options,NRHS,nrhs,prhs);
|
---|
[8681] | 37 |
|
---|
[8717] | 38 | /* defaults are in Xy2lldef, so don't duplicate them here, and only use user values if both have been specified */
|
---|
[8681] | 39 | if (options->GetOption("central_meridian") || options->GetOption("standard_parallel")) {
|
---|
| 40 | options->Get(&cm,"central_meridian");
|
---|
| 41 | if (verbose) printf(" cm=%g\n",cm);
|
---|
| 42 | options->Get(&sp,"standard_parallel");
|
---|
| 43 | if (verbose) printf(" sp=%g\n",sp);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /*some checks*/
|
---|
| 47 | if (nx != ny) _error_("Must have same number of x[%d] and y[%d] coordinates.",nx,ny);
|
---|
| 48 | else ncoord=nx;
|
---|
| 49 | if (sgn != +1 && sgn != -1) _error_("Hemisphere sgn=%d must be +1 (north) or -1 (south).",sgn);
|
---|
[8697] | 50 | if (fabs(cm) > 180.) _error_("Central meridian cm=%g must be between -180 (west) and +180 (east) degrees.",cm);
|
---|
| 51 | if (sp < 0. || sp > 90.) _error_("Standard parallel sp=%g must be between 0 and 90 degrees (in specified hemisphere).",sp);
|
---|
[8681] | 52 |
|
---|
[12049] | 53 | lat=(double*)mxMalloc(ncoord*sizeof(double));
|
---|
| 54 | lon=(double*)mxMalloc(ncoord*sizeof(double));
|
---|
[8681] | 55 |
|
---|
| 56 | /* Run core computations: */
|
---|
| 57 | if (verbose) printf("Calling core:\n");
|
---|
| 58 | if (options->GetOption("central_meridian") && options->GetOption("standard_parallel"))
|
---|
[12049] | 59 | iret=Xy2llx(lat,lon,x,y,ncoord,sgn,cm,sp);
|
---|
[8681] | 60 | else
|
---|
[12049] | 61 | iret=Xy2llx(lat,lon,x,y,ncoord,sgn);
|
---|
[8681] | 62 |
|
---|
| 63 | /*Write data: */
|
---|
[11933] | 64 | WriteData(LAT_OUT,lat,ncoord);
|
---|
| 65 | WriteData(LON_OUT,lon,ncoord);
|
---|
[8681] | 66 |
|
---|
| 67 | /*Clean-up*/
|
---|
| 68 | delete options;
|
---|
| 69 |
|
---|
| 70 | /*end module: */
|
---|
| 71 | MODULEEND();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[12049] | 74 | void Xy2llUsage(void){
|
---|
[8681] | 75 | _printf_(true,"Xy2ll - x/y to lat/long coordinate transformation module:\n");
|
---|
| 76 | _printf_(true,"\n");
|
---|
| 77 | _printf_(true," This module transforms x/y to lat/long coordinates.\n");
|
---|
| 78 | _printf_(true,"\n");
|
---|
| 79 | _printf_(true," Usage:\n");
|
---|
| 80 | _printf_(true," [lat,lon]=Xy2ll(x,y,sgn,'param name',param,...);\n");
|
---|
| 81 | _printf_(true,"\n");
|
---|
| 82 | _printf_(true," x x coordinates (double vector)\n");
|
---|
| 83 | _printf_(true," y y coordinates (double vector)\n");
|
---|
| 84 | _printf_(true," sgn sign for hemisphere (double, +1 (north) or -1 (south))\n");
|
---|
| 85 | _printf_(true,"\n");
|
---|
| 86 | _printf_(true," central_meridian central meridian (double, optional, but must specify with sp)\n");
|
---|
| 87 | _printf_(true," standard_parallel standard parallel (double, optional, but must specify with cm)\n");
|
---|
| 88 | _printf_(true,"\n");
|
---|
| 89 | _printf_(true," lat latitude coordinates (double vector)\n");
|
---|
| 90 | _printf_(true," lon longitude coordinates (double vector)\n");
|
---|
| 91 | _printf_(true,"\n");
|
---|
| 92 | _printf_(true," Examples:\n");
|
---|
| 93 | _printf_(true," [lat,lon]=Xy2ll(x,y, 1);\n");
|
---|
| 94 | _printf_(true," [lat,lon]=Xy2ll(x,y, 1,'central_meridian',45,'standard_parallel',70);\n");
|
---|
| 95 | _printf_(true," [lat,lon]=Xy2ll(x,y,-1,'central_meridian', 0,'standard_parallel',71);\n");
|
---|
| 96 | _printf_(true,"\n");
|
---|
| 97 | }
|
---|
| 98 |
|
---|