Adds more checks to avoid silent dead due to missing data.

From: Alec Leamas <leamas.alec@gmail.com>


---

 mythweather/scripts/us_nws/nwsxml.pl |   74 +++++++++++++++++++++++-----------
 1 files changed, 49 insertions(+), 25 deletions(-)
 mode change 100755 => 100644 mythweather/scripts/us_nws/nwsxml.pl


diff --git a/mythweather/scripts/us_nws/nwsxml.pl b/mythweather/scripts/us_nws/nwsxml.pl
old mode 100755
new mode 100644
index 2e9cf82..729debc
--- a/mythweather/scripts/us_nws/nwsxml.pl
+++ b/mythweather/scripts/us_nws/nwsxml.pl
@@ -77,6 +77,15 @@ my $response = get $base_url . $loc . '.xml';
 die unless defined $response;
 
 my $xml = XMLin($response);
+
+sub has_value
+{
+    my $key = pop @_;
+
+    return defined $xml->{$key} &&
+        $xml->{$key} ne "NA" && $xml->{$key} ne "";
+}
+
 foreach (@types) {
     my $label;
     my $key;
@@ -84,53 +93,68 @@ foreach (@types) {
     $label = $_;
 
     if (/temp$/ || /dewpoint$/ || /heat_index$/ || /windchill$/) {
-        if (defined(  $xml->{$_ . '_c'})){
+        if (has_value( $_ . '_c') && has_value( $_ . '_f')) {
             printf( "%s::%s;%s\n",
                     $label, $xml->{$_ . '_c'}, $xml->{$_ . '_f'});
+        } else {
+            print "${label}::NA\n";
         }
     }
     elsif (/pressure$/) {
         printf( "%s::;%s\n", $label, $xml->{$_ . '_in'});
     }
     elsif (/wind_speed/) {
-          printf( "%s::;%s\n", $label, $xml->{'wind_mph'});
+        if( has_value('wind_mph')) {
+            printf( "%s::;%s\n", $label, $xml->{'wind_mph'});
+        } else {
+            print "wind_speed::NA\n";
+        }
     } elsif (/wind_gust/) {
-        if (defined($xml->{'wind_gust_mph'})) {
-            printf( "%s::;%s\n", $label, $xml->{'wind_gust_mph'});
-            printf( "wind_spdgst::;%s\n", $xml->{'wind_gust_mph'});
-
+        if (has_value( 'wind_gust_mph')) {
+             printf( "wind_gust::;%s\n",  $xml->{'wind_gust_mph'});
+             printf( "wind_spdgst::;%s\n",$xml->{'wind_gust_mph'});
         } else {
             print "wind_gust::NA\n";
             print "wind_spdgst::NA\n"
         }
     } elsif (/visibility/) {
-        printf( "%s::;%s\n", $label,  $xml->{'visibility_mi'});
+        if (has_value( 'visibility_mi')) {
+            printf( "%s::;%s\n", $label,  $xml->{'visibility_mi'});
+        } else {
+            print "visibility::NA\n";
+        }
     } elsif (/weather_icon/) {
-        $key = 'weather_icon';
-        $xml->{$key} = 'unknown.png';
-        local *FH;
-        open(FH, "icons") or die "Cannot open icons";
-        while(my $line = <FH>) {
-            chomp $line;
-            if ($line =~ /$xml->{'icon_url_name'}::/) {
-                $line =~ s/.*:://;
-                $xml->{$key} = $line;
-                last;
+        my $icon = 'unknown.png';
+        if (has_value('icon_url_name')) {
+            local *FH;
+            open(FH, "icons") or die "Cannot open icons";
+            while(my $line = <FH>) {
+                chomp $line;
+                if ($line =~ /$xml->{'icon_url_name'}::/) {
+                    $line =~ s/.*:://;
+                    $icon = $line;
+                    last;
+                }
             }
         }
+        print "weather_icon::${icon}\n";
     } elsif (/cclocation/) {
          print "${label}::" . $xml->{'location'};
     } elsif (/appt$/) {
-        if (defined($xml->{windchill_f})) {
-            printf( "%s::%s;%s\n", 
-                    $label, $xml->{windchill_c}, $xml->{windchill_f});
-        
-        } elsif (defined($xml->{appt_f})) {
-             printf( "%s::%s;%s\n", 
-                     $label, $xml->{appt_c}, $xml->{appt_f});
+        if (has_value( 'appt_f') && has_value( 'appt_c')) {
+            printf( "appt::%s;%s\n", $xml->{'appt_c'}, $xml->{'appt_f'});
+        } elsif (has_value( 'windchill_f') && has_value( 'windchill_c') ) {
+            printf( "appt::%s;%s\n",
+                    $xml->{'windchill_c'}, $xml->{'windchill_f'});
+        } elsif (has_value( 'heat_index_f') && has_value( 'heat_index_c')) {
+            printf( "appt::%s;%s\n",
+                    $xml->{'heat_index_c'}, $xml->{'heat_index_f'});
+        } else {
+            print "appt::NA\n";
         }
     } elsif (/copyright/) {
-        print "copyright::$xml->{credit}\n";
+        my $value = has_value('credit') ? $xml->{credit} : "NA");
+        print "copyright::${value}\n";
     } else {
         my $value = (defined( $xml->{$_}) ? $xml->{$_} : "NA");
         print( "${label}::${value}\n");
