/*\file Kml2Exp.c
 *\brief: kml to exp file conversion mex module.
 */
#include "./Kml2Exp.h"

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

	int i,verbose=1;

	/*input: */
	char    *filkml=NULL,*filexp=NULL;
	int     sgn;
	Options* options=NULL;
	double   cm=0.,sp=0.;

	/* output: */
	int     iret=0;

	/*Boot module: */
	MODULEBOOT();

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

	/*Input datasets: */
	FetchData(&filkml,KML_IN);
	FetchData(&filexp,EXP_IN);
	FetchData(&sgn,SGN_IN);
	FetchData(&options,NRHS,nrhs,prhs);

	/*  defaults are in Ll2xydef, 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) printf("  cm=%g\n",cm);
		options->Get(&sp,"standard_parallel");
		if (verbose) printf("  sp=%g\n",sp);
	}

	/*some checks*/
	if (sgn !=+1 && sgn!= -1) _error_("Hemisphere sgn=%d must be +1 (north) or -1 (south).",sgn);
	if (fabs(cm)      > 180.) _error_("Central meridian cm=%g must be between -180 (west) and +180 (east) degrees.",cm);
	if (sp < 0. || sp >  90.) _error_("Standard parallel sp=%g must be between 0 and 90 degrees (in specified hemisphere).",sp);

	/* Run core computations: */
	if (options->GetOption("central_meridian") && options->GetOption("standard_parallel"))
		iret=Kml2Expx(filkml,filexp,sgn,cm,sp);
	else
		iret=Kml2Expx(filkml,filexp,sgn);

	/*Write data: */
	WriteData(RET_OUT,iret);

	/*Clean-up*/
	delete options;
	xfree((void**)&filexp);
	xfree((void**)&filkml);

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

void Kml2ExpUsage(void){
	_printf_(true,"Kml2Exp - kml to exp file conversion module:\n");
	_printf_(true,"\n");
	_printf_(true,"   This module converts a file from kml to exp format.\n");
	_printf_(true,"\n");
	_printf_(true,"   Usage:\n");
	_printf_(true,"      [ret]=Kml2Exp(filexp,filkml,sgn,'param name',param,...);\n");
	_printf_(true,"\n");
	_printf_(true,"      filkml      file name of kml file to be read (char)\n");
	_printf_(true,"      filexp      file name of exp file to be written (char)\n");
	_printf_(true,"      sgn         sign for hemisphere (double, +1 (north) or -1 (south))\n");
	_printf_(true,"\n");
	_printf_(true,"      central_meridian     central meridian (double, optional, but must specify with sp)\n");
	_printf_(true,"      standard_parallel    standard parallel (double, optional, but must specify with cm)\n");
	_printf_(true,"\n");
	_printf_(true,"      ret         return code (non-zero for warning)\n");
	_printf_(true,"\n");
	_printf_(true,"   Examples:\n");
	_printf_(true,"      [ret]=Kml2Exp('file.kml','file.exp', 1);\n");
	_printf_(true,"      [ret]=Kml2Exp('file.kml','file.exp', 1,'central_meridian',45,'standard_parallel',70);\n");
	_printf_(true,"      [ret]=Kml2Exp('file.kml','file.exp',-1,'central_meridian', 0,'standard_parallel',71);\n");
	_printf_(true,"\n");
}

