| 1 |
#!/usr/bin/perl -w |
|---|
| 2 |
|
|---|
| 3 |
use CGI qw(:standard); |
|---|
| 4 |
my $query = new CGI; |
|---|
| 5 |
|
|---|
| 6 |
# look at an 1.5 hours ago as default time |
|---|
| 7 |
my $this_time_stamp = gmtime(time-60*60*1.5); |
|---|
| 8 |
|
|---|
| 9 |
# see if an offset from now was passed |
|---|
| 10 |
$this_time_offset_hours = $query->param('time_offset_hours'); |
|---|
| 11 |
if (length($this_time_offset_hours) > 0) { |
|---|
| 12 |
$this_time_offset_hours = $query->param('time_offset_hours'); |
|---|
| 13 |
$this_time_stamp = gmtime(time-60*60*$this_time_offset_hours); |
|---|
| 14 |
} |
|---|
| 15 |
|
|---|
| 16 |
# get the passed time_stamp |
|---|
| 17 |
if ($query->param('time')) { |
|---|
| 18 |
$this_time_stamp = $query->param('time'); |
|---|
| 19 |
} |
|---|
| 20 |
elsif ($query->param('TIME')) { |
|---|
| 21 |
$this_time_stamp = $query->param('TIME'); |
|---|
| 22 |
} |
|---|
| 23 |
elsif ($query->param('Time')) { |
|---|
| 24 |
$this_time_stamp = $query->param('Time'); |
|---|
| 25 |
} |
|---|
| 26 |
if ($this_time_stamp =~ /current/) { |
|---|
| 27 |
$this_time_stamp = gmtime(time-60*60*1.5); |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
# any time zone (only for time formatting) -- assume GMT |
|---|
| 31 |
my $time_zone_for_formatting_only = 'GMT'; |
|---|
| 32 |
if ($query->param('time_zone_for_formatting_only')) { |
|---|
| 33 |
$time_zone_for_formatting_only = $query->param('time_zone_for_formatting_only'); |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
# get current time |
|---|
| 37 |
$cmd = 'date -d \''.$this_time_stamp.' UTC\' +%s'; |
|---|
| 38 |
my $this_time_stamp_sec = `$cmd`; |
|---|
| 39 |
chop($this_time_stamp_sec); |
|---|
| 40 |
|
|---|
| 41 |
# get bbox |
|---|
| 42 |
my $this_bbox_param = $query->param('bbox'); |
|---|
| 43 |
if (length($this_bbox_param) < 1) { |
|---|
| 44 |
$this_bbox_param = $query->param('BBOX'); |
|---|
| 45 |
} |
|---|
| 46 |
if (length($this_bbox_param) < 1) { |
|---|
| 47 |
$this_bbox_param = '0,0,0,0'; |
|---|
| 48 |
} |
|---|
| 49 |
my @this_bbox = split(/,/,$this_bbox_param); |
|---|
| 50 |
|
|---|
| 51 |
# logging stuff |
|---|
| 52 |
my $log_map_name = 'wms_seacoos_in_situ'; |
|---|
| 53 |
my $log_map_file = '/opt/ogc/logs/wms_in_situ_log.txt'; |
|---|
| 54 |
my $log_map_time_stamp = "timestamp with time zone '$this_time_stamp UTC'"; |
|---|
| 55 |
my $log_remote_ip = $query->remote_addr(); |
|---|
| 56 |
my $log_extents = 'GeometryFromText(\'POLYGON((' |
|---|
| 57 |
.$this_bbox[0] |
|---|
| 58 |
.' '.$this_bbox[1] |
|---|
| 59 |
.', '.$this_bbox[0] |
|---|
| 60 |
.' '.$this_bbox[3] |
|---|
| 61 |
.', '.$this_bbox[2] |
|---|
| 62 |
.' '.$this_bbox[3] |
|---|
| 63 |
.', '.$this_bbox[2] |
|---|
| 64 |
.' '.$this_bbox[1] |
|---|
| 65 |
.', '.$this_bbox[0] |
|---|
| 66 |
.' '.$this_bbox[1] |
|---|
| 67 |
.'))\',-1)'; |
|---|
| 68 |
my $log_center_point = 'GeometryFromText(\'POINT(' |
|---|
| 69 |
.(($this_bbox[2] + $this_bbox[0])/2) |
|---|
| 70 |
.' '.(($this_bbox[3] + $this_bbox[1])/2) |
|---|
| 71 |
.', '.$this_bbox[0] |
|---|
| 72 |
.')\',-1)'; |
|---|
| 73 |
|
|---|
| 74 |
sub log_layer { |
|---|
| 75 |
$this_layer_name = shift @_; |
|---|
| 76 |
$log_sql = 'insert into map_log (' |
|---|
| 77 |
.'access_time_stamp' |
|---|
| 78 |
.',map_time_stamp' |
|---|
| 79 |
.',source_ip' |
|---|
| 80 |
.',referer' |
|---|
| 81 |
.',layer_name' |
|---|
| 82 |
.',map_name' |
|---|
| 83 |
.',extents' |
|---|
| 84 |
.',center_point' |
|---|
| 85 |
.')' |
|---|
| 86 |
.' select' |
|---|
| 87 |
.' timestamp with time zone \'' |
|---|
| 88 |
.gmtime.' UTC' |
|---|
| 89 |
.'\'' |
|---|
| 90 |
.','.$log_map_time_stamp |
|---|
| 91 |
.',\''.$log_remote_ip.'\'' |
|---|
| 92 |
.',\''.$log_referer.'\'' |
|---|
| 93 |
.',\''.$this_layer_name.'\'' |
|---|
| 94 |
.",'$log_map_name'" |
|---|
| 95 |
.','.$log_extents |
|---|
| 96 |
.','.$log_center_point |
|---|
| 97 |
.';'; |
|---|
| 98 |
$log_cmd = "echo \"$log_sql\" >> $log_map_file"; |
|---|
| 99 |
`$log_cmd`; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
# check to see if $this_time_stamp should require activation of the archive layers |
|---|
| 103 |
# < 2 weeks |
|---|
| 104 |
my $archive_flag = 0; |
|---|
| 105 |
if ($this_time_stamp_sec < time-60*60*24*14) { |
|---|
| 106 |
$archive_flag = 1; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
# station_id filter is optional as a parameter |
|---|
| 110 |
my $station_id = ''; |
|---|
| 111 |
if ($query->param('station_id')) { |
|---|
| 112 |
$station_id = $query->param('station_id'); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
# water_level_reference is optional as a parameter but required come showtime |
|---|
| 116 |
# valid values: MSL (default), MLLW, NAVD88 |
|---|
| 117 |
my $water_level_reference = 'MSL'; |
|---|
| 118 |
if ($query->param('water_level_reference')) { |
|---|
| 119 |
$water_level_reference = $query->param('water_level_reference'); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
# set default units, and get any if passed |
|---|
| 123 |
# valid values: mps (default), mph, knots |
|---|
| 124 |
my $wind_label_char_column = 'label_char'; |
|---|
| 125 |
if ($query->param('wind_speed_units')) { |
|---|
| 126 |
$wind_label_char_column .= '_'.$query->param('wind_speed_units'); |
|---|
| 127 |
if ($wind_label_char_column =~ /mps/) { |
|---|
| 128 |
$wind_label_char_column = 'label_char'; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
# get all the layers |
|---|
| 133 |
my $this_layers = $query->param('layers'); |
|---|
| 134 |
if (length($this_layers) < 1) { |
|---|
| 135 |
$this_layers = $query->param('LAYERS'); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
# produce layer-specific params |
|---|
| 139 |
my $layers_params = ''; |
|---|
| 140 |
if ($this_layers =~ /sst_obs_hourly/) { |
|---|
| 141 |
if ($archive_flag == 1) { |
|---|
| 142 |
$layers_params .= 'sst_obs_hourly_archive,'; |
|---|
| 143 |
} |
|---|
| 144 |
else { |
|---|
| 145 |
$layers_params .= 'sst_obs_hourly_recent,'; |
|---|
| 146 |
} |
|---|
| 147 |
log_layer('sst_obs_hourly'); |
|---|
| 148 |
} |
|---|
| 149 |
if ($this_layers =~ /wind_obs_hourly/) { |
|---|
| 150 |
if ($archive_flag == 1) { |
|---|
| 151 |
$layers_params .= 'wind_obs_hourly_archive,'; |
|---|
| 152 |
} |
|---|
| 153 |
else { |
|---|
| 154 |
$layers_params .= 'wind_obs_hourly_recent,'; |
|---|
| 155 |
} |
|---|
| 156 |
log_layer('wind_obs_hourly'); |
|---|
| 157 |
} |
|---|
| 158 |
if ($this_layers =~ /wind_obs_normalized_hourly/) { |
|---|
| 159 |
if ($archive_flag == 1) { |
|---|
| 160 |
$layers_params .= 'wind_obs_normalized_hourly_archive,'; |
|---|
| 161 |
} |
|---|
| 162 |
else { |
|---|
| 163 |
$layers_params .= 'wind_obs_normalized_hourly_recent,'; |
|---|
| 164 |
} |
|---|
| 165 |
log_layer('wind_obs_normalized_hourly'); |
|---|
| 166 |
} |
|---|
| 167 |
if ($this_layers =~ /water_level_obs_hourly/) { |
|---|
| 168 |
if ($archive_flag == 1) { |
|---|
| 169 |
$layers_params .= 'water_level_obs_hourly_archive,'; |
|---|
| 170 |
} |
|---|
| 171 |
else { |
|---|
| 172 |
$layers_params .= 'water_level_obs_hourly_recent,'; |
|---|
| 173 |
} |
|---|
| 174 |
log_layer('water_level_obs_hourly'); |
|---|
| 175 |
} |
|---|
| 176 |
if ($this_layers =~ /timestamp_label/) { |
|---|
| 177 |
$layers_params .= 'timestamp_label,'; |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
# get rid of last comma |
|---|
| 181 |
chop($layers_params); |
|---|
| 182 |
|
|---|
| 183 |
# get all the query_layers |
|---|
| 184 |
my $this_query_layers = $query->param('query_layers'); |
|---|
| 185 |
if (length($this_query_layers) < 1) { |
|---|
| 186 |
$this_query_layers = $query->param('QUERY_LAYERS'); |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
# produce query_layer-specific params |
|---|
| 190 |
my $query_layers_params = ''; |
|---|
| 191 |
if ($this_query_layers =~ /sst_obs_hourly/) { |
|---|
| 192 |
if ($archive_flag == 1) { |
|---|
| 193 |
$query_layers_params .= 'sst_obs_hourly_archive,'; |
|---|
| 194 |
} |
|---|
| 195 |
else { |
|---|
| 196 |
$query_layers_params .= 'sst_obs_hourly_recent,'; |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
if ($this_query_layers =~ /wind_obs_hourly/) { |
|---|
| 200 |
if ($archive_flag == 1) { |
|---|
| 201 |
$query_layers_params .= 'wind_obs_hourly_archive,'; |
|---|
| 202 |
} |
|---|
| 203 |
else { |
|---|
| 204 |
$query_layers_params .= 'wind_obs_hourly_recent,'; |
|---|
| 205 |
} |
|---|
| 206 |
} |
|---|
| 207 |
if ($this_query_layers =~ /wind_obs_normalized_hourly/) { |
|---|
| 208 |
if ($archive_flag == 1) { |
|---|
| 209 |
$query_layers_params .= 'wind_obs_normalized_hourly_archive,'; |
|---|
| 210 |
} |
|---|
| 211 |
else { |
|---|
| 212 |
$query_layers_params .= 'wind_obs_normalized_hourly_recent,'; |
|---|
| 213 |
} |
|---|
| 214 |
} |
|---|
| 215 |
if ($this_query_layers =~ /water_level_obs_hourly/) { |
|---|
| 216 |
if ($archive_flag == 1) { |
|---|
| 217 |
$query_layers_params .= 'water_level_obs_hourly_archive,'; |
|---|
| 218 |
} |
|---|
| 219 |
else { |
|---|
| 220 |
$query_layers_params .= 'water_level_obs_hourly_recent,'; |
|---|
| 221 |
} |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
# get rid of last comma |
|---|
| 225 |
chop($query_layers_params); |
|---|
| 226 |
|
|---|
| 227 |
# get only the params (subtract the URL) |
|---|
| 228 |
my $orig_url_params = substr($query->url(-path_info=>0,-query=>1),length($query->url())); |
|---|
| 229 |
|
|---|
| 230 |
# hide original layer request |
|---|
| 231 |
$orig_url_params =~ s/layers=/layers_hide=/g; |
|---|
| 232 |
$orig_url_params =~ s/LAYERS=/layers_hide=/g; |
|---|
| 233 |
|
|---|
| 234 |
# build complete target URL |
|---|
| 235 |
my $target_url = 'http://maury.marine.unc.edu/cgi-bin/in_situ' |
|---|
| 236 |
.$orig_url_params |
|---|
| 237 |
.'&formatted_time_stamp='.$this_time_stamp |
|---|
| 238 |
.'&layers='.$layers_params |
|---|
| 239 |
.'&query_layers='.$query_layers_params |
|---|
| 240 |
.'&wind_label_char_column='.$wind_label_char_column |
|---|
| 241 |
.'&water_level_reference='.$water_level_reference |
|---|
| 242 |
.'&station_id='.$station_id |
|---|
| 243 |
.'&time_zone_for_formatting_only='.$time_zone_for_formatting_only; |
|---|
| 244 |
|
|---|
| 245 |
# replace ; w/ & (don't know why ;'s are done by default) |
|---|
| 246 |
$target_url =~ s/;/&/g; |
|---|
| 247 |
# url-encode (sort of) a space |
|---|
| 248 |
$target_url =~ s/ /%20/g; |
|---|
| 249 |
|
|---|
| 250 |
# redirect to the target URL w/ the completed raster filenames |
|---|
| 251 |
print redirect($target_url); |
|---|