#! /usr/bin/perl -w

## NAME: allinsitu_2_shape.pl
##
## Converts an hourly WFS request to a shapefile for NCCOOS IM mouseovers
##
## This script makes an hourly WFS request to the main SEACOOS OBS DB via an OGC wrapper
## that returns a GML containing formatted strings of the latest observations for each station in the BBOX.
## The resultant GML is then reformatted to make its subsequent conversion to shapefile
## more clean.  OGR2OGR does the conversion on only the fields of interest.  The new shapefile
## is placed in a directory to be SFTP.d to Maury.  This shapefile is used on Maury to power
## the Mouse Over Observations layer in the NCCOOS Interactive Map.

use LWP::Simple;
use warnings;
use lib ".";

my ($dest_dir, $file_url, $target);


# data directory
$dest_dir='/home/jcleary/data/';

# GML data from Nautilus
#$file_url='http://nautilus.baruch.sc.edu/wfs/seacoos_in_situ?version=1.0.0&service=WFS&request=getfeature&bbox=-79.2,32.5,-73.9,37.2&typename=latest_in_situ_obs&time_offset_hours=2';

# GML data from Maury
$file_url='http://maury.marine.unc.edu/cgi-bin/wfs_seacoos_in_situ?version=1.0.0&service=WFS&request=getfeature&bbox=-79.2,32.5,-73.9,37.2&typename=latest_in_situ_obs&time_offset_hours=2';


# get the data saved locally
$filename = $dest_dir.'in_situ.gml';
getstore($file_url,$filename);

 
if(-e $filename)
  {
    open (IN, $filename) || die("Error Reading File: $filename $!");
	{
		undef $/;          
		$infile = <IN>;
	}
   close (IN) || die("Error Closing File: $filename $!");
   
   $infile =~ s/ms:station_id\>/STATION_ID\>/gi;
   $infile =~ s/ms:air_pressure_mb\>/PRESSURE\>/gi;
   $infile =~ s/ms:air_temperature_fahrenheit\>/AIR_TEMP\>/gi;
   $infile =~ s/ms:salinity_ppt\>/SALINITY\>/gi;
   $infile =~ s/ms:water_level_msl_ft\>/WATERLEVEL\>/gi;
   $infile =~ s/ms:wind_speed_and_dir_knots\>/WIND_SPEED\>/gi;
   $infile =~ s/ms:sea_surface_temperature_fahrenheit\>/SEA_TEMP\>/gi;
   $infile =~ s/ms:sea_surface_current_speed_and_dir_knots\>/SURFC_CURR\>/gi;
   $infile =~ s/ms:report_time_stamp\>/TIMESTAMP\>/gi;
   $infile =~ s/ms:water_level_time_stamp\>/WL_TIME\>/gi;
   $infile =~ s/ms:sea_surface_temperature_time_stamp\>/SST_TIME\>/gi; 
   $infile =~ s/ms:wind_speed_and_dir_time_stamp\>/WIND_TIME\>/gi;
   $infile =~ s/ms:sea_surface_current_speed_and_dir_time_stamp\>/SCUR_TIME\>/gi;
   
   # eliminate whitespace as the last character of any attribute strings - causes issues with subsequent maptips rendering
   $infile =~ s/\s\<\//\<\//gi; 

   # write complete file 
   open (PROD, ">$filename") || die("Error Writing to File: $filename $!");
	 print PROD $infile;
   close (PROD) || die("Error Closing File: $filename $!");
   
  # command line utlities to create to shapefile
  # ogr2ogr 
  system ("/usr/local/bin/ogr2ogr -f 'ESRI Shapefile' -lco SHPT=POINT -select 'STATION_ID,PRESSURE,AIR_TEMP,SALINITY,WATERLEVEL,WIND_SPEED,SEA_TEMP,SURFC_CURR,TIMESTAMP,WL_TIME,SST_TIME,WIND_TIME,SCUR_TIME' ".$dest_dir."in_situ ".$filename);

  # copy files into SFTP dir for SFTPing to Maury 
  system ("mv ".$dest_dir."in_situ/ms:latest_in_situ_obs.* /home/jcleary/data/data_for_sftp/");

  # remove gml/gfs files from data dir
  system ("rm ".$dest_dir."in_situ.g*");
  }
  
  else
  {	
     exit (0);
  }
	    
  
exit (0);





 


