package ucar.nc2.dt.radial;


import ucar.nc2.dt.StationRadarCollection;
import ucar.nc2.dt.Station;
import ucar.nc2.dt.TypedDatasetImpl;
import ucar.nc2.util.CancelTask;
import ucar.nc2.units.DateUnit;
import ucar.unidata.geoloc.LatLonRect;
import ucar.unidata.geoloc.LatLonPointImpl;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.IOException;


/**
 * Created by IntelliJ IDEA.
 * User: yuanho
 * Date: Feb 13, 2007
 * Time: 1:28:37 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class StationRadarCollectionImpl extends TypedDatasetImpl implements StationRadarCollection {

    private StationRadarCollection radarCollection;
    protected HashMap stations;
    protected HashMap relTimesList;
    protected HashMap absTimesList;
    protected DateUnit timeUnit;

    public StationRadarCollectionImpl() {
      super();
    }

    public StationRadarCollectionImpl( StationRadarCollection radarDataset) {
      this.radarCollection = radarDataset;
    }
        /* if (location.startsWith("thredds:")) { // LOOK need to distinguish between a DQC and a Catalog !!
      location = location.substring(8);
      DqcFactory dqcFactory = new DqcFactory(true);
      QueryCapability dqc = dqcFactory.readXML(location);
      if (dqc.hasFatalError()) {
        if (null != log) log.append(dqc.getErrorMessages());
        return null;
      }

      return ucar.nc2.thredds.DqcStationObsDataset.factory( null, dqc);
    } */
    
    private LatLonRect rect;
    public LatLonRect getBoundingBox() {
        if (rect == null) {
          List stations = null;
          try {
            stations = radarCollection.getRadarStations();
          } catch (IOException e) {
            return null;
          }
          if (stations.size() == 0)
            return null;

          Station s =  (Station) stations.get(0);
          LatLonPointImpl llpt = new LatLonPointImpl();
          llpt.set( s.getLatitude(), s.getLongitude());
          rect = new LatLonRect(llpt, .001, .001);

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

        return rect;
    }

    public List getStations(LatLonRect boundingBox, CancelTask cancel) throws IOException {
        LatLonPointImpl latlonPt = new LatLonPointImpl();
        ArrayList result = new ArrayList();
        List stationC = radarCollection.getRadarStations();
        for (int i = 0; i < stationC.size(); i++) {
          Station s =  (Station) stationC.get(i);
          latlonPt.set( s.getLatitude(), s.getLongitude());
          if (boundingBox.contains( latlonPt))
            result.add( s);
          if ((cancel != null) && cancel.isCancel()) return null;
        }
        return result;
    }

    public Station getStation(String name) throws IOException {
        if (stations == null) {
          List stationC = null;
          try {
            stationC = radarCollection.getRadarStations();
          } catch (IOException e) {
            return null;
          }

          stations = new HashMap( 2*stations.size());
          for (int i = 0; i < stationC.size(); i++) {
            Station s =  (Station) stationC.get(i);
            stations.put( s.getName(), s);
          }
        }

        return (Station) stations.get(name);
    }

     public List getStations( )throws IOException {
         return null;
     }



    public List getStations( CancelTask cancel) throws IOException {
         if ((cancel != null) && cancel.isCancel()) return null;
         return  getStations( );
     }

    public List getStations( LatLonRect boundingBox)throws IOException {
        LatLonPointImpl latlonPt = new LatLonPointImpl();
        ArrayList result = new ArrayList();
        List stationC = radarCollection.getRadarStations();
        for (int i = 0; i < stationC.size(); i++) {
          Station s =  (Station) stationC.get(i);
          latlonPt.set( s.getLatitude(), s.getLongitude());
          if (boundingBox.contains( latlonPt))
            result.add( s);
        }
        return result;

    }

    public int getStationDataCount( Station s) {
          return 0;
    }

    public boolean checkStationProduct(Station s, String product) {
        return true;
    }

     public boolean checkStationProduct(String product) {
         return true;
     }


     public List getStationDataProducts( Station s) {
         return null;
     }







}

@@ -34,38 +34,33 @@
 ort 
-ucar.nc2.dt.StationRadarCollec
+thredds.catalog.query.Sta
 tion
@@ -80,23 +80,32 @@
 .nc2.dt.
-Station
+TypedDatasetImpl
 ;%0Aimport
@@ -117,32 +117,38 @@
 .nc2.dt.
-TypedDatasetImpl
+StationRadarCollection
 ;%0Aimport
@@ -367,16 +367,17 @@
 ashMap;%0A
+%0A
 import j
@@ -1514,20 +1514,16 @@
    %7D */%0A
-    
 %0A    pri
@@ -1695,37 +1695,32 @@
 arCollection.get
-Radar
 Stations();%0A    
@@ -1978,36 +1978,64 @@
 .set( s.getL
-atitude(), s
+ocation().getLatitude(), s.getLocation()
 .getLongitud
@@ -2214,36 +2214,64 @@
 .set( s.getL
-atitude(), s
+ocation().getLatitude(), s.getLocation()
 .getLongitud
@@ -2588,37 +2588,32 @@
 arCollection.get
-Radar
 Stations();%0A    
@@ -2736,36 +2736,64 @@
 .set( s.getL
-atitude(), s
+ocation().getLatitude(), s.getLocation()
 .getLongitud
@@ -3157,37 +3157,32 @@
 arCollection.get
-Radar
 Stations();%0A    
@@ -3611,16 +3611,197 @@
      %7D%0A%0A
+      /** Get all the Stations in the collection, allow user to cancel.%0A     * @param cancel allow user to cancel. Implementors should return ASAP.%0A     * @return List of Station */
 %0A%0A    pu
@@ -3964,24 +3964,110 @@
 ( );%0A     %7D%0A
+     /** Get all the Stations within a bounding box.%0A     * @return List of Station */
 %0A    public 
@@ -4273,21 +4273,16 @@
 tion.get
-Radar
 Stations
@@ -4413,36 +4413,64 @@
 .set( s.getL
-atitude(), s
+ocation().getLatitude(), s.getLocation()
 .getLongitud
@@ -4939,13 +4939,12 @@
   %7D%0A%0A%0A%0A%0A
-%0A
 %0A%0A%7D%0A

change name to stationRadarCollection