Index: /trunk/htdocs2/Classes/Queries/ConeQuery.php
===================================================================
--- /trunk/htdocs2/Classes/Queries/ConeQuery.php	(revision 479)
+++ /trunk/htdocs2/Classes/Queries/ConeQuery.php	(revision 480)
@@ -50,17 +50,41 @@
     $this->observations = Array();
 
-    $this->BuildQueryString();
-    $this->database->DoQuery( $this->sql );
-    $this->ExtractResultSet( $this->database->GetLastResult() );
-    
-    // if we have MAX_ROWS results, see how many were truncated
-    if( count( $this->observations ) == MAX_ROWS )
-    {
-      $this->sql = ' SELECT count(*) as trunc_count ';
-      $this->BuildFromString();
-      $this->BuildWhereString();
-      $this->database->DoQuery( $this->sql );
-      $only_row = $this->database->FetchRowAsArray();
-      $this->num_rows_truncated = $only_row['trunc_count'] - MAX_ROWS;
+    // if we are only looking at timing data
+    //   see how many results we would get from the observations table
+    //   get those results limited to MAX_ROWS
+    //   store the number truncated
+    // otherwise if we are only looking at search mode data
+    //   see how many results we would get from the s70 table
+    //   get those results limited to MAX_ROWS
+    // otherwise
+    //   see how many results we would get from the observations table
+    //   see how many results we would get from the s70 table
+    //   get the results from the observations table limited to MAX_ROWS/2
+    //   get the results from the s70 table limited to MAX_ROWS/2
+
+    if( $this->parameters->GetObsMode() == 'timing' )
+    {
+      $obs_count = $this->CountObservationResults();
+      $this->GetObservationHeaders( 'observations', MAX_ROWS );
+      if( $obs_count > MAX_ROWS )
+        $this->num_rows_truncated = $obs_count - MAX_ROWS;
+    }
+    else if( $this->parameters->GetObsMode() == 'search' )
+    {
+      $s70_count = $this->CountObservationResults();
+      $this->GetObservationHeaders( 's70', MAX_ROWS );
+      if( $s70_count > MAX_ROWS )
+        $this->num_rows_truncated = $s70_count - MAX_ROWS;
+    }
+    else
+    {
+      $obs_count = $this->CountObservationResults();
+      $s70_count = $this->CountObservationResults();
+      $this->GetObservationHeaders( 'observations', MAX_ROWS / 2 );
+      $this->GetObservationHeaders( 's70', MAX_ROWS / 2 );
+      if( $s70_count > (MAX_ROWS / 2) )
+        $this->num_rows_truncated += $s70_count - (MAX_ROWS / 2);
+      if( $obs_count > (MAX_ROWS / 2) )
+        $this->num_rows_truncated += $obs_count - (MAX_ROWS / 2);
     }
   }
@@ -77,5 +101,8 @@
   private $num_rows_truncated;
   
-    //! Convert the result set array to an array of observations
+  /// This function takes an array of tuples from the database (observation or s70 tables)
+  /// and adds an entry in the observations array for each of them, as an instance of
+  /// ObservationHeader.
+  
   private function ExtractResultSet( $result_set )
   {
@@ -87,4 +114,7 @@
     }
   }
+  
+  /// This function builds the conesearch calculate part of the sql query and stores it
+  /// in $this->cone_string as it's used in both the select and where parts of the queries.
   
   private function BuildConeString()
@@ -102,4 +132,8 @@
   }
   
+  
+  
+  /// Build the basic select part of the query and add it to $this->sql
+  
   private function BuildSelectString()
   {
@@ -107,8 +141,16 @@
   }
   
