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

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

Revision 21 (checked in by jcleary, 17 years ago)

Production version on Maury

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')
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 seacoos_test OBS layers
196                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '1')
197                 {
198                     //timestamp format is 2006-01-19 16:00
199                     $request_time_db = date('Y-m-d H\:i', $request_time);       
200                     $oLayer->setFilter('report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$request_time_db.'\')');
201                 }
202                
203                 // for codar layer - dif time stamp than regular obs
204                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '3')
205                 {
206                     //timestamp format is 2006-01-19 16:00
207                     $request_codar_db = date('Y-m-d H\:i', $codar_request_time);       
208                     $oLayer->setFilter('report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$request_codar_db.'\')');
209                 }
210                
211                 // for IS surface currents - codar TS
212                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '2')
213                 {
214                     $cur_request_time = date('Y-m-d H\:i', $codar_request_time);
215                     $oLayer->setFilter('(report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$cur_request_time.'\')) and (surface_or_bottom = \'surface\')');
216                 }
217
218                 //for ADCP currents from Xenia - codar TS
219                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '4')
220                 {
221                     //timestamp format is 2006-01-19 16:00
222                     $cur_request_time = date('Y-m-d H\:i', $codar_request_time);
223                     //$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)');
224                     $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\'))');
225                 }
226                                
227             }
228
229         }
230         return true;
231     }   
232    
233     /**
234      * draw this widget
235      */
236     function DrawPublish()
237     {
238         if (!$this->mbVisible)
239             return "<!-- Offset is hidden -->";
240
241         if ($this->isVarSet( "SEL_OFFSET" ))
242             $szSelOffset = $this->getVar( "SEL_OFFSET" );
243         else
244             $szSelOffset = "";
245
246         if ($this->mszWidgetClass != "")
247             $szWidgetClass = " CLASS=\"".$this->mszWidgetClass."\"";
248         else
249             $szWidgetClass = "";
250
251         $szResult = "<SELECT NAME=\"OFFSET\" onchange=\"changeOffset(this)\"$szWidgetClass>\n";
252
253         if (isset( $this->maszContents["OFFSET"] ))
254         {
255             foreach( $this->maszContents["OFFSET"] as $anOffset )
256             {
257                 if (isset($anOffset["HOUR"]))
258                 {                       
259                   $szResult .= "<OPTION VALUE=\"".$anOffset["HOUR"]."\" ".
260                         (($szSelOffset==$anOffset["NAME"]) ? "SELECTED":"").
261                         ">".$anOffset["NAME"]."</OPTION>\n";
262                 }
263                 else
264                 {
265                     $_SESSION['gErrorManager']->setError(ERR_WARNING,
266                       trim($this->moMLT->get("3", "ERROR: Invalid HOUR tag definition in TimeHandler.php.")));
267                 }
268             }
269         }
270
271         $szResult .= "</SELECT></td>\n";
272                    
273            if ($this->isVarSet( "NAV_OFFSET" ) &&
274                $this->getVar( "NAV_OFFSET" ) != "")
275             {
276                $timeOffset_end = $this->getVar( "NAV_OFFSET" );
277             }       
278             else
279             {
280                $timeOffset_end = 2;
281             }
282            
283             //Eastern Time
284             $EasternTime = time();
285                    
286             // use offset to build request times           
287             $request_time_end = $EasternTime - ($timeOffset_end * 60 * 60);
288             $timeOffset_begin = $timeOffset_end + 1;
289             $request_time_begin = $EasternTime - ($timeOffset_begin * 60 * 60);
290            
291             //$timestamp_now = date('m-d-Y g\:\0\0 a');
292             $timestamp_end = date('m-d-Y g\:\0\0 a', $request_time_end);
293             $timestamp_end_hour = date('g\:\0\0 a', $request_time_end);
294            
295             $timestamp_begin = date('m-d-Y g\:\3\0 a', $request_time_begin);
296             $timestamp_begin_hour = date('m-d-Y g\:\3\0', $request_time_begin);     
297            
298            
299         // RS Timestamp lookup for labeling - in GMT, out Eastern
300         // Site Path
301         $site_path = "/opt/inventory/htdocs/";
302        
303         // Include ADODB library
304         require_once($site_path . "/admin/adodb/adodb.inc.php");
305        
306         // Connection definitions
307         $database_type = "postgres7";
308         $database_user = "lookup";
309         $database_database = "seacoos_test";
310         $database_server = "coriolis.marine.unc.edu";
311         $database_password = "lookup";
312
313         // takes request time from WMS call above and reformts it
314         $request_time = (strtotime (gmdate("m/d/Y H:i:s"))) - ($timeOffset_end * 60 * 60);
315         $rs_request_time = date('Y-m-d H:i:\0\0', $request_time);
316        
317         // Initialize ADODB connection
318         $db = ADONewConnection($database_type);
319         $db->Connect($database_server, $database_user,$database_password,$database_database);
320
321
322         // Construct queries - looks for nearest overpass time not older than 48 hrs
323         // loop through for quikscat, modis_sst, avhrr
324
325         $rs_layers = array("quikscat","modis_sst","avhrr");
326         foreach ($rs_layers as $l)
327         {
328         $table = $l."_timestamps";
329
330         $query = "select $l from $table"
331            ." where abs(extract(epoch from timestamp without time zone '$rs_request_time')"
332            ." - extract(epoch from $l))"
333            ." = (select min(abs((extract(epoch from timestamp without time zone '$rs_request_time'))"
334            ." - extract(epoch from $l)))"
335            ." from $table)"
336            ." and abs(extract(epoch from timestamp without time zone '$rs_request_time')"
337            ." - extract(epoch from $l))"
338            ." <= 60*60*24*2";
339        
340         $recordSet = $db->Execute($query);
341         if (!$recordSet)
342         {
343                 print $db->ErrorMsg();
344         }
345         else
346         {
347                 while (!$recordSet->EOF)
348                 {
349                         $db_time = $recordSet->fields[0];
350                         $recordSet->MoveNext();
351                 }
352         }
353        
354         $recordSet->Close();
355         $rs_timestamp_utc = strtotime($db_time);
356
357         ## need to add Eastern to GMT offset var - hardcoded for now
358         $rs_timestamp_eastern = $rs_timestamp_utc - (5*60*60);
359
360         if ($l == "quikscat")
361         {
362         $quikscat_time = date('m-d-Y g\:\0\0 a', $rs_timestamp_eastern);
363         }
364         if ($l == "modis_sst")
365         {
366         $modis_timestamp = date('m-d-Y g\:i a', $rs_timestamp_eastern);
367         }
368         if ($l == "avhrr")
369         {
370         $avhrr_timestamp = date('m-d-Y g\:i a', $rs_timestamp_eastern);
371         }
372        
373         }
374         $db->Close();
375            
376             //added b/c CODAR default will be -4 hrs
377             if ($this->getVar( "NAV_OFFSET" ) == "" OR
378                 $this->getVar( "NAV_OFFSET" ) == "2" )
379             { 
380                 $codar_time_raw = $EasternTime - (4 * 60 * 60);
381                 $codar_time = date('m-d-Y g\:\0\0 a', $codar_time_raw);
382             }
383             else
384             {
385                 $codar_time = $timestamp_end;
386             }   
387    
388             //return time table
389             //$szResult .="<tr><td>Now: </td><td>".$timestamp_now."</td></tr>\n";
390             $szResult .="<td><b>Data Display Time: </b></td><td><b>".$timestamp_end."</b></td></tr>\n";
391             $szResult .="<tr><td>SST, Water Level, & Wind:</td><td>".$timestamp_begin_hour." - ".$timestamp_end_hour."</td></tr>\n";
392             $szResult .="<tr><td rowspan=\"6\"></td><td>Precipitation RADAR: </td><td>".$timestamp_end."</td></tr>\n";
393             $szResult .="<tr><td colspan=\"2\"><hr></td></tr>\n";
394             $szResult .="<tr><td>QuikSCAT winds: </td><td>".$quikscat_time."</td></tr>\n";
395             $szResult .="<tr><td>Surface Currents: </td><td>".$codar_time."</td></tr>\n";
396             $szResult .="<tr><td>AVHRR SST: </td><td>".$avhrr_timestamp."</td></tr>\n";
397             $szResult .="<tr><td>MODIS RGB/SST: </td><td>".$modis_timestamp."</td></tr>\n";
398        
399        
400         $szResult = $this->moLabel->DrawPublish( $szResult );
401         return $szResult;
402     }
403 }
404 ?>
Note: See TracBrowser for help on using the browser.