Source for file search_class.php
Documentation is available at search_class.php
* Composes search queries and returns results.
* This file contains the {@link search} class. The class is responsible for
* composing search queries and returning the search results.
* This file is part of HRDIODB.
* HRDIODB is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* HRDIODB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with HRDIODB; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* The search class handles all SQL related searching logic. An object of this
* class populates a results array with key value pairs from the database based
* on conditions supplied to the class instance.
* Takes a {@link hrdi_db} class instance. Sets the internal result array to
* @param hrdi_db $hdb The hrdi_db class instance
$this->results = array();
* Takes an array of conditions and based on those conditions stores a list
* of UARS_Day numbers in the results array.
* @param array $conditions An indexed array of associative arrays with four
* values: Table, Field, Operator, Value
* @return mixed An indexed array of UARS days
$regex_year_day = "/^\d{4}-\d{1,3}$/";
$regex_uars_day = "/^\d+$/";
$this->conditions = $conditions;
foreach ( $conditions as $i=> $condition ){
if($condition['Field']== 'UARS_Day'){
if(preg_match($regex_year_day,$condition['Value'])){
elseif(preg_match($regex_uars_day,$condition['Value'])){
if($condition['Field']== 'Created'){
if(preg_match($regex_year_day,$condition['Value'])){
elseif(preg_match($regex_uars_day,$condition['Value'])){
if($condition['Field']== 'Modified'){
if(preg_match($regex_year_day,$condition['Value'])){
elseif(preg_match($regex_uars_day,$condition['Value'])){
if($condition['Field']== 'year_doy'){
$this->conditions[$i]['Field']= 'UARS_Day';
if($condition['Field']== 'calendar_date'){
$this->conditions[$i]['Field']= 'UARS_Day';
return $this->results = $this->hdb->get_column($query);
* Filters search results by file type.
* Optionally called after a search has been made. It will compose and
* execute a query which retrieves all UARS_Day numbers which have filetypes
* matching the given array of filetypes. The search instances result set is
* then overwritten by the intersection of the previous search results and
* the filetype search results. This method a query for each file type and
* discards some fetched data, however it makes up for this by never needing
* to reference all UARS days in one query. That would potentially by tens
* of thousands of integers in one query.
* @param array $filetypes An array of integers which are filetypes stored in
* @return mixed An indexed array of UARS days
if (!isset ($this->results)) {
return inline_error("A search must be made before results can be filtered");
foreach($filetypes as $filetype){
$sql = "SELECT d.UARS_Day
FROM HRDI_Days d, HRDI_Files f
WHERE f.UARS_Day = d.UARS_Day
$files = $this->hdb->get_column($sql);
return sort($this->results);
* Converts an array of conditions into a SQL query. Used by the free form
* @return string SQL query
if (($this->conditions == "") | !isset ($this->conditions)) {
return error("No search conditions set");
if(!isset ($this->conditions[0]['Table'])){
return error("The first conditions entry MUST have a table.");
// Only the first Table statement in the condition array is used
FROM ". $this->conditions[0]['Table'] . "
foreach($this->conditions as $condition) {
if(($condition['Table'] == "") | ($condition['Field'] == "") | ($condition['Value'] == "")) {
return error("Invalid search conditions");
$sub_query[] .= "{ $condition['Field']} { $condition['Operator']} '{ $condition['Value']}' ";
$sql .= implode($sub_query, " AND ");
$sql .= " GROUP BY UARS_Day";
* Builds Pid day coverage search query.
* This function takes several variables, builds a query, and returns an
* array of UARS days. It is a companion function to a custom search form.
* @param mixed $pid A Process_ID number or the keyword "any"
* @param mixed $date_type A string which describes the format of the high
* @param mixed $date_value_low The low end of a date range
* @param mixed $date_value_high The high end of a date range
* @param int $lat_day_min The minimum acceptable day latitude
* @param int $lat_day_max The maximum acceptable day latitude
* @param int $lat_night_min The minimum acceptable night latitude
* @param int $lat_night_max The maximum acceptable night latitude
* @return mixed An indexed array of UARS days
function pid_day_coverage($pid= "any", $date_type= "none", $date_value_low, $date_value_high,
$lat_day_min, $lat_day_max, $lat_night_min, $lat_night_max) {
$sets[]= 'Daytime mesosphere';
$sets[]= 'Nighttime mesosphere';
$queries[]= " m.UARS_Day = d.UARS_Day AND m.Process_ID = $pid ";
$queries[]= "m.UARS_Day = d.UARS_Day AND m.Process_ID IN (27, 29, 35, 37, 41) ";
$queries[]= "m.UARS_Day = d.UARS_Day AND
(m.Process_ID in (27, 29, 35, 36, 45, 60 ) OR
(m.Process_ID = 37 AND d.UARS_Day > 2740))
$queries[]= "m.UARS_Day = d.UARS_Day AND
(m.Process_ID = 37 AND d.UARS_Day < 188) OR
(m.Process_ID = 41 AND d.UARS_Day < 173) OR
(m.Process_ID = 41 AND d.UARS_Day > 2740))
case "Daytime mesosphere":
$queries[]= "m.UARS_Day = d.UARS_Day AND
(m.Process_ID in (27, 29, 35 ) OR
(m.Process_ID = 37 AND d.UARS_Day > 2740))
case "Nighttime mesosphere":
$queries[]= "m.UARS_Day = d.UARS_Day AND m.Process_ID IN (36, 45, 60) ";
return error("Pid must be \"any\" or a number, not: \"$pid\"");
if ($date_type != 'none') {
if(!empty($date_value_low) && empty($date_value_high)){
$date_value_high = $date_value_low;
$_GET['date_value_high']= $date_value_high;
if ($date_type == 'UARS_Day') {
$high = $date_value_high;
// Convert day of year to UARS_Day
elseif ($date_type == 'year_day') {
// Convert calendar day to UARS_Day
elseif ($date_type == 'calendar_day') {
} elseif ($date_type != 'none') {
return inline_error("unable convert \"$date_type\" to UARS_day");
if ($date_type != 'none') {
$queries[] = "d.UARS_day >= $low and d.UARS_day <= $high";
// Compose day coverage query
return inline_error("You must enter numbers for both latitude min and max");
$between = "BETWEEN $lat_day_min AND $lat_day_max";
$fields = array("d.cov_LAT_Min_WDA","d.cov_LAT_Max_WDA","d.cov_LAT_Min_WDD",
"d.cov_LAT_Max_WDD","d.cov_LAT_Min_CDA","d.cov_LAT_Max_CDA",
"d.cov_LAT_Min_CDD","cov_LAT_Max_CDD");
foreach($fields as $field){
$pieces[] = "($field $between)";
$queries[] = "(". implode(" OR ", $pieces). ")";
// Compose night coverage query
if($lat_night_min != ""){
return inline_error("You must enter numbers for both latitude min and max");
$between = "BETWEEN $lat_night_min AND $lat_night_max";
$fields = array("d.cov_LAT_Min_WNA","d.cov_LAT_Max_WNA","d.cov_LAT_Min_WND",
"d.cov_LAT_Max_WND","d.cov_LAT_Min_CNA","d.cov_LAT_Max_CNA",
"d.cov_LAT_Min_CND","cov_LAT_Max_CND");
foreach($fields as $field){
$pieces[] = "($field $between)";
$queries[] = "(". implode(" OR ", $pieces). ")";
$sql = "SELECT d.UARS_Day
FROM HRDI_Days d, HRDI_Modes m
WHERE d.UARS_Day=m.UARS_Day ";
$sql .= "AND ". implode(" AND ", $queries);
$sql .= " GROUP BY d.UARS_Day;";
return $this->results = $this->hdb->get_column($sql);
|