-  private function BuildFromString()
-  {
-    $this->sql .= ' FROM  observations ';
-  }
+  /// Build the from part of the query and add it to $this->sql
+  private function BuildFromString( $tablename )
+  {
+    $this->sql .= " FROM $tablename ";
+  }
+  
+  
+  
+  /// Build the where clause for the query and add it to $this->sql. This includes
+  /// the conesearch part, the maximum date range and matching project_ids for any
+  /// that the user might have access to. We also limit our results in order to
+  /// reduce memory usage (only 16M per script) and set an order for the results.
   
   private function BuildWhereString()
@@ -126,4 +168,21 @@
     if( $project_id != '')
       $this->sql .= " AND project_id = '$project_id' ";
+    
+    $bands = $this->parameters->GetBands();
+    $nbands = count( $bands );
+    if( $nbands > 0 )
+    {
+      $this->sql .= ' AND (';
+      for( $bind = 0; $bind < $nbands; $bind ++ ) 
+      {
+        $this->sql .= ' ( ';
+        $this->sql .= ' obsfreq >= ' . $bands[$bind]->GetLowerBound();
+        $this->sql .= ' AND obsfreq <= ' . $bands[$bind]->GetUpperBound();
+        $this->sql .= ' ) ';
+        if( $bind < $nbands - 1 )
+          $this->sql .= ' OR ';
+      }
+      $this->sql .= ' ) ';
+    }
     
     $date_range = $this->parameters->GetDateRange();
@@ -141,8 +200,16 @@
   }
   
-  private function BuildLimitString()
-  {
-    $this->sql .= ' LIMIT ' . MAX_ROWS;
-  }
+  
+  
+  /// Add a simply limit clause to the $this->sql query string.
+  
+  private function BuildLimitString( $limit )
+  {
+    $this->sql .= " LIMIT $limit";
+  }
+  
+  
+  
+  /// Add a simply order string to the $this->sql query string.
   
   private function BuildOrderString()
@@ -152,15 +219,8 @@
   }
   
-  private function BuildQueryString()
-  {
-    $this->BuildConeString();
-    $this->BuildSelectString();
-    $this->BuildFromString();
-    $this->BuildWhereString();
-    $this->BuildOrderString();
-    $this->BuildLimitString();
-    
-//     echo 'the query built was ' . $this->sql .' <br> ';
-  }
+  
+  /// This function uses the list of projects that the user is authorized to see to
+  /// create an IN clause that will allow them to see any observations for these
+  /// projects even if they are within the proprietary period.
   
   private function BuildAuthorizedProjectsString()
@@ -183,4 +243,49 @@
     }
   }
+  
+  
+  /// This function counts how many observations we will get given the query parameters
+  /// without actually fetching those results, so we can figure out how many we are
+  /// truncating.
+  
+  private function CountObservationResults()
+  {
+    $this->sql = ' SELECT count(*) AS TRUNC_COUNT ';
+    $this->BuildFromString( 'observations' );
+    $this->BuildWhereString();
+    $this->database->DoQuery( $this->sql );
+    $only_row = $this->database->FetchRowAsArray();
+    return $only_row['TRUNC_COUNT'];
+  }
+  
+  /// This function counts how many s70 results we will get given the query parameters
+  /// without actaullyl fetching those results.
+  
+  private function CountS70Results()
+  {
+    $this->sql = ' SELECT count(*) AS TRUNC_COUNT ';
+    $this->BuildFromString( 's70' );
+    $this->BuildWhereString();
+    $this->database->DoQuery( $this->sql );
+    $only_row = $this->database->GetRowAsArray();
+    return $only_row['TRUNC_COUNT'];
+  }
+  
+  
+  
+  /// This function creates a query string 
+  
+  private function GetObservationHeaders( $table, $limit )
+  {
+    $this->sql = '';
+    $this->BuildConeString();
+    $this->BuildSelectString();
+    $this->BuildFromString( $table );
+    $this->BuildWhereString();
+    $this->BuildOrderString();
+    $this->BuildLimitString( $limit );
+    $this->database->DoQuery( $this->sql );
+    $this->ExtractResultSet( $this->database->GetLastResult() );
+  }
 };
 
