use strict;

my $start_date = '2012-05-16';

#drifter

`wget -Oframe_drifter.png 'http://maury.marine.unc.edu/cgi-bin/mapserv?MAP=/opt/secoora_ogc/maps/drifter.map&VERSION=1.1&SERVICE=WMS&REQUEST=getmap&WIDTH=768&HEIGHT=600&FORMAT=image/png&TRANSPARENT=true&BBOX=-82,24,-36,50&LAYERS=drifter_trajectory&OFFSET=120&OFFSET_END=96&SRS=EPSG:4269&TIME=2012_05_16_11_00_00'`;

`zip drifter_$start_date.kmz frame_drifter.png`;

`cp -f drifter.kml.template drifter.kml`;
my $search_command = "s/\Q<GroundOverlay><name>\E/\Q<GroundOverlay><TimeStamp><when>$start_date\T00:00:00Z<\/when><\/TimeStamp><name>\E/g";
#print $search_command."\n";
`perl -pi -e '$search_command' drifter.kml`;

`zip drifter_$start_date.kmz drifter.kml`;

#oi_sst

`wget -Oframe_oi_sst.png 'http://129.252.139.124/cgi-bin/mapserv?MAP=%2Fhome%2Fxeniaprod%2Fmapping%2Fcommon%2Fgeneral.map&TRANSPARENT=true&LAYERS=oi_sst&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4269&BBOX=-82,24,-58,40&WIDTH=768&HEIGHT=600'`;

`wget -Olegend_oi_sst.png 'http://secoora.org/maps/resources/images/legend/InterpolatedSST.png'`;

`zip oi_sst_$start_date.kmz frame_oi_sst.png`;
`zip oi_sst_$start_date.kmz legend_oi_sst.png`;

`cp -f oi_sst.kml.template oi_sst.kml`;
my $search_command = "s/\Q<GroundOverlay><name>\E/\Q<GroundOverlay><TimeStamp><when>$start_date\T00:00:00Z<\/when><\/TimeStamp><name>\E/g";
#print $search_command."\n";
`perl -pi -e '$search_command' oi_sst.kml`;

`zip oi_sst_$start_date.kmz oi_sst.kml`;

exit 0;


my @products = qw(
sabgom_current 
sabgom_current 
http://129.252.139.124/ncWMS/wms?LAYERS=sabgom%2Fcurrent&ELEVATION=-0.013888888888888888&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=vector%2Frainbow&COLORSCALERANGE=0%2C2&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=application/vnd.google-earth.kmz&SRS=EPSG%3A4326&BBOX=-82,24,-68.219128519487,39.359177151945&WIDTH=768&HEIGHT=600 
miami_hfradar
miami.nc_current
http://129.252.139.124/thredds/wms/miami.nc?LAYERS=current&ELEVATION=0&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=vector%2Frainbow&CRS=EPSG%3A4326&COLORSCALERANGE=0%2C200&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&EXCEPTIONS=XML&FORMAT=application/vnd.google-earth.kmz&BBOX=-80.03179168701172,25.627933502197266,-79.4023666381836,26.140892028808594&WIDTH=768&HEIGHT=600
ncom_global_current_atlantic
ncom_global_sea_water_velocity
http://129.252.139.124/ncWMS/wms?LAYERS=ncom_global%2Fsea_water_velocity&ELEVATION=0&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=vector%2Frainbow&COLORSCALERANGE=0%2C2&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=application/vnd.google-earth.kmz&SRS=EPSG%3A4326&BBOX=-82,24,-6,61&WIDTH=768&HEIGHT=600
ncom_global_current_southeast
ncom_global_sea_water_velocity
http://129.252.139.124/ncWMS/wms?LAYERS=ncom_global%2Fsea_water_velocity&ELEVATION=0&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=vector%2Frainbow&COLORSCALERANGE=0%2C2&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=application/vnd.google-earth.kmz&SRS=EPSG%3A4326&BBOX=-82,24,-68.219128519487,39.359177151945&WIDTH=768&HEIGHT=600
ww3_global_wave_height_atlantic
ww3_global_Thgt
http://129.252.139.124/ncWMS/wms?LAYERS=ww3_global%2FThgt&ELEVATION=0&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=boxfill%2Frainbow&COLORSCALERANGE=0%2C8&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=application/vnd.google-earth.kmz&SRS=EPSG%3A4326&BBOX=-82,24,-6,61&WIDTH=768&HEIGHT=600
ncep_global_wind_atlantic
ncep_global_wind
http://129.252.139.124/ncWMS/wms?LAYERS=ncep_global%2Fwind&ELEVATION=0&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=vector%2Frainbow&COLORSCALERANGE=0%2C40&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=application/vnd.google-earth.kmz&SRS=EPSG%3A4326&BBOX=-82,24,-6,61&WIDTH=768&HEIGHT=600
ncep_global_wind_southeast
ncep_global_wind
http://129.252.139.124/ncWMS/wms?LAYERS=ncep_global%2Fwind&ELEVATION=0&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=vector%2Frainbow&COLORSCALERANGE=0%2C40&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=application/vnd.google-earth.kmz&SRS=EPSG%3A4326&BBOX=-82,24,-68.219128519487,39.359177151945&WIDTH=768&HEIGHT=600
ncep_global_air_pressure_atlantic
ncep_global_prmslmsl
http://129.252.139.124/ncWMS/wms?LAYERS=ncep_global%2Fprmslmsl&ELEVATION=0&TIME=TIMESTAMP.000Z&TRANSPARENT=true&STYLES=boxfill%2Frainbow&COLORSCALERANGE=98000%2C103500&NUMCOLORBANDS=254&LOGSCALE=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&FORMAT=application/vnd.google-earth.kmz&SRS=EPSG%3A4326&BBOX=-82,24,-6,61&WIDTH=768&HEIGHT=600
);

while (@products) { 

my $product_name = shift @products; 
my $product_kml = shift @products; 
my $product_url = shift @products; 

my $start_date = '2012-05-16';

for (my $step = 0; $step < 9; $step++) {

my $offset = $step*1;

my $timestamp = `date --date='$start_date +$offset hours' +"%Y-%m-%dT%H:%M:%S"`;
chomp($timestamp);
print $timestamp."\n";

#my $timestamp = '2012-04-28T00:00:00';
my $file_zip = "$product_name\_$timestamp\.kmz";
my $filename = "$product_kml\.kml";

#layer,time,elevation,styles,bbox,colorscale/bands,width/height
$product_url =~ s/TIMESTAMP/$timestamp/;
`wget -O$file_zip '$product_url'`;

`unzip $file_zip`;

my $search_command = "s/\Q<GroundOverlay><name>\E/\Q<GroundOverlay><TimeStamp><when>$timestamp\Z<\/when><\/TimeStamp><name>\E/g";
#print $search_command."\n";
`perl -pi -e '$search_command' $filename`;

`zip $file_zip $filename`;

`rm -f $filename`;
`rm -f frame0.png`;
`rm -f legend.png`;

exit 0;
} #for step
} #while products



exit 0;
