private boolean compareTimeLists(List<DateTime> timesA, List<DateTime> timesB) {
    if (timesA.size() != timesB.size()) return false;

    Collections.sort(timesA);
    Collections.sort(timesB);
    for (int i = 0; i < timesA.size(); i++) {
      DateTime a = timesA.get(i);
      DateTime b = timesB.get(i);

      double test = Math.abs(a.getValue() - b.getValue());
      if (test > 65.0) return false;
      // System.out.println("TTTTTTT =  " + test );
    }
    return true;
  }
 /**
  * Construct a new radar data source.
  *
  * @param descriptor descriptor for this datasource
  * @param sources List of sources of data (filenames, URLs)
  * @param description Description of the files
  * @param properties hashtable of properties. Includes radar location
  * @throws VisADException couldn't create the data
  */
 public RadarDataSource(
     DataSourceDescriptor descriptor, List sources, String description, Hashtable properties)
     throws VisADException {
   super(
       descriptor,
       sources,
       (sources.size() == 1) ? (String) sources.get(0) : description,
       description,
       properties);
 }
Esempio n. 3
0
 /**
  * Override the init method for when this data source is unpersisted. We simply check the
  * imageList to see if this object came from a legacy bundle.
  */
 public void initAfterUnpersistence() {
   super.initAfterUnpersistence();
   List tmp = getTmpPaths();
   if (tmp != null) {
     List obs = getRDS().getSoundingObs();
     AddeSoundingAdapter asa = (AddeSoundingAdapter) getRDS().getSoundingAdapter();
     for (int i = 0; i < tmp.size(); i += 2) {
       SoundingOb ob = (SoundingOb) obs.get(i / 2);
       ob.setMandatoryFile(tmp.get(i).toString());
       ob.setMandatoryFile(tmp.get(i + 1).toString());
     }
   }
 }
Esempio n. 4
0
 /**
  * _Save the remote data to local disk
  *
  * @param prefix Where to write the files to
  * @param loadId For the JobManager dialog
  * @param changeLinks Should we change the internal data references
  * @return List of files we wrote
  * @throws Exception On badness
  */
 protected List saveDataToLocalDisk(String prefix, Object loadId, boolean changeLinks)
     throws Exception {
   List urls = new ArrayList();
   List obs = getRDS().getSoundingObs();
   AddeSoundingAdapter asa = (AddeSoundingAdapter) getRDS().getSoundingAdapter();
   for (int i = 0; i < obs.size(); i++) {
     SoundingOb ob = (SoundingOb) obs.get(i);
     urls.add(asa.getMandatoryURL(ob) + "&rawstream=true");
     urls.add(asa.getSigURL(ob) + "&rawstream=true");
   }
   List newFiles = IOUtil.writeTo(urls, prefix, "raob", loadId);
   if (newFiles == null) {
     return null;
   }
   if (changeLinks) {
     for (int i = 0; i < newFiles.size(); i += 2) {
       SoundingOb ob = (SoundingOb) obs.get(i / 2);
       ob.setMandatoryFile(newFiles.get(i).toString());
       ob.setSigFile(newFiles.get(i + 1).toString());
     }
   }
   return newFiles;
 }
