/***************************** LICENSE START ***********************************

 Copyright 2013 ECMWF and INPE. This software is distributed under the terms
 of the Apache License version 2.0. In applying this license, ECMWF does not
 waive the privileges and immunities granted to it by virtue of its status as
 an Intergovernmental Organization or submit itself to any jurisdiction.

 ***************************** LICENSE END *************************************/

#include "Metview.h"
#include "MvNetCDF.h"

#include "MvRttov.h"

class Base : public MvService { 
protected:
    Base(char* a) : MvService(a) {};
};


class RttovVisualiser: public Base {	
public:
    RttovVisualiser(char* a) : Base(a) {saveToPool(false);};
    void serve(MvRequest&,MvRequest&);	

private:
    bool exportRttovToNetCDF(MvRttov &rttov, string &fileName);
    bool getDataPath(MvRequest &in, const char *dataParamName, const char *dataPathParamName, string &result);
	string getString(MvRequest &in, const char *parameter, const char *defaultVal);
};



// RttovVisualiser::getDataPath
// for user parameters which have a data icon and  the alternative of
// a path icon. Returns the path of the data icon if it's there;
// otherwise thh path parameters if it's there. The path is returned
// in the 'result' string.
// Returns true if it found a path, otherwise false.

bool RttovVisualiser::getDataPath(MvRequest &in, const char *dataParamName, const char *dataPathParamName, string &result)
{
	MvRequest dataR=in(dataParamName);
	const char *dataPath=dataR("PATH");

	if(dataPath)
	{
		result=string(dataPath);
	}
	else
	{
		const char *cval=in(dataPathParamName);
		if(cval)
		{
		  	result=string(cval);
			if (result == "OFF")
				result.clear();
		}	
	}	
	
	if(result.empty())
	{
	  	marslog(LOG_EROR,"No value found for paramaters: %s and %s", dataParamName, dataPathParamName);
		setError(13);
		return false;	
	}
	
	return true;
}



// RttovVisualiser::getString
// Tries to get the given pstring arameter from the request, returning
// a user-specified default if it is not there.

string RttovVisualiser::getString(MvRequest &in, const char *parameter, const char *defaultVal)
{
	const char *val = in(parameter);

	if (!val)
		val = defaultVal;


	return string(val);
}


void RttovVisualiser::serve( MvRequest& in, MvRequest& out)
{
  	cout << "--------------RttovVisualiser::serve()--------------" << endl;
  	in.print();

    
//    const char *verb = in.getVerb();
	string dataPath;
    bool ok;
	const char *defVal = "dummy";


    // get the path to the data from the requst
    ok = getDataPath(in, "RTTOV_DATA", "RTTOV_FILENAME", dataPath);


    // create and initialise an MvRttov object to handle the data file
    MvRttov rttov;
    ok = rttov.parseFile(dataPath);

    if (!ok)
    {
        marslog(LOG_EROR, (char *)(rttov.errorMessage().c_str()));
        setError(13);
        return;
    }


    // export as netCDF
    string outNetCDFFileName(marstmp());
    ok = exportRttovToNetCDF(rttov, outNetCDFFileName);


    // note that we create a 'raw' NETCDF requests rather than a
    // NETCDF_VISUALISER request. This is so as to avoid going through
    // StdAppManager, which can force the caching of the result

	string str = getString(in, "RTTOV_PLOT_TYPE", defVal);

    if (str == "CHANNEL_TB_GRAPH" || str == defVal)
    {
        MvRequest netCDFVisualiserReq("NETCDF_XY_POINTS");
        netCDFVisualiserReq("NETCDF_FILENAME")       = outNetCDFFileName.c_str();
        netCDFVisualiserReq("NETCDF_X_VARIABLE")     = "channel";
        netCDFVisualiserReq("NETCDF_Y_VARIABLE")     = "tb";
        netCDFVisualiserReq("NETCDF_VALUE_VARIABLE") = "tb";
        netCDFVisualiserReq("_VERB")                 = "NETCDF_XY_POINTS";

        out = netCDFVisualiserReq;
    }

    else if (str == "JACOBIAN_MATRIX")
    {
        MvRequest netCDFVisualiserReq("NETCDF_XY_MATRIX");
        netCDFVisualiserReq("NETCDF_FILENAME")       = outNetCDFFileName.c_str();
        netCDFVisualiserReq("NETCDF_X_VARIABLE")     = "channel";
        netCDFVisualiserReq("NETCDF_Y_VARIABLE")     = "pressure";
        netCDFVisualiserReq("NETCDF_VALUE_VARIABLE") = "jacobian";
        netCDFVisualiserReq("_VERB")                 = "NETCDF_XY_MATRIX";

        out = netCDFVisualiserReq;
    }

    else if (str == "JACOBIAN_CHANNEL_CURVE")
    {
        defVal = "1";
	    string channelString = getString(in, "RTTOV_JACOBIAN_CHANNEL", defVal);
        channelString = "channel/" + channelString;

        MvRequest netCDFVisualiserReq("NETCDF_XY_POINTS");
        netCDFVisualiserReq("NETCDF_FILENAME")          = outNetCDFFileName.c_str();
        netCDFVisualiserReq("NETCDF_X_VARIABLE")        = "jacobian";
        netCDFVisualiserReq("NETCDF_Y_VARIABLE")        = "pressure";
        netCDFVisualiserReq("NETCDF_VALUE_VARIABLE")    = "jacobian";
        netCDFVisualiserReq("NETCDF_DIMENSION_SETTING") = channelString.c_str();
        netCDFVisualiserReq("_VERB")                    = "NETCDF_XY_POINTS";

        // currently, NETCDF_XY_POINTS does not honour the order of the
        // values in the y-axis variable, so we have to create a Cartesian
        // View which reverses the axis
        MvRequest viewReq("CARTESIANVIEW");
        viewReq("X_AUTOMATIC")         = "on";
        viewReq("Y_AUTOMATIC")         = "on";
        viewReq("Y_AUTOMATIC_REVERSE") = "on";

        out = netCDFVisualiserReq + viewReq;
    }


    cout << "RttovVisualiser output request:" << endl;
    out.print();
}




bool RttovVisualiser::exportRttovToNetCDF(MvRttov &rttov, string &fileName)
{
    MvNetCDF netcdf(fileName,'w');

    // add the 'channel' variable
    long channelsSize = (long)rttov.channels().size();
    vector<long> channels_dimsize(1, channelsSize);
    vector<string> channels_name(1, "channel");
    MvNcVar *ncChannels = netcdf.addVariable(channels_name[0], ncInt, channels_dimsize, channels_name);
    ncChannels->addAttribute("long_name", "channel");
    ncChannels->put(rttov.channels(), channelsSize);

    // add the 'tb' variable
    long tbsSize = (long)rttov.tbs().size();
    vector<long> tbs_dimsize(1, tbsSize);
    vector<string> tbs_name(1, "tb");
    MvNcVar *ncTbs = netcdf.addVariable(tbs_name[0], ncDouble, tbs_dimsize, tbs_name);
    ncTbs->addAttribute("long_name", "brightness temperature");
    ncTbs->addAttribute("units",     "kelvin");
    ncTbs->put(rttov.tbs(), tbsSize);

    // add the Jacobian pressures
    long jpsSize = (long)rttov.jacobianPressures().size();
    vector<long> jps_dimsize(1, jpsSize);
    vector<string> jps_name(1, "pressure");
    MvNcVar *ncJps = netcdf.addVariable(jps_name[0], ncDouble, jps_dimsize, jps_name);
    ncJps->addAttribute("long_name", "pressure");
    ncJps->addAttribute("units",     "hPa");
    ncJps->put(rttov.jacobianPressures(), jpsSize);


    // add the Jacobians themselves
    vector<long> jvals_dimsize(2);
    vector<string> jacobiansDimNames(2);
    jvals_dimsize[0] = channelsSize;
    jvals_dimsize[1] = jpsSize;
    jacobiansDimNames[0] = "channel";
    jacobiansDimNames[1] = "pressure";
    MvNcVar *ncJVals = netcdf.addVariable("jacobian",ncDouble,jvals_dimsize,jacobiansDimNames);

    for (size_t c = 0; c < channelsSize; c++)
    {
        ncJVals->setCurrent(c);
        ncJVals->put(&(rttov.jacobianValues(c)[0]), 1, jpsSize);
    }


    netcdf.addAttribute("title", "Output from RTTOV");

    netcdf.close();

    return true;
}


int main(int argc, char** argv)
{
    MvApplication theApp( argc, argv );

//    RttovVisualiser rttovOutVis("RTTOV_OUTPUT_FILE");
    RttovVisualiser rttovVisVis("RTTOV_VISUALISER");

    theApp.run();
}
