use strict; use XML::LibXML; #the next sections jump between outputting the kml file and querying the repeating xml elements for subtitution in the corresponding file elements. ##header section my $xml_content = <<"END_OF_FILE"; Near Real-Time Ocean Data published by SEACOOS 1 END_OF_FILE ##repeating elements section my $xp = XML::LibXML->new->parse_file('/var/www/html/gearth/oostech.xml'); my $xp_platform = XML::LibXML->new->parse_file('./seacoos/seacoos_platform_list.xml'); foreach my $platform ($xp->findnodes('//platform')) { #print "platform:$platform\n"; #debug my $platform_id = get_last_word($platform->find('platformId')); #initially I thought about just using $platform_name below, but decided to use the full $platform_id instead my ($organization_name, $platform_name, $package_name) = split(/_/,$platform_id); my $platform_label = $organization_name.'.'.$platform_name.'.'.$package_name; #remove duplicates from this feed that conflict with other more specific feeds(usgs,nerrs,etc) if ($organization_name eq 'nerrs') { next; } my @usgs_platforms = qw(02110815 02176585 321603080432202 02176603 02176611 02176635 02176589 02176640 02172020 02172050 021720698 02172080 02172002 02172040 02175000 02172053 021720677 021720709 02171639 021720710 02172084 02110760 02110805 02110809 02135200 02110777 02110704 02110802 02110770 02110755 021108125 02110725 022035975 02198840 0208455155 0208455560 02092162 0209265810 0208453300 0208114150 0209262905 02084472 02081022 02081094 02108690 294213081345300 02248380 02297100 02301721 02310678 02301638 02301988 02306774 023000095 02312000 02300021 264053081572501 02326550 02310663 023060003 02323592 02300009 02310650 02310545 02306000 02304510 02301718 02313000 022908295); if (&search_array($platform_name,@usgs_platforms)) { next; } my $platform_url = $xp_platform->find('//platform[@id="'.$platform_id.'"]/platform_url'); #print "yes:".$platform_url."\n"; $xml_content .= ""; my $metadata_content = ""; $metadata_content .= "$platform_url\n"; my $desc = ""; my ($longitude, $latitude, $depth, $datetime); foreach my $observation ($platform->findnodes('soapenc:Array/observation')) { $longitude = $observation->find('boundedBy/longitude'); #same long/lat/datetime for all observations within same platform #print "$longitude\n"; $latitude = $observation->find('boundedBy/latitude'); #print "$latitude\n"; $depth = $observation->find('boundedBy/depth'); $datetime = $observation->find('boundedBy/dateTime'); #$datetime =~ s/T/ /g; $datetime = substr($datetime,0,19).'Z'; #print "$datetime\n"; my $parameter = get_last_word($observation->find('resultOf/typedQuantity/@axis')); #print "$parameter\n"; $parameter = &get_observed_property($parameter); my $measurement = $observation->find('resultOf/typedQuantity'); #my $graph = $observation->find('graphTimeSeries'); #not valid xml if literals aren't susbtituted with entities #$graph =~ s/&/&/g; my $uom = get_last_word($observation->find('resultOf/typedQuantity/@uom')); #print $uom."\n"; #some uom exceptions if ($parameter eq 'water_level') { $uom = 'm(MLLW)'; } if ($parameter eq 'current_speed') { $uom = 'm_s-1'; } $metadata_content .= <<"END_OF_FILE"; $parameter $uom $measurement $depth END_OF_FILE $desc .= "" } #foreach observation $metadata_content .= ""; $xml_content .= $metadata_content; $xml_content .= <<"END_OF_FILE"; $platform_label]]> $longitude,$latitude $datetime END_OF_FILE } #foreach platform ##footer section $xml_content .= <<"END_OF_FILE"; END_OF_FILE close (FILE_IN); ##### my $current_date = `date -u +"%Y%m%d%H%M%S"`; chomp($current_date); my $output_name = 'seacoos'; ##write file open (FILE_XML,">/var/www/html/obskml/feeds/$output_name/archive/$output_name\_metadata_$current_date.kml"); print FILE_XML $xml_content; close (FILE_XML); ##zip file `cd /var/www/html/obskml/feeds/$output_name/archive ; zip -m $output_name\_metadata_$current_date.kmz $output_name\_metadata_$current_date.kml`; ##copy file to latest `cp -f /var/www/html/obskml/feeds/$output_name/archive/$output_name\_metadata_$current_date.kmz /var/www/html/obskml/feeds/$output_name/$output_name\_metadata_latest.kmz`; exit 0; ##support functions sub get_last_word { #this sub gets the last word after the '#' symbol for xml markup my $string = shift; my @temp = split(/#/, $string); $string = @temp[-1]; return $string; } sub get_observed_property { #this subs other observation_type refs for existing my $string = shift; if ($string eq 'salinity') { $string = 'salinity'} if ($string eq 'sea_surface_temperature') { $string = 'water_temperature'} if ($string eq 'sea_bottom_temperature') { $string = 'water_temperature'} if ($string eq 'wind_speed') { $string = 'wind_speed'} if ($string eq 'wind_gust') { $string = 'wind_gust'} if ($string eq 'wind_from_direction') { $string = 'wind_from_direction'} if ($string eq 'significant_wave_height') { $string = 'significant_wave_height'} if ($string eq 'dominant_wave_period') { $string = 'dominant_wave_period'} if ($string eq 'dominant_wave_direction') { $string = 'significant_wave_to_direction'} if ($string eq 'current_speed') { $string = 'current_speed'} if ($string eq 'current_to_direction') { $string = 'current_to_direction'} if ($string eq 'water_level') { $string = 'water_level'} if ($string eq 'air_temperature') { $string = 'air_temperature'} if ($string eq 'air_pressure') { $string = 'air_pressure'} return $string; } ######################## sub search_array { my $search_term = shift @_; my @search_array = @_; foreach my $search_page (@search_array) { if ($search_term =~ $search_page) { return 1; } } return 0; }
$parameter$measurement$uom