package org.gcube.dataanalysis.seadatanet; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; import org.gcube.dataanalysis.ecoengine.datatypes.ColumnType; import org.gcube.dataanalysis.ecoengine.datatypes.DatabaseType; import org.gcube.dataanalysis.ecoengine.datatypes.InputTable; import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType; import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType; import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes; import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.TableTemplates; import org.gcube.dataanalysis.ecoengine.interfaces.StandardLocalExternalAlgorithm; import org.gcube.dataanalysis.ecoengine.utils.DatabaseFactory; import org.gcube.dataanalysis.ecoengine.utils.DatabaseUtils; import org.hibernate.SessionFactory; import weka.classifiers.trees.m5.Impurity; public class SeaDataNetConnector extends StandardLocalExternalAlgorithm { //Statistical result by DIVA LinkedHashMap statResultMap = new LinkedHashMap(); //HashMap for LinkedHashMap outputDivaMap = new LinkedHashMap(); SessionFactory dbconnection; @Override public void init() throws Exception { // TODO Auto-generated method stub } @Override public String getDescription() { // TODO Auto-generated method stub return "Connector for the SeaDataNet infrastructure"; } File outputfile; @Override protected void process() throws Exception { String outpath = config.getPersistencePath(); File neofile = new File(outpath,"seadn_diva_"+UUID.randomUUID()+".nc"); /* BufferedWriter fileWriter = new BufferedWriter(new FileWriter(neofile)); fileWriter.write("test test"); fileWriter.close(); */ //outputfile=neofile; AnalysisLogger.getLogger().debug("Input Table: "+config.getParam("InputTable")); AnalysisLogger.getLogger().debug("Input Long: "+config.getParam("Longitude")); AnalysisLogger.getLogger().debug("Input Lat: "+config.getParam("Latitude")); AnalysisLogger.getLogger().debug("Input Qt: "+config.getParam("Quantity")); AnalysisLogger.getLogger().debug("Longitude min X: "+config.getParam("LongitudeMinValue")); AnalysisLogger.getLogger().debug("Longitude max X: "+config.getParam("LongitudeMaxValue")); AnalysisLogger.getLogger().debug("Longitude resolution: "+config.getParam("LongitudeResolution")); AnalysisLogger.getLogger().debug("Latitude min Y: "+config.getParam("LatitudeMinValue")); AnalysisLogger.getLogger().debug("Latitude max Y: "+config.getParam("LatitudeMaxValue")); AnalysisLogger.getLogger().debug("Latitude resolution: "+config.getParam("LatitudeResolution")); AnalysisLogger.getLogger().debug("Correlation length: "+config.getParam("CorrelationLength")); AnalysisLogger.getLogger().debug("Signal noise value: "+config.getParam("SignalNoise")); AnalysisLogger.getLogger().debug("Depth Level: "+config.getParam("DepthLevel")); try{ double longitudeMinValue = Double.parseDouble(config.getParam("LongitudeMinValue")); }catch(Exception e){ new Exception ("Bad value for LongitudeMinValue"); } dbconnection = DatabaseUtils.initDBSession(config); String query = "select "+config.getParam("Longitude")+","+ config.getParam("Latitude")+","+config.getParam("Quantity") + " From " + getInputParameter("InputTable")+" ORDER BY RANDOM() limit 1000"; AnalysisLogger.getLogger().debug("Query: "+query); List dataList = DatabaseFactory.executeSQLQuery(query , dbconnection); File fileForDiva= new File(outpath,"file_for_diva_"+UUID.randomUUID()+".txt"); BufferedWriter fileWriterDiva = new BufferedWriter(new FileWriter(fileForDiva)); AnalysisLogger.getLogger().debug("writing input file in: "+fileForDiva.getAbsolutePath() ); for(Object o : dataList){ Object[] oarray = (Object[]) o; fileWriterDiva.write(" "+oarray[0]+" "+oarray[1]+" "+oarray[2]+"\n"); } fileWriterDiva.close(); //integration DivaHttpClient //UPLOADFILE for DIVA DivaHTTPClient neo= new DivaHTTPClient(); DivaFilePostResponse response=neo.uploadFile(fileForDiva); AnalysisLogger.getLogger().debug("Server Response: "+response.getSessionid()); try{ //ANALYSIS Double correlationVal = Double.parseDouble(config.getParam("CorrelationLength")); Double signalNoiseVal = Double.parseDouble(config.getParam("SignalNoise")); Double longMinVal = Double.parseDouble(config.getParam("LongitudeMinValue")); if(longMinVal<-180) throw new Exception("Longitudine minumum value is less than -180."); Double longMaxVal = Double.parseDouble(config.getParam("LongitudeMaxValue")); if(longMaxVal>180) throw new Exception("Longitudine maximum value is more than 180."); Double longResolutionVal = Double.parseDouble(config.getParam("LongitudeResolution")); Double latMinVal = Double.parseDouble(config.getParam("LatitudeMinValue")); if(latMinVal<-90) throw new Exception("Latitude minumum value is less than -90."); Double latMaxVal = Double.parseDouble(config.getParam("LatitudeMaxValue")); if(latMaxVal>90) throw new Exception("Latitude maximum value is more than 90."); Double latResolutionVal = Double.parseDouble(config.getParam("LatitudeResolution")); Double depthLevelVal = Double.parseDouble(config.getParam("DepthLevel")); if((depthLevelVal<-10)||(depthLevelVal>10)) throw new Exception("Depth Level is out of range [-10,10]"); DivaAnalysisGetResponse respAnalysis = neo.getAnalysis(response.getSessionid(), correlationVal, signalNoiseVal, longMinVal, longMaxVal, longResolutionVal, latMinVal, latMaxVal, latResolutionVal, depthLevelVal); statResultMap.put("Minimum value of the analysis",""+respAnalysis.getVmin()); statResultMap.put("Maximum value of the analysis", ""+respAnalysis.getVmax()); statResultMap.put("Number of observations used", ""+respAnalysis.getStat_obs_count_used()); statResultMap.put("Posterior probability of the model", ""+respAnalysis.getStat_posteriori_stn()); //DOWNLOAD FILE neo.downloadFileDiva(neo.WEB_HTTP+neo.DOWN_FILE_NC+respAnalysis.getIdentifier(), neofile.getAbsolutePath()); } catch(Exception e){ throw e; } finally{ neofile.delete(); AnalysisLogger.getLogger().debug("file deleted");; } } @Override protected void setInputParameters() { List templates = new ArrayList(); templates.add(TableTemplates.GENERIC); InputTable tinput = new InputTable(templates, "InputTable", "Input tabular resource"); inputs.add(tinput); ColumnType p1 = new ColumnType("InputTable", "Longitude", "The column containing longitude decimal values", "longitude", false); ColumnType p2 = new ColumnType("InputTable", "Latitude", "The column containing latitude decimal values", "latitude", false); ColumnType p3 = new ColumnType("InputTable", "Quantity", "The column containing quantity values", "quantity", false); inputs.add(p1); inputs.add(p2); inputs.add(p3); PrimitiveType p4 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "LongitudeMinValue", "Minimum value of Longitude Range","-180"); PrimitiveType p5 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "LongitudeMaxValue", "Maximum value of Longitude Range","180"); PrimitiveType p6 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "LongitudeResolution", "Longitude resolution (minimum 0.1 - maximum 10)", "1"); PrimitiveType p7 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "LatitudeMinValue", "Minimum value of Latitude Range","-85"); PrimitiveType p8 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "LatitudeMaxValue", "Maximum value of Latitude Range","85"); PrimitiveType p9 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "LatitudeResolution", "Latitude resolution (minimum 0.1 - maximum 10)", "1"); PrimitiveType p10 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "CorrelationLength", "Correlation length (arc degrees)", "10.35"); PrimitiveType p11 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "SignalNoise", "Signal to noise ratio (non-dimensional)", "1.08"); PrimitiveType p12 = new PrimitiveType(Double.class.getName(), null, PrimitiveTypes.NUMBER, "DepthLevel", "Depth level", "0"); inputs.add(p4); inputs.add(p5); inputs.add(p6); inputs.add(p7); inputs.add(p8); inputs.add(p9); inputs.add(p10); inputs.add(p11); inputs.add(p12); DatabaseType.addDefaultDBPars(inputs); } @Override public void shutdown() { if (dbconnection!=null) dbconnection.close(); } public StatisticalType getOutput(){ PrimitiveType file = new PrimitiveType(File.class.getName(), outputfile, PrimitiveTypes.FILE, "NetCDFOutputFile", "Output file in NetCDF format"); for (String key:statResultMap.keySet()){ String value = statResultMap.get(key); PrimitiveType val = new PrimitiveType(String.class.getName(),value, PrimitiveTypes.STRING, key, key); outputDivaMap.put(key, val); } outputDivaMap.put("Netcdf output file", file); PrimitiveType hashma = new PrimitiveType(HashMap.class.getName(), outputDivaMap , PrimitiveTypes.MAP, "Diva results","Output of DIVA fit"); return hashma; } }