NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/TimeHandler/TimeHandler.widget.php

Revision 40 (checked in by jcleary, 16 years ago)

Production code checkin

Line 
1 <?php
2 /**
3  * My attempt at a Time Handler widget
4  */
5
6 include_once(dirname(__FILE__)."/../Widget.php");
7 include_once(dirname(__FILE__)."/../Label.php");
8
9
10 class TimeHandler extends CWCWidget
11 {
12     var $moLabel;
13     var $mszTitle = "Select an offset Time";
14     var $mszWidgetClass = "";
15
16     /**
17      * build a new time handler widget
18      */
19     function TimeHandler()
20     {
21         $this->mszLanguageResource = str_replace("\\","/",dirname(__FILE__))."/TimeHandler.dbf";
22
23         // invoke constructor of parent
24         parent::CWCWidget();
25
26         $this->mszTitle = trim($this->moMLT->get("0", "Select an Offset Time"));
27
28         $this->mnPriority = PRIORITY_HIGH;
29
30         // set the description for this widget
31         $this->szWidgetDescription = <<<EOT
32 Sends along an offset time in hours to various data sources and returns with data
33 EOT;
34
35         $this->moLabel = new CWCLabel( $this );
36
37         $this->maAttributes["TITLE"] = new StringAttribute( "TITLE", false );
38         $this->maAttributes["WIDGETCLASS"] = new StringAttribute( "WIDGETCLASS", false );
39         $this->mnMaturityLevel = MATURITY_BETA;
40     }
41     
42     
43     function InitDefaults()
44     {
45         parent::InitDefaults();
46         if (isset($this->maParams["WIDGETCLASS"]))
47             $this->mszWidgetClass = strtoupper($this->maParams["WIDGETCLASS"]);
48     }
49
50     /**
51      * get the javascript functions for this widget
52      */
53     function GetJavascriptFunctions()
54     {
55         parent::GetJavascriptFunctions();
56
57         if (isset($this->maSharedResourceWidgets["CWCJSAPI"]))
58           $bCWCJSAPI = 1;
59         else
60           $bCWCJSAPI = 0;
61
62         $aReturn = array();
63         $szJsFunctionName = "changeOffset";
64         $szFunction = <<<EOT
65 /**
66  * {$szJsFunctionName}
67  * called when the Time Offset value is changed.
68  */
69 function {$szJsFunctionName}(obj)
70 {
71     box = document.forms[0].OFFSET;
72     offset = box.options[box.selectedIndex].value;
73     if (offset != "")
74     {
75         //alert(offset);
76     {$this->mszHTMLForm}.NAV_OFFSET.value=offset;
77         {$this->mszHTMLForm}.SEL_OFFSET.value=box.options[box.selectedIndex].text;
78         {$this->mszHTMLForm}.NAV_CMD.value = "";
79         {$this->mszHTMLForm}.submit();
80     }
81     
82     return;
83 }
84 EOT;
85         $aReturn[$szJsFunctionName] = $szFunction;
86         
87         return $aReturn;
88     }
89
90     /**
91      * get the HTML variables for this widget
92      */
93    function GetHTMLHiddenVariables()
94     {
95         parent::GetHTMLHiddenVariables();
96
97         $szVariable = "NAV_OFFSET";
98     $szNavOffset = $this->getVar( "NAV_OFFSET" );
99         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"$szNavOffset\">\n";
100         $aReturn[$szVariable] = $szValue;
101
102         $szVariable = "SEL_OFFSET";
103     $szSelOffset = $this->getVar( "SEL_OFFSET" );
104         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"$szSelOffset\">\n";
105         $aReturn[$szVariable] = $szValue;
106         
107         return $aReturn;
108     }   
109     
110     /**
111      * process the url looking for timehandler requests
112      */
113     function  ParseURL()
114     {
115         parent::ParseURL();
116
117         if ($this->isVarSet( "NAV_OFFSET" ) &&
118             $this->getVar( "NAV_OFFSET" ) != "")
119         {
120             $szOffset = $this->getVar( "NAV_OFFSET" );
121
122         $oMap = $this->moMapObject->oMap;
123       
124         //hide mouseovers/insitu currents if offset is not 2
125         
126         //get layers to alter
127         $mouse_layer = $oMap->getLayerByName('Observation MouseOvers');
128         
129         if ($szOffset != 2)
130         {
131         // hide MouseOver option in legend
132         $mouse_layer->set('group','Hidden');             
133         $mouse_layer->SetMetaData('legendvis','hidden');
134                 
135         //if MouseOvers are ON when the time is changed these will make sure it doesn't render
136         $mouse_layer->set('minscale','10000000');
137         
138         $codar_offset = $szOffset;
139         }
140         
141         else
142         {
143             //return MouseOver to legend since -2 hrs
144             $mouse_layer->set('group','Observations');
145         $mouse_layer->SetMetaData('legendvis','');
146         
147         $codar_offset = $szOffset + 2;
148         }
149         
150
151         //here is the meat of the time offset handling
152             $label_layer = $oMap->getLayerByName('request_timestamp');
153             $class = $label_layer->getClass(0);
154         
155         //Eastern Time - offset for timestamp labelling
156         $request_time_eastern = time() - ($szOffset * 60 * 60);
157         
158         //format and send to mapfile for labelling
159         $timestamp = date('m-d-Y g\:\0\0 a', $request_time_eastern);
160         $class->settext($label_layer,"$timestamp");
161             
162         //GMT time - offset for DB request
163             $request_time = (strtotime (gmdate("m/d/Y H:i:s"))) - ($szOffset * 60 * 60);
164             
165         //GMT time - offset and then reformatted for QuikSCAT WMS service
166         $quikscat_time = date('Y_m_d_H_i_\0', $request_time);
167         
168         //GMT time - offset for CODAR DB request
169         $codar_request_time = (strtotime (gmdate("m/d/Y H:i:s"))) - ($codar_offset * 60 * 60);
170         
171         $nLayers = $oMap->numlayers;
172             for ($i=0;$i<$nLayers;$i++)
173             {
174                 $oLayer = $oMap->getLayer($i);
175         $db_processing = $oLayer->getMetaData('db_processing');
176         
177                 if ($oLayer->connectiontype == MS_WMS && $oLayer->group == 'Observations' && $oLayer->name != 'quikscat' && $oLayer->name != 'RADAR_large')
178                 {
179                     //$con_string = $oLayer->connection;
180                     $con_string = $oLayer->GetMetaData("wms_onlineresource");
181                     $new_con_string = $con_string.'&time_offset_hours='.$szOffset;
182                     //print $new_con_string;
183                     $oLayer->set('connection', $new_con_string);
184                 }
185                 
186         //handling for local QuikSCAT layers       
187         if ($oLayer->connectiontype == MS_WMS && $oLayer->name == 'quikscat_wms')
188                 {
189                     $qs_connection = $oLayer->GetMetaData("wms_onlineresource");
190                     $new_qs_connection = $qs_connection.'&time='.$quikscat_time;
191                     //print $new_qs_connection;
192                     $oLayer->set('connection', $new_qs_connection);
193                 }
194
195                 // for codar layer - dif time stamp than regular obs
196                 if ($oLayer->connectiontype == MS_WMS && $oLayer->name == 'RADAR_large')
197                 {
198                     //timestamp format is 2006-01-19 16:00
199                     $request_codar_time= date('Y_m_d_H_00_00', $codar_request_time);               
200
201                     $codar_connection = $oLayer->GetMetaData("wms_onlineresource");
202                     $new_codar_connection = $codar_connection.'&time='.$request_codar_time;
203                     $oLayer->set('connection', $new_codar_connection);
204                 }
205
206             
207             // for seacoos_test OBS layers
208         if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '1')
209                 {
210             //timestamp format is 2006-01-19 16:00
211             $request_time_db = date('Y-m-d H\:i', $request_time);   
212             $oLayer->setFilter('report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$request_time_db.'\')');
213         }
214             
215             
216         // for IS surface currents - codar TS
217                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '2')
218                 {
219                     $cur_request_time = date('Y-m-d H\:i', $codar_request_time);
220                     $oLayer->setFilter('(report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$cur_request_time.'\')) and (surface_or_bottom = \'surface\')');
221                 }
222
223         //for ADCP currents from Xenia - codar TS
224                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '4')
225                 {
226                     //timestamp format is 2006-01-19 16:00
227                     $cur_request_time = date('Y-m-d H\:i', $codar_request_time);
228             //$oLayer->setFilter('m_type_id = 41 and (sensor_id IN (select row_id from sensor where type_id = 4 )) and (d_report_hour = (timestamp without time zone \''.$cur_request_time.'\')) and (d_top_of_hour = 1)');
229                     $oLayer->setFilter('m_type_id = 41 and (sensor_id IN (select row_id from sensor where type_id =\'4\')) and  (m_date > (timestamp without time zone \''.$cur_request_time.'\' - interval \'30 minutes\' )) and (m_date < (timestamp without time zone \''.$cur_request_time.'\' + interval \'31 minutes\'))');
230                 }
231                 
232         }
233
234     }
235         return true;
236     }   
237     
238     /**
239      * draw this widget
240      */
241     function DrawPublish()
242     {
243         if (!$this->mbVisible)
244             return "<!-- Offset is hidden -->";
245
246         if ($this->isVarSet( "SEL_OFFSET" ))
247             $szSelOffset = $this->getVar( "SEL_OFFSET" );
248         else
249             $szSelOffset = "";
250
251         if ($this->mszWidgetClass != "")
252             $szWidgetClass = " CLASS=\"".$this->mszWidgetClass."\"";
253         else
254             $szWidgetClass = "";
255
256         $szResult = "<SELECT NAME=\"OFFSET\" onchange=\"changeOffset(this)\"$szWidgetClass>\n";
257
258         if (isset( $this->maszContents["OFFSET"] ))
259         {
260             foreach( $this->maszContents["OFFSET"] as $anOffset )
261             {
262                 if (isset($anOffset["HOUR"]))
263                 {           
264           $szResult .= "<OPTION VALUE=\"".$anOffset["HOUR"]."\" ".
265                         (($szSelOffset==$anOffset["NAME"]) ? "SELECTED":"").
266                         ">".$anOffset["NAME"]."</OPTION>\n";
267                 }
268                 else
269                 {
270                     $_SESSION['gErrorManager']->setError(ERR_WARNING,
271                       trim($this->moMLT->get("3", "ERROR: Invalid HOUR tag definition in TimeHandler.php.")));
272                 }
273             }
274         }
275
276         $szResult .= "</SELECT></td>\n";
277             
278        if ($this->isVarSet( "NAV_OFFSET" ) &&
279                $this->getVar( "NAV_OFFSET" ) != "")
280         {
281            $timeOffset_end = $this->getVar( "NAV_OFFSET" );
282         }       
283             else
284         {
285                $timeOffset_end = 2;
286         }
287         
288         //Eastern Time
289         $EasternTime = time();
290             
291         // use offset to build request times       
292         $request_time_end = $EasternTime - ($timeOffset_end * 60 * 60);
293         $timeOffset_begin = $timeOffset_end + 1;
294         $request_time_begin = $EasternTime - ($timeOffset_begin * 60 * 60);
295             
296         //$timestamp_now = date('m-d-Y g\:\0\0 a');
297         $timestamp_end = date('m-d-Y g\:\0\0 a', $request_time_end);
298         $timestamp_end_hour = date('g\:\0\0 a', $request_time_end);
299         
300         $timestamp_begin = date('m-d-Y g\:\3\0 a', $request_time_begin);
301         $timestamp_begin_hour = date('m-d-Y g\:\3\0', $request_time_begin);       
302         
303         
304     // RS Timestamp lookup for labeling - in GMT, out Eastern
305     // Site Path
306     $site_path = "/opt/inventory/htdocs/";
307     
308     // Include ADODB library
309     require_once($site_path . "/admin/adodb/adodb.inc.php");
310     
311     // Connection definitions
312     $database_type = "postgres7";
313     $database_user = "lookup";
314     $database_database = "seacoos_test";
315     $database_server = "coriolis.marine.unc.edu";
316     $database_password = "lookup";
317
318     // takes request time from WMS call above and reformts it
319         $request_time = (strtotime (gmdate("m/d/Y H:i:s"))) - ($timeOffset_end * 60 * 60);
320     $rs_request_time = date('Y-m-d H:i:\0\0', $request_time);
321     
322     // Initialize ADODB connection
323     $db = ADONewConnection($database_type);
324     $db->Connect($database_server, $database_user,$database_password,$database_database);
325
326
327     // Construct queries - looks for nearest overpass time not older than 48 hrs
328     // loop through for quikscat, modis_sst, avhrr
329
330         $rs_layers = array("quikscat","modis_sst","avhrr");
331         foreach ($rs_layers as $l)
332         {
333         $table = $l."_timestamps";
334
335     $query = "select $l from $table"
336        ." where abs(extract(epoch from timestamp without time zone '$rs_request_time')"
337        ." - extract(epoch from $l))"
338        ." = (select min(abs((extract(epoch from timestamp without time zone '$rs_request_time'))"
339        ." - extract(epoch from $l)))"
340        ." from $table)"
341        ." and abs(extract(epoch from timestamp without time zone '$rs_request_time')"
342        ." - extract(epoch from $l))"
343        ." <= 60*60*24*2";
344     
345     $recordSet = $db->Execute($query);
346     if (!$recordSet)
347     {
348         print $db->ErrorMsg();
349     }
350     else
351     {
352         while (!$recordSet->EOF)
353         {
354             $db_time = $recordSet->fields[0];
355             $recordSet->MoveNext();
356         }
357     }
358     
359     $recordSet->Close();
360     $rs_timestamp_utc = strtotime($db_time);
361
362         ## need to add Eastern to GMT offset var - hardcoded for now
363     $rs_timestamp_eastern = $rs_timestamp_utc - (5*60*60);
364
365         if ($l == "quikscat")
366         {
367       $quikscat_time = date('m-d-Y g\:\0\0 a', $rs_timestamp_eastern);
368         }
369         if ($l == "modis_sst")
370         {
371         $modis_timestamp = date('m-d-Y g\:i a', $rs_timestamp_eastern);
372         }
373     if ($l == "avhrr")
374         {
375         $avhrr_timestamp = date('m-d-Y g\:i a', $rs_timestamp_eastern);
376         }
377         
378         }
379     $db->Close();
380         
381         //added b/c CODAR default will be -4 hrs
382         if ($this->getVar( "NAV_OFFSET" ) == "" OR
383                 $this->getVar( "NAV_OFFSET" ) == "2" )
384         { 
385         $codar_time_raw = $EasternTime - (4 * 60 * 60);
386         $codar_time = date('m-d-Y g\:\0\0 a', $codar_time_raw);
387             }
388         else
389         {
390         $codar_time = $timestamp_end;
391         }   
392   
393         //return time table
394         //$szResult .="<tr><td>Now: </td><td>".$timestamp_now."</td></tr>\n";
395         $szResult .="<td><b>Data Display Time: </b></td><td><b>".$timestamp_end."</b></td></tr>\n";
396         $szResult .="<tr><td>SST, Water Level, & Wind:</td><td>".$timestamp_begin_hour." - ".$timestamp_end_hour."</td></tr>\n";
397         $szResult .="<tr><td rowspan=\"6\"></td><td>Precipitation RADAR: </td><td>".$timestamp_end."</td></tr>\n";
398             $szResult .="<tr><td colspan=\"2\"><hr></td></tr>\n";
399         $szResult .="<tr><td>QuikSCAT winds: </td><td>".$quikscat_time."</td></tr>\n";
400         $szResult .="<tr><td>Surface Currents: </td><td>".$codar_time."</td></tr>\n";
401         $szResult .="<tr><td>AVHRR SST: </td><td>".$avhrr_timestamp."</td></tr>\n";
402         $szResult .="<tr><td>MODIS RGB/SST: </td><td>".$modis_timestamp."</td></tr>\n";
403     
404     
405         $szResult = $this->moLabel->DrawPublish( $szResult );
406         return $szResult;
407     }
408 }
409 ?>
410
Note: See TracBrowser for help on using the browser.