/**
   * Getting URI for a single radar station.
   *
   * @param stnName radar station name
   * @param productID _more_
   * @param absTime is absolute time
   * @return InvDataset
   * @throws IOException java io exception
   */
  private InvDataset queryRadarStation(String stnName, String productID, Date absTime)
      throws IOException {
    String stime = DateUtil.getTimeAsISO8601(absTime).replaceAll("GMT", "");
    // construct a query like this:
    // http://motherlode.ucar.edu:9080/thredds/idd/radarLevel2?returns=catalog&stn=KFTG&dtime=latest
    StringBuilder queryb = new StringBuilder();
    String baseURI = dsc_location.replaceFirst("/dataset.xml", "?");
    queryb.append(baseURI);
    queryb.append("&stn=" + stnName);
    if (productID != null) {
      queryb.append("&var=" + productID);
    }
    if (absTime == null) {
      queryb.append("&time=present");
    } else {

      queryb.append("&time=" + 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);
    // visad.util.Trace.call1("TDSRadarDatasetCollection.readXML");

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

    // visad.util.Trace.call2("TDSRadarDatasetCollection.readXML");
    // visad.util.Trace.call1("TDSRadarDatasetCollection.checkCatalog");
    StringBuilder buff = new StringBuilder();
    if (!catalog.check(buff)) {
      throw new IOException("Invalid catalog <" + catalogURI + ">\n" + buff.toString());
    }
    // visad.util.Trace.call2("TDSRadarDatasetCollection.checkCatalog");
    // catalog.writeXML(System.out);  // debugg

    List<InvDataset> datasets = catalog.getDatasets();

    InvDataset idata = (InvDataset) datasets.get(0);
    List<InvDataset> dsets = idata.getDatasets();
    InvDataset tdata = (InvDataset) dsets.get(0);
    return tdata;
  }
  /**
   * initiate a new TDS radar dataset collection object
   *
   * @param desc description
   * @param dsc_location location of dataset
   * @param elem dataset root element
   * @param ns dataset namespace
   * @param errlog error log
   * @throws IOException _more_
   */
  private IdvRadarDatasetCollection(
      String desc, String dsc_location, Element elem, Namespace ns, StringBuffer errlog)
      throws IOException {
    super();
    Element serviceElem = readElements(elem, "service");
    Element dsElem = readElements(elem, "dataset");

    Element metaElem = readElements(dsElem, "metadata");
    // HashMap stationHMap = readSelectStations(metaElem, ns);
    String sts = dsc_location.replaceFirst("dataset.xml", "stations.xml");
    HashMap stationHMap = readRadarStations(sts);
    LatLonRect radarRegion = readSelectRegion(metaElem, ns);
    List<String> radarTimeSpan = readSelectTime(metaElem, ns);
    List<Product> productList = readSelectVariable(metaElem, ns);
    String summary = readSelectDocument(metaElem, ns);

    // gotta have these
    if (stationHMap == null) {
      errlog.append("TDSRadarDatasetCollection must have station selected");
      return;
    }
    if (radarRegion == null) {
      errlog.append("TDSRadarDatasetCollection must have region selected");
      return;
    }
    if (radarTimeSpan == null) {
      errlog.append("TDSRadarDatasetCollection must have time span selected");
      return;
    }

    this.desc = desc;
    this.dsc_location = dsc_location;
    this.radarProducts = productList;
    this.summary = summary;
    this.stationHMap = stationHMap;
    this.radarRegion = radarRegion;
    this.radarTimeSpan = radarTimeSpan;
    this.startDate = DateUnit.getStandardOrISO((String) radarTimeSpan.get(0));
    this.endDate = DateUnit.getStandardOrISO((String) radarTimeSpan.get(1));

    try {
      timeUnit = new DateUnit("hours since 1991-01-01T00:00");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Getting URI for a single radar station.
   *
   * @param stnName radar station name
   * @param absTime is absolute time
   * @return URI
   * @throws IOException java io exception
   */
  public URI getRadarDatasetURI(String stnName, Date absTime) throws IOException {
    // absTime is a member of  datasetsDateURI
    InvDataset invdata = queryRadarStation(stnName, absTime);
    List<InvAccess> acess = invdata.getAccess();
    InvAccess ia = (InvAccess) acess.get(0);
    URI ui = ia.getStandardUri();

    if (ui == null) {
      throw new IOException("Invalid time selected: " + absTime.toString() + "\n");
    }

    return ui;
  }
  /**
   * _more_
   *
   * @param stnName _more_
   * @param productID _more_
   * @param absTime _more_
   * @return _more_
   * @throws IOException _more_
   */
  public URI getRadarDatasetURI(String stnName, String productID, Date absTime) throws IOException {
    // absTime is a member of  datasetsDateURI

    if (productID == null) {
      return getRadarDatasetURI(stnName, absTime);
    }

    InvDataset invdata = queryRadarStation(stnName, productID, absTime);
    /*  List dsets = idata.getDatasets();
    int siz = dsets.size();
    if(siz != 1)
        return null;

    InvDataset invdata = (InvDataset)dsets.get(0);     */
    List acess = invdata.getAccess();
    InvAccess ia = (InvAccess) acess.get(0);
    URI ui = ia.getStandardUri();

    if (ui == null) {
      throw new IOException("Invalid time selected: " + absTime.toString() + "\n");
    }

    return ui;
  }
  /**
   * Getting invDataset list for a single radar station.
   *
   * @param stnName radar station name
   * @param productID _more_
   * @param start of the time
   * @param end of the time
   * @return list of invDataset
   * @throws IOException java io exception
   */
  private TDSRadarDatasetInfo queryRadarStation(
      String stnName, String productID, Date start, Date end) throws IOException {
    // http://motherlode.ucar.edu:9080/thredds/idd/radarLevel2?returns=catalog&stn=KFTG&dtime=latest
    StringBuilder queryb = new StringBuilder();
    String baseURI = dsc_location.replaceFirst("/dataset.xml", "?");
    queryb.append(baseURI);
    queryb.append("&stn=" + stnName);
    if (productID != null) {
      queryb.append("&var=" + productID);
    }
    if ((start == null) && (end == null)) {
      queryb.append("&time=present");
    } else if (end == null) {
      String stime = DateUtil.getTimeAsISO8601(start).replaceAll("GMT", "");
      queryb.append("&time_start=" + stime);
      queryb.append("&time_end=present");
    } else {
      String stime = DateUtil.getTimeAsISO8601(start).replaceAll("GMT", "");
      String etime = DateUtil.getTimeAsISO8601(end).replaceAll("GMT", "");
      queryb.append("&time_start=" + stime);
      queryb.append("&time_end=" + 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);

    // visad.util.Trace.call1("TDSRadarDatasetCollection.readXML");
    InvCatalogImpl catalog = (InvCatalogImpl) factory.readXML(catalogURI);
    // visad.util.Trace.call2("TDSRadarDatasetCollection.readXML");
    StringBuilder buff = new StringBuilder();
    // visad.util.Trace.call1("TDSRadarDatasetCollection.checkCatalog");
    if (!catalog.check(buff)) {
      throw new IOException("Invalid catalog <" + catalogURI + ">\n" + buff.toString());
    }
    // visad.util.Trace.call2("TDSRadarDatasetCollection.checkCatalog");

    List<InvDataset> datasets = catalog.getDatasets();

    InvDataset idata = (InvDataset) datasets.get(0);

    List<InvDataset> dsets = idata.getDatasets();

    List<Date> absTimeList = new ArrayList<Date>();
    List<DatasetURIInfo> dURIList = new ArrayList<DatasetURIInfo>();
    List<InvDatasetInfo> dInvList = new ArrayList<InvDatasetInfo>();

    // visad.util.Trace.call1("TDSRadarDatasetCollection.getLists");
    for (InvDataset tdata : dsets) {
      List<InvAccess> acess = tdata.getAccess();
      List<DateType> dates = tdata.getDates();
      InvAccess ia = (InvAccess) acess.get(0);
      URI d = ia.getStandardUri();
      Date date = ((DateType) dates.get(0)).getDate();
      absTimeList.add(date);
      dURIList.add(new DatasetURIInfo(d, date));
      dInvList.add(new InvDatasetInfo(tdata, date));
    }
    // visad.util.Trace.call2("TDSRadarDatasetCollection.getLists");

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

    return dri;
  }
  /**
   * Test the program
   *
   * @param args the args
   * @throws IOException _more_
   */
  public static void main(String args[]) throws IOException {
    StringBuffer errlog = new StringBuffer();
    String ds_location = null;
    TDSRadarDatasetCollection dsc = null;
    List stns = null;

    ds_location =
        "http://motherlode.ucar.edu:9080/thredds/radarServer/nexrad/level3/CCS039/dataset.xml";
    dsc = TDSRadarDatasetCollection.factory("test", ds_location, errlog);
    System.out.println(" errs= " + errlog);
    stns = dsc.getStations();
    System.out.println(" nstns= " + stns.size());

    // System.exit(0);
    stns = dsc.getStations();
    System.out.println(" nstns= " + stns.size());

    Station stn = dsc.getRadarStation("DVN"); // (StationImpl)stns.get(12);
    System.out.println("stn = " + stn);

    // List ulist = stn.getRadarStationURIs();
    // assert null != ulist;
    List tl = dsc.getRadarTimeSpan();
    Date ts1 = DateUnit.getStandardOrISO("1998-06-28T01:01:21Z");
    Date ts2 = DateUnit.getStandardOrISO("1998-07-30T19:01:21Z");
    List pd = dsc.getRadarProducts();
    List<Date> tlist = dsc.getRadarStationTimes(stn.getName(), "BREF1", ts1, ts2);
    int sz = tlist.size();
    for (int i = 0; i < 3; i++) {
      Date ts0 = (Date) tlist.get(i);
      RadialDatasetSweep rds = dsc.getRadarDataset(stn.getName(), "BREF1", ts0);
      int tt = 0;
    }

    Date ts0 = (Date) tlist.get(0);
    URI stURL = dsc.getRadarDatasetURI(stn.getName(), "BREF1", ts0);
    assert null != stURL;
    DateSelection dateS = new DateSelection(ts1, ts2);
    dateS.setInterval((double) 3600 * 1000);
    dateS.setRoundTo((double) 3600 * 1000);
    dateS.setPreRange((double) 500 * 1000);
    dateS.setPostRange((double) 500 * 1000);

    for (int i = 0; i < stns.size(); i++) {
      stn = (Station) stns.get(i);
      List<Date> times =
          dsc.getRadarStationTimes(
              stn.getName(),
              new Date(System.currentTimeMillis() - 3600 * 1000 * 24 * 100),
              new Date(System.currentTimeMillis()));
      if (times.size() > 0) {
        System.err.println(
            stn
                + " times:"
                + times.size()
                + " "
                + times.get(0)
                + " - "
                + times.get(times.size() - 1));
      } else {
        System.err.println(stn + " no times");
      }
    }

    List jList = dsc.getDataURIs("KABX", dateS);

    assert null != jList;
    List mList = dsc.getData("KABX", dateS, null);
    assert null != mList;

    // Date ts0 =
    // DateFromString.getDateUsingCompleteDateFormat((String)tlist.get(1),"yyyy-MM-dd'T'HH:mm:ss");
    Date ts = (Date) tlist.get(1);
    java.text.SimpleDateFormat isoDateTimeFormat;
    isoDateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    isoDateTimeFormat.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
    String st = isoDateTimeFormat.format(ts);
  }