/**
   * Getting invDataset list for a single radar station.
   *
   * @param stnName radar station name
   * @param start of the time
   * @param end of the time
   * @return list of invDataset
   * @throws IOException java io exception
   */
  private DqcRadarDatasetInfo queryRadarStation(String stnName, Date start, Date end)
      throws IOException {
    // http://motherlode.ucar.edu:9080/thredds/idd/radarLevel2?returns=catalog&stn=KFTG&dtime=latest
    StringBuffer queryb = new StringBuffer();

    queryb.append(dqc.getQuery().getUriResolved().toString());
    queryb.append("serviceType=OPENDAP");
    queryb.append("&stn=" + stnName);
    if ((start == null) && (end == null)) {
      queryb.append("&dtime=all");
    } else {
      String stime = DateUtil.getTimeAsISO8601(start);
      String etime = DateUtil.getTimeAsISO8601(end);
      queryb.append("&dateStart=" + stime);
      queryb.append("&dateEnd=" + etime);
    }

    URI catalogURI;
    try {
      catalogURI = new URI(queryb.toString());
    } catch (java.net.URISyntaxException e) {
      throw new IOException("** MalformedURLException on URL <" + ">\n" + e.getMessage() + "\n");
    }

    InvCatalogFactory factory = new InvCatalogFactory("default", false);

    InvCatalogImpl catalog = (InvCatalogImpl) factory.readXML(catalogURI);
    StringBuilder buff = new StringBuilder();
    if (!catalog.check(buff)) {
      throw new IOException("Invalid catalog <" + catalogURI + ">\n" + buff.toString());
    }

    List datasets = catalog.getDatasets();

    InvDataset idata = (InvDataset) datasets.get(0);
    //    List ddate = idata.getDates();

    ArrayList dsets = (ArrayList) idata.getDatasets();

    ArrayList absTimeList = new ArrayList();
    ArrayList dURIList = new ArrayList();
    ArrayList dInvList = new ArrayList();

    for (int i = 0; i < dsets.size(); i++) {
      InvDataset tdata = (InvDataset) dsets.get(i);
      List acess = tdata.getAccess();
      List dates = tdata.getDates();
      InvAccess ia = (InvAccess) acess.get(0);
      URI d = ia.getStandardUri();
      Date date = DateUnit.getStandardOrISO(dates.get(0).toString());
      absTimeList.add(date);
      dURIList.add(new DatasetURIInfo(d, date));
      dInvList.add(new InvDatasetInfo(tdata, date));
    }

    DqcRadarDatasetInfo dri = new DqcRadarDatasetInfo(absTimeList, dURIList, dInvList);

    return dri;
  }
  /**
   * _more_
   *
   * @param desc _more_
   * @param dqc _more_
   * @param selService _more_
   * @param service _more_
   * @param selStation _more_
   * @param selRegion _more_
   * @param selTime _more_
   */
  private DqcRadarDatasetCollection(
      String desc,
      QueryCapability dqc,
      SelectService selService,
      SelectService.ServiceChoice service,
      SelectStation selStation,
      SelectGeoRegion selRegion,
      SelectList selTime) {
    super();
    //  this.ds = ds;
    this.desc = desc;
    this.dqc = dqc;
    this.selService = selService;
    this.selStation = selStation;
    this.selRegion = selRegion;
    this.selTime = selTime;
    this.service = service;

    ArrayList stationList = selStation.getStations();
    stations = new HashMap(stationList.size());
    for (int i = 0; i < stationList.size(); i++) {
      thredds.catalog.query.Station station = (thredds.catalog.query.Station) stationList.get(i);
      //  DqcRadarStation dd = new DqcRadarStation(station);
      stations.put(station.getValue(), station);
    }

    ArrayList timeList = selTime.getChoices();
    relTimesList = new HashMap(timeList.size());
    for (int i = 0; i < timeList.size(); i++) {
      thredds.catalog.query.Choice tt = (thredds.catalog.query.Choice) timeList.get(i);
      relTimesList.put(tt.getValue(), tt);
    }

    String ql = dqc.getQuery().getUriResolved().toString();

    startDate = new Date();
    endDate = new Date();

    try {
      timeUnit = new DateUnit("hours since 1991-01-01T00:00");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Getting data for a single radar station.
   *
   * @param stnName radar station name
   * @param start of the time
   * @param end of the time
   * @return data URI list
   * @throws IOException java io exception
   */
  public ArrayList getRadarStationURIs(String stnName, Date start, Date end) throws IOException {

    DqcRadarDatasetInfo dri = queryRadarStation(stnName, start, end);
    ArrayList uList = dri.getURIList();

    int size = uList.size();
    ArrayList datasetsURI = new ArrayList();

    for (int i = 0; i < size; i++) {
      DatasetURIInfo du = (DatasetURIInfo) uList.get(i);
      datasetsURI.add(du.uri);
    }

    return datasetsURI;
  }
  /**
   * Getting data Iterator for a single radar station.
   *
   * @param stnName radar station name
   * @param start of the time
   * @param end of the time
   * @return dataset list
   * @throws IOException java io exception
   */
  public ArrayList getRadarStationDatasets(String stnName, Date start, Date end)
      throws IOException {

    ArrayList datasetList = new ArrayList();

    DqcRadarDatasetInfo dri = queryRadarStation(stnName, start, end);
    ArrayList iList = dri.getInvList();
    int size = iList.size();

    for (int i = 0; i < size; i++) {
      InvDatasetInfo iv = (InvDatasetInfo) iList.get(i);
      InvDataset tdata = iv.inv;
      ThreddsDataFactory tdFactory = new ThreddsDataFactory();
      ThreddsDataFactory.Result result;
      result = tdFactory.openFeatureDataset(tdata, null);
      datasetList.add(result.featureDataset);
    }

    return datasetList;
  }
  /**
   * Getting URI for a single radar station.
   *
   * @param stnName radar station name
   * @param absTime is absolute time
   * @return InvDataset
   * @throws IOException java io exception
   */
  private InvDataset queryRadarStation(String stnName, Date absTime) throws IOException {
    String stime = DateUtil.getTimeAsISO8601(absTime);
    // construct a query like this:
    // http://motherlode.ucar.edu:9080/thredds/idd/radarLevel2?returns=catalog&stn=KFTG&dtime=latest
    StringBuffer queryb = new StringBuffer();

    queryb.append(dqc.getQuery().getUriResolved().toString());
    queryb.append("serviceType=OPENDAP");
    queryb.append("&stn=" + stnName);
    queryb.append("&dtime=" + stime);

    URI catalogURI;

    try {
      catalogURI = new URI(queryb.toString());
    } catch (java.net.URISyntaxException e) {
      throw new IOException("** MalformedURLException on URL <" + ">\n" + e.getMessage() + "\n");
    }

    InvCatalogFactory factory = new InvCatalogFactory("default", false);

    InvCatalogImpl catalog = (InvCatalogImpl) factory.readXML(catalogURI);

    StringBuilder buff = new StringBuilder();
    if (!catalog.check(buff)) {
      throw new IOException("Invalid catalog <" + catalogURI + ">\n" + buff.toString());
    }

    List datasets = catalog.getDatasets();

    InvDataset idata = (InvDataset) datasets.get(0);
    ArrayList dsets = (ArrayList) idata.getDatasets();

    InvDataset tdata = (InvDataset) dsets.get(0);
    return tdata;
  }
  /**
   * _more_
   *
   * @param desc _more_
   * @param dqc_location _more_
   * @param errlog _more_
   * @return _more_
   * @throws IOException _more_
   */
  public static DqcRadarDatasetCollection factory(
      String desc, String dqc_location, StringBuffer errlog) throws IOException {

    DqcFactory dqcFactory = new DqcFactory(true);
    QueryCapability dqc = dqcFactory.readXML(dqc_location + "?returns=dqc");
    if (dqc.hasFatalError()) {
      errlog.append(dqc.getErrorMessages());
      return null;
    }

    // have a look at what selectors there are before proceeding
    SelectStation selStation = null;
    SelectList selTime = null;
    SelectService selService = null;
    // SelectGeoRegion selRegion = null;

    ArrayList selectors = dqc.getSelectors();
    for (int i = 0; i < selectors.size(); i++) {
      Selector s = (Selector) selectors.get(i);
      if (s instanceof SelectStation) {
        selStation = (SelectStation) s;
      }
      if (s instanceof SelectList) {
        selTime = (SelectList) s;
      }
      if (s instanceof SelectService) {
        selService = (SelectService) s;
      }
      // if (s instanceof SelectGeoRegion)
      //   selRegion = (SelectGeoRegion) s;
    }

    // gotta have these
    if (selService == null) {
      errlog.append("DqcStationaryRadarDataset must have Service selector");
      return null;
    }
    if (selStation == null) {
      errlog.append("DqcStationaryRadarDataset must have Station selector");
      return null;
    }
    if (selTime == null) {
      errlog.append("DqcStationaryRadarDataset must have Date selector");
      return null;
    }
    // if (selRegion == null) {
    //   errlog.append("DqcStationaryRadarDataset must have GeoRegion selector");
    //   return null;
    // }

    // decide on which service
    SelectService.ServiceChoice wantServiceChoice = null;
    List services = selService.getChoices();
    for (int i = 0; i < services.size(); i++) {
      SelectService.ServiceChoice serviceChoice = (SelectService.ServiceChoice) services.get(i);
      if (serviceChoice.getService().equals("HTTPServer")
          && serviceChoice.getDataFormat().equals("text/xml")) {
        // && serviceChoice.getReturns().equals("data")     ) // LOOK kludge
        wantServiceChoice = serviceChoice;
      }
    }

    if (wantServiceChoice == null) {
      errlog.append(
          "DqcStationObsDataset must have HTTPServer Service with DataFormat=text/plain, and returns=data");
      return null;
    }

    return new DqcRadarDatasetCollection(
        desc, dqc, selService, wantServiceChoice, selStation, null, selTime);
  }