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

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

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

Latest Chameleon code checkout from previous repository

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             $is_currents_layer = $oMap->getLayerByName('in_situ_currents');
129             $is_currents_arrow_layer = $oMap->getLayerByName('in_situ_currents_arrow');
130             $qs_layer = $oMap->getLayerByName('QS_1');
131            
132             if ($szOffset != 2)
133             {
134             // hide MouseOver option in legend
135             $mouse_layer->set('group','Hidden');               
136             $mouse_layer->SetMetaData('legendvis','hidden');
137                    
138             //if MouseOvers are ON when the time is changed these will make sure it doesn't render
139             $mouse_layer->set('minscale','10000000');
140            
141             //if is_current are ON when the time is changed these will make sure they don't render
142             $is_currents_layer->set('minscale','10000000');
143             $is_currents_arrow_layer->set('minscale','10000000');
144            
145             $codar_offset = $szOffset;
146             }
147            
148             else
149             {
150             //return MouseOver to legend since -2 hrs
151             $mouse_layer->set('group','Observations');
152             $mouse_layer->SetMetaData('legendvis','');
153            
154             //return IS current layers to normal scale dependencies
155             $is_currents_layer->set('minscale','150000');
156             $is_currents_arrow_layer->set('minscale','150000');
157            
158             $codar_offset = $szOffset + 2;
159             }
160            
161
162             //here is the meat of the time offset handling
163             $label_layer = $oMap->getLayerByName('request_timestamp');
164             $class = $label_layer->getClass(0);
165            
166             //Eastern Time - offset for timestamp labelling
167             $request_time_eastern = time() - ($szOffset * 60 * 60);
168            
169             //format and send to mapfile for labelling
170             $timestamp = date('m-d-Y g\:\0\0 a', $request_time_eastern);
171             $class->settext($label_layer,"$timestamp");
172            
173             //GMT time - offset for DB request
174             $request_time = (strtotime (gmdate("m/d/Y H:i:s"))) - ($szOffset * 60 * 60);
175            
176             //GMT time - offset and then reformatted for QuikSCAT WMS service
177             $quikscat_time = date('Y_m_d_H_\0_\0', $request_time);
178            
179             //GMT time - offset for CODAR DB request
180             $codar_request_time = (strtotime (gmdate("m/d/Y H:i:s"))) - ($codar_offset * 60 * 60);
181            
182             $nLayers = $oMap->numlayers;
183             for ($i=0;$i<$nLayers;$i++)
184             {
185                 $oLayer = $oMap->getLayer($i);
186                 $db_processing = $oLayer->getMetaData('db_processing');
187                
188                 if ($oLayer->connectiontype == MS_WMS && $oLayer->group == 'Observations' && $oLayer->name != 'quikscat')
189                 {
190                     //$con_string = $oLayer->connection;
191                     $con_string = $oLayer->GetMetaData("wms_onlineresource");
192                     $new_con_string = $con_string.'&time_offset_hours='.$szOffset;
193                     //print $new_con_string;
194                     $oLayer->set('connection', $new_con_string);
195                 }
196                
197                 //handling for local QuikSCAT layers           
198                 if ($oLayer->connectiontype == MS_WMS && $oLayer->name == 'quikscat_wms')
199                 {
200                     $qs_connection = $oLayer->GetMetaData("wms_onlineresource");
201                     $new_qs_connection = $qs_connection.'&time='.$quikscat_time;
202                     //print $new_qs_connection;
203                     $oLayer->set('connection', $new_qs_connection);
204                 }
205                
206                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '1')
207                 {
208                     //timestamp format is 2006-01-19 16:00
209                     $request_time_db = date('Y-m-d H\:i', $request_time);       
210                     $oLayer->setFilter('report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$request_time_db.'\')');
211                 }
212                
213                 if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '3')
214                 {
215                     //timestamp format is 2006-01-19 16:00
216                     $request_codar_db = date('Y-m-d H\:i', $codar_request_time);       
217                     $oLayer->setFilter('report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$request_codar_db.'\')');
218                 }
219                
220 //              if ($oLayer->connectiontype == MS_POSTGIS && $db_processing == '2')
221 //              {
222 //                 $request_time_db = date('Y-m-d H\:i', $request_time);
223 //                 $oLayer->setFilter('((select count(*) from current_prod where (report_time_stamp = date_trunc(\'hour\',timestamp without time zone \''.$request_time_db.'\'))) < 1)');
224 //              }
225                                
226             }
227
228         }
229         return true;
230     }   
231    
232     /**
233      * draw this widget
234      */
235     function DrawPublish()
236     {
237         if (!$this->mbVisible)
238             return "<!-- Offset is hidden -->";
239
240         if ($this->isVarSet( "SEL_OFFSET" ))
241             $szSelOffset = $this->getVar( "SEL_OFFSET" );
242         else
243             $szSelOffset = "";
244
245         if ($this->mszWidgetClass != "")
246             $szWidgetClass = " CLASS=\"".$this->mszWidgetClass."\"";
247         else
248             $szWidgetClass = "";
249
250         $szResult = "<SELECT NAME=\"OFFSET\" onchange=\"changeOffset(this)\"$szWidgetClass>\n";
251
252         if (isset( $this->maszContents["OFFSET"] ))
253         {
254             foreach( $this->maszContents["OFFSET"] as $anOffset )
255             {
256                 if (isset($anOffset["HOUR"]))
257                 {                       
258                   $szResult .= "<OPTION VALUE=\"".$anOffset["HOUR"]."\" ".
259                         (($szSelOffset==$anOffset["NAME"]) ? "SELECTED":"").
260                         ">".$anOffset["NAME"]."</OPTION>\n";
261                 }
262                 else
263                 {
264                     $_SESSION['gErrorManager']->setError(ERR_WARNING,
265                       trim($this->moMLT->get("3", "ERROR: Invalid HOUR tag definition in TimeHandler.php.")));
266                 }
267             }
268         }
269
270         $szResult .= "</SELECT></td>\n";
271                    
272            if ($this->isVarSet( "NAV_OFFSET" ) &&
273                $this->getVar( "NAV_OFFSET" ) != "")
274             {
275                $timeOffset_end = $this->getVar( "NAV_OFFSET" );
276             }       
277             else
278             {
279                $timeOffset_end = 2;
280             }
281            
282             //Eastern Time
283             $EasternTime = time();
284                    
285             // use offset to build request times           
286             $request_time_end = $EasternTime - ($timeOffset_end * 60 * 60);
287             $timeOffset_begin = $timeOffset_end + 1;
288             $request_time_begin = $EasternTime - ($timeOffset_begin * 60 * 60);
289            
290             //$timestamp_now = date('m-d-Y g\:\0\0 a');
291             $timestamp_end = date('m-d-Y g\:\0\0 a', $request_time_end);
292             $timestamp_end_hour = date('g\:\0\0 a', $request_time_end);
293            
294             $timestamp_begin = date('m-d-Y g\:\3\0 a', $request_time_begin);
295             $timestamp_begin_hour = date('m-d-Y g\:\3\0', $request_time_begin);     
296                
297            
298             //added to handle QuikSCAT overpass labeling - depends on request time of day
299             $request_hour = date ('G', $request_time_end);
300
301             //if request hour + offset is less than 9 (access time is 2am~9am), then use PM timestamp of the date prior or "no data" - this handles files that have not been created yet
302             if ($request_hour + $timeOffset_end < "9")
303             {
304                     //$quikscat_time = 'No overpass available';
305                     $request_date_prior = $request_time_end - (24 * 60 * 60);
306                     $quikscat_time = date('m-d-Y \6\:\0\0\ - \7\:\0\0 \p\m', $request_date_prior);
307             }
308
309             //if request hour + offset is > 14 but < 22 (access time is 2pm~10pm), then AM timestamp should be returned or "no data" - this handles files that have not been created yet
310             elseif ($request_hour + $timeOffset_end > "14" && $request_hour + $timeOffset_end < "22")
311             {
312                     //$quikscat_time = 'No overpass available';
313                     $quikscat_time = date('m-d-Y \5\:\0\0\ - \7\:\0\0 \a\m', $request_time_end);
314             }
315            
316             //if request time is 1 Eastern and before or = 13 then QS time is 5am-7am of that date
317             elseif ($request_hour >= "1" AND $request_hour <= "13")
318             {
319                     $quikscat_time = date('m-d-Y \5\:\0\0\ - \7\:\0\0 \a\m', $request_time_end);
320             }
321            
322             //if request time is 13 Eastern and before 23:59 then QS time is 6pm-7pm of that date
323             else
324             {
325                     $quikscat_time = date('m-d-Y \6\:\0\0\ - \7\:\0\0 \p\m', $request_time_end);                   
326             }
327            
328            
329             //added b/c CODAR default will be -4 hrs
330             if ($this->getVar( "NAV_OFFSET" ) == "" OR
331                 $this->getVar( "NAV_OFFSET" ) == "2" )
332             { 
333                 $codar_time_raw = $EasternTime - (4 * 60 * 60);
334                 $codar_time = date('m-d-Y g\:\0\0 a', $codar_time_raw);
335             }
336             else
337             {
338                 $codar_time = $timestamp_end;
339             }
340            
341             //get RS timestamps for given offset
342             $avhrr_timestamp_raw = file_get_contents('http://seacoos3.marine.unc.edu/msapps/rs_timestamps/avhrrsst'.$timeOffset_end.'.txt');
343             //$avhrr_timestamp = str_replace("/","-",$avhrr_timestamp_raw);
344             $avhrr_timestamp_utc = strtotime($avhrr_timestamp_raw);
345             $avhrr_timestamp_eastern = $avhrr_timestamp_utc - ($TimeDiff*60*60);
346             $avhrr_timestamp = date('m-d-Y g\:i a', $avhrr_timestamp_eastern);
347            
348             $modis_timestamp_raw = file_get_contents('http://seacoos3.marine.unc.edu/msapps/rs_timestamps/modissst'.$timeOffset_end.'.txt');
349             //$avhrr_timestamp_utc = str_replace("/","-",$modis_timestamp_raw);
350             $modis_timestamp_utc = strtotime($modis_timestamp_raw);
351             $modis_timestamp_eastern = $modis_timestamp_utc - ($TimeDiff*60*60);
352             $modis_timestamp = date('m-d-Y g\:i a', $modis_timestamp_eastern);
353            
354             //return time table
355             //$szResult .="<tr><td>Now: </td><td>".$timestamp_now."</td></tr>\n";
356             $szResult .="<td><b>Data Display Time: </b></td><td><b>".$timestamp_end."</b></td></tr>\n";
357             $szResult .="<tr><td>SST, Water Level, & Wind:</td><td>".$timestamp_begin_hour." - ".$timestamp_end_hour."</td></tr>\n";
358             $szResult .="<tr><td rowspan=\"6\"></td><td>Precipitation RADAR: </td><td>".$timestamp_end."</td></tr>\n";
359             $szResult .="<tr><td colspan=\"2\"><hr></td></tr>\n";
360             $szResult .="<tr><td>QuikSCAT winds: </td><td>".$quikscat_time."</td></tr>\n";
361             $szResult .="<tr><td>Surface Currents (CODAR): </td><td>".$codar_time."</td></tr>\n";
362             $szResult .="<tr><td>AVHRR SST: </td><td>".$avhrr_timestamp."</td></tr>\n";
363             $szResult .="<tr><td>MODIS RGB/SST: </td><td>".$modis_timestamp."</td></tr>\n";
364        
365        
366         $szResult = $this->moLabel->DrawPublish( $szResult );
367         return $szResult;
368     }
369 }
370 ?>
Note: See TracBrowser for help on using the browser.