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