001package com.astrolabsoftware.FinkBrowser.Januser;
002
003import com.Lomikel.Utils.LomikelException;
004import com.astrolabsoftware.FinkBrowser.FinkPortalClient.FPC;
005
006// org.json
007import org.json.JSONArray;
008import org.json.JSONObject;
009
010// Java
011import java.util.Set;
012import java.util.TreeSet;
013import java.util.Map;
014import java.util.TreeMap;
015
016// Log4J
017import org.apache.logging.log4j.Logger;
018import org.apache.logging.log4j.LogManager;
019
020/** <code>FinkPortalClassifier</code> classifies sources according to
021  * <a href="https://api.fink-portal.org/api">Fink Portal</a> REST service.
022  * @opt attributes
023  * @opt operations
024  * @opt types
025  * @opt visibility
026  * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
027public class FinkPortalClassifier implements Classifier {
028  
029  @Override
030  public void classify(FinkGremlinRecipies recipies,
031                       String              oid,
032                       boolean             enhance,
033                       String              columns) throws LomikelException {
034    JSONArray ja;
035    JSONObject jo;
036    Map<String, Set<Double>> classes; // cl -> [jd]
037    String cl;
038    double jd;
039    Set<Double> jds;
040    String key;
041    Set<Double> val;
042    double weight;
043    double totalWeight;
044    ja = FPC.objects(new JSONObject().put("objectId",      oid   ).
045                                      put("output-format", "json"));
046    classes =  new TreeMap<>();
047    // get all alerts (jd) and their classes
048    for (int i = 0; i < ja.length(); i++) {
049      jo = ja.getJSONObject(i);
050      cl = jo.getString("v:classification");
051      jd = jo.getDouble("i:jd");
052      if (!cl.equals("Unknown")) {
053        if (classes.containsKey(cl)) {
054          jds = classes.get(cl);
055          jds.add(jd);
056          }
057        else {
058          jds = new TreeSet<Double>();
059          jds.add(jd);
060          classes.put(cl, jds);
061          }
062        }
063      }
064    totalWeight = 0;
065    for (Map.Entry<String, Set<Double>> cls : classes.entrySet()) {
066      totalWeight += cls.getValue().size();
067      }
068    for (Map.Entry<String, Set<Double>> cls : classes.entrySet()) {
069      key = cls.getKey();
070      val = cls.getValue();
071      weight = val.size() / totalWeight;
072      recipies.registerSourcesOfInterest(Classifiers.FINK_PORTAL, key, oid, weight, val, enhance, columns);
073      }
074    }
075
076  /** Logging . */
077  private static Logger log = LogManager.getLogger(FinkPortalClassifier.class);
078  
079  }
080           
081