/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import csip.ServiceException;
import csip.SessionLogger;
import database.WindgenClimateDb;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import m.climate.cligen_prism.WeppClimate;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author Brad
 */
public class Utils {

    /**
     *
     * @param db
     * @param latitude
     * @param longitude
     * @param LOG session log
     * @return
     * @throws ServiceException
     * @throws Exception
     */
    public static StationResult getCliStation(WindgenClimateDb db, double latitude, double longitude, SessionLogger LOG) throws ServiceException, Exception {
	StationResult climateStation;

	//TODO:  If NRCS decides to no longer use the weps_cli_geom table the db.cliGeomIntersect() call below needs to be removed.           
	climateStation = db.cliGeomIntersect(latitude, longitude);

	if (climateStation != null) {
	    LOG.info("The cliStation name is '" + climateStation.name + "'");
	    if (climateStation.name.equalsIgnoreCase("Out")) {
		// in the mountain west polygon - use the nearest cligen station
		climateStation = db.findCligenStation(latitude, longitude);
		LOG.info("set cligen station 1=" + climateStation.name);
	    } else {
		// use the cli station found by intersecting the climate station geometries
		//climateStation = climateStation;
		LOG.info("set cligen station 2=" + climateStation.name);
	    }
	} else {
	    // get climate station for this lat / long
	    if (LOG.isLoggable(Level.FINE)) {
		LOG.fine("FIND NEAREST CLIGEN STATION!");
		LOG.fine("lat=" + latitude);
		LOG.fine("long=" + longitude);
	    }
	    // not in the mountain west polygon, did not intersect any climate station, use the nearest station
	    climateStation = db.findCligenStation(latitude, longitude);
	    LOG.fine("set cligen station 3=" + climateStation.name);

	}

	return climateStation;
    }
}