Esempio n. 5
0
 /**
  * Get the list of urls from the remote server
  *
  * @return List of urls
  */
 public List getDataPaths() {
   List paths = new ArrayList();
   AddeSoundingAdapter asa = (AddeSoundingAdapter) getRDS().getSoundingAdapter();
   List obs = getRDS().getSoundingObs();
   for (int i = 0; i < obs.size(); i++) {
     SoundingOb ob = (SoundingOb) obs.get(i);
     if (ob.getMandatoryFile() != null) {
       // file based
       paths.add(ob.getMandatoryFile());
       paths.add(ob.getSigFile());
     } else {
       paths.add(asa.getMandatoryURL(ob));
       paths.add(asa.getSigURL(ob));
     }
   }
   return paths;
 }
  /**
   * Create the list of times associated with this DataSource.
   *
   * @return list of times.
   */
  protected List doMakeDateTimes() {
    List times = new ArrayList();
    boolean isRealTime = isRealTime();
    List<RadarAdapter> adapters = getAdapters();
    for (int i = 0; i < adapters.size(); i++) {
      if (!isRealTime) {
        times.add(((RadarAdapter) adapters.get(i)).getBaseTime());
      } else {
        String name = i + "th most recent";
        if (i == 0) {
          name = "Most recent";
        }
        if ((i > 0) && (i < DataSource.ordinalNames.length)) {
          name = DataSource.ordinalNames[i] + " most recent";
        }
        times.add(new TwoFacedObject(name, i));
      }
    }
    Collections.sort(times);

    return times;
  }
  /**
   * Get the data for the given DataChoice and selection criteria.
   *
   * @param dataChoice DataChoice for selection
   * @param category DataCategory for the DataChoice (not used)
   * @param subset subsetting criteria
   * @param requestProperties extra request properties
   * @return the Data object for the request
   * @throws RemoteException couldn't create a remote data object
   * @throws VisADException couldn't create the data
   */
  protected Data getDataInner(
      final DataChoice dataChoice,
      DataCategory category,
      final DataSelection subset,
      final Hashtable requestProperties)
      throws VisADException, RemoteException {

    try {
      List times = null;
      if (subset != null) {
        times = getTimesFromDataSelection(subset, dataChoice);
      }
      if (times == null) {
        times = dataChoice.getSelectedDateTimes();
      }

      List dtimes = subset.getTimeDriverTimes();

      if (dtimes != null && useDriverTime == false) {
        useDriverTime = true;
      }

      List<RadarAdapter> adapters = getAdapters();

      DateTime[] realDateTimes = new DateTime[adapters.size()];
      for (int i = 0; i < adapters.size(); i++) {
        realDateTimes[i] = ((RadarAdapter) adapters.get(i)).getBaseTime();
      }
      Arrays.sort(realDateTimes);
      // Flip it to get youngest date first
      boolean isRealTime = isRealTime();
      if (isRealTime) {
        realDateTimes =
            (DateTime[]) Misc.reverseArray(realDateTimes, new DateTime[realDateTimes.length]);
      }
      // if use time driver
      if (useDriverTime) {
        List tests = resetTimesList(realDateTimes, times);
        if (!compareTimeLists(times, tests)) {
          reloadData();
          adapters = getAdapters();

          realDateTimes = new DateTime[adapters.size()];
          for (int i = 0; i < adapters.size(); i++) {
            realDateTimes[i] = ((RadarAdapter) adapters.get(i)).getBaseTime();
          }
          Arrays.sort(realDateTimes);
          // Flip it to get youngest date first
          isRealTime = isRealTime();
          if (isRealTime) {
            realDateTimes =
                (DateTime[]) Misc.reverseArray(realDateTimes, new DateTime[realDateTimes.length]);
          }
          tests = resetTimesList(realDateTimes, times);
        }
        times = tests;
      }
      // if times are null, then that means all times
      DateTime[] dateTimes = null;

      if ((times == null) || (times.size() == 0)) {
        dateTimes = realDateTimes;
      } else {
        dateTimes = new DateTime[times.size()];
        for (int i = 0; i < times.size(); i++) {
          Object time = times.get(i);
          if (time instanceof TwoFacedObject) {
            int index = ((Integer) ((TwoFacedObject) time).getId()).intValue();
            dateTimes[i] = realDateTimes[index];
          } else if (time instanceof DateTime) {
            dateTimes[i] = (DateTime) time;
          }
        }
      }
      Arrays.sort(dateTimes);
      final Data[] datas = new Data[dateTimes.length];
      int timeIndex = 0;
      final MathType[] mt = {null};
      // create a new field of (Time -> (radar data)).
      // fill in the times array and data array with dates/data
      // only from those adapters which match the selected times.
      // if a data object is null, stick it in the list.
      // if all are null, then the MathType (mt) will never get set,
      // so return null.
      //            System.err.println ("Reading " + adapters.size() + " radar files");
      int cnt = 0;
      ThreadManager threadManager = new visad.util.ThreadManager("radar data reading");

      for (Iterator iter = adapters.iterator(); iter.hasNext(); ) {
        final RadarAdapter adapter = (RadarAdapter) iter.next();
        timeIndex = Arrays.binarySearch(dateTimes, adapter.getBaseTime());
        //              System.err.println ("timeIndex:" + timeIndex);
        if (timeIndex < 0) {
          continue;
        }
        cnt++;
        LogUtil.message("Time: " + (cnt) + "/" + dateTimes.length + " From:" + toString());
        final int theTimeIndex = timeIndex;
        threadManager.addRunnable(
            new visad.util.ThreadManager.MyRunnable() {
              public void run() throws Exception {
                Trace.call1("RDS.getData");
                Data d = adapter.getData(dataChoice, subset, requestProperties);
                Trace.call2("RDS.getData");
                datas[theTimeIndex] = d;
                if (d != null) {
                  mt[0] = d.getType();
                } else {
                }
              }
            });
      }

      try {
        // threadManager.debug = true;
        threadManager.runInParallel(getDataContext().getIdv().getMaxDataThreadCount());
      } catch (VisADException ve) {
        LogUtil.printMessage(ve.toString());
      }

      if (mt[0] == null) {
        return null;
      }

      FunctionType ft = new FunctionType(RealType.Time, mt[0]);
      SampledSet domainSet =
          (dateTimes.length == 1)
              ? (SampledSet) new SingletonSet(new RealTuple(dateTimes))
              : (SampledSet) DateTime.makeTimeSet(dateTimes);
      FieldImpl fi = new FieldImpl(ft, domainSet);
      fi.setSamples(datas, false);
      return fi;
    } catch (Exception exc) {
      logException("Creating obs", exc);
    }
    return null;
  }
  /**
   * Make the adapters for the given list of files
   *
   * @param files Data files
   * @throws Exception When bad things happen
   */
  private void makeAdapters(List files) throws Exception {
    adapters = new ArrayList<RadarAdapter>();
    Hashtable<String, RadarAdapter> oldAdapterMap = fileToAdapter;
    fileToAdapter = new Hashtable<String, RadarAdapter>();

    int cnt = 0;

    final List<String> badFiles = new ArrayList<String>();
    final List<Exception> badExceptions = new ArrayList<Exception>();

    final List<RadarAdapter> goodAdapters = new ArrayList<RadarAdapter>();
    final List<String> goodFiles = new ArrayList<String>();
    visad.util.ThreadManager threadManager =
        new visad.util.ThreadManager("radar data initialization");
    LogUtil.message("Initializing radar files");
    for (Iterator iter = files.iterator(); iter.hasNext(); ) {
      final String filename = iter.next().toString();
      RadarAdapter adapter = (RadarAdapter) oldAdapterMap.get(filename);
      cnt++;
      if (adapter == null) {
        threadManager.addRunnable(
            new visad.util.ThreadManager.MyRunnable() {
              public void run() throws Exception {
                try {
                  RadarAdapter myAdapter = makeRadarAdapter(filename);
                  synchronized (goodAdapters) {
                    goodAdapters.add(myAdapter);
                    goodFiles.add(filename);
                  }
                } catch (Exception e) {
                  synchronized (badExceptions) {
                    badExceptions.add(e);
                    badFiles.add(filename);
                  }
                }
              }
            });
      } else {
        goodAdapters.add(adapter);
        goodFiles.add(filename);
      }
    }

    //        threadManager.debug = true;
    threadManager.runInParallel(getDataContext().getIdv().getMaxDataThreadCount());
    LogUtil.message("");

    for (int i = 0; i < goodAdapters.size(); i++) {
      adapters.add(goodAdapters.get(i));
      fileToAdapter.put(goodFiles.get(i), goodAdapters.get(i));
    }

    if (!badFiles.isEmpty()) {
      StringBuffer buf = new StringBuffer();
      if (badFiles.size() < files.size()) {
        buf.append("<html>");
        buf.append("There were problems reading these files:");
        buf.append("<ul>");
        for (Iterator iterator = badFiles.iterator(); iterator.hasNext(); ) {
          buf.append("<li>");
          buf.append((String) iterator.next());
          buf.append("</li>");
        }
        buf.append("</ul>");
        buf.append("<p>Continue loading good data?<p></html>");
        boolean ok = ucar.unidata.util.GuiUtils.askYesNo("Error reading data", buf.toString());

        badExceptions.get(0).printStackTrace();
        if (ok) {
          files.removeAll(badFiles);
        } else {
          throw new VisADException("error reading files");
        }
      } else {
        throw badExceptions.get(0);
      }
    }
    // clean up any old adapters
    for (String source : oldAdapterMap.keySet()) {
      if (fileToAdapter.get(source) == null) {
        ((RadarAdapter) oldAdapterMap.get(source)).doRemove();
      }
    }
    oldAdapterMap = null;
  }