PageRenderTime 41ms CodeModel.GetById 12ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://alageospatialportal.googlecode.com/
Java | 338 lines | 259 code | 49 blank | 30 comment | 47 complexity | 9b1d52c969443451cba0ccbb7a899d26 MD5 | raw file
  1/**
  2 * ************************************************************************
  3 * Copyright (C) 2010 Atlas of Living Australia All Rights Reserved.
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with the
  7 * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 11 * the specific language governing rights and limitations under the License.
 12 * *************************************************************************
 13 */
 14package org.ala.spatial.util;
 15
 16import java.io.BufferedReader;
 17import java.io.File;
 18import java.io.FileReader;
 19import java.util.Arrays;
 20import org.ala.layers.intersect.SimpleRegion;
 21import org.ala.spatial.analysis.index.LayerFilter;
 22import org.ala.spatial.analysis.service.AlocServiceImpl;
 23import org.ala.spatial.analysis.service.AlocSettings;
 24
 25/**
 26 * Use to run ALOC requests.
 27 * 
 28 * @author Adam
 29 */
 30public class AnalysisJobAloc extends AnalysisJob {
 31
 32    Layer[] layers;
 33    int numberOfGroups;
 34    SimpleRegion region;
 35    LayerFilter[] envelope;
 36    String filename;
 37    String filepath;
 38    String currentPath;
 39    int cells;
 40    long[] stageTimes;
 41    public String area;
 42    String envlist;
 43    int layerCount;
 44    String resolution;
 45
 46    public AnalysisJobAloc(String pid, String currentpath_, String envlist_, int numberofgroups_, SimpleRegion region_, LayerFilter[] envelope_, String area_, String resolution_) {
 47        super(pid);
 48        currentPath = currentpath_;
 49        numberOfGroups = numberofgroups_;
 50        //layers = layers_;
 51        envlist = envlist_;
 52        region = region_;
 53        envelope = envelope_;
 54        area = area_;
 55        resolution = resolution_;
 56
 57        layerCount = envlist.split(":").length;
 58        
 59        if (region != null) {
 60            cells = (int) Math.ceil((region.getWidth() / Double.parseDouble(resolution))
 61                    * (region.getHeight() / Double.parseDouble(resolution)));
 62        } else {
 63            cells = 1000000; //or something
 64        }
 65        
 66        stageTimes = new long[4];
 67
 68        setStage(0);
 69        setProgress(0);
 70    }
 71
 72    void exportResults() {
 73        setStage(3); //exporting results
 74
 75        String line;
 76
 77        try {
 78            //get extents from aloc run
 79            StringBuffer extents = new StringBuffer();
 80            BufferedReader br = new BufferedReader(new FileReader(filepath + "extents.txt"));
 81            int width = Integer.parseInt(br.readLine());
 82            int height = Integer.parseInt(br.readLine());
 83            double xmin = Double.parseDouble(br.readLine());
 84            double ymin = Double.parseDouble(br.readLine());
 85            double xmax = Double.parseDouble(br.readLine());
 86            double ymax = Double.parseDouble(br.readLine());
 87            br.close();
 88            br = new BufferedReader(new FileReader(filepath + "extents.txt"));
 89            while ((line = br.readLine()) != null) {
 90                extents.append(line).append("\n");
 91            }
 92            br.close();
 93            setProgress(0.2);
 94
 95            CoordinateTransformer.generateWorldFiles(filepath, "aloc",
 96                    String.valueOf((xmax - xmin) / (double) width),
 97                    "-" + String.valueOf((ymax - ymin) / (double) height),
 98                    String.valueOf(xmin),
 99                    String.valueOf(ymax));
100            setProgress(0.3);
101
102            String outputfile = CoordinateTransformer.transformToGoogleMercator(filepath + "aloc.png");
103            setProgress(0.4);
104
105            AnalysisJobMaxent.readReplace(filepath + "classification.html", "<insert job number here>", getName());
106
107            /* register with LayerImgService */
108            StringBuffer legend = new StringBuffer();
109            BufferedReader flegend = new BufferedReader(new FileReader(filepath + "classification_means.csv"));
110            while ((line = flegend.readLine()) != null) {
111                legend.append(line);
112                legend.append("\r\n");
113            }
114            flegend.close();
115
116            StringBuffer metadata = new StringBuffer();            
117            BufferedReader fmetadata = new BufferedReader(new FileReader(filepath + "classification.html"));
118            while ((line = fmetadata.readLine()) != null) {
119                metadata.append(line);
120                metadata.append("\n");
121            }
122            fmetadata.close();
123
124            // generate the readme.txt file
125            CitationService.generateClassificationReadme(filepath, "Classification");
126
127            setProgress(0.5);
128
129            //publish layer
130            String url = (String) AlaspatialProperties.getGeoserverUrl()
131                    + "/rest/workspaces/ALA/coveragestores/aloc_"
132                    + getName()
133                    + "/file.arcgrid?coverageName=aloc_"
134                    + getName();
135            String extra = "";
136            String username = (String) AlaspatialProperties.getGeoserverUsername();
137            String password = (String) AlaspatialProperties.getGeoserverPassword();
138            String[] infiles = {filepath + "aloc.asc", filepath + "aloc.prj"};
139            String ascZipFile = filepath + getName() + ".asc.zip";
140            Zipper.zipFiles(infiles, ascZipFile);
141
142            // Upload the file to GeoServer using REST calls
143            UploadSpatialResource.loadResource(url, extra, username, password, ascZipFile);
144
145            //Create style
146            url = (String) AlaspatialProperties.getGeoserverUrl() + "/rest/styles/";
147            UploadSpatialResource.loadCreateStyle(url, extra, username, password, "aloc_" + getName());
148
149            //Upload sld
150            url = (String) AlaspatialProperties.getGeoserverUrl() + "/rest/styles/aloc_" + getName();
151            UploadSpatialResource.loadSld(url, extra, username, password, filepath + "aloc.sld");
152
153            //Apply style
154            String data = "<layer><enabled>true</enabled><defaultStyle><name>aloc_" + getName() + "</name></defaultStyle></layer>";
155            url = (String) AlaspatialProperties.getGeoserverUrl() + "/rest/layers/ALA:aloc_" + getName();
156            UploadSpatialResource.assignSld(url, extra, username, password, data);
157
158            setProgress(1);
159        } catch (Exception e) {
160            e.printStackTrace();
161        }
162    }
163
164    @Override
165    public long getEstimate() {
166        //if(getProgress() == 0) return 0;
167
168        if (getStage() < 0 || getStage() >= stageTimes.length) {
169            return 0;
170        }
171
172        long timeElapsed;
173        long t1 = 0, t2 = 0, t3 = 0, t4 = 0;
174        double prog;
175        long progTime;
176        synchronized (progress) {
177            progTime = progressTime;
178            timeElapsed = progressTime - stageTimes[getStage()];
179            prog = progress;
180        }
181        long timeRemaining = 0;
182
183        if (stage <= 0) { //data load; 0 to 0.2
184            if (prog > 0.2) {
185                t1 = (long) (timeElapsed * (.2 - prog) / prog); //projected
186            }
187            if (t1 <= 0 || prog <= 0.2) {
188                t1 = (long) (cells * AlaspatialProperties.getAnalysisAlocEstimateMult0() * layerCount
189                        + AlaspatialProperties.getAnalysisAlocEstimateAdd0()); //default
190            }
191        }
192        if (stage <= 2) { //seeding and iterations
193            if (prog > 0.22) {
194                t2 = (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult1()) * (double) layerCount * numberOfGroups
195                        + AlaspatialProperties.getAnalysisAlocEstimateAdd1())
196                        + (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult2()) * (double) numberOfGroups * layerCount * layerCount
197                        + AlaspatialProperties.getAnalysisAlocEstimateAdd2()); //default
198                t2 = t2 + progTime - stageTimes[1];
199            }
200            if (t2 <= 0 || prog <= 0.22) {
201                t2 = (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult1()) * (double) layerCount * numberOfGroups
202                        + AlaspatialProperties.getAnalysisAlocEstimateAdd1())
203                        + (long) ((cells * AlaspatialProperties.getAnalysisAlocEstimateMult2()) * (double) numberOfGroups * layerCount * layerCount
204                        + AlaspatialProperties.getAnalysisAlocEstimateAdd2()); //default
205            }
206        }
207        if (stage <= 3) { //transforming data; 0.9 to 1.0
208            if (prog > 0.9) {
209                //t4 = (long) (timeElapsed  * (.1 - (prog-.9))/(prog-.9)); //projected
210                t4 = (long) (2000 * AlaspatialProperties.getAnalysisAlocEstimateMult3()); //default
211                t4 = t4 + progTime - stageTimes[3];
212            }
213            if (t4 <= 0 || prog <= 0.9) {
214                t4 = (long) (2000 * AlaspatialProperties.getAnalysisAlocEstimateMult3()); //default
215            }
216        }
217
218        timeRemaining = t1 + t2 + t3 + t4;
219        return smoothEstimate(timeRemaining);
220    }
221
222    @Override
223    public void setProgress(double d) {
224        if (stage == 0) { //data load; 0 to 0.2
225            progress = d / 5;
226        } else if (stage == 1 || stage == 2) { //seeding and iterations; 0.2 to 0.9
227            progress = 0.2 + d * 7 / 10;
228        } else {    //transforming data; 0.9 to 1.0
229            progress = 0.9 + d / 10;
230        }
231        super.setProgress(progress);
232    }
233
234    @Override
235    public String getStatus() {
236        if (getProgress() < 1) {
237            String msg;
238            if (stage == 0) { //data load; 0 to 0.2
239                msg = "Data preparation, ";
240            } else if (stage == 1) { //seeding; 0.2 to 0.3
241                msg = "Running ALOC, ";
242            } else if (stage == 2) { //iterations; 0.3 to 0.9
243                msg = "Running ALOC, ";
244            } else {    //transforming data; 0.9 to 1.0
245                msg = "Exporting results, ";
246            }
247            return msg + "est remaining: " + getEstimateInMinutes() + " min";
248        } else {
249            if (stage == -1) {
250                return "not started, est: " + getEstimateInMinutes() + " min";
251            } else {
252                return "finished, total run time=" + Math.round(getRunTime() / 1000) + "s";
253            }
254        }
255    }
256
257    @Override
258    public void setStage(int i) {
259        super.setStage(i);
260
261        if (i < 4) {
262            stageTimes[i] = System.currentTimeMillis();
263        }
264    }
265
266    public void setCells(int i) {
267        cells = i;
268    }
269
270    public String toString() {
271        StringBuffer sb = new StringBuffer();
272        sb.append(getName());
273        sb.append("; Classification");
274        sb.append("; state=").append(getCurrentState());
275        sb.append("; status=").append(getStatus());
276        sb.append("; grid cell count=").append(cells);
277        sb.append("; number of groups=").append(numberOfGroups);
278        sb.append("; number of layers=").append(layerCount);
279
280        return sb.toString();
281    }
282
283    public String getImage() {
284        return "output/aloc/" + getName() + "/t_aloc.png";
285    }
286
287    public void run() {
288        try {
289            long start = System.currentTimeMillis();
290
291            setCurrentState(RUNNING);
292
293            setStage(0);
294
295            filepath = currentPath + "output" + File.separator + "aloc" + File.separator + getName() + File.separator;
296            filename = filepath + "aloc.png";
297            File fDir = new File(filepath);
298            fDir.mkdir();
299
300            String[] envnameslist = envlist.split(":");
301            String cutDataPath = GridCutter.cut2(envnameslist, resolution, region, envelope, null);
302
303            AlocSettings msets = new AlocSettings();
304            msets.setAlocPath(AlaspatialProperties.getAnalysisAlocCmd());
305            msets.setEnvList(Arrays.asList(envnameslist));
306            msets.setNumberOfGroups(numberOfGroups);
307            msets.setEnvPath(cutDataPath);          //use (possibly) cut layers
308            msets.setOutputPath(currentPath + "output" + File.separator + "aloc" + File.separator + getName() + File.separator);
309
310            AlocServiceImpl aloc = new AlocServiceImpl();
311            aloc.setAlocSettings(msets);
312            System.out.println("To run: " + msets.toString());
313            setStage(1);
314            setProgress(0, "running Aloc");
315            int exitValue = aloc.process(this);
316            System.out.println("Completed: " + exitValue);
317            setProgress(1, "Aloc finished with exit value=" + exitValue);
318
319            if (isCancelled()) {
320                return;
321            }
322
323            exportResults();
324
325            long end = System.currentTimeMillis();
326            setRunTime(end - start);
327
328            setCurrentState(SUCCESSFUL);
329
330            //write out infor for adjusting input parameters
331            System.out.println("ALOC:" + cells + "," + numberOfGroups + "," + layerCount + "," + resolution + " " + (stageTimes[1] - stageTimes[0]) + " " + (stageTimes[2] - stageTimes[0]) + " " + (stageTimes[3] - stageTimes[2]) + " " + (end - stageTimes[3]));
332        } catch (Exception e) {
333            setProgress(1, "failed: " + e.toString());
334            setCurrentState(FAILED);
335            e.printStackTrace();
336        }
337    }
338}