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

root/RS_timestamps/trunk/RS_timestamps/get_times.pl

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

Checkin from Data Viz project - RM'd form that project

Line 
1 #! /usr/bin/perl -wT
2
3 ## NAME: get_times.pl
4 ##
5 ## Gets latest RS overpass timestamp from SC web page
6 ##
7 ## This script gets the latest RS timestamps from a SC web page to insert into a local DB.  It then queries the DB for the overapss tiemstamps for various hourly offsets.
8 ## These offset text files are used for labeling on the NCCOOS Interactive Map and  also for labeling the RS images generated by SEACOOS cachebot.
9
10 use lib ".";
11 use LWP::Simple;
12 use Date::Format;
13 use Date::Parse;
14 use DBI;
15
16 $dest_dir='/var/www/html/rs_timestamps/'; 
17
18 ### get html file
19 $url_base='http://nautilus.baruch.sc.edu/seacoos_misc/show_sea_coos_obs_time_ranges.php';
20 $target_raw = $dest_dir.'rs_timestamp.html';
21 getstore($url_base,$target_raw);
22
23 ### parse through file and cut out relevant timestamps
24 @layers = ('avhrr_sst','modis_sst','modis_rgb_composite','oi_sst');
25
26 if(-e $target_raw)
27 {
28
29 my ($this_layer_time);
30 foreach (@layers)
31         {
32         $this_layer = $_;
33         $search_string = '\?layer_name='.$this_layer.'.+';
34
35         open (TIME, $target_raw);
36         @t = readline TIME;
37         @timestring = grep(/$search_string/, @t);
38         close TIME;
39
40         ### cut out timestamp string only
41
42         $timestring[0] =~ m/max_time_stamp=(.*?")/;
43         $this_layer_time = $1;
44         $this_layer_time =~ s/:00"$//;
45
46         if ($this_layer =~ /avhrr_sst/)
47                 {
48                         ## insert TS into DB
49                         my ($dbh, $sql, $sth);
50                         $dbh = DBI->connect ( 'dbi:Pg:dbname=seacoos_test;host=coriolis.marine.unc.edu', 'jcleary', '',
51                                 {
52                                         PrintError => 1,
53                                         RaiseError => 0
54                                 }) || die "$DBI::errstr";
55
56                                 $sql = "INSERT INTO avhrr_timestamps VALUES ('$this_layer_time')";                                                                                                                         
57                                 $dbh->do( $sql );
58                                 $dbh->disconnect();
59                 }
60         elsif ($this_layer =~ /modis_sst/)
61                 {
62                         ## insert TS into DB
63                         my ($dbh, $sql, $sth);
64                         $dbh = DBI->connect ( 'dbi:Pg:dbname=seacoos_test;host=coriolis.marine.unc.edu', 'jcleary', '',
65                                 {
66                                         PrintError => 1,
67                                         RaiseError => 0
68                                 }) || die "$DBI::errstr";
69                                
70                         $sql = "INSERT INTO modis_sst_timestamps VALUES ('$this_layer_time')";                                                                                                                         
71                         $dbh->do( $sql );
72                         $dbh->disconnect();
73                 }
74                 elsif ($this_layer =~ /modis_rgb_composite/)
75                 {
76                         ## insert TS into DB
77                         my ($dbh, $sql, $sth);
78                         $dbh = DBI->connect ( 'dbi:Pg:dbname=seacoos_test;host=coriolis.marine.unc.edu', 'jcleary', '',
79                                 {
80                                         PrintError => 1,
81                                         RaiseError => 0
82                                 }) || die "$DBI::errstr";
83                         $sql = "INSERT INTO modis_rgb_timestamps VALUES ('$this_layer_time')";                                                                                                                         
84                         $dbh->do( $sql );
85                         $dbh->disconnect();
86                 }
87                 elsif ($this_layer =~ /oi_sst/) {
88                         ## insert TS into DB
89                         my ($dbh, $sql, $sth);
90                         $dbh = DBI->connect ( 'dbi:Pg:dbname=seacoos_test;host=coriolis.marine.unc.edu', 'jcleary', '',
91                                 {
92                                         PrintError => 1,
93                                         RaiseError => 0
94                                 }) || die "$DBI::errstr";
95                         $sql = "INSERT INTO oi_sst_timestamps VALUES ('$this_layer_time')";                                                                                                                         
96                         $dbh->do( $sql );
97                         $dbh->disconnect();
98                 }
99                 else
100                 {}
101         }
102
103 ## Timestamps extraction for cached image labels
104 my ($timestamp,$raw_timestamp,$time,$pass_timestamp);
105 @offsets = ('2','3','4','5','6','8','7');
106
107 foreach (@offsets)
108         {
109         $offset = $_;           
110
111         ## Date subtraction and formatting for query
112         my ($sec,$min,$hour,$mday,$mon,$year) = gmtime(time-60*60*$offset);
113         my $format_year = $year + 1900;
114         my $format_month = sprintf ("%02d",$mon+1);
115         my $format_day = sprintf ("%02d",$mday);
116         my $format_hour = sprintf ("%02d",$hour);
117
118         # format query timestamp
119         $timestamp = $format_year."_".$format_month."_".$format_day."_".$format_hour."_".$min."_00";
120        
121         @layers = ('avhrr','modis_sst','modis_rgb','quikscat');
122         foreach (@layers)
123                 {
124
125                 $this_layer = $_;
126        
127                 ## lookup time in DB
128                 my ($dbh, $sql, $sth);
129                 $dbh = DBI->connect ( 'dbi:Pg:dbname=seacoos_test;host=coriolis.marine.unc.edu', 'lookup', 'lookup',
130                         {
131                                 PrintError => 1,
132                                 RaiseError => 0
133                         }) || die "$DBI::errstr";
134
135                 #select data from DB
136                 my $this_table = $this_layer."_timestamps";
137                 $sql= "select $this_layer from $this_table"
138                 ." where abs(extract(epoch from to_timestamp('$timestamp','YYYY_MM_DD_HH_MI_SS'))"
139                 ." - extract(epoch from $this_layer))"
140                 ." = (select min(abs((extract(epoch from to_timestamp('$timestamp','YYYY_MM_DD_HH_MI_SS')))"
141                 ." - extract(epoch from $this_layer)))"
142                 ." from $this_table)"
143                 ." and abs(extract(epoch from to_timestamp('$timestamp','YYYY_MM_DD_HH_MI_SS'))"
144                 ." - extract(epoch from $this_layer))"
145                 ." <= 60*60*24*2";
146
147                 $sth= $dbh->prepare( $sql );
148                 $sth->execute();
149                 $sth->bind_columns( \$raw_timestamp );
150                 $sth->fetch();
151
152                 $sth->finish;
153                 $dbh->disconnect;
154                
155                 $time = str2time($raw_timestamp);
156                 $pass_timestamp = time2str("%m/%d/%Y %R", $time);
157                        
158                 if ($this_layer =~ /avhrr/)                                                                                                                                               
159                         {
160                                 open (FOO, ">$dest_dir/avhrrsst$offset.txt");
161                                 print {FOO } $pass_timestamp." UTC";
162                                 close FOO;
163                         }
164                 elsif ($this_layer =~ /modis_sst/)                                                                                                                                           
165                         {
166                                 open (FOO, ">$dest_dir/modissst$offset.txt");                                                                                                           
167                                 print {FOO } $pass_timestamp." UTC";                                                                                                                                             
168                                 close FOO;     
169                         }               
170                 elsif ($this_layer =~ /modis_rgb/)                                                                                                                                           
171                         {       
172                                 open (RGB, ">$dest_dir/modisrgb$offset.txt");
173                                 print {RGB } $pass_timestamp." UTC+";
174                                 close RGB;
175
176                                 open (CA, ">$dest_dir/modisca$offset.txt");                                                                                                                   
177                                 print {CA } $pass_timestamp." UTC";                                                                                                 
178                                 close CA;
179
180                                 open (ERGB, ">$dest_dir/modisergb$offset.txt");                                                                                                                   
181                                 print {ERGB } $pass_timestamp." UTC";                                                                                                                   
182                                 close ERGB;                                                                                                                       
183                         }
184                         elsif ($this_layer =~ /quikscat/)                                                                                                                                           
185                         {
186                                 open (FOO, ">$dest_dir/quikscat$offset.txt");                                                                                                           
187                                 print {FOO } $pass_timestamp." UTC";                                                                                                                                             
188                                 close FOO;
189                         }
190                 }       
191         }
192 }
193
194 exit 0;
Note: See TracBrowser for help on using the browser.