PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/alaspatial/src/main/java/org/ala/spatial/util/AnalysisJobSampling.java

http://alageospatialportal.googlecode.com/
Java | 187 lines | 137 code | 35 blank | 15 comment | 20 complexity | 140f7bb4d0a3bd93eff842ebf77e5743 MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package org.ala.spatial.util;
  6
  7import java.io.File;
  8import java.util.ArrayList;
  9import java.util.Iterator;
 10import java.util.Vector;
 11import org.ala.spatial.analysis.index.LayerFilter;
 12import org.ala.spatial.analysis.index.OccurrenceRecordNumbers;
 13import org.ala.spatial.analysis.index.OccurrencesCollection;
 14import org.ala.spatial.analysis.service.FilteringService;
 15import org.ala.spatial.analysis.service.SamplingService;
 16
 17/**
 18 *
 19 * @author Adam
 20 */
 21public class AnalysisJobSampling extends AnalysisJob {
 22
 23    String[] layers;
 24    int numberOfGroups;
 25    SimpleRegion region;
 26    LayerFilter[] envelope;
 27    String species;
 28    ArrayList<OccurrenceRecordNumbers> records;
 29    long[] stageTimes;
 30    String envlist;
 31    String area;
 32    String currentPath;
 33    int points;
 34
 35    public AnalysisJobSampling(String pid, String currentPath_, String species_, String envlist_, String area_) {
 36        super(pid);
 37        species = species_;
 38        currentPath = currentPath_;
 39        envlist = envlist_;
 40        area = area_;
 41
 42        layers = getLayerFiles(envlist);
 43        records = null;
 44        region = null;
 45        if (area != null && area.startsWith("ENVELOPE")) {
 46            records = FilteringService.getRecords(area);
 47        } else {
 48            region = SimpleShapeFile.parseWKT(area);
 49        }
 50
 51        //TODO: update for new occurrencescollection
 52        //points = OccurrencesService.getSpeciesCount(species_);
 53
 54        stageTimes = new long[4];
 55    }
 56
 57    @Override
 58    public void run() {
 59        try {
 60            long start = System.currentTimeMillis();
 61
 62            setCurrentState(RUNNING);
 63            setStage(0);
 64
 65            SpatialSettings ssets;
 66            ssets = new SpatialSettings();
 67
 68            SamplingService ss = SamplingService.newForLSID(species);
 69            String datafile = ss.sampleSpeciesAsCSV(species, layers, region, records, ssets.getInt("max_record_count_download"), this);
 70
 71            Vector<String> vFiles = new Vector<String>();
 72            vFiles.add(datafile);
 73
 74            String[] files = (String[]) vFiles.toArray(new String[vFiles.size()]);
 75
 76            Iterator it = vFiles.iterator();
 77            while (it.hasNext()) {
 78                System.out.println("Adding to download: " + it.next());
 79            }
 80
 81            String outputpath = currentPath + File.separator + "output" + File.separator + "sampling" + File.separator;
 82            File fDir = new File(outputpath);
 83            fDir.mkdir();
 84
 85            String[] n = OccurrencesCollection.getFirstName(species);
 86            String speciesName = "";
 87            if (n != null) {
 88                speciesName = n[0];
 89            }
 90            String outfile = fDir.getAbsolutePath() + File.separator + speciesName.replaceAll(" ", "_") + "_sample_" + getName() + ".zip";
 91            Zipper.zipFiles(files, outfile);
 92
 93            //return "/output/sampling/" + species.replaceAll(" ", "_") + "_sample_" + getName() + ".zip";
 94
 95            long end = System.currentTimeMillis();
 96            setRunTime(end - start);
 97
 98            setCurrentState(SUCCESSFUL);
 99
100            //write out infor for adjusting input parameters
101            System.out.println("Sampling:" + " " + (end - start));
102        } catch (Exception e) {
103            setProgress(1, "failed: " + e.getMessage());
104            setCurrentState(FAILED);
105            e.printStackTrace();
106        }
107    }
108
109    @Override
110    public long getEstimate() {
111        if (getProgress() == 0) {
112            return 0;
113        }
114
115        long timeElapsed;
116        long t1 = 0;
117        double prog;
118        synchronized (progress) {
119            timeElapsed = progressTime - stageTimes[getStage()];
120            prog = progress;
121        }
122        long timeRemaining = 0;
123
124
125        if (prog > 0) {
126            t1 = (long) (timeElapsed * (.2 - prog) / prog); //projected
127        }
128        if (t1 <= 0 || prog <= 0) {
129            t1 = (long) (1000); //default
130        }
131
132        timeRemaining = t1;
133        return smoothEstimate(timeRemaining);
134    }
135
136    @Override
137    public String getStatus() {
138        if (getProgress() < 1) {
139            return "est remaining: " + getEstimateInMinutes() + " min";
140        } else {
141            if (stage == -1) {
142                return "not started, est: " + getEstimateInMinutes() + " min";
143            } else {
144                return "finished, total run time=" + Math.round(getRunTime() / 1000) + "s";
145            }
146        }
147    }
148
149    @Override
150    public void setStage(int i) {
151        super.setStage(i);
152
153        if (i < 4) {
154            stageTimes[i] = System.currentTimeMillis();
155        }
156    }
157
158    public String toString() {
159        StringBuffer sb = new StringBuffer();
160        sb.append(getName());
161        sb.append("; Sampling");
162        sb.append("; state=").append(getCurrentState());
163        sb.append("; status=").append(getStatus());
164        sb.append("; species=").append(species);
165        //sb.append("; layers list=").append(envlist);
166        //sb.append("; area=").append(area);
167
168        return sb.toString();
169    }
170
171    private String[] getLayerFiles(String envNames) {
172        if (envNames.equals("none")) {
173            return null;
174        }
175        String[] nameslist = envNames.split(":");
176        String[] pathlist = new String[nameslist.length];
177
178        //System.out.println("Got envlist.count: " + nameslist.length);
179
180        for (int j = 0; j < nameslist.length; j++) {
181            pathlist[j] = Layers.layerDisplayNameToName(nameslist[j]);
182
183        }
184
185        return pathlist;
186    }
187}