// @todo Make sure units are meters
 public Array getElevation(Range range) throws IOException, InvalidRangeException {
   List section = new ArrayList(1);
   section.add(range);
   Array a = elevVar.read(section);
   if (elevVarUnitsConversionFactor == 1.0) return (a);
   for (IndexIterator it = a.getIndexIterator(); it.hasNext(); ) {
     if (elevVar.getDataType() == DataType.DOUBLE) {
       double val = it.getDoubleNext();
       it.setDoubleCurrent(val * elevVarUnitsConversionFactor);
     } else if (elevVar.getDataType() == DataType.FLOAT) {
       float val = it.getFloatNext();
       it.setFloatCurrent((float) (val * elevVarUnitsConversionFactor));
     } else if (elevVar.getDataType() == DataType.INT) {
       int val = it.getIntNext();
       it.setIntCurrent((int) (val * elevVarUnitsConversionFactor));
     } else if (elevVar.getDataType() == DataType.LONG) {
       long val = it.getLongNext();
       it.setLongCurrent((long) (val * elevVarUnitsConversionFactor));
     } else {
       throw new IllegalStateException(
           "Elevation variable type <"
               + elevVar.getDataType().toString()
               + "> not double, float, int, or long.");
     }
   }
   return (a);
 }
  /**
   * Getting data for a single radar station, with time range.
   *
   * @param sName radar station name
   * @param dateSelect the date time selection information
   * @param cancel _more_
   * @return list of radialDatasetSweep
   * @throws IOException _more_
   */
  public ArrayList getData(String sName, DateSelection dateSelect, ucar.nc2.util.CancelTask cancel)
      throws IOException {
    if ((cancel != null) && cancel.isCancel()) {
      return null;
    }
    DqcRadarDatasetInfo dri =
        queryRadarStation(sName, dateSelect.getStartFixedDate(), dateSelect.getEndFixedDate());
    ArrayList datasetList = new ArrayList();

    List datasetINVs = dateSelect.apply(dri.getInvList());

    Iterator it = datasetINVs.iterator();
    while (it.hasNext()) {
      InvDatasetInfo ifo = (InvDatasetInfo) it.next();
      InvDataset tdata = ifo.inv;
      ThreddsDataFactory tdFactory = new ThreddsDataFactory();
      ThreddsDataFactory.Result result;
      result = tdFactory.openFeatureDataset(tdata, null);
      datasetList.add(result.featureDataset);
      if ((cancel != null) && cancel.isCancel()) {
        return null;
      }
    }

    return datasetList;
  }
  /**
   * get all radar station.
   *
   * @return List of type DqcRadarStation objects
   * @throws IOException java io exception
   */
  public List getRadarStations() {
    List sl = selStation.getStations();
    ArrayList dsl = new ArrayList();

    for (Iterator it = sl.iterator(); it.hasNext(); ) {
      Station s = (Station) it.next();
      dsl.add(s);
    }
    return dsl;
  }
  private LatLonRect getBoundingBox(List stnList) {
    ucar.unidata.geoloc.Station s = (ucar.unidata.geoloc.Station) stnList.get(0);
    LatLonPointImpl llpt = new LatLonPointImpl();
    llpt.set(s.getLatitude(), s.getLongitude());
    LatLonRect rect = new LatLonRect(llpt, .001, .001);

    for (int i = 1; i < stnList.size(); i++) {
      s = (ucar.unidata.geoloc.Station) stnList.get(i);
      llpt.set(s.getLatitude(), s.getLongitude());
      rect.extend(llpt);
    }

    return rect;
  }
  private void createProfiles(int nprofiles) throws IOException {
    this.nprofiles = nprofiles;

    // add the dimensions
    Dimension profileDim = ncfile.addDimension(profileDimName, nprofiles);
    profileDims.add(profileDim);

    Variable v = ncfile.addVariable(numObsName, DataType.INT, profileDimName);
    ncfile.addVariableAttribute(
        v, new Attribute("long_name", "number of children in linked list for this profile"));

    v = ncfile.addVariable(numProfilesTotalName, DataType.INT, "");
    ncfile.addVariableAttribute(v, new Attribute("long_name", "number of valid profiles"));

    v = ncfile.addVariable(firstObsName, DataType.INT, profileDimName);
    ncfile.addVariableAttribute(
        v,
        new Attribute("long_name", "record number of first obs in linked list for this profile"));

    // time variable
    Variable timeVar = ncfile.addStringVariable(timeName, profileDims, 20);
    ncfile.addVariableAttribute(
        timeVar, new Attribute("long_name", "ISO-8601 Date - time of observation"));

    v = ncfile.addVariable(parentStationIndex, DataType.INT, profileDimName);
    ncfile.addVariableAttribute(v, new Attribute("long_name", "index of parent station"));

    v = ncfile.addVariable(nextProfileName, DataType.INT, profileDimName);
    ncfile.addVariableAttribute(
        v, new Attribute("long_name", "index of next profile in linked list for this station"));
  }
 public Array getData(Range range, String parameterName)
     throws IOException, InvalidRangeException {
   Variable variable = ncfile.getRootGroup().findVariable(parameterName);
   int varRank = variable.getRank();
   int[] varShape = variable.getShape();
   List section = new ArrayList(varRank);
   section.add(range);
   for (int i = 1; i < varRank; i++) {
     section.add(new Range(0, varShape[i] - 1));
   }
   Array array = variable.read(section);
   if (array.getShape()[0] == 1) {
     return (array.reduce(0));
   } else {
     return (array);
   }
   // return( array.getShape()[0] == 1 ? array.reduce( 0 ) : array);
 }
  /**
   * 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 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;
  }
  /**
   * get all radar station within box.
   *
   * @param boundingBox _more_
   * @param cancel _more_
   * @return List of type DqcRadarStation objects
   * @throws IOException java io exception
   */
  public List getStations(
      ucar.unidata.geoloc.LatLonRect boundingBox, ucar.nc2.util.CancelTask cancel)
      throws IOException {
    List sl = selStation.getStations();
    ArrayList dsl = new ArrayList();

    for (Iterator it = sl.iterator(); it.hasNext(); ) {
      Station s = (Station) it.next();
      LatLonPointImpl latlonPt = new LatLonPointImpl();
      latlonPt.set(s.getLocation().getLatitude(), s.getLocation().getLongitude());
      if (boundingBox.contains(latlonPt)) {
        dsl.add(s);
      }
      if ((cancel != null) && cancel.isCancel()) {
        return null;
      }
    }

    return dsl;
  }
  private void writeStationData(List<ucar.unidata.geoloc.Station> stnList) throws IOException {
    this.stnList = stnList;
    int nstns = stnList.size();
    stationMap = new HashMap<String, StationTracker>(2 * nstns);
    if (debug) System.out.println("stationMap created");

    // now write the station data
    ArrayDouble.D1 latArray = new ArrayDouble.D1(nstns);
    ArrayDouble.D1 lonArray = new ArrayDouble.D1(nstns);
    ArrayDouble.D1 altArray = new ArrayDouble.D1(nstns);
    ArrayObject.D1 idArray = new ArrayObject.D1(String.class, nstns);
    ArrayObject.D1 descArray = new ArrayObject.D1(String.class, nstns);
    ArrayObject.D1 wmoArray = new ArrayObject.D1(String.class, nstns);

    for (int i = 0; i < stnList.size(); i++) {
      ucar.unidata.geoloc.Station stn = stnList.get(i);
      stationMap.put(stn.getName(), new StationTracker(i));

      latArray.set(i, stn.getLatitude());
      lonArray.set(i, stn.getLongitude());
      if (useAlt) altArray.set(i, stn.getAltitude());

      idArray.set(i, stn.getName());
      descArray.set(i, stn.getDescription());
      if (useWmoId) wmoArray.set(i, stn.getWmoId());
    }

    try {
      ncfile.write(latName, latArray);
      ncfile.write(lonName, lonArray);
      if (useAlt) ncfile.write(altName, altArray);
      ncfile.writeStringData(idName, idArray);
      ncfile.writeStringData(descName, descArray);
      if (useWmoId) ncfile.writeStringData(wmoName, wmoArray);

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }
  }
  /**
   * 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;
  }
  /**
   * 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 sName _more_
   * @param dateSelect _more_
   * @param cancel _more_
   * @return _more_
   * @throws IOException _more_
   */
  public ArrayList getDataURIs(
      String sName, DateSelection dateSelect, ucar.nc2.util.CancelTask cancel) throws IOException {
    if ((cancel != null) && cancel.isCancel()) {
      return null;
    }
    DqcRadarDatasetInfo dri =
        queryRadarStation(sName, dateSelect.getStartFixedDate(), dateSelect.getEndFixedDate());

    // create a list to hold URIs
    List datasetsURIs = dateSelect.apply(dri.getURIList());
    ArrayList uriList = new ArrayList();

    Iterator it = datasetsURIs.iterator();
    while (it.hasNext()) {
      DatasetURIInfo ufo = (DatasetURIInfo) it.next();
      URI u = ufo.uri;
      uriList.add(u);
      if ((cancel != null) && cancel.isCancel()) {
        return null;
      }
    }

    return uriList;
  }
  private void writeDataFinish() throws IOException {
    // finish global variables
    ArrayInt.D0 totalArray = new ArrayInt.D0();
    totalArray.set(profileIndex);
    try {
      ncfile.write(numProfilesTotalName, totalArray);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }

    // finish the station data
    int nstns = stnList.size();
    ArrayInt.D1 firstProfileArray = new ArrayInt.D1(nstns);
    ArrayInt.D1 numProfileArray = new ArrayInt.D1(nstns);
    ArrayInt.D1 nextProfileArray = new ArrayInt.D1(nprofiles);

    for (int i = 0; i < stnList.size(); i++) {
      ucar.unidata.geoloc.Station stn = stnList.get(i);
      StationTracker tracker = stationMap.get(stn.getName());

      numProfileArray.set(i, tracker.numChildren);

      int first = (tracker.link.size() > 0) ? tracker.link.get(0) : -1;
      firstProfileArray.set(i, first);

      if (tracker.link.size() > 0) {
        // construct forward link
        List<Integer> nextList = tracker.link;
        for (int j = 0; j < nextList.size() - 1; j++) {
          Integer curr = nextList.get(j);
          Integer next = nextList.get(j + 1);
          nextProfileArray.set(curr, next);
        }
        Integer curr = nextList.get(nextList.size() - 1);
        nextProfileArray.set(curr, -1);
      }
    }

    try {
      ncfile.write(firstProfileName, firstProfileArray);
      ncfile.write(numProfilesName, numProfileArray);
      ncfile.write(nextProfileName, nextProfileArray);

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }

    // finish the profile data
    ArrayInt.D1 nextObsArray = new ArrayInt.D1(recno);
    ArrayInt.D1 firstObsArray = new ArrayInt.D1(nprofiles);
    ArrayInt.D1 numObsArray = new ArrayInt.D1(nprofiles);

    for (int i = 0; i < stnList.size(); i++) {
      ucar.unidata.geoloc.Station stn = stnList.get(i);
      StationTracker stnTracker = stationMap.get(stn.getName());

      Set<Date> dates = stnTracker.profileMap.keySet();
      for (Date date : dates) {
        ProfileTracker proTracker = stnTracker.profileMap.get(date);
        int trackerIndex = proTracker.parent_index;
        numObsArray.set(trackerIndex, proTracker.numChildren);

        int first = (proTracker.link.size() > 0) ? proTracker.link.get(0) : -1;
        firstObsArray.set(trackerIndex, first);

        if (proTracker.link.size() > 0) {
          // construct forward link
          List<Integer> nextList = proTracker.link;
          for (int j = 0; j < nextList.size() - 1; j++) {
            Integer curr = nextList.get(j);
            Integer next = nextList.get(j + 1);
            nextObsArray.set(curr, next);
          }
          Integer curr = nextList.get(nextList.size() - 1);
          nextObsArray.set(curr, -1);
        }
      }
    }

    try {
      ncfile.write(firstObsName, firstObsArray);
      ncfile.write(numObsName, numObsArray);
      ncfile.write(nextObsName, nextObsArray);

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IllegalStateException(e);
    }

    // finish the obs data

    ncfile.updateAttribute(
        null, new Attribute("time_coverage_start", dateFormatter.toDateTimeStringISO(minDate)));
    ncfile.updateAttribute(
        null, new Attribute("time_coverage_end", dateFormatter.toDateTimeStringISO(maxDate)));
  }
 public List getTrajectories() // throws IOException;
     {
   List l = new ArrayList();
   l.add(trajectory);
   return (l);
 }
 // @todo Make sure units are degrees_east
 public Array getLongitude(Range range) throws IOException, InvalidRangeException {
   List section = new ArrayList(1);
   section.add(range);
   return (lonVar.read(section));
 }
  private void createStations(List<ucar.unidata.geoloc.Station> stnList) throws IOException {
    int nstns = stnList.size();

    // see if there's altitude, wmoId for any stations
    for (int i = 0; i < nstns; i++) {
      ucar.unidata.geoloc.Station stn = stnList.get(i);

      // if (!Double.isNaN(stn.getAltitude()))
      //  useAlt = true;
      if ((stn.getWmoId() != null) && (stn.getWmoId().trim().length() > 0)) useWmoId = true;
    }

    /* if (useAlt)
    ncfile.addGlobalAttribute("altitude_coordinate", altName); */

    // find string lengths
    for (int i = 0; i < nstns; i++) {
      ucar.unidata.geoloc.Station station = stnList.get(i);
      name_strlen = Math.max(name_strlen, station.getName().length());
      desc_strlen = Math.max(desc_strlen, station.getDescription().length());
      if (useWmoId) wmo_strlen = Math.max(wmo_strlen, station.getName().length());
    }

    LatLonRect llbb = getBoundingBox(stnList);
    ncfile.addGlobalAttribute(
        "geospatial_lat_min", Double.toString(llbb.getLowerLeftPoint().getLatitude()));
    ncfile.addGlobalAttribute(
        "geospatial_lat_max", Double.toString(llbb.getUpperRightPoint().getLatitude()));
    ncfile.addGlobalAttribute(
        "geospatial_lon_min", Double.toString(llbb.getLowerLeftPoint().getLongitude()));
    ncfile.addGlobalAttribute(
        "geospatial_lon_max", Double.toString(llbb.getUpperRightPoint().getLongitude()));

    // add the dimensions
    Dimension recordDim = ncfile.addUnlimitedDimension(recordDimName);
    recordDims.add(recordDim);

    Dimension stationDim = ncfile.addDimension(stationDimName, nstns);
    stationDims.add(stationDim);

    // add the station Variables using the station dimension
    Variable v = ncfile.addVariable(latName, DataType.DOUBLE, stationDimName);
    ncfile.addVariableAttribute(v, new Attribute("units", "degrees_north"));
    ncfile.addVariableAttribute(v, new Attribute("long_name", "station latitude"));

    v = ncfile.addVariable(lonName, DataType.DOUBLE, stationDimName);
    ncfile.addVariableAttribute(v, new Attribute("units", "degrees_east"));
    ncfile.addVariableAttribute(v, new Attribute("long_name", "station longitude"));

    if (useAlt) {
      v = ncfile.addVariable(altName, DataType.DOUBLE, stationDimName);
      ncfile.addVariableAttribute(v, new Attribute("units", "meters"));
      ncfile.addVariableAttribute(v, new Attribute("long_name", "station altitude"));
    }

    v = ncfile.addStringVariable(idName, stationDims, name_strlen);
    ncfile.addVariableAttribute(v, new Attribute("long_name", "station identifier"));

    v = ncfile.addStringVariable(descName, stationDims, desc_strlen);
    ncfile.addVariableAttribute(v, new Attribute("long_name", "station description"));

    if (useWmoId) {
      v = ncfile.addStringVariable(wmoName, stationDims, wmo_strlen);
      ncfile.addVariableAttribute(v, new Attribute("long_name", "station WMO id"));
    }

    v = ncfile.addVariable(numProfilesName, DataType.INT, stationDimName);
    ncfile.addVariableAttribute(
        v, new Attribute("long_name", "number of profiles in linked list for this station"));

    v = ncfile.addVariable(firstProfileName, DataType.INT, stationDimName);
    ncfile.addVariableAttribute(
        v, new Attribute("long_name", "index of first profile in linked list for this station"));
  }
  /**
   * _more_
   *
   * @param args _more_
   * @throws IOException _more_
   */
  public static void main(String args[]) throws IOException {
    StringBuffer errlog = new StringBuffer();
    String dqc_location = "http://thredds.ucar.edu/thredds/idd/radarLevel2";
    DqcRadarDatasetCollection ds = factory("test", dqc_location, errlog);
    System.out.println(" errs= " + errlog);

    List stns = ds.getStations();
    System.out.println(" nstns= " + stns.size());

    Station stn = (Station) (stns.get(2));

    // List ulist = stn.getRadarStationURIs();
    // assert null != ulist;
    Date ts1 = DateUnit.getStandardOrISO("2007-06-9T12:12:00");
    Date ts2 = DateUnit.getStandardOrISO("2007-06-9T23:12:00");

    List tlist = ds.getRadarStationTimes(stn.getValue(), ts1, ts2);
    int sz = tlist.size();
    Date ts0 = DateUnit.getStandardOrISO((String) tlist.get(1));
    RadialDatasetSweep rds = ds.getRadarDataset(stn.getValue(), ts0);
    URI stURL = ds.getRadarDatasetURI(stn.getValue(), ts0);
    assert null != stURL;
    assert 0 != sz;
    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 times =
          ds.getRadarStationTimes(
              stn.getValue(),
              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");
      }
    }
    System.exit(0);

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

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

    // Date ts0 =
    // DateFromString.getDateUsingCompleteDateFormat((String)tlist.get(1),"yyyy-MM-dd'T'HH:mm:ss");
    Date ts = DateUnit.getStandardOrISO((String) 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);
  }
  /**
   * _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);
  }
 public List getTrajectoryIds() {
   List l = new ArrayList();
   l.add(trajectoryId);
   return (l);
 }
  public static void main(String args[]) throws Exception {
    long start = System.currentTimeMillis();
    Map<String, ucar.unidata.geoloc.Station> staHash =
        new HashMap<String, ucar.unidata.geoloc.Station>();

    String location = "R:/testdata/sounding/netcdf/Upperair_20070401_0000.nc";
    NetcdfDataset ncfile = NetcdfDataset.openDataset(location);
    ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);

    // look through record varibles, for those that have "manLevel" dimension
    // make a StructureData object for those
    StructureMembers sm = new StructureMembers("manLevel");
    Dimension manDim = ncfile.findDimension("manLevel");
    Structure record = (Structure) ncfile.findVariable("record");
    List<Variable> allList = record.getVariables();
    List<VariableSimpleIF> varList = new ArrayList<VariableSimpleIF>();
    for (Variable v : allList) {
      if ((v.getRank() == 1) && v.getDimension(0).equals(manDim)) {
        // public VariableDS(NetcdfDataset ds, Group group, Structure parentStructure, String
        // shortName, DataType dataType,
        // String dims, String units, String desc) {
        varList.add(
            new VariableDS(
                ncfile,
                null,
                null,
                v.getShortName(),
                v.getDataType(),
                "",
                v.getUnitsString(),
                v.getDescription()));
        // (String name, String desc, String units, DataType dtype, int []shape)
        sm.addMember(
            v.getShortName(),
            v.getDescription(),
            v.getUnitsString(),
            v.getDataType(),
            new int[0]); // scalar
      }
    }

    ArrayStructureMA manAS = new ArrayStructureMA(sm, new int[] {manDim.getLength()});

    // need the date units
    Variable time = ncfile.findVariable("synTime");
    String timeUnits = ncfile.findAttValueIgnoreCase(time, "units", null);
    timeUnits = StringUtil.remove(timeUnits, '('); // crappy fsl'ism
    timeUnits = StringUtil.remove(timeUnits, ')');
    DateUnit timeUnit = new DateUnit(timeUnits);

    // extract stations
    int nrecs = 0;
    StructureDataIterator iter = record.getStructureIterator();
    while (iter.hasNext()) {
      StructureData sdata = iter.next();
      String name = sdata.getScalarString("staName");
      ucar.unidata.geoloc.Station s = staHash.get(name);
      if (s == null) {
        float lat = sdata.convertScalarFloat("staLat");
        float lon = sdata.convertScalarFloat("staLon");
        float elev = sdata.convertScalarFloat("staElev");
        s = new StationImpl(name, "", lat, lon, elev);
        staHash.put(name, s);
      }
      nrecs++;
    }
    List<ucar.unidata.geoloc.Station> stnList =
        Arrays.asList(staHash.values().toArray(new ucar.unidata.geoloc.Station[staHash.size()]));
    Collections.sort(stnList);

    // create the writer
    WriterProfileObsDataset writer =
        new WriterProfileObsDataset(location + ".out", "rewrite " + location);
    writer.writeHeader(stnList, varList, nrecs, "prMan");

    // extract records
    iter = record.getStructureIterator();
    while (iter.hasNext()) {
      StructureData sdata = iter.next();
      String name = sdata.getScalarString("staName");
      double timeValue = sdata.convertScalarDouble("synTime");
      Date date = timeUnit.makeDate(timeValue);

      // transfer to the ArrayStructure
      List<String> names = sm.getMemberNames();
      for (String mname : names) {
        manAS.setMemberArray(mname, sdata.getArray(mname));
      }

      // each level is weritten as a seperate structure
      int numMand = sdata.getScalarInt("numMand");
      if (numMand >= manDim.getLength()) continue;

      for (int i = 0; i < numMand; i++) {
        StructureData useData = manAS.getStructureData(i);
        writer.writeRecord(name, date, useData);
      }
    }

    writer.finish();

    long took = System.currentTimeMillis() - start;
    System.out.println("That took = " + took);
  }
  public static boolean isValidFile(NetcdfDataset ds) {
    // Check that has a time dimension and a trajectory dimension.
    List list = ds.getRootGroup().getDimensions();
    if (list.size() != 2) return (false);
    Dimension d;
    for (int i = 0; i < 2; i++) {
      d = (Dimension) list.get(i);
      if (!d.getShortName().equals(timeDimNameDefault)
          && !d.getShortName().equals(trajDimNameDefault)) return (false);
    }

    // Check that has a trajectory coordinate variable.
    Variable var = ds.getRootGroup().findVariable(trajVarNameDefault);
    if (var == null) return (false);
    list = var.getDimensions();
    if (list.size() != 1) return (false);
    d = (Dimension) list.get(0);
    if (!d.getShortName().equals(trajDimNameDefault)) return (false);

    // Check that has a time coordinate variable with units that are udunits time
    var = ds.getRootGroup().findVariable(timeVarNameDefault);
    if (var == null) return (false);
    list = var.getDimensions();
    if (list.size() != 1) return (false);
    d = (Dimension) list.get(0);
    if (!d.getShortName().equals(timeDimNameDefault)) return (false);
    String units = var.findAttribute("units").getStringValue();
    Date date = DateUnit.getStandardDate("0 " + units);
    if (date == null) return (false);

    // Check for variable latitude(time) with units of "deg".
    var = ds.getRootGroup().findVariable(latVarNameDefault);
    if (var == null) return (false);
    list = var.getDimensions();
    if (list.size() != 2) return (false);
    for (int i = 0; i < 2; i++) {
      d = (Dimension) list.get(i);
      if (!d.getShortName().equals(timeDimNameDefault)
          && !d.getShortName().equals(trajDimNameDefault)) return (false);
    }
    //    units = var.findAttribute( "units").getStringValue();
    //    if ( ! SimpleUnit.isCompatible( units, "degrees_north")) return( false);

    // Check for variable longitude(time) with units of "deg".
    var = ds.getRootGroup().findVariable(lonVarNameDefault);
    if (var == null) return (false);
    list = var.getDimensions();
    if (list.size() != 2) return (false);
    for (int i = 0; i < 2; i++) {
      d = (Dimension) list.get(i);
      if (!d.getShortName().equals(timeDimNameDefault)
          && !d.getShortName().equals(trajDimNameDefault)) return (false);
    }
    //    units = var.findAttribute( "units").getStringValue();
    //    if ( ! SimpleUnit.isCompatible( units, "degrees_east")) return( false);

    // Check for variable altitude(time) with units of "m".
    var = ds.getRootGroup().findVariable(elevVarNameDefault);
    if (var == null) return (false);
    list = var.getDimensions();
    if (list.size() != 2) return (false);
    for (int i = 0; i < 2; i++) {
      d = (Dimension) list.get(i);
      if (!d.getShortName().equals(timeDimNameDefault)
          && !d.getShortName().equals(trajDimNameDefault)) return (false);
    }
    units = var.findAttribute("units").getStringValue();
    if (!SimpleUnit.isCompatible(units, "m")) return (false);

    return (true);
  }