/*\file Xy2ll.c
 *\brief: x/y to lat/long coordinate mex module.
 */
#include "./Xy2ll.h"

void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){

	int i,verbose=1;

	/*input: */
	double  *x=NULL,*y=NULL;
	int     nx,ny,ncoord;
	int     sgn;
	Options* options=NULL;
	double   cm=0.,sp=0.;

	/* output: */
	double  *lat=NULL,*lon=NULL;
	int     iret=0;

	/*Boot module: */
	MODULEBOOT();

	/*checks on arguments on the matlab side: */
	if (nlhs > NLHS) {
		Xy2llUsage(); _error2_("Xy2ll usage error");
	}
	if (nrhs < NRHS) {
		Xy2llUsage(); _error2_("Xy2ll usage error");
	}

	/*Input datasets: */
	FetchData(&x,&nx,X_IN);
	FetchData(&y,&ny,Y_IN);
	FetchData(&sgn,SGN_IN);
	FetchData(&options,NRHS,nrhs,prhs);

	/*  defaults are in Xy2lldef, so don't duplicate them here, and only use user values if both have been specified  */
	if (options->GetOption("central_meridian") || options->GetOption("standard_parallel")) {
		options->Get(&cm,"central_meridian");
		if (verbose) _printLine_("  cm=" << cm);
		options->Get(&sp,"standard_parallel");
		if (verbose) _printLine_("  sp=" << sp);
	}

	/*some checks*/
	if   (nx != ny){_error2_("Must have same number of x[" << nx << "] and y[" << ny << "] coordinates.");}
	else            ncoord=nx;
	if (sgn != +1 && sgn != -1) _error2_("Hemisphere sgn=" << sgn << " must be +1 (north) or -1 (south).");
	if (fabs(cm)      > 180.) _error2_("Central meridian cm=" << cm << " must be between -180 (west) and +180 (east) degrees.");
	if (sp < 0. || sp >  90.) _error2_("Standard parallel sp=" << sp << " must be between 0 and 90 degrees (in specified hemisphere).");

	lat=(double*)mxMalloc(ncoord*sizeof(double));
	lon=(double*)mxMalloc(ncoord*sizeof(double));

	/* Run core computations: */
	if (verbose) _printLine_("Calling core:");
	if (options->GetOption("central_meridian") && options->GetOption("standard_parallel"))
		iret=Xy2llx(lat,lon,x,y,ncoord,sgn,cm,sp);
	else
		iret=Xy2llx(lat,lon,x,y,ncoord,sgn);

	/*Write data: */
	WriteData(LAT_OUT,lat,ncoord);
	WriteData(LON_OUT,lon,ncoord);

	/*Clean-up*/
	delete options;

	/*end module: */
	MODULEEND();
}

void Xy2llUsage(void){
	_pprintLine_("Xy2ll - x/y to lat/long coordinate transformation module:");
	_pprintLine_("");
	_pprintLine_("   This module transforms x/y to lat/long coordinates.");
	_pprintLine_("");
	_pprintLine_("   Usage:");
	_pprintLine_("      [lat,lon]=Xy2ll(x,y,sgn,'param name',param,...);");
	_pprintLine_("");
	_pprintLine_("      x           x coordinates (double vector)");
	_pprintLine_("      y           y coordinates (double vector)");
	_pprintLine_("      sgn         sign for hemisphere (double, +1 (north) or -1 (south))");
	_pprintLine_("");
	_pprintLine_("      central_meridian     central meridian (double, optional, but must specify with sp)");
	_pprintLine_("      standard_parallel    standard parallel (double, optional, but must specify with cm)");
	_pprintLine_("");
	_pprintLine_("      lat         latitude coordinates (double vector)");
	_pprintLine_("      lon         longitude coordinates (double vector)");
	_pprintLine_("");
	_pprintLine_("   Examples:");
	_pprintLine_("      [lat,lon]=Xy2ll(x,y, 1);");
	_pprintLine_("      [lat,lon]=Xy2ll(x,y, 1,'central_meridian',45,'standard_parallel',70);");
	_pprintLine_("      [lat,lon]=Xy2ll(x,y,-1,'central_meridian', 0,'standard_parallel',71);");
	_pprintLine_("");
}

