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

void Exp2KmlUsage(void){/*{{{*/
	_pprintLine_("Exp2Kml - exp to kml file conversion module:");
	_pprintLine_("");
	_pprintLine_("   This module converts a file from exp to kml format.");
	_pprintLine_("");
	_pprintLine_("   Usage:");
	_pprintLine_("      [ret]=Exp2Kml(filexp,filkml,sgn,'param name',param,...);");
	_pprintLine_("");
	_pprintLine_("      filexp      file name of exp file to be read (char)");
	_pprintLine_("      filkml      file name of kml file to be written (char)");
	_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_("      holes       flag for treatment of multiple profiles (char, optional, 'yes' for holes))");
	_pprintLine_("");
	_pprintLine_("      ret         return code (non-zero for warning)");
	_pprintLine_("");
	_pprintLine_("   Examples:");
	_pprintLine_("      [ret]=Exp2Kml('file.exp','file.kml', 1);");
	_pprintLine_("      [ret]=Exp2Kml('file.exp','file.kml', 1,'central_meridian',45,'standard_parallel',70,'holes','yes');");
	_pprintLine_("      [ret]=Exp2Kml('file.exp','file.kml',-1,'central_meridian', 0,'standard_parallel',71,'holes','yes');");
	_pprintLine_("");
}/*}}}*/
WRAPPER(Exp2Kml){

	int i,verbose=1;

	/*input: */
	char    *filexp=NULL,*filkml=NULL;
	int     sgn;

	Options* options=NULL;
	char     *choles=NULL;
	bool     holes=false;
	double   cm=0.,sp=0.;

	/* output: */
	int     iret=0;

	/*Boot module: */
	MODULEBOOT();

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

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

	options->Get(&choles,"holes",(char*)"no");
	if (!strncmp(choles,"y",1) || !strncmp(choles,"on",2)) holes=true;

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

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

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

	/*Clean-up*/
	xDelete<char>(choles);
	xDelete<char>(filkml);
	xDelete<char>(filexp);
	delete options;

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