Changeset 179

Show
Ignore:
Timestamp:
04/08/08 15:05:08
Author:
dan
Message:

Implemented the ability to create obsKML files from the fixed_point operations.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • scout/trunk/cdl_0master.pl

    r1 r179  
     1############################################################################### 
     2# Revisions 
     3# Author: DWR 
     4# Date: 4/7/2008 
     5# CHanges: Added the ability to turn on the writing of an obsKml file.  
     6# The script now takes 2 optional command line arguments. 
     7# The 2nd command line argument is which file(s) to create. The following values are defined: 
     8# 0 = default value which only produces the orignal sql files. 
     9# 1 = Writes only an obsKML file 
     10# 2 = Writes both the sql files and obsKML file. 
     11# The 3rd command line argument specifies what directory to create the obsKML file in.  
     12# The directory must already exist. 
     13# 
     14# THis script now uses the obsKMLSubRoutines package to create the obsKML file. For now, make sure this 
     15# package is in the same directory as this script or in a directory which is in the Perl path. 
     16################################################################################ 
    117#!/bin/perl 
     18 
     19#use strict; 
     20#use warnings; 
     21 
    222 
    323use NetCDF; 
    424use UDUNITS; 
    525use Time::Local; 
     26use obsKMLSubRoutines; 
     27 
     28#DWR 4/7/2008 Added constants for use with ARGV[1] to tell us what file(s) to write. 
     29use constant { 
     30  DEFAULTWRITESQLONLY      => 0, 
     31  WRITEKMLONLY             => 1, 
     32  WRITEBOTH                => 2 
     33}; 
    634 
    735# 
     
    2250# udunits initialization 
    2351 
    24 UDUNITS::init("/usr/local/download/udunits-1.12.1/src/lib/udunits.dat") == 0  
     52UDUNITS::init("/usr/local/lib/perl/5.8.8/udunits/udunits-1.12.4/etc/udunits.dat") == 0  
    2553  || die "ABORT! Cannot initialize udunits.\n"; 
    2654 
     
    3159$net_cdf_file = @ARGV[0]; 
    3260 
     61#DWR 4/7/2008 
     62#Optional file creation arguments 
     63my $FileCreationOption = 0; 
     64my $strObsKMLFilePath; 
     65if( @ARGV == 0 ) 
     66{ 
     67  die( "ERROR: Missing command line arguments. Ths script uses the following command line parameters:\n". 
     68       "Parameter 0 is the netcdf filename to process.\n". 
     69       "Parameter 1 is an optional parameter. Valid values are: 0 Write only SQL files, 1 Write only obsKML file, or 2 Write both SQL files and obsKML file. Default value is 0.\n". 
     70       "Parameter 2 is the directory where the obsKML file is stored. If not provided the default directory is the current directory.\n" ); 
     71} 
     72if( @ARGV > 1 ) 
     73{ 
     74  $strObsKMLFilePath = './';  
     75  if( @ARGV[1] >= DEFAULTWRITESQLONLY && @ARGV[1] <= WRITEBOTH ) 
     76  { 
     77  $FileCreationOption = @ARGV[1]; 
     78  } 
     79  else 
     80  {  
     81  print( "ERROR: Command line argument 2: @ARGV[1] not valid\n. Valid values are: 0 Write only SQL files\n 1: Write only obsKML file\n or 2: Write both SQL files and obsKML file.\nDefault value will be 0.\n" ); 
     82  }   
     83  # 3rd command line argument is the path to store the obsKML files. 
     84  if( @ARGV >= 3 ) 
     85  { 
     86    $strObsKMLFilePath = @ARGV[2]; 
     87  } 
     88} 
    3389# 
    3490# Turn off fatal error aborts.  We need to allow some attributes to error out w/o crashing. 
     
    132188 
    133189if ($format_category_value eq 'fixed-point') { 
    134   fixed_point($this_station_id_top_ts); 
     190  #DWR 4/7/2008 
     191  # ADded parameters $FileCreationOption and $strObsKMLFilePath. 
     192  fixed_point($this_station_id_top_ts, $FileCreationOption, $strObsKMLFilePath ); 
    135193} 
    136194elsif ($format_category_value eq 'moving-point-2D') { 
     
    146204  fixed_map(); 
    147205} 
     206 
  • scout/trunk/cdl_fixed_point.pl

    r1 r179  
    55  # top timestamp 
    66  $this_station_id_top_ts = $_[0]; 
     7  #DWR 4/7/2008 
     8  my $FileCreationOptions = $_[1]; 
     9  my $strObsKMLFilePath = $_[2]; 
     10   
     11  my $bWriteSQLFiles = 1; 
     12  my $bWriteobsKMLFile = 0; 
     13  if( $FileCreationOptions == WRITEKMLONLY ) 
     14  { 
     15   $bWriteSQLFiles = 0; 
     16   $bWriteobsKMLFile = 1; 
     17  } 
     18  elsif( $FileCreationOptions == WRITEBOTH ) 
     19  { 
     20   $bWriteobsKMLFile = 1;    
     21  } 
     22   
     23  #DWR 4/5/2008 
     24  #The XML file that has our obsKML units conversion. WE use it here to convert the units string names from the netcdf 
     25  #file into the strings we use in the obsKML and then in our database. 
     26  my $strUnitsXMLFilename; 
     27  $strUnitsXMLFilename = './UnitsConversion.xml'; 
     28  my $XMLControlFile = XML::LibXML->new->parse_file("$strUnitsXMLFilename"); 
    729   
    830  # 
     
    95117          reference_to_mllw => '', 
    96118          reference_to_msl  => '', 
    97           reference_to_navd88  => '' 
     119          reference_to_navd88  => '', 
     120          units => ''       #DWR 4/5/2008 
    98121        ); 
    99122        push @water_level, {%this_water_level}; 
     
    105128          var_id   => $i, 
    106129          dim_id   => $this_var_dimid[0], 
    107           height   => '' 
     130          height   => '', 
     131          units => ''       #DWR 4/5/2008 
    108132        ); 
    109133        push @sea_surface_temperature, {%this_sea_surface_temperature}; 
     
    115139          var_id   => $i, 
    116140          dim_id   => $this_var_dimid[0], 
    117           height   => '' 
     141          height   => '', 
     142          units    => ''       #DWR 4/5/2008 
    118143        ); 
    119144        push @sea_bottom_temperature, {%this_sea_bottom_temperature}; 
     
    125150          var_id   => $i, 
    126151          dim_id   => $this_var_dimid[0], 
    127           height   => '' 
     152          height   => '', 
     153          units    => ''       #DWR 4/5/2008 
    128154        ); 
    129155        push @air_temperature, {%this_air_temperature}; 
     
    136162          dim_id            => $this_var_dimid[0], 
    137163          height            => '', 
    138           can_be_normalized => '' 
     164          can_be_normalized => '', 
     165          units             => ''       #DWR 4/5/2008 
    139166        ); 
    140167        push @wind_speed, {%this_wind_speed}; 
     
    147174          dim_id            => $this_var_dimid[0], 
    148175          height            => '', 
    149           can_be_normalized => '' 
     176          can_be_normalized => '', 
     177          units             => ''       #DWR 4/5/2008 
    150178        ); 
    151179        push @wind_gust, {%this_wind_gust}; 
     
    158186          dim_id            => $this_var_dimid[0], 
    159187          height            => '', 
    160           can_be_normalized => '' 
     188          can_be_normalized => '', 
     189          units             => ''       #DWR 4/5/2008 
     190           
    161191        ); 
    162192        push @wind_from_direction, {%this_wind_from_direction}; 
     
    168198          var_id   => $i, 
    169199          dim_id   => $this_var_dimid[0], 
    170           height   => '' 
     200          height   => '', 
     201          units    => ''       #DWR 4/5/2008 
    171202        ); 
    172203        push @air_pressure, {%this_air_pressure}; 
     
    178209          var_id   => $i, 
    179210          dim_id   => $this_var_dimid[0], 
    180           height   => '' 
     211          height   => '', 
     212          units    => ''       #DWR 4/5/2008 
    181213        ); 
    182214        push @salinity, {%this_salinity}; 
     
    188220          var_id            => $i, 
    189221          dim_id            => $this_var_dimid[0], 
    190           height            => '' 
     222          height            => '', 
     223          units             => ''       #DWR 4/5/2008 
    191224        ); 
    192225        push @sea_surface_eastward_current, {%this_sea_surface_eastward_current}; 
     
    198231          var_id            => $i, 
    199232          dim_id            => $this_var_dimid[0], 
    200           height            => '' 
     233          height            => '', 
     234          units             => ''       #DWR 4/5/2008 
    201235        ); 
    202236        push @sea_surface_northward_current, {%this_sea_surface_northward_current}; 
     
    208242          var_id            => $i, 
    209243          dim_id            => $this_var_dimid[0], 
    210           height            => '' 
     244          height            => '', 
     245          units             => ''       #DWR 4/5/2008 
    211246        ); 
    212247        push @significant_wave_height, {%this_significant_wave_height}; 
     
    218253          var_id            => $i, 
    219254          dim_id            => $this_var_dimid[0], 
    220           height            => '' 
     255          height            => '', 
     256          units             => ''       #DWR 4/5/2008 
    221257        ); 
    222258        push @dominant_wave_period, {%this_dominant_wave_period}; 
     
    414450      my @dimids  = ''; 
    415451      my $natts   = ''; 
     452 
     453      #DWR 4/5/2008 
     454      my $units_value = ''; 
     455      my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, \$units_value); 
     456      if ($attget < 0)  
     457      { 
     458       die "ABORT! $water_level[$i]{'var_name'} has no units.\n"; 
     459      } 
     460      $water_level[$i]{'units'} = $units_value; 
     461       
    416462      # we need to loop through the attributes, so find out how many there are 
    417463      my $varinq = NetCDF::varinq($ncid, $water_level[$i]{'var_id'}, 
     
    488534      my @dimids  = ''; 
    489535      my $natts   = ''; 
     536 
     537      #DWR 4/5/2008 
     538      my $units_value = ''; 
     539      my $attget = NetCDF::attget($ncid, $sea_surface_temperature[$i]{'var_id'}, 'units', \$units_value); 
     540      if ($attget < 0)  
     541      { 
     542       die "ABORT! $sea_surface_temperature[$i]{'var_name'} has no units.\n"; 
     543      } 
     544      $sea_surface_temperature[$i]{'units'} = $units_value; 
     545       
    490546      # we need to loop through the attributes, so find out how many there are 
    491547      my $varinq = NetCDF::varinq($ncid, $sea_surface_temperature[$i]{'var_id'}, 
     
    534590      my @dimids  = ''; 
    535591      my $natts   = ''; 
     592 
     593      #DWR 4/5/2008 
     594      my $units_value = ''; 
     595      my $attget = NetCDF::attget($ncid, $sea_bottom_temperature[$i]{'var_id'}, 'units', \$units_value); 
     596      if ($attget < 0)  
     597      { 
     598       die "ABORT! $sea_bottom_temperature[$i]{'var_name'} has no units.\n"; 
     599      } 
     600      $sea_bottom_temperature[$i]{'units'} = $units_value; 
     601 
    536602      # we need to loop through the attributes, so find out how many there are 
    537603      my $varinq = NetCDF::varinq($ncid, $sea_bottom_temperature[$i]{'var_id'}, 
     
    580646      my @dimids  = ''; 
    581647      my $natts   = ''; 
     648       
     649      #DWR 4/5/2008 
     650      my $units_value = ''; 
     651      my $attget = NetCDF::attget($ncid, $air_temperature[$i]{'var_id'}, 'units', \$units_value); 
     652      if ($attget < 0)  
     653      { 
     654       die "ABORT! $air_temperature[$i]{'var_name'} has no units.\n"; 
     655      } 
     656      $air_temperature[$i]{'units'} = $units_value; 
     657       
    582658      # we need to loop through the attributes, so find out how many there are 
    583659      my $varinq = NetCDF::varinq($ncid, $air_temperature[$i]{'var_id'}, 
     
    626702      my @dimids  = ''; 
    627703      my $natts   = ''; 
     704 
     705      #DWR 4/5/2008 
     706      my $units_value = ''; 
     707      my $attget = NetCDF::attget($ncid, $wind_speed[$i]{'var_id'}, 'units', \$units_value); 
     708      if ($attget < 0)  
     709      { 
     710       die "ABORT! $wind_speed[$i]{'var_name'} has no units.\n"; 
     711      } 
     712      $wind_speed[$i]{'units'} = $units_value; 
     713       
     714       
    628715      # we need to loop through the attributes, so find out how many there are 
    629716      my $varinq = NetCDF::varinq($ncid, $wind_speed[$i]{'var_id'}, 
     
    680767      my @dimids  = ''; 
    681768      my $natts   = ''; 
     769       
     770      #DWR 4/5/2008 
     771      my $units_value = ''; 
     772      my $attget = NetCDF::attget($ncid, $wind_gust[$i]{'var_id'}, 'units', \$units_value); 
     773      if ($attget < 0)  
     774      { 
     775       die "ABORT! $wind_gust[$i]{'var_name'} has no units.\n"; 
     776      } 
     777      $wind_gust[$i]{'units'} = $units_value; 
     778 
     779       
    682780      # we need to loop through the attributes, so find out how many there are 
    683781      my $varinq = NetCDF::varinq($ncid, $wind_gust[$i]{'var_id'}, 
     
    724822    } 
    725823    else { 
     824      
     825      #DWR 4/5/2008 
     826      my $units_value = ''; 
     827      my $attget = NetCDF::attget($ncid, $wind_from_direction[$i]{'var_id'}, 'units', \$units_value); 
     828      if ($attget < 0)  
     829      { 
     830       die "ABORT! $wind_from_direction[$i]{'var_name'} has no units.\n"; 
     831      } 
     832      $wind_from_direction[$i]{'units'} = $units_value; 
     833      
    726834      # get all the variable goodies 
    727835      $varget = NetCDF::varget($ncid, $wind_from_direction[$i]{'var_id'}, 
     
    778886    } 
    779887    else { 
     888          
    780889      # get all the variable goodies 
    781890      $varget = NetCDF::varget($ncid, $air_pressure[$i]{'var_id'}, 
     
    801910      $base_units->convert($dest_units,$this_slope,$this_intercept); 
    802911       
     912      #DWR 4/5/2008 
     913      $air_pressure[$i]{'units'} = $units_value; 
     914       
    803915      # we need to loop through the attributes, so find out how many there are 
    804916      my $varinq = NetCDF::varinq($ncid, $air_pressure[$i]{'var_id'}, 
     
    847959    } 
    848960    else { 
     961      
     962      #DWR 4/5/2008 
     963      my $units_value = ''; 
     964      my $attget = NetCDF::attget($ncid, $salinity[$i]{'var_id'}, 'units', \$units_value); 
     965      if ($attget < 0)  
     966      { 
     967       die "ABORT! $salinity[$i]{'var_name'} has no units.\n"; 
     968      } 
     969      $salinity[$i]{'units'} = $units_value; 
     970      
    849971      # get all the variable goodies 
    850972      $varget = NetCDF::varget($ncid, $salinity[$i]{'var_id'}, 
     
    8931015      die "ABORT!  $sea_surface_eastward_current[$i]{'var_name'} has wrong time dimension.\n"; 
    8941016    } 
    895     else { 
     1017    else {              
    8961018      # get all the variable goodies 
    8971019      $varget = NetCDF::varget($ncid, $sea_surface_eastward_current[$i]{'var_id'}, 
     
    9161038      my $dest_units = UDUNITS::scan('m s-1'); 
    9171039      $base_units->convert($dest_units,$this_slope,$this_intercept); 
    918        
     1040 
     1041      #DWR 4/5/2008 
     1042      $sea_surface_eastward_current[$i]{'units'} = $units_value; 
     1043 
    9191044      # we need to loop through the attributes, so find out how many there are 
    9201045      my $varinq = NetCDF::varinq($ncid, $sea_surface_eastward_current[$i]{'var_id'}, 
     
    9571082    } 
    9581083    else { 
     1084          
    9591085      # get all the variable goodies 
    9601086      $varget = NetCDF::varget($ncid, $sea_surface_northward_current[$i]{'var_id'}, 
     
    9791105      my $dest_units = UDUNITS::scan('m s-1'); 
    9801106      $base_units->convert($dest_units,$this_slope,$this_intercept); 
     1107      #DWR 4/5/2008 
     1108      $sea_surface_northward_current[$i]{'units'} = $units_value; 
    9811109       
    9821110      # we need to loop through the attributes, so find out how many there are 
     
    10201148    } 
    10211149    else { 
     1150           
    10221151      # get all the variable goodies 
    10231152      $varget = NetCDF::varget($ncid, $significant_wave_height[$i]{'var_id'}, 
     
    10421171      my $dest_units = UDUNITS::scan('m'); 
    10431172      $base_units->convert($dest_units,$this_slope,$this_intercept); 
     1173      #DWR 4/5/2008 
     1174      $significant_wave_height[$i]{'units'} = $units_value; 
    10441175       
    10451176      # we need to loop through the attributes, so find out how many there are 
     
    10831214    } 
    10841215    else { 
     1216           
    10851217      # get all the variable goodies 
    10861218      $varget = NetCDF::varget($ncid, $dominant_wave_period[$i]{'var_id'}, 
     
    11051237      my $dest_units = UDUNITS::scan('second'); 
    11061238      $base_units->convert($dest_units,$this_slope,$this_intercept); 
     1239      #DWR 4/5/2008 
     1240      $dominant_wave_period[$i]{'units'} = $units_value; 
    11071241       
    11081242      # we need to loop through the attributes, so find out how many there are 
     
    11471281  # so new stations weren't being created. 
    11481282  if ($this_station_id_top_ts <= 0) { 
    1149     open(STATION_ID_SQLFILE,'>>../sql_in_situ_station_id/in_situ_station_id_'.$institution_code_value.'_' 
    1150       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1151     print STATION_ID_SQLFILE "-- format_category      = $format_category_value\n"; 
    1152     print STATION_ID_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1153     print STATION_ID_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1154     print STATION_ID_SQLFILE "-- package_code         = $package_code_value\n"; 
    1155     print STATION_ID_SQLFILE "-- title                = $title_value\n"; 
    1156     print STATION_ID_SQLFILE "-- institution          = $institution_value\n"; 
    1157     print STATION_ID_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1158     print STATION_ID_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1159     print STATION_ID_SQLFILE "-- source               = $source_value\n"; 
    1160     print STATION_ID_SQLFILE "-- references           = $references_value\n"; 
    1161     print STATION_ID_SQLFILE "-- contact              = $contact_value\n"; 
    1162     print STATION_ID_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1163     print STATION_ID_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1164     print STATION_ID_SQLFILE "INSERT INTO in_situ_station_id (";  
    1165     print STATION_ID_SQLFILE "station_id,"; 
    1166     print STATION_ID_SQLFILE "title,"; 
    1167     print STATION_ID_SQLFILE "institution,"; 
    1168     print STATION_ID_SQLFILE "institution_url,"; 
    1169     print STATION_ID_SQLFILE "institution_dods_url,"; 
    1170     print STATION_ID_SQLFILE "source,"; 
    1171     print STATION_ID_SQLFILE "refs,"; 
    1172     print STATION_ID_SQLFILE "contact"; 
    1173     print STATION_ID_SQLFILE ") ";  
    1174     print STATION_ID_SQLFILE "VALUES ("; 
    1175     print STATION_ID_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1176     print STATION_ID_SQLFILE ','.'\''.$title_value.'\''; 
    1177     print STATION_ID_SQLFILE ','.'\''.$institution_value.'\''; 
    1178     print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1179     print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1180     print STATION_ID_SQLFILE ','.'\''.$source_value.'\''; 
    1181     print STATION_ID_SQLFILE ','.'\''.$references_value.'\''; 
    1182     print STATION_ID_SQLFILE ','.'\''.$contact_value.'\''; 
    1183     print STATION_ID_SQLFILE ");\n"; 
    1184     close(STATION_ID_SQLFILE); 
    1185   } 
     1283    if( $bWriteSQLFiles ) 
     1284    { 
     1285      open(STATION_ID_SQLFILE,'>>../sql_in_situ_station_id/in_situ_station_id_'.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'.sql'); 
     1286      print STATION_ID_SQLFILE "-- format_category      = $format_category_value\n"; 
     1287      print STATION_ID_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1288      print STATION_ID_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1289      print STATION_ID_SQLFILE "-- package_code         = $package_code_value\n"; 
     1290      print STATION_ID_SQLFILE "-- title                = $title_value\n"; 
     1291      print STATION_ID_SQLFILE "-- institution          = $institution_value\n"; 
     1292      print STATION_ID_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1293      print STATION_ID_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1294      print STATION_ID_SQLFILE "-- source               = $source_value\n"; 
     1295      print STATION_ID_SQLFILE "-- references           = $references_value\n"; 
     1296      print STATION_ID_SQLFILE "-- contact              = $contact_value\n"; 
     1297      print STATION_ID_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1298      print STATION_ID_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     1299      print STATION_ID_SQLFILE "INSERT INTO in_situ_station_id (";  
     1300      print STATION_ID_SQLFILE "station_id,"; 
     1301      print STATION_ID_SQLFILE "title,"; 
     1302      print STATION_ID_SQLFILE "institution,"; 
     1303      print STATION_ID_SQLFILE "institution_url,"; 
     1304      print STATION_ID_SQLFILE "institution_dods_url,"; 
     1305      print STATION_ID_SQLFILE "source,"; 
     1306      print STATION_ID_SQLFILE "refs,"; 
     1307      print STATION_ID_SQLFILE "contact"; 
     1308      print STATION_ID_SQLFILE ") ";  
     1309      print STATION_ID_SQLFILE "VALUES ("; 
     1310      print STATION_ID_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1311      print STATION_ID_SQLFILE ','.'\''.$title_value.'\''; 
     1312      print STATION_ID_SQLFILE ','.'\''.$institution_value.'\''; 
     1313      print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1314      print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1315      print STATION_ID_SQLFILE ','.'\''.$source_value.'\''; 
     1316      print STATION_ID_SQLFILE ','.'\''.$references_value.'\''; 
     1317      print STATION_ID_SQLFILE ','.'\''.$contact_value.'\''; 
     1318      print STATION_ID_SQLFILE ");\n"; 
     1319      close(STATION_ID_SQLFILE); 
     1320    } 
     1321  } 
     1322   
     1323  #DWR 4/5/2008 
     1324  #Implemented code to write the obsKML files.  
     1325  my %ObsHash; 
     1326  my $rObsHash = \%ObsHash; 
     1327  my $strPlatformID = $institution_code_value.'.'.$platform_code_value.'.'.$package_code_value; 
     1328  obsKMLSubRoutines::KMLAddPlatformHashEntry( $strPlatformID, $institution_url_value, $latitude_value[0], $longitude_value[0], $rObsHash ); 
    11861329   
    11871330  # water_level (water_level) 
    1188   if ($#water_level > -1) { 
    1189     open(WATER_LEVEL_SQLFILE,'>>../sql/water_level_prod_'.$institution_code_value.'_' 
    1190       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1191     print WATER_LEVEL_SQLFILE "-- format_category      = $format_category_value\n"; 
    1192     print WATER_LEVEL_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1193     print WATER_LEVEL_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1194     print WATER_LEVEL_SQLFILE "-- package_code         = $package_code_value\n"; 
    1195     print WATER_LEVEL_SQLFILE "-- title                = $title_value\n"; 
    1196     print WATER_LEVEL_SQLFILE "-- institution          = $institution_value\n"; 
    1197     print WATER_LEVEL_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1198     print WATER_LEVEL_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1199     print WATER_LEVEL_SQLFILE "-- source               = $source_value\n"; 
    1200     print WATER_LEVEL_SQLFILE "-- references           = $references_value\n"; 
    1201     print WATER_LEVEL_SQLFILE "-- contact              = $contact_value\n"; 
    1202     print WATER_LEVEL_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1203     print WATER_LEVEL_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1204     for my $i (0..$#water_level) { 
    1205       for my $j (0..$#this_water_level_data) { 
    1206         $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1207         $this_time_stamp = $time_formatted_values[$j]; 
    1208         $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
    1209  
    1210         if ($water_level[$i]{'data'}[$j] != $missing_value_value 
    1211           && $water_level[$i]{'data'}[$j] != $Fill_value_value 
    1212           && $this_station_id_top_ts < $this_time_stamp_sec 
    1213           && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1214           print WATER_LEVEL_SQLFILE "INSERT INTO water_level_prod (";  
    1215           print WATER_LEVEL_SQLFILE "station_id,"; 
    1216           print WATER_LEVEL_SQLFILE "time_stamp,";  
    1217           print WATER_LEVEL_SQLFILE "z,"; 
    1218           print WATER_LEVEL_SQLFILE "positive,"; 
    1219           print WATER_LEVEL_SQLFILE "water_level,"; 
    1220           print WATER_LEVEL_SQLFILE "reference,"; 
    1221           print WATER_LEVEL_SQLFILE "reference_to_mllw,"; 
    1222           print WATER_LEVEL_SQLFILE "reference_to_msl,"; 
    1223           print WATER_LEVEL_SQLFILE "reference_to_navd88,"; 
    1224           print WATER_LEVEL_SQLFILE "title,"; 
    1225           print WATER_LEVEL_SQLFILE "institution,"; 
    1226           print WATER_LEVEL_SQLFILE "institution_url,"; 
    1227           print WATER_LEVEL_SQLFILE "institution_dods_url,"; 
    1228           print WATER_LEVEL_SQLFILE "source,"; 
    1229           print WATER_LEVEL_SQLFILE "refs,"; 
    1230           print WATER_LEVEL_SQLFILE "contact,"; 
    1231           print WATER_LEVEL_SQLFILE "the_geom"; 
    1232           print WATER_LEVEL_SQLFILE ") ";  
    1233           print WATER_LEVEL_SQLFILE "VALUES (";  
    1234           print WATER_LEVEL_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1235           print WATER_LEVEL_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1236           if ($water_level[$i]{'height'} == $missing_value_value 
    1237             || $water_level[$i]{'height'} == $Fill_value_value) { 
    1238             print WATER_LEVEL_SQLFILE ','.'\'\''; 
     1331  if ($#water_level > -1) 
     1332  { 
     1333    if( $bWriteSQLFiles ) 
     1334    { 
     1335      open(WATER_LEVEL_SQLFILE,'>>../sql/water_level_prod_'.$institution_code_value.'_' 
     1336        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     1337      print WATER_LEVEL_SQLFILE "-- format_category      = $format_category_value\n"; 
     1338      print WATER_LEVEL_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1339      print WATER_LEVEL_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1340      print WATER_LEVEL_SQLFILE "-- package_code         = $package_code_value\n"; 
     1341      print WATER_LEVEL_SQLFILE "-- title                = $title_value\n"; 
     1342      print WATER_LEVEL_SQLFILE "-- institution          = $institution_value\n"; 
     1343      print WATER_LEVEL_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1344      print WATER_LEVEL_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1345      print WATER_LEVEL_SQLFILE "-- source               = $source_value\n"; 
     1346      print WATER_LEVEL_SQLFILE "-- references           = $references_value\n"; 
     1347      print WATER_LEVEL_SQLFILE "-- contact              = $contact_value\n"; 
     1348      print WATER_LEVEL_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1349      print WATER_LEVEL_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     1350      for my $i (0..$#water_level) { 
     1351        for my $j (0..$#this_water_level_data) { 
     1352          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1353          $this_time_stamp = $time_formatted_values[$j]; 
     1354          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1355 
     1356          if ($water_level[$i]{'data'}[$j] != $missing_value_value 
     1357            && $water_level[$i]{'data'}[$j] != $Fill_value_value 
     1358            && $this_station_id_top_ts < $this_time_stamp_sec 
     1359            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     1360            print WATER_LEVEL_SQLFILE "INSERT INTO water_level_prod (";  
     1361            print WATER_LEVEL_SQLFILE "station_id,"; 
     1362            print WATER_LEVEL_SQLFILE "time_stamp,";  
     1363            print WATER_LEVEL_SQLFILE "z,"; 
     1364            print WATER_LEVEL_SQLFILE "positive,"; 
     1365            print WATER_LEVEL_SQLFILE "water_level,"; 
     1366            print WATER_LEVEL_SQLFILE "reference,"; 
     1367            print WATER_LEVEL_SQLFILE "reference_to_mllw,"; 
     1368            print WATER_LEVEL_SQLFILE "reference_to_msl,"; 
     1369            print WATER_LEVEL_SQLFILE "reference_to_navd88,"; 
     1370            print WATER_LEVEL_SQLFILE "title,"; 
     1371            print WATER_LEVEL_SQLFILE "institution,"; 
     1372            print WATER_LEVEL_SQLFILE "institution_url,"; 
     1373            print WATER_LEVEL_SQLFILE "institution_dods_url,"; 
     1374            print WATER_LEVEL_SQLFILE "source,"; 
     1375            print WATER_LEVEL_SQLFILE "refs,"; 
     1376            print WATER_LEVEL_SQLFILE "contact,"; 
     1377            print WATER_LEVEL_SQLFILE "the_geom"; 
     1378            print WATER_LEVEL_SQLFILE ") ";  
     1379            print WATER_LEVEL_SQLFILE "VALUES (";  
     1380            print WATER_LEVEL_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1381            print WATER_LEVEL_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     1382            if ($water_level[$i]{'height'} == $missing_value_value 
     1383              || $water_level[$i]{'height'} == $Fill_value_value) { 
     1384              print WATER_LEVEL_SQLFILE ','.'\'\''; 
     1385            } 
     1386            else { 
     1387              $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
     1388              print WATER_LEVEL_SQLFILE ','.$this_val; 
     1389            } 
     1390            print WATER_LEVEL_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     1391            $this_val = sprintf("%.2f",$water_level[$i]{'data'}[$j]); 
     1392            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1393            print WATER_LEVEL_SQLFILE ','.'\''.$water_level[$i]{'reference'}.'\''; 
     1394             
     1395             
     1396            if (length $water_level[$i]{'reference_to_mllw'} > 0) { 
     1397              $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_mllw'}); 
     1398            } 
     1399            else { 
     1400              $this_val = 'NULL'; 
     1401            } 
     1402 
     1403            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1404            if (length $water_level[$i]{'reference_to_msl'} > 0) { 
     1405              $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_msl'}); 
     1406            } 
     1407            else { 
     1408              $this_val = 'NULL'; 
     1409            } 
     1410 
     1411            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1412            if (length $water_level[$i]{'reference_to_navd88'} > 0) { 
     1413              $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_navd88'}); 
     1414            } 
     1415            else { 
     1416              $this_val = 'NULL'; 
     1417            } 
     1418 
     1419            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1420            print WATER_LEVEL_SQLFILE ','.'\''.$title_value.'\''; 
     1421            print WATER_LEVEL_SQLFILE ','.'\''.$institution_value.'\''; 
     1422            print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1423            print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1424            print WATER_LEVEL_SQLFILE ','.'\''.$source_value.'\''; 
     1425            print WATER_LEVEL_SQLFILE ','.'\''.$references_value.'\''; 
     1426            print WATER_LEVEL_SQLFILE ','.'\''.$contact_value.'\''; 
     1427            print WATER_LEVEL_SQLFILE ",GeometryFromText('POINT(";  
     1428            print WATER_LEVEL_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     1429            print WATER_LEVEL_SQLFILE ")',-1));\n"; 
    12391430          } 
    1240           else { 
    1241             $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
    1242             print WATER_LEVEL_SQLFILE ','.$this_val; 
     1431        } 
     1432         
     1433        print WATER_LEVEL_SQLFILE "\n"; 
     1434      } 
     1435      close(WATER_LEVEL_SQLFILE); 
     1436    } 
     1437    if( $bWriteobsKMLFile ) 
     1438    {        
     1439      my $MLLWDataVal = 'NULL'; 
     1440      my $MSLDataVal  = 'NULL'; 
     1441      my $NavD88DataVal = 'NULL'; 
     1442      my $Height = ''; 
     1443     
     1444      for my $i (0..$#water_level)  
     1445      { 
     1446        for my $j (0..$#this_water_level_data)  
     1447        { 
     1448          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1449          $this_time_stamp = $time_formatted_values[$j]; 
     1450          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1451 
     1452          if ($water_level[$i]{'data'}[$j] != $missing_value_value 
     1453            && $water_level[$i]{'data'}[$j] != $Fill_value_value 
     1454            && $this_station_id_top_ts < $this_time_stamp_sec 
     1455            && $this_time_stamp_sec > $oldest_ok_timestamp)  
     1456            { 
     1457     
     1458              #DWR 4/3/2008 
     1459              if ($water_level[$i]{'height'} != $missing_value_value 
     1460                && $water_level[$i]{'height'} != $Fill_value_value)  
     1461              { 
     1462                $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
     1463                $Height = $this_val; 
     1464              } 
     1465              $this_val = sprintf("%.2f",$water_level[$i]{'data'}[$j]); 
     1466               
     1467              $DataVal = $water_level[$i]{'reference'}; 
     1468               
     1469              if (length $water_level[$i]{'reference_to_mllw'} > 0) { 
     1470                $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_mllw'}); 
     1471              } 
     1472              else { 
     1473                $this_val = 'NULL'; 
     1474              } 
     1475              $MLLWDataVal = $this_val; 
     1476 
     1477              if (length $water_level[$i]{'reference_to_msl'} > 0) { 
     1478                $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_msl'}); 
     1479              } 
     1480              else { 
     1481                $this_val = 'NULL'; 
     1482              } 
     1483              $MSLDataVal = $this_val; 
     1484 
     1485              if (length $water_level[$i]{'reference_to_navd88'} > 0) { 
     1486                $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_navd88'}); 
     1487              } 
     1488              else { 
     1489                $this_val = 'NULL'; 
     1490              } 
     1491              #DWR 4/5/2008 
     1492              $NavD88DataVal = $this_val; 
    12431493          } 
    1244           print WATER_LEVEL_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1245           $this_val = sprintf("%.2f",$water_level[$i]{'data'}[$j]); 
    1246           print WATER_LEVEL_SQLFILE ','.$this_val; 
    1247           print WATER_LEVEL_SQLFILE ','.'\''.$water_level[$i]{'reference'}.'\''; 
    1248           if (length $water_level[$i]{'reference_to_mllw'} > 0) { 
    1249             $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_mllw'}); 
    1250           } 
    1251           else { 
    1252             $this_val = 'NULL'; 
    1253           } 
    1254           print WATER_LEVEL_SQLFILE ','.$this_val; 
    1255           if (length $water_level[$i]{'reference_to_msl'} > 0) { 
    1256             $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_msl'}); 
    1257           } 
    1258           else { 
    1259             $this_val = 'NULL'; 
    1260           } 
    1261           print WATER_LEVEL_SQLFILE ','.$this_val; 
    1262           if (length $water_level[$i]{'reference_to_navd88'} > 0) { 
    1263             $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_navd88'}); 
    1264           } 
    1265           else { 
    1266             $this_val = 'NULL'; 
    1267           } 
    1268           print WATER_LEVEL_SQLFILE ','.$this_val; 
    1269           print WATER_LEVEL_SQLFILE ','.'\''.$title_value.'\''; 
    1270           print WATER_LEVEL_SQLFILE ','.'\''.$institution_value.'\''; 
    1271           print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1272           print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1273           print WATER_LEVEL_SQLFILE ','.'\''.$source_value.'\''; 
    1274           print WATER_LEVEL_SQLFILE ','.'\''.$references_value.'\''; 
    1275           print WATER_LEVEL_SQLFILE ','.'\''.$contact_value.'\''; 
    1276           print WATER_LEVEL_SQLFILE ",GeometryFromText('POINT(";  
    1277           print WATER_LEVEL_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1278           print WATER_LEVEL_SQLFILE ")',-1));\n"; 
    1279         } 
    1280       } 
    1281       print WATER_LEVEL_SQLFILE "\n"; 
    1282     } 
    1283     close(WATER_LEVEL_SQLFILE); 
    1284   } 
    1285    
     1494          obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
     1495                                            $time_formatted_values[$j], 
     1496                                            $MLLWDataVal, 
     1497                                            1, 
     1498                                            $strPlatformID, 
     1499                                            $Height, 
     1500                                            'm(MLLW)', 
     1501                                            $rObsHash ); 
     1502          obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
     1503                                            $time_formatted_values[$j], 
     1504                                            $MSLDataVal, 
     1505                                            2, 
     1506                                            $strPlatformID, 
     1507                                            $Height, 
     1508                                            'm(MSL)', 
     1509                                            $rObsHash ); 
     1510          obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
     1511                                            $time_formatted_values[$j], 
     1512                                            $NavD88DataVal, 
     1513                                            3, 
     1514                                            $strPlatformID, 
     1515                                            $Height, 
     1516                                            'm(NAVD88)', 
     1517                                            $rObsHash ); 
     1518        } 
     1519      }             
     1520    } 
     1521  } 
    12861522  # sea_surface_temperature (sst) 
    12871523  if ($#sea_surface_temperature > -1) { 
    1288     open(SST_SQLFILE,'>>../sql/sst_prod_'.$institution_code_value.'_' 
    1289       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1290     print SST_SQLFILE "-- format_category      = $format_category_value\n"; 
    1291     print SST_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1292     print SST_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1293     print SST_SQLFILE "-- package_code         = $package_code_value\n"; 
    1294     print SST_SQLFILE "-- title                = $title_value\n"; 
    1295     print SST_SQLFILE "-- institution          = $institution_value\n"; 
    1296     print SST_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1297     print SST_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1298     print SST_SQLFILE "-- source               = $source_value\n"; 
    1299     print SST_SQLFILE "-- references           = $references_value\n"; 
    1300     print SST_SQLFILE "-- contact              = $contact_value\n"; 
    1301     print SST_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1302     print SST_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1303     for my $i (0..$#sea_surface_temperature) { 
    1304       for my $j (0..$#this_sea_surface_temperature_data) { 
    1305         $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1306         $this_time_stamp = $time_formatted_values[$j]; 
    1307         $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1524    if( $bWriteSQLFiles ) 
     1525    { 
     1526      open(SST_SQLFILE,'>>../sql/sst_prod_'.$institution_code_value.'_' 
     1527        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     1528      print SST_SQLFILE "-- format_category      = $format_category_value\n"; 
     1529      print SST_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1530      print SST_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1531      print SST_SQLFILE "-- package_code         = $package_code_value\n"; 
     1532      print SST_SQLFILE "-- title                = $title_value\n"; 
     1533      print SST_SQLFILE "-- institution          = $institution_value\n"; 
     1534      print SST_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1535      print SST_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1536      print SST_SQLFILE "-- source               = $source_value\n"; 
     1537      print SST_SQLFILE "-- references           = $references_value\n"; 
     1538      print SST_SQLFILE "-- contact              = $contact_value\n"; 
     1539      print SST_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1540      print SST_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    13081541       
    1309         if ($sea_surface_temperature[$i]{'data'}[$j] != $missing_value_value 
    1310           && $sea_surface_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1311           && $this_station_id_top_ts < $this_time_stamp_sec 
    1312           && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1313           print SST_SQLFILE "INSERT INTO sst_prod (";  
    1314           print SST_SQLFILE "station_id,"; 
    1315           print SST_SQLFILE "time_stamp,";  
    1316           print SST_SQLFILE "z,"; 
    1317           print SST_SQLFILE "positive,"; 
    1318           print SST_SQLFILE "temperature_celcius,"; 
    1319           print SST_SQLFILE "title,"; 
    1320           print SST_SQLFILE "institution,"; 
    1321           print SST_SQLFILE "institution_url,"; 
    1322           print SST_SQLFILE "institution_dods_url,"; 
    1323           print SST_SQLFILE "source,"; 
    1324           print SST_SQLFILE "refs,"; 
    1325           print SST_SQLFILE "contact,"; 
    1326           print SST_SQLFILE "the_geom"; 
    1327           print SST_SQLFILE ") ";  
    1328           print SST_SQLFILE "VALUES (";  
    1329           print SST_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1330           print SST_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1331           if ($sea_surface_temperature[$i]{'height'} == $missing_value_value 
    1332             || $sea_surface_temperature[$i]{'height'} == $Fill_value_value) { 
    1333             print SST_SQLFILE ','.'\'\''; 
     1542      my $DataVal = 'NULL'; 
     1543      my $Height = ''; 
     1544      for my $i (0..$#sea_surface_temperature) { 
     1545        for my $j (0..$#this_sea_surface_temperature_data) { 
     1546          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1547          $this_time_stamp = $time_formatted_values[$j]; 
     1548          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1549         
     1550          if ($sea_surface_temperature[$i]{'data'}[$j] != $missing_value_value 
     1551            && $sea_surface_temperature[$i]{'data'}[$j] != $Fill_value_value 
     1552            && $this_station_id_top_ts < $this_time_stamp_sec 
     1553            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     1554            print SST_SQLFILE "INSERT INTO sst_prod (";  
     1555            print SST_SQLFILE "station_id,"; 
     1556            print SST_SQLFILE "time_stamp,";  
     1557            print SST_SQLFILE "z,"; 
     1558            print SST_SQLFILE "positive,"; 
     1559            print SST_SQLFILE "temperature_celcius,"; 
     1560            print SST_SQLFILE "title,"; 
     1561            print SST_SQLFILE "institution,"; 
     1562            print SST_SQLFILE "institution_url,"; 
     1563            print SST_SQLFILE "institution_dods_url,"; 
     1564            print SST_SQLFILE "source,"; 
     1565            print SST_SQLFILE "refs,"; 
     1566            print SST_SQLFILE "contact,"; 
     1567            print SST_SQLFILE "the_geom"; 
     1568            print SST_SQLFILE ") ";  
     1569            print SST_SQLFILE "VALUES (";  
     1570            print SST_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1571            print SST_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     1572            if ($sea_surface_temperature[$i]{'height'} == $missing_value_value 
     1573              || $sea_surface_temperature[$i]{'height'} == $Fill_value_value) { 
     1574              print SST_SQLFILE ','.'\'\''; 
     1575            } 
     1576            else { 
     1577              $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'height'}); 
     1578              print SST_SQLFILE ','.$this_val; 
     1579            } 
     1580 
     1581            print SST_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     1582            $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'data'}[$j]); 
     1583             
     1584             
     1585            print SST_SQLFILE ','.$this_val; 
     1586            print SST_SQLFILE ','.'\''.$title_value.'\''; 
     1587            print SST_SQLFILE ','.'\''.$institution_value.'\''; 
     1588            print SST_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1589            print SST_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1590            print SST_SQLFILE ','.'\''.$source_value.'\''; 
     1591            print SST_SQLFILE ','.'\''.$references_value.'\''; 
     1592            print SST_SQLFILE ','.'\''.$contact_value.'\''; 
     1593            print SST_SQLFILE ",GeometryFromText('POINT(";  
     1594            print SST_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     1595            print SST_SQLFILE ")',-1));\n"; 
    13341596          } 
    1335           else { 
    1336             $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'height'}); 
    1337             print SST_SQLFILE ','.$this_val; 
     1597           
     1598        } 
     1599        print SST_SQLFILE "\n"; 
     1600      } 
     1601      close(SST_SQLFILE); 
     1602    } 
     1603    #DWR 4/5/2008 
     1604    if( $bWriteobsKMLFile ) 
     1605    { 
     1606      my $DataVal = 'NULL'; 
     1607      my $Height = ''; 
     1608      for my $i (0..$#sea_surface_temperature)  
     1609      { 
     1610        for my $j (0..$#this_sea_surface_temperature_data)  
     1611        { 
     1612          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1613          $this_time_stamp = $time_formatted_values[$j]; 
     1614          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1615         
     1616          if ($sea_surface_temperature[$i]{'data'}[$j] != $missing_value_value 
     1617              && $sea_surface_temperature[$i]{'data'}[$j] != $Fill_value_value 
     1618              && $this_station_id_top_ts < $this_time_stamp_sec 
     1619              && $this_time_stamp_sec > $oldest_ok_timestamp)  
     1620          { 
     1621 
     1622            if ($sea_surface_temperature[$i]{'height'} != $missing_value_value 
     1623              && $sea_surface_temperature[$i]{'height'} != $Fill_value_value)  
     1624            { 
     1625              $Height = sprintf("%.2f",$sea_surface_temperature[$i]{'height'}); 
     1626            } 
     1627            $DataVal = sprintf("%.2f",$sea_surface_temperature[$i]{'data'}[$j]); 
     1628                       
    13381629          } 
    1339           print SST_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1340           $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'data'}[$j]); 
    1341           print SST_SQLFILE ','.$this_val; 
    1342           print SST_SQLFILE ','.'\''.$title_value.'\''; 
    1343           print SST_SQLFILE ','.'\''.$institution_value.'\''; 
    1344           print SST_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1345           print SST_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1346           print SST_SQLFILE ','.'\''.$source_value.'\''; 
    1347           print SST_SQLFILE ','.'\''.$references_value.'\''; 
    1348           print SST_SQLFILE ','.'\''.$contact_value.'\''; 
    1349           print SST_SQLFILE ",GeometryFromText('POINT(";  
    1350           print SST_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1351           print SST_SQLFILE ")',-1));\n"; 
    1352         } 
    1353       } 
    1354       print SST_SQLFILE "\n"; 
    1355     } 
    1356     close(SST_SQLFILE); 
    1357   } 
    1358  
     1630          my $strUnits; 
     1631          $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_surface_temperature[$i]{'units'}, $XMLControlFile ); 
     1632          if( length( $strUnits ) == 0 ) 
     1633          { 
     1634            $strUnits = $sea_surface_temperature[$i]{'units'}; 
     1635          } 
     1636          obsKMLSubRoutines::KMLAddObsToHash( 'water_temperature',  
     1637                                            $time_formatted_values[$j], 
     1638                                            $DataVal, 
     1639                                            1, 
     1640                                            $strPlatformID, 
     1641                                            $Height, 
     1642                                            $strUnits, 
     1643                                            $rObsHash ); 
     1644        } 
     1645      }                                             
     1646    } 
     1647  } 
    13591648  # sea_bottom_temperature (sbt) 
    13601649  if ($#sea_bottom_temperature > -1) { 
    1361     open(SBT_SQLFILE,'>>../sql/bottom_water_temp_prod_'.$institution_code_value.'_' 
    1362       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1363     print SBT_SQLFILE "-- format_category      = $format_category_value\n"; 
    1364     print SBT_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1365     print SBT_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1366     print SBT_SQLFILE "-- package_code         = $package_code_value\n"; 
    1367     print SBT_SQLFILE "-- title                = $title_value\n"; 
    1368     print SBT_SQLFILE "-- institution          = $institution_value\n"; 
    1369     print SBT_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1370     print SBT_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1371     print SBT_SQLFILE "-- source               = $source_value\n"; 
    1372     print SBT_SQLFILE "-- references           = $references_value\n"; 
    1373     print SBT_SQLFILE "-- contact              = $contact_value\n"; 
    1374     print SBT_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1375     print SBT_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1376     for my $i (0..$#sea_bottom_temperature) { 
    1377       for my $j (0..$#this_sea_bottom_temperature_data) { 
    1378         $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1379         $this_time_stamp = $time_formatted_values[$j]; 
    1380         $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1650    if( $bWriteSQLFiles ) 
     1651    { 
     1652      open(SBT_SQLFILE,'>>../sql/bottom_water_temp_prod_'.$institution_code_value.'_' 
     1653        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     1654      print SBT_SQLFILE "-- format_category      = $format_category_value\n"; 
     1655      print SBT_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1656      print SBT_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1657      print SBT_SQLFILE "-- package_code         = $package_code_value\n"; 
     1658      print SBT_SQLFILE "-- title                = $title_value\n"; 
     1659      print SBT_SQLFILE "-- institution          = $institution_value\n"; 
     1660      print SBT_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1661      print SBT_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1662      print SBT_SQLFILE "-- source               = $source_value\n"; 
     1663      print SBT_SQLFILE "-- references           = $references_value\n"; 
     1664      print SBT_SQLFILE "-- contact              = $contact_value\n"; 
     1665      print SBT_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1666      print SBT_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    13811667       
    1382         if ($sea_bottom_temperature[$i]{'data'}[$j] != $missing_value_value 
    1383           && $sea_bottom_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1384           && $this_station_id_top_ts < $this_time_stamp_sec 
    1385           && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1386           print SBT_SQLFILE "INSERT INTO bottom_water_temp_prod (";  
    1387           print SBT_SQLFILE "station_id,"; 
    1388           print SBT_SQLFILE "time_stamp,";  
    1389           print SBT_SQLFILE "z,"; 
    1390           print SBT_SQLFILE "positive,"; 
    1391           print SBT_SQLFILE "temperature_celcius,"; 
    1392           print SBT_SQLFILE "title,"; 
    1393           print SBT_SQLFILE "institution,"; 
    1394           print SBT_SQLFILE "institution_url,"; 
    1395           print SBT_SQLFILE "institution_dods_url,"; 
    1396           print SBT_SQLFILE "source,"; 
    1397           print SBT_SQLFILE "refs,"; 
    1398           print SBT_SQLFILE "contact,"; 
    1399           print SBT_SQLFILE "the_geom"; 
    1400           print SBT_SQLFILE ") ";  
    1401           print SBT_SQLFILE "VALUES (";  
    1402           print SBT_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1403           print SBT_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1404           if ($sea_bottom_temperature[$i]{'height'} == $missing_value_value 
    1405             || $sea_bottom_temperature[$i]{'height'} == $Fill_value_value) { 
    1406             print SBT_SQLFILE ','.'\'\''; 
     1668      for my $i (0..$#sea_bottom_temperature) { 
     1669        for my $j (0..$#this_sea_bottom_temperature_data) { 
     1670          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1671          $this_time_stamp = $time_formatted_values[$j]; 
     1672          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1673         
     1674          if ($sea_bottom_temperature[$i]{'data'}[$j] != $missing_value_value 
     1675            && $sea_bottom_temperature[$i]{'data'}[$j] != $Fill_value_value 
     1676            && $this_station_id_top_ts < $this_time_stamp_sec 
     1677            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     1678            print SBT_SQLFILE "INSERT INTO bottom_water_temp_prod (";  
     1679            print SBT_SQLFILE "station_id,"; 
     1680            print SBT_SQLFILE "time_stamp,";  
     1681            print SBT_SQLFILE "z,"; 
     1682            print SBT_SQLFILE "positive,"; 
     1683            print SBT_SQLFILE "temperature_celcius,"; 
     1684            print SBT_SQLFILE "title,"; 
     1685            print SBT_SQLFILE "institution,"; 
     1686            print SBT_SQLFILE "institution_url,"; 
     1687            print SBT_SQLFILE "institution_dods_url,"; 
     1688            print SBT_SQLFILE "source,"; 
     1689            print SBT_SQLFILE "refs,"; 
     1690            print SBT_SQLFILE "contact,"; 
     1691            print SBT_SQLFILE "the_geom"; 
     1692            print SBT_SQLFILE ") ";  
     1693            print SBT_SQLFILE "VALUES (";  
     1694            print SBT_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1695            print SBT_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     1696            if ($sea_bottom_temperature[$i]{'height'} == $missing_value_value 
     1697              || $sea_bottom_temperature[$i]{'height'} == $Fill_value_value) { 
     1698              print SBT_SQLFILE ','.'\'\''; 
     1699            } 
     1700            else { 
     1701              $this_val = sprintf("%.2f",$sea_bottom_temperature[$i]{'height'}); 
     1702              print SBT_SQLFILE ','.$this_val; 
     1703            } 
     1704 
     1705            print SBT_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     1706            $this_val = sprintf("%.2f",$sea_bottom_temperature[$i]{'data'}[$j]); 
     1707             
     1708             
     1709            print SBT_SQLFILE ','.$this_val; 
     1710            print SBT_SQLFILE ','.'\''.$title_value.'\''; 
     1711            print SBT_SQLFILE ','.'\''.$institution_value.'\''; 
     1712            print SBT_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1713            print SBT_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1714            print SBT_SQLFILE ','.'\''.$source_value.'\''; 
     1715            print SBT_SQLFILE ','.'\''.$references_value.'\''; 
     1716            print SBT_SQLFILE ','.'\''.$contact_value.'\''; 
     1717            print SBT_SQLFILE ",GeometryFromText('POINT(";  
     1718            print SBT_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     1719            print SBT_SQLFILE ")',-1));\n"; 
    14071720          } 
    1408           else { 
    1409             $this_val = sprintf("%.2f",$sea_bottom_temperature[$i]{'height'}); 
    1410             print SBT_SQLFILE ','.$this_val; 
     1721 
     1722        } 
     1723        print SBT_SQLFILE "\n"; 
     1724      } 
     1725      close(SBT_SQLFILE); 
     1726    } 
     1727    #DWR 4/5/2008 
     1728    if( $bWriteobsKMLFile ) 
     1729    { 
     1730      my $DataVal = 'NULL'; 
     1731      my $Height = ''; 
     1732      for my $i (0..$#sea_bottom_temperature)  
     1733      { 
     1734        for my $j (0..$#sea_bottom_temperature)  
     1735        { 
     1736          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1737          $this_time_stamp = $time_formatted_values[$j]; 
     1738          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4));        
     1739          if ($sea_bottom_temperature[$i]{'data'}[$j] != $missing_value_value 
     1740              && $sea_bottom_temperature[$i]{'data'}[$j] != $Fill_value_value 
     1741              && $this_station_id_top_ts < $this_time_stamp_sec 
     1742              && $this_time_stamp_sec > $oldest_ok_timestamp)  
     1743          { 
     1744 
     1745            if ($sea_bottom_temperature[$i]{'height'} != $missing_value_value 
     1746              && $sea_bottom_temperature[$i]{'height'} != $Fill_value_value)  
     1747            { 
     1748              $Height = sprintf("%.2f",$sea_bottom_temperature[$i]{'height'}); 
     1749            } 
     1750            $DataVal = sprintf("%.2f",$sea_bottom_temperature[$i]{'data'}[$j]); 
     1751                       
    14111752          } 
    1412           print SBT_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1413           $this_val = sprintf("%.2f",$sea_bottom_temperature[$i]{'data'}[$j]); 
    1414           print SBT_SQLFILE ','.$this_val; 
    1415           print SBT_SQLFILE ','.'\''.$title_value.'\''; 
    1416           print SBT_SQLFILE ','.'\''.$institution_value.'\''; 
    1417           print SBT_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1418           print SBT_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1419           print SBT_SQLFILE ','.'\''.$source_value.'\''; 
    1420           print SBT_SQLFILE ','.'\''.$references_value.'\''; 
    1421           print SBT_SQLFILE ','.'\''.$contact_value.'\''; 
    1422           print SBT_SQLFILE ",GeometryFromText('POINT(";  
    1423           print SBT_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1424           print SBT_SQLFILE ")',-1));\n"; 
    1425         } 
    1426       } 
    1427       print SBT_SQLFILE "\n"; 
    1428     } 
    1429     close(SBT_SQLFILE); 
    1430   } 
    1431  
     1753          my $strUnits; 
     1754          $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_bottom_temperature[$i]{'units'}, $XMLControlFile );         
     1755          if( length( $strUnits ) == 0 ) 
     1756          { 
     1757           $strUnits = $sea_bottom_temperature[$i]{'units'}; 
     1758          } 
     1759          obsKMLSubRoutines::KMLAddObsToHash( 'water_temperature',  
     1760                                            $time_formatted_values[$j], 
     1761                                            $DataVal, 
     1762                                            2, 
     1763                                            $strPlatformID, 
     1764                                            $Height, 
     1765                                            $strUnits, 
     1766                                            $rObsHash ); 
     1767        } 
     1768      }                                             
     1769    }     
     1770  } 
     1771   
    14321772  # air_temperature 
    14331773  # Going to do this right this time.  Instead of populating each row w/ all 
    14341774  # the metadata, use the station_id lookup, instead. 
    14351775  if ($#air_temperature > -1) { 
    1436     open(AIR_TEMP_SQLFILE,'>>../sql/air_temperature_prod_'.$institution_code_value.'_' 
    1437       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1438     print AIR_TEMP_SQLFILE "-- format_category      = $format_category_value\n"; 
    1439     print AIR_TEMP_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1440     print AIR_TEMP_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1441     print AIR_TEMP_SQLFILE "-- package_code         = $package_code_value\n"; 
    1442     print AIR_TEMP_SQLFILE "-- title                = $title_value\n"; 
    1443     print AIR_TEMP_SQLFILE "-- institution          = $institution_value\n"; 
    1444     print AIR_TEMP_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1445     print AIR_TEMP_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1446     print AIR_TEMP_SQLFILE "-- source               = $source_value\n"; 
    1447     print AIR_TEMP_SQLFILE "-- references           = $references_value\n"; 
    1448     print AIR_TEMP_SQLFILE "-- contact              = $contact_value\n"; 
    1449     print AIR_TEMP_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1450     print AIR_TEMP_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1451     for my $i (0..$#air_temperature) { 
    1452       for my $j (0..$#this_air_temperature_data) { 
    1453         $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1454         $this_time_stamp = $time_formatted_values[$j]; 
    1455         $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1776    #DWR 4/5/2008  
     1777    if( $bWriteSQLFiles ) 
     1778    { 
     1779      open(AIR_TEMP_SQLFILE,'>>../sql/air_temperature_prod_'.$institution_code_value.'_' 
     1780        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     1781      print AIR_TEMP_SQLFILE "-- format_category      = $format_category_value\n"; 
     1782      print AIR_TEMP_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1783      print AIR_TEMP_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1784      print AIR_TEMP_SQLFILE "-- package_code         = $package_code_value\n"; 
     1785      print AIR_TEMP_SQLFILE "-- title                = $title_value\n"; 
     1786      print AIR_TEMP_SQLFILE "-- institution          = $institution_value\n"; 
     1787      print AIR_TEMP_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1788      print AIR_TEMP_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1789      print AIR_TEMP_SQLFILE "-- source               = $source_value\n"; 
     1790      print AIR_TEMP_SQLFILE "-- references           = $references_value\n"; 
     1791      print AIR_TEMP_SQLFILE "-- contact              = $contact_value\n"; 
     1792      print AIR_TEMP_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1793      print AIR_TEMP_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     1794      for my $i (0..$#air_temperature) { 
     1795        for my $j (0..$#this_air_temperature_data) { 
     1796          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1797          $this_time_stamp = $time_formatted_values[$j]; 
     1798          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1799 
     1800          my $DataValue = 'NULL'; 
     1801          my $Height    = ''; 
     1802          if ($air_temperature[$i]{'data'}[$j] != $missing_value_value 
     1803            && $air_temperature[$i]{'data'}[$j] != $Fill_value_value 
     1804            && $this_station_id_top_ts < $this_time_stamp_sec 
     1805            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     1806            print AIR_TEMP_SQLFILE "INSERT INTO air_temperature_prod (";  
     1807            print AIR_TEMP_SQLFILE "station_id,"; 
     1808            print AIR_TEMP_SQLFILE "time_stamp,";  
     1809            print AIR_TEMP_SQLFILE "z,"; 
     1810            print AIR_TEMP_SQLFILE "positive,"; 
     1811            print AIR_TEMP_SQLFILE "temperature_celcius,"; 
     1812            print AIR_TEMP_SQLFILE "the_geom"; 
     1813            print AIR_TEMP_SQLFILE ") ";  
     1814            print AIR_TEMP_SQLFILE "VALUES (";  
     1815            print AIR_TEMP_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1816            print AIR_TEMP_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     1817            if ($air_temperature[$i]{'height'} == $missing_value_value 
     1818              || $air_temperature[$i]{'height'} == $Fill_value_value) { 
     1819              print AIR_TEMP_SQLFILE ',NULL'; 
     1820            } 
     1821            else { 
     1822              $this_val = sprintf("%.2f",$air_temperature[$i]{'height'}); 
     1823              print AIR_TEMP_SQLFILE ','.$this_val;               
     1824            } 
     1825            print AIR_TEMP_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     1826            $this_val = sprintf("%.2f",$air_temperature[$i]{'data'}[$j]); 
     1827            print AIR_TEMP_SQLFILE ','.$this_val; 
     1828            print AIR_TEMP_SQLFILE ",GeometryFromText('POINT(";  
     1829            print AIR_TEMP_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     1830            print AIR_TEMP_SQLFILE ")',-1));\n"; 
     1831                        
     1832          } 
     1833        } 
     1834        print AIR_TEMP_SQLFILE "\n"; 
     1835      } 
     1836      close(AIR_TEMP_SQLFILE); 
     1837    } 
     1838    #DWR 4/5/2008 
     1839    if( $bWriteobsKMLFile ) 
     1840    { 
     1841      my $DataVal = 'NULL'; 
     1842      my $Height = ''; 
     1843      for my $i (0..$#air_temperature)  
     1844      { 
     1845        for my $j (0..$#air_temperature)  
     1846        { 
     1847          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1848          $this_time_stamp = $time_formatted_values[$j]; 
     1849          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
    14561850         
    1457         if ($air_temperature[$i]{'data'}[$j] != $missing_value_value 
    1458           && $air_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1459           && $this_station_id_top_ts < $this_time_stamp_sec 
    1460           && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1461           print AIR_TEMP_SQLFILE "INSERT INTO air_temperature_prod (";  
    1462           print AIR_TEMP_SQLFILE "station_id,"; 
    1463           print AIR_TEMP_SQLFILE "time_stamp,";  
    1464           print AIR_TEMP_SQLFILE "z,"; 
    1465           print AIR_TEMP_SQLFILE "positive,"; 
    1466           print AIR_TEMP_SQLFILE "temperature_celcius,"; 
    1467           print AIR_TEMP_SQLFILE "the_geom"; 
    1468           print AIR_TEMP_SQLFILE ") ";  
    1469           print AIR_TEMP_SQLFILE "VALUES (";  
    1470           print AIR_TEMP_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1471           print AIR_TEMP_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1472           if ($air_temperature[$i]{'height'} == $missing_value_value 
    1473             || $air_temperature[$i]{'height'} == $Fill_value_value) { 
    1474             print AIR_TEMP_SQLFILE ',NULL'; 
     1851          if ($air_temperature[$i]{'data'}[$j] != $missing_value_value 
     1852              && $air_temperature[$i]{'data'}[$j] != $Fill_value_value 
     1853              && $this_station_id_top_ts < $this_time_stamp_sec 
     1854              && $this_time_stamp_sec > $oldest_ok_timestamp)  
     1855          { 
     1856 
     1857            if ($air_temperature[$i]{'height'} != $missing_value_value 
     1858              && $air_temperature[$i]{'height'} != $Fill_value_value)  
     1859            { 
     1860              $Height = sprintf("%.2f",$air_temperature[$i]{'height'}); 
     1861            } 
     1862            $DataVal = sprintf("%.2f",$air_temperature[$i]{'data'}[$j]); 
     1863                       
    14751864          } 
    1476           else { 
    1477             $this_val = sprintf("%.2f",$air_temperature[$i]{'height'}); 
    1478             print AIR_TEMP_SQLFILE ','.$this_val; 
     1865          my $strUnits; 
     1866          $strUnits = obsKMLSubRoutines::UnitsStringConversion( $air_temperature[$i]{'units'}, $XMLControlFile );         
     1867          if( length( $strUnits ) == 0 ) 
     1868          { 
     1869           $strUnits = $air_temperature[$i]{'units'}; 
    14791870          } 
    1480           print AIR_TEMP_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1481           $this_val = sprintf("%.2f",$air_temperature[$i]{'data'}[$j]); 
    1482           print AIR_TEMP_SQLFILE ','.$this_val; 
    1483           print AIR_TEMP_SQLFILE ",GeometryFromText('POINT(";  
    1484           print AIR_TEMP_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1485           print AIR_TEMP_SQLFILE ")',-1));\n"; 
    1486         } 
    1487       } 
    1488       print AIR_TEMP_SQLFILE "\n"; 
    1489     } 
    1490     close(AIR_TEMP_SQLFILE); 
    1491   } 
     1871          obsKMLSubRoutines::KMLAddObsToHash( 'air_temperature',  
     1872                                            $time_formatted_values[$j], 
     1873                                            $DataVal, 
     1874                                            1, 
     1875                                            $strPlatformID, 
     1876                                            $Height, 
     1877                                            $strUnits, 
     1878                                            $rObsHash ); 
     1879        } 
     1880      }                                             
     1881    }         
     1882  } 
     1883  
    14921884 
    14931885  # wind_speed and wind_gust and wind_from_direction 
     
    14961888  # Assume that wind_speed controls everything (from_dir, gust, z, normalized) index-wise. 
    14971889  if ($#wind_speed > -1) { 
    1498     open(WIND_SQLFILE,'>>../sql/wind_prod_'.$institution_code_value.'_' 
    1499       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1500     print WIND_SQLFILE "-- format_category      = $format_category_value\n"; 
    1501     print WIND_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1502     print WIND_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1503     print WIND_SQLFILE "-- package_code         = $package_code_value\n"; 
    1504     print WIND_SQLFILE "-- title                = $title_value\n"; 
    1505     print WIND_SQLFILE "-- institution          = $institution_value\n"; 
    1506     print WIND_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1507     print WIND_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1508     print WIND_SQLFILE "-- source               = $source_value\n"; 
    1509     print WIND_SQLFILE "-- references           = $references_value\n"; 
    1510     print WIND_SQLFILE "-- contact              = $contact_value\n"; 
    1511     print WIND_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1512     print WIND_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1513     for my $i (0..$#wind_speed) { 
    1514       my $j = 0; 
    1515       while ($j <= $#wind_from_direction 
    1516         && $wind_from_direction[$j]{'height'} != $wind_speed[$i]{'height'}) { 
    1517         $j++; 
    1518       } 
    1519       if ($j > $#wind_from_direction) { 
    1520         die "ABORT!  Could not find matching wind_from_direction for $wind_speed[$i]  {'var_name'}.\n"; 
    1521       } 
    1522       else { 
    1523         for my $k (0..$#this_wind_speed_data) { 
    1524           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1525           $this_time_stamp = $time_formatted_values[$k]; 
    1526           $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
    1527            
    1528           if ($wind_speed[$i]{'data'}[$k] != $missing_value_value 
    1529             && $wind_speed[$i]{'data'}[$k] != $Fill_value_value 
    1530             && $wind_from_direction[$j]{'data'}[$k] != $missing_value_value 
    1531             && $wind_from_direction[$j]{'data'}[$k] != $Fill_value_value 
    1532             && $this_station_id_top_ts < $this_time_stamp_sec 
    1533             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1534             print WIND_SQLFILE "INSERT INTO wind_prod (";  
    1535             print WIND_SQLFILE "station_id,"; 
    1536             print WIND_SQLFILE "time_stamp,";  
    1537             print WIND_SQLFILE "z,"; 
    1538             print WIND_SQLFILE "positive,"; 
    1539             print WIND_SQLFILE "wind_speed,"; 
    1540             print WIND_SQLFILE "wind_gust,"; 
    1541             print WIND_SQLFILE "wind_from_direction,"; 
    1542             print WIND_SQLFILE "can_be_normalized,"; 
    1543             print WIND_SQLFILE "title,"; 
    1544             print WIND_SQLFILE "institution,"; 
    1545             print WIND_SQLFILE "institution_url,"; 
    1546             print WIND_SQLFILE "institution_dods_url,"; 
    1547             print WIND_SQLFILE "source,"; 
    1548             print WIND_SQLFILE "refs,"; 
    1549             print WIND_SQLFILE "contact,"; 
    1550             print WIND_SQLFILE "the_geom"; 
    1551             print WIND_SQLFILE ") ";  
    1552             print WIND_SQLFILE "VALUES (";  
    1553             print WIND_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1554             print WIND_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$k].'\''; 
    1555             if ($wind_speed[$i]{'height'} == $missing_value_value 
    1556               || $wind_speed[$i]{'height'} == $Fill_value_value) { 
    1557               print WIND_SQLFILE ','.'\'\''; 
    1558             } 
    1559             else { 
    1560               $this_val = sprintf("%.2f",$wind_speed[$i]{'height'}); 
     1890    #dwr 4/5/2008 
     1891    if( $bWriteSQLFiles ) 
     1892    { 
     1893      open(WIND_SQLFILE,'>>../sql/wind_prod_'.$institution_code_value.'_' 
     1894        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     1895      print WIND_SQLFILE "-- format_category      = $format_category_value\n"; 
     1896      print WIND_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1897      print WIND_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1898      print WIND_SQLFILE "-- package_code         = $package_code_value\n"; 
     1899      print WIND_SQLFILE "-- title                = $title_value\n"; 
     1900      print WIND_SQLFILE "-- institution          = $institution_value\n"; 
     1901      print WIND_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1902      print WIND_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1903      print WIND_SQLFILE "-- source               = $source_value\n"; 
     1904      print WIND_SQLFILE "-- references           = $references_value\n"; 
     1905      print WIND_SQLFILE "-- contact              = $contact_value\n"; 
     1906      print WIND_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1907      print WIND_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     1908      for my $i (0..$#wind_speed) { 
     1909        my $j = 0; 
     1910        while ($j <= $#wind_from_direction 
     1911          && $wind_from_direction[$j]{'height'} != $wind_speed[$i]{'height'}) { 
     1912          $j++; 
     1913        } 
     1914        if ($j > $#wind_from_direction) { 
     1915          die "ABORT!  Could not find matching wind_from_direction for $wind_speed[$i]  {'var_name'}.\n"; 
     1916        } 
     1917        else { 
     1918          for my $k (0..$#this_wind_speed_data) { 
     1919            $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1920            $this_time_stamp = $time_formatted_values[$k]; 
     1921            $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     1922             
     1923            if ($wind_speed[$i]{'data'}[$k] != $missing_value_value 
     1924              && $wind_speed[$i]{'data'}[$k] != $Fill_value_value 
     1925              && $wind_from_direction[$j]{'data'}[$k] != $missing_value_value 
     1926              && $wind_from_direction[$j]{'data'}[$k] != $Fill_value_value 
     1927              && $this_station_id_top_ts < $this_time_stamp_sec 
     1928              && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     1929              print WIND_SQLFILE "INSERT INTO wind_prod (";  
     1930              print WIND_SQLFILE "station_id,"; 
     1931              print WIND_SQLFILE "time_stamp,";  
     1932              print WIND_SQLFILE "z,"; 
     1933              print WIND_SQLFILE "positive,"; 
     1934              print WIND_SQLFILE "wind_speed,"; 
     1935              print WIND_SQLFILE "wind_gust,"; 
     1936              print WIND_SQLFILE "wind_from_direction,"; 
     1937              print WIND_SQLFILE "can_be_normalized,"; 
     1938              print WIND_SQLFILE "title,"; 
     1939              print WIND_SQLFILE "institution,"; 
     1940              print WIND_SQLFILE "institution_url,"; 
     1941              print WIND_SQLFILE "institution_dods_url,"; 
     1942              print WIND_SQLFILE "source,"; 
     1943              print WIND_SQLFILE "refs,"; 
     1944              print WIND_SQLFILE "contact,"; 
     1945              print WIND_SQLFILE "the_geom"; 
     1946              print WIND_SQLFILE ") ";  
     1947              print WIND_SQLFILE "VALUES (";  
     1948              print WIND_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1949              print WIND_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$k].'\''; 
     1950              if ($wind_speed[$i]{'height'} == $missing_value_value 
     1951                || $wind_speed[$i]{'height'} == $Fill_value_value) { 
     1952                print WIND_SQLFILE ','.'\'\''; 
     1953              } 
     1954              else { 
     1955                $this_val = sprintf("%.2f",$wind_speed[$i]{'height'}); 
     1956                print WIND_SQLFILE ','.$this_val; 
     1957              } 
     1958 
     1959              print WIND_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     1960              $this_val = sprintf("%.2f",$wind_speed[$i]{'data'}[$k]); 
    15611961              print WIND_SQLFILE ','.$this_val; 
    1562             } 
    1563             print WIND_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1564             $this_val = sprintf("%.2f",$wind_speed[$i]{'data'}[$k]); 
    1565             print WIND_SQLFILE ','.$this_val; 
    1566             if ($wind_gust[$j]{'data'}[$k] == $missing_value_value 
    1567               || $wind_gust[$j]{'data'}[$k] == $Fill_value_value 
    1568               || $wind_gust[$j]{'data'}[$k] == '') { 
    1569               print WIND_SQLFILE ',NULL'; 
    1570             } 
    1571             else { 
    1572               $this_val = sprintf("%.2f",$wind_gust[$j]{'data'}[$k]); 
     1962 
     1963 
     1964              if ($wind_gust[$j]{'data'}[$k] == $missing_value_value 
     1965                || $wind_gust[$j]{'data'}[$k] == $Fill_value_value 
     1966                || $wind_gust[$j]{'data'}[$k] == '') { 
     1967                print WIND_SQLFILE ',NULL'; 
     1968              } 
     1969              else { 
     1970                $this_val = sprintf("%.2f",$wind_gust[$j]{'data'}[$k]); 
     1971                print WIND_SQLFILE ','.$this_val; 
     1972              } 
     1973               
     1974              $this_val = sprintf("%.2f",$wind_from_direction[$j]{'data'}[$k]); 
     1975               
    15731976              print WIND_SQLFILE ','.$this_val; 
    1574             } 
    1575             $this_val = sprintf("%.2f",$wind_from_direction[$j]{'data'}[$k]); 
    1576             print WIND_SQLFILE ','.$this_val; 
    1577             print WIND_SQLFILE ','.'\''.$wind_speed[$i]{'can_be_normalized'}.'\''; 
    1578             print WIND_SQLFILE ','.'\''.$title_value.'\''; 
    1579             print WIND_SQLFILE ','.'\''.$institution_value.'\''; 
    1580             print WIND_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1581             print WIND_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1582             print WIND_SQLFILE ','.'\''.$source_value.'\''; 
    1583             print WIND_SQLFILE ','.'\''.$references_value.'\''; 
    1584             print WIND_SQLFILE ','.'\''.$contact_value.'\''; 
    1585             print WIND_SQLFILE ",GeometryFromText('POINT(";  
    1586             print WIND_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1587             print WIND_SQLFILE ")',-1));\n"; 
     1977              print WIND_SQLFILE ','.'\''.$wind_speed[$i]{'can_be_normalized'}.'\''; 
     1978              print WIND_SQLFILE ','.'\''.$title_value.'\''; 
     1979              print WIND_SQLFILE ','.'\''.$institution_value.'\''; 
     1980              print WIND_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1981              print WIND_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1982              print WIND_SQLFILE ','.'\''.$source_value.'\''; 
     1983              print WIND_SQLFILE ','.'\''.$references_value.'\''; 
     1984              print WIND_SQLFILE ','.'\''.$contact_value.'\''; 
     1985              print WIND_SQLFILE ",GeometryFromText('POINT(";  
     1986              print WIND_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     1987              print WIND_SQLFILE ")',-1));\n"; 
     1988                             
     1989            } 
    15881990          } 
    15891991        } 
    1590       } 
    1591       print WIND_SQLFILE "\n"; 
    1592     } 
    1593     close(WIND_SQLFILE); 
     1992        print WIND_SQLFILE "\n"; 
     1993      } 
     1994      close(WIND_SQLFILE); 
     1995    } 
     1996    #DWR 4/5/2008 
     1997    if( $bWriteobsKMLFile ) 
     1998    { 
     1999      for my $i (0..$#wind_speed)  
     2000      { 
     2001        my $j = 0; 
     2002        while ($j <= $#wind_from_direction 
     2003          && $wind_from_direction[$j]{'height'} != $wind_speed[$i]{'height'})  
     2004          { 
     2005          $j++; 
     2006        } 
     2007        if ($j > $#wind_from_direction)  
     2008        { 
     2009          die "ABORT!  Could not find matching wind_from_direction for $wind_speed[$i]  {'var_name'}.\n"; 
     2010        } 
     2011        else  
     2012        { 
     2013          for my $k (0..$#this_wind_speed_data)  
     2014          { 
     2015            $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2016            $this_time_stamp = $time_formatted_values[$k]; 
     2017            $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2018             
     2019            my $WindSpdDataVal = 'NULL'; 
     2020            my $WindDirDataVal = 'NULL'; 
     2021            my $WindGstDataVal = 'NULL'; 
     2022            my $Height = ''; 
     2023            if ($wind_speed[$i]{'data'}[$k] != $missing_value_value 
     2024              && $wind_speed[$i]{'data'}[$k] != $Fill_value_value 
     2025              && $wind_from_direction[$j]{'data'}[$k] != $missing_value_value 
     2026              && $wind_from_direction[$j]{'data'}[$k] != $Fill_value_value 
     2027              && $this_station_id_top_ts < $this_time_stamp_sec 
     2028              && $this_time_stamp_sec > $oldest_ok_timestamp)  
     2029              { 
     2030                if ($wind_speed[$i]{'height'} != $missing_value_value 
     2031                  && $wind_speed[$i]{'height'} != $Fill_value_value)  
     2032                { 
     2033                  $Height = sprintf("%.2f",$wind_speed[$i]{'height'}); 
     2034                } 
     2035 
     2036                $this_val = sprintf("%.2f",$wind_speed[$i]{'data'}[$k]); 
     2037                if( length( $this_val ) > 0 ) 
     2038                { 
     2039                  $WindSpdDataVal = $this_val; 
     2040                } 
     2041 
     2042                if ($wind_gust[$j]{'data'}[$k] != $missing_value_value 
     2043                    && $wind_gust[$j]{'data'}[$k] != $Fill_value_value 
     2044                    && $wind_gust[$j]{'data'}[$k] != '')  
     2045                { 
     2046                  $WindGstDataVal = sprintf("%.2f",$wind_gust[$j]{'data'}[$k]); 
     2047                } 
     2048                 
     2049                $this_val = sprintf("%.2f",$wind_from_direction[$j]{'data'}[$k]); 
     2050                #DWR 4/5/2008 
     2051                if( length( $this_val ) > 0 ) 
     2052                { 
     2053                  $WindDirDataVal = $this_val; 
     2054                }               
     2055              } 
     2056              my $strUnits; 
     2057              $strUnits = obsKMLSubRoutines::UnitsStringConversion( $wind_speed[$i]{'units'}, $XMLControlFile );         
     2058              if( length( $strUnits ) == 0 ) 
     2059              { 
     2060               $strUnits = $wind_speed[$i]{'units'}; 
     2061              } 
     2062               
     2063              obsKMLSubRoutines::KMLAddObsToHash( 'wind_speed',  
     2064                                                  $time_formatted_values[$j], 
     2065                                                  $WindSpdDataVal, 
     2066                                                  1, 
     2067                                                  $strPlatformID, 
     2068                                                  $Height, 
     2069                                                  $strUnits, 
     2070                                                  $rObsHash ); 
     2071              $strUnits = ''; 
     2072              $strUnits = obsKMLSubRoutines::UnitsStringConversion( $wind_from_direction[$i]{'units'}, $XMLControlFile );         
     2073              if( length( $strUnits ) == 0 ) 
     2074              { 
     2075               $strUnits = $wind_from_direction[$i]{'units'}; 
     2076              } 
     2077              obsKMLSubRoutines::KMLAddObsToHash( 'wind_from_direction',  
     2078                                                  $time_formatted_values[$j], 
     2079                                                  $WindDirDataVal, 
     2080                                                  1, 
     2081                                                  $strPlatformID, 
     2082                                                  $Height, 
     2083                                                  $strUnits, 
     2084                                                  $rObsHash ); 
     2085              $strUnits = ''; 
     2086              $strUnits = obsKMLSubRoutines::UnitsStringConversion( $wind_gust[$i]{'units'}, $XMLControlFile );         
     2087              if( length( $strUnits ) == 0 ) 
     2088              { 
     2089               $strUnits = $wind_gust[$i]{'units'}; 
     2090              } 
     2091              obsKMLSubRoutines::KMLAddObsToHash( 'wind_gust',  
     2092                                                  $time_formatted_values[$j], 
     2093                                                  $WindGstDataVal, 
     2094                                                  1, 
     2095                                                  $strPlatformID, 
     2096                                                  $Height, 
     2097                                                  $strUnits, 
     2098                                                  $rObsHash ); 
     2099          } 
     2100        } 
     2101      }                                             
     2102    }             
    15942103  } 
    15952104   
     
    15982107  # the metadata, use the station_id lookup, instead. 
    15992108  if ($#air_pressure > -1) { 
    1600     open(AIR_PRESSURE_SQLFILE,'>>../sql/air_pressure_prod_'.$institution_code_value.'_' 
    1601       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1602     print AIR_PRESSURE_SQLFILE "-- format_category      = $format_category_value\n"; 
    1603     print AIR_PRESSURE_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1604     print AIR_PRESSURE_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1605     print AIR_PRESSURE_SQLFILE "-- package_code         = $package_code_value\n"; 
    1606     print AIR_PRESSURE_SQLFILE "-- title                = $title_value\n"; 
    1607     print AIR_PRESSURE_SQLFILE "-- institution          = $institution_value\n"; 
    1608     print AIR_PRESSURE_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1609     print AIR_PRESSURE_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1610     print AIR_PRESSURE_SQLFILE "-- source               = $source_value\n"; 
    1611     print AIR_PRESSURE_SQLFILE "-- references           = $references_value\n"; 
    1612     print AIR_PRESSURE_SQLFILE "-- contact              = $contact_value\n"; 
    1613     print AIR_PRESSURE_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1614     print AIR_PRESSURE_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1615     for my $i (0..$#air_pressure) { 
    1616       for my $j (0..$#this_air_pressure_data) { 
    1617         $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1618         $this_time_stamp = $time_formatted_values[$j]; 
    1619         $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
    1620          
    1621         if ($air_pressure[$i]{'data'}[$j] != $missing_value_value 
    1622           && $air_pressure[$i]{'data'}[$j] != $Fill_value_value 
    1623           && $this_station_id_top_ts < $this_time_stamp_sec 
    1624           && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1625           print AIR_PRESSURE_SQLFILE "INSERT INTO air_pressure_prod (";  
    1626           print AIR_PRESSURE_SQLFILE "station_id,"; 
    1627           print AIR_PRESSURE_SQLFILE "time_stamp,";  
    1628           print AIR_PRESSURE_SQLFILE "z,"; 
    1629           print AIR_PRESSURE_SQLFILE "positive,"; 
    1630           print AIR_PRESSURE_SQLFILE "pressure,"; 
    1631           print AIR_PRESSURE_SQLFILE "the_geom"; 
    1632           print AIR_PRESSURE_SQLFILE ") ";  
    1633           print AIR_PRESSURE_SQLFILE "VALUES (";  
    1634           print AIR_PRESSURE_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1635           print AIR_PRESSURE_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1636           if ($air_pressure[$i]{'height'} == $missing_value_value 
    1637             || $air_pressure[$i]{'height'} == $Fill_value_value) { 
    1638             print AIR_PRESSURE_SQLFILE ',NULL'; 
     2109    if( $bWriteSQLFiles ) 
     2110    { 
     2111      open(AIR_PRESSURE_SQLFILE,'>>../sql/air_pressure_prod_'.$institution_code_value.'_' 
     2112        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     2113      print AIR_PRESSURE_SQLFILE "-- format_category      = $format_category_value\n"; 
     2114      print AIR_PRESSURE_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     2115      print AIR_PRESSURE_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     2116      print AIR_PRESSURE_SQLFILE "-- package_code         = $package_code_value\n"; 
     2117      print AIR_PRESSURE_SQLFILE "-- title                = $title_value\n"; 
     2118      print AIR_PRESSURE_SQLFILE "-- institution          = $institution_value\n"; 
     2119      print AIR_PRESSURE_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     2120      print AIR_PRESSURE_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     2121      print AIR_PRESSURE_SQLFILE "-- source               = $source_value\n"; 
     2122      print AIR_PRESSURE_SQLFILE "-- references           = $references_value\n"; 
     2123      print AIR_PRESSURE_SQLFILE "-- contact              = $contact_value\n"; 
     2124      print AIR_PRESSURE_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     2125      print AIR_PRESSURE_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     2126      for my $i (0..$#air_pressure) { 
     2127        for my $j (0..$#this_air_pressure_data) { 
     2128          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2129          $this_time_stamp = $time_formatted_values[$j]; 
     2130          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2131           
     2132          if ($air_pressure[$i]{'data'}[$j] != $missing_value_value 
     2133            && $air_pressure[$i]{'data'}[$j] != $Fill_value_value 
     2134            && $this_station_id_top_ts < $this_time_stamp_sec 
     2135            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     2136            print AIR_PRESSURE_SQLFILE "INSERT INTO air_pressure_prod (";  
     2137            print AIR_PRESSURE_SQLFILE "station_id,"; 
     2138            print AIR_PRESSURE_SQLFILE "time_stamp,";  
     2139            print AIR_PRESSURE_SQLFILE "z,"; 
     2140            print AIR_PRESSURE_SQLFILE "positive,"; 
     2141            print AIR_PRESSURE_SQLFILE "pressure,"; 
     2142            print AIR_PRESSURE_SQLFILE "the_geom"; 
     2143            print AIR_PRESSURE_SQLFILE ") ";  
     2144            print AIR_PRESSURE_SQLFILE "VALUES (";  
     2145            print AIR_PRESSURE_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     2146            print AIR_PRESSURE_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     2147            if ($air_pressure[$i]{'height'} == $missing_value_value 
     2148              || $air_pressure[$i]{'height'} == $Fill_value_value) { 
     2149              print AIR_PRESSURE_SQLFILE ',NULL'; 
     2150            } 
     2151            else { 
     2152              $this_val = sprintf("%.2f",$air_pressure[$i]{'height'}); 
     2153              print AIR_PRESSURE_SQLFILE ','.$this_val; 
     2154            } 
     2155             
     2156            print AIR_PRESSURE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     2157            $this_val = sprintf("%.2f",$air_pressure[$i]{'data'}[$j]); 
     2158             
     2159            print AIR_PRESSURE_SQLFILE ','.$this_val; 
     2160            print AIR_PRESSURE_SQLFILE ",GeometryFromText('POINT(";  
     2161            print AIR_PRESSURE_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     2162            print AIR_PRESSURE_SQLFILE ")',-1));\n"; 
    16392163          } 
    1640           else { 
    1641             $this_val = sprintf("%.2f",$air_pressure[$i]{'height'}); 
    1642             print AIR_PRESSURE_SQLFILE ','.$this_val; 
     2164           
     2165        } 
     2166        print AIR_PRESSURE_SQLFILE "\n"; 
     2167      } 
     2168      close(AIR_PRESSURE_SQLFILE); 
     2169    } 
     2170    #DWR 4/5/2008 
     2171    if( $bWriteobsKMLFile ) 
     2172    { 
     2173      my $DataVal = 'NULL'; 
     2174      my $Height = ''; 
     2175      for my $i (0..$#air_pressure)  
     2176      { 
     2177        for my $j (0..$#this_air_pressure_data)  
     2178        { 
     2179          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2180          $this_time_stamp = $time_formatted_values[$j]; 
     2181          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2182           
     2183          my $DataVal = 'NULL'; 
     2184          my $Height = ''; 
     2185          if ($air_pressure[$i]{'data'}[$j] != $missing_value_value 
     2186            && $air_pressure[$i]{'data'}[$j] != $Fill_value_value 
     2187            && $this_station_id_top_ts < $this_time_stamp_sec 
     2188            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     2189            if ($air_pressure[$i]{'height'} != $missing_value_value 
     2190              && $air_pressure[$i]{'height'} != $Fill_value_value)  
     2191            { 
     2192              $Height = sprintf("%.2f",$air_pressure[$i]{'height'}); 
     2193            } 
     2194            $DataVal = sprintf("%.2f",$air_pressure[$i]{'data'}[$j]); 
     2195          }            
     2196          #DWR 4/3/2008 
     2197          my $strUnits; 
     2198          $strUnits = obsKMLSubRoutines::UnitsStringConversion( $air_pressure[$i]{'units'}, $XMLControlFile );  
     2199          if( length( $strUnits ) == 0 ) 
     2200          { 
     2201           $strUnits =  $air_pressure[$i]{'units'}; 
    16432202          } 
    1644           print AIR_PRESSURE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1645           $this_val = sprintf("%.2f",$air_pressure[$i]{'data'}[$j]); 
    1646           print AIR_PRESSURE_SQLFILE ','.$this_val; 
    1647           print AIR_PRESSURE_SQLFILE ",GeometryFromText('POINT(";  
    1648           print AIR_PRESSURE_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1649           print AIR_PRESSURE_SQLFILE ")',-1));\n"; 
    1650         } 
    1651       } 
    1652       print AIR_PRESSURE_SQLFILE "\n"; 
    1653     } 
    1654     close(AIR_PRESSURE_SQLFILE); 
     2203                  
     2204          obsKMLSubRoutines::KMLAddObsToHash( 'air_pressure',  
     2205                                              $time_formatted_values[$j], 
     2206                                              $DataVal, 
     2207                                              1, 
     2208                                              $strPlatformID, 
     2209                                              $Height, 
     2210                                              $strUnits, 
     2211                                              $rObsHash ); 
     2212        } 
     2213      }                                             
     2214    }         
    16552215  } 
    16562216   
     
    16592219  # the metadata, use the station_id lookup, instead. 
    16602220  if ($#salinity > -1) { 
    1661     open(SALINITY_SQLFILE,'>>../sql/salinity_prod_'.$institution_code_value.'_' 
    1662       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1663     print SALINITY_SQLFILE "-- format_category      = $format_category_value\n"; 
    1664     print SALINITY_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1665     print SALINITY_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1666     print SALINITY_SQLFILE "-- package_code         = $package_code_value\n"; 
    1667     print SALINITY_SQLFILE "-- title                = $title_value\n"; 
    1668     print SALINITY_SQLFILE "-- institution          = $institution_value\n"; 
    1669     print SALINITY_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1670     print SALINITY_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1671     print SALINITY_SQLFILE "-- source               = $source_value\n"; 
    1672     print SALINITY_SQLFILE "-- references           = $references_value\n"; 
    1673     print SALINITY_SQLFILE "-- contact              = $contact_value\n"; 
    1674     print SALINITY_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1675     print SALINITY_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1676     for my $i (0..$#salinity) { 
    1677       for my $j (0..$#this_salinity_data) { 
    1678         $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1679         $this_time_stamp = $time_formatted_values[$j]; 
    1680         $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
    1681          
    1682             print "debug: $salinity[$i]{'data'}[$k]\n";             
    1683         if ($salinity[$i]{'data'}[$j] != $missing_value_value 
    1684           && $salinity[$i]{'data'}[$j] != $Fill_value_value 
    1685           && $this_station_id_top_ts < $this_time_stamp_sec 
    1686           && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1687           print SALINITY_SQLFILE "INSERT INTO salinity_prod (";  
    1688           print SALINITY_SQLFILE "station_id,"; 
    1689           print SALINITY_SQLFILE "time_stamp,";  
    1690           print SALINITY_SQLFILE "z,"; 
    1691           print SALINITY_SQLFILE "positive,"; 
    1692           print SALINITY_SQLFILE "salinity,"; 
    1693           print SALINITY_SQLFILE "the_geom"; 
    1694           print SALINITY_SQLFILE ") ";  
    1695           print SALINITY_SQLFILE "VALUES (";  
    1696           print SALINITY_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1697           print SALINITY_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1698           if ($salinity[$i]{'height'} == $missing_value_value 
    1699             || $salinity[$i]{'height'} == $Fill_value_value) { 
    1700             print SALINITY_SQLFILE ',NULL'; 
     2221    if( $bWriteSQLFiles ) 
     2222    { 
     2223      open(SALINITY_SQLFILE,'>>../sql/salinity_prod_'.$institution_code_value.'_' 
     2224        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     2225      print SALINITY_SQLFILE "-- format_category      = $format_category_value\n"; 
     2226      print SALINITY_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     2227      print SALINITY_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     2228      print SALINITY_SQLFILE "-- package_code         = $package_code_value\n"; 
     2229      print SALINITY_SQLFILE "-- title                = $title_value\n"; 
     2230      print SALINITY_SQLFILE "-- institution          = $institution_value\n"; 
     2231      print SALINITY_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     2232      print SALINITY_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     2233      print SALINITY_SQLFILE "-- source               = $source_value\n"; 
     2234      print SALINITY_SQLFILE "-- references           = $references_value\n"; 
     2235      print SALINITY_SQLFILE "-- contact              = $contact_value\n"; 
     2236      print SALINITY_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     2237      print SALINITY_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     2238      for my $i (0..$#salinity) { 
     2239        for my $j (0..$#this_salinity_data) { 
     2240          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2241          $this_time_stamp = $time_formatted_values[$j]; 
     2242          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2243           
     2244          #print "debug: $salinity[$i]{'data'}[$k]\n";             
     2245          if ($salinity[$i]{'data'}[$j] != $missing_value_value 
     2246            && $salinity[$i]{'data'}[$j] != $Fill_value_value 
     2247            && $this_station_id_top_ts < $this_time_stamp_sec 
     2248            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     2249            print SALINITY_SQLFILE "INSERT INTO salinity_prod (";  
     2250            print SALINITY_SQLFILE "station_id,"; 
     2251            print SALINITY_SQLFILE "time_stamp,";  
     2252            print SALINITY_SQLFILE "z,"; 
     2253            print SALINITY_SQLFILE "positive,"; 
     2254            print SALINITY_SQLFILE "salinity,"; 
     2255            print SALINITY_SQLFILE "the_geom"; 
     2256            print SALINITY_SQLFILE ") ";  
     2257            print SALINITY_SQLFILE "VALUES (";  
     2258            print SALINITY_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     2259            print SALINITY_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     2260            if ($salinity[$i]{'height'} == $missing_value_value 
     2261              || $salinity[$i]{'height'} == $Fill_value_value) { 
     2262              print SALINITY_SQLFILE ',NULL'; 
     2263            } 
     2264            else { 
     2265              $this_val = sprintf("%.2f",$salinity[$i]{'height'}); 
     2266              print SALINITY_SQLFILE ','.$this_val; 
     2267            } 
     2268             
     2269            print SALINITY_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     2270            $this_val = sprintf("%.2f",$salinity[$i]{'data'}[$j]); 
     2271             
     2272            print SALINITY_SQLFILE ','.$this_val; 
     2273            print SALINITY_SQLFILE ",GeometryFromText('POINT(";  
     2274            print SALINITY_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     2275            print SALINITY_SQLFILE ")',-1));\n"; 
     2276          }           
     2277        } 
     2278        print SALINITY_SQLFILE "\n"; 
     2279      } 
     2280      close(SALINITY_SQLFILE); 
     2281    } 
     2282    #DWR 4/5/2008 
     2283    if( $bWriteobsKMLFile ) 
     2284    { 
     2285      my $DataVal = 'NULL'; 
     2286      my $Height = ''; 
     2287      for my $i (0..$#salinity)  
     2288      { 
     2289        for my $j (0..$#this_salinity_data)  
     2290        { 
     2291          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2292          $this_time_stamp = $time_formatted_values[$j]; 
     2293          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2294           
     2295          my $DataVal = 'NULL'; 
     2296          my $Height = ''; 
     2297          if ($salinity[$i]{'data'}[$j] != $missing_value_value 
     2298              && $salinity[$i]{'data'}[$j] != $Fill_value_value 
     2299              && $this_station_id_top_ts < $this_time_stamp_sec 
     2300              && $this_time_stamp_sec > $oldest_ok_timestamp)  
     2301          { 
     2302            if ($salinity[$i]{'height'} != $missing_value_value 
     2303              && $salinity[$i]{'height'} != $Fill_value_value)  
     2304            { 
     2305              $Height = sprintf("%.2f",$salinity[$i]{'height'}); 
     2306            } 
     2307            $DataVal = sprintf("%.2f",$salinity[$i]{'data'}[$j]); 
     2308          }            
     2309          my $strUnits; 
     2310          if( lc( $salinity[$i]{'units'} ) eq 'ppt' ) 
     2311          { 
     2312            $strUnits =  'psu';         
    17012313          } 
    1702           else { 
    1703             $this_val = sprintf("%.2f",$salinity[$i]{'height'}); 
    1704             print SALINITY_SQLFILE ','.$this_val
     2314          if( length( $strUnits ) == 0 ) 
     2315          { 
     2316           $strUnits = $salinity[$i]{'units'}
    17052317          } 
    1706           print SALINITY_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1707           $this_val = sprintf("%.2f",$salinity[$i]{'data'}[$j]); 
    1708           print SALINITY_SQLFILE ','.$this_val; 
    1709           print SALINITY_SQLFILE ",GeometryFromText('POINT(";  
    1710           print SALINITY_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1711           print SALINITY_SQLFILE ")',-1));\n"; 
    1712         } 
    1713       } 
    1714       print SALINITY_SQLFILE "\n"; 
    1715     } 
    1716     close(SALINITY_SQLFILE); 
     2318           
     2319          obsKMLSubRoutines::KMLAddObsToHash( 'salinity',  
     2320                                              $time_formatted_values[$j], 
     2321                                              $DataVal, 
     2322                                              1, 
     2323                                              $strPlatformID, 
     2324                                              $Height, 
     2325                                              $strUnits, 
     2326                                              $rObsHash ); 
     2327        } 
     2328      }                                             
     2329    }             
    17172330  } 
    17182331   
     
    17212334    # to find its pair by looking at the heights.  (This is overkill for sea_surface_currents. 
    17222335    # Assume that sea_surface_eastward_current controls everything index-wise. 
    1723     if ($#sea_surface_eastward_current > -1) { 
    1724       open(CURRENT_IN_SITU_SQLFILE,'>>../sql/current_in_situ_prod_'.$institution_code_value.'_' 
    1725         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1726       print CURRENT_IN_SITU_SQLFILE "-- format_category      = $format_category_value\n"; 
    1727       print CURRENT_IN_SITU_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1728       print CURRENT_IN_SITU_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1729       print CURRENT_IN_SITU_SQLFILE "-- package_code         = $package_code_value\n"; 
    1730       print CURRENT_IN_SITU_SQLFILE "-- title                = $title_value\n"; 
    1731       print CURRENT_IN_SITU_SQLFILE "-- institution          = $institution_value\n"; 
    1732       print CURRENT_IN_SITU_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1733       print CURRENT_IN_SITU_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1734       print CURRENT_IN_SITU_SQLFILE "-- source               = $source_value\n"; 
    1735       print CURRENT_IN_SITU_SQLFILE "-- references           = $references_value\n"; 
    1736       print CURRENT_IN_SITU_SQLFILE "-- contact              = $contact_value\n"; 
    1737       print CURRENT_IN_SITU_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1738       print CURRENT_IN_SITU_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1739       for my $i (0..$#sea_surface_eastward_current) { 
    1740         my $j = 0; 
    1741         while ($j <= $#sea_surface_northward_current 
    1742           && $sea_surface_northward_current[$j]{'height'} != $sea_surface_eastward_current[$i]{'height'}) { 
    1743           $j++; 
    1744         } 
    1745         if ($j > $#sea_surface_northward_current) { 
    1746           die "ABORT!  Could not find matching sea_surface_northward_current for $sea_surface_eastward_current[$i]  {'var_name'}.\n"; 
    1747         } 
    1748         else { 
    1749           for my $k (0..$#this_sea_surface_eastward_current_data) { 
    1750             $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1751             $this_time_stamp = $time_formatted_values[$k]; 
    1752             $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
    1753  
    1754             #print "debug: $sea_surface_eastward_current[$i]{'data'}[$k]\n";             
    1755             if ($sea_surface_eastward_current[$i]{'data'}[$k] != $missing_value_value 
    1756               && $sea_surface_eastward_current[$i]{'data'}[$k] != $Fill_value_value 
    1757               && $sea_surface_northward_current[$j]{'data'}[$k] != $missing_value_value 
    1758               && $sea_surface_northward_current[$j]{'data'}[$k] != $Fill_value_value 
    1759               && $this_station_id_top_ts < $this_time_stamp_sec 
    1760               && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1761               print CURRENT_IN_SITU_SQLFILE "INSERT INTO current_in_situ_prod (";  
    1762               print CURRENT_IN_SITU_SQLFILE "station_id,"; 
    1763               print CURRENT_IN_SITU_SQLFILE "time_stamp,";  
    1764               print CURRENT_IN_SITU_SQLFILE "z,"; 
    1765               print CURRENT_IN_SITU_SQLFILE "positive,"; 
    1766               print CURRENT_IN_SITU_SQLFILE "eastward_current,"; 
    1767               print CURRENT_IN_SITU_SQLFILE "northward_current,"; 
    1768               print CURRENT_IN_SITU_SQLFILE "surface_or_bottom,"; 
    1769               print CURRENT_IN_SITU_SQLFILE "the_geom"; 
    1770               print CURRENT_IN_SITU_SQLFILE ") ";  
    1771               print CURRENT_IN_SITU_SQLFILE "VALUES (";  
    1772               print CURRENT_IN_SITU_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1773               print CURRENT_IN_SITU_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$k].'\''; 
    1774               if ($sea_surface_eastward_current[$i]{'height'} == $missing_value_value 
    1775                 || $sea_surface_eastward_current[$i]{'height'} == $Fill_value_value) { 
    1776                 print CURRENT_IN_SITU_SQLFILE ','.'\'\''; 
     2336    if ($#sea_surface_eastward_current > -1)  
     2337    { 
     2338      if( $bWriteSQLFiles ) 
     2339      { 
     2340        open(CURRENT_IN_SITU_SQLFILE,'>>../sql/current_in_situ_prod_'.$institution_code_value.'_' 
     2341          .$platform_code_value.'_'.$package_code_value.'.sql'); 
     2342        print CURRENT_IN_SITU_SQLFILE "-- format_category      = $format_category_value\n"; 
     2343        print CURRENT_IN_SITU_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     2344        print CURRENT_IN_SITU_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     2345        print CURRENT_IN_SITU_SQLFILE "-- package_code         = $package_code_value\n"; 
     2346        print CURRENT_IN_SITU_SQLFILE "-- title                = $title_value\n"; 
     2347        print CURRENT_IN_SITU_SQLFILE "-- institution          = $institution_value\n"; 
     2348        print CURRENT_IN_SITU_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     2349        print CURRENT_IN_SITU_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     2350        print CURRENT_IN_SITU_SQLFILE "-- source               = $source_value\n"; 
     2351        print CURRENT_IN_SITU_SQLFILE "-- references           = $references_value\n"; 
     2352        print CURRENT_IN_SITU_SQLFILE "-- contact              = $contact_value\n"; 
     2353        print CURRENT_IN_SITU_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     2354        print CURRENT_IN_SITU_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     2355        for my $i (0..$#sea_surface_eastward_current) { 
     2356          my $j = 0; 
     2357          while ($j <= $#sea_surface_northward_current 
     2358            && $sea_surface_northward_current[$j]{'height'} != $sea_surface_eastward_current[$i]{'height'}) { 
     2359            $j++; 
     2360          } 
     2361          if ($j > $#sea_surface_northward_current) { 
     2362            die "ABORT!  Could not find matching sea_surface_northward_current for $sea_surface_eastward_current[$i]  {'var_name'}.\n"; 
     2363          } 
     2364          else { 
     2365            for my $k (0..$#this_sea_surface_eastward_current_data) { 
     2366              $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2367              $this_time_stamp = $time_formatted_values[$k]; 
     2368              $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2369                           
     2370        #print "debug: $sea_surface_eastward_current[$i]{'data'}[$k]\n";             
     2371              if ($sea_surface_eastward_current[$i]{'data'}[$k] != $missing_value_value 
     2372                && $sea_surface_eastward_current[$i]{'data'}[$k] != $Fill_value_value 
     2373                && $sea_surface_northward_current[$j]{'data'}[$k] != $missing_value_value 
     2374                && $sea_surface_northward_current[$j]{'data'}[$k] != $Fill_value_value 
     2375                && $this_station_id_top_ts < $this_time_stamp_sec 
     2376                && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     2377                print CURRENT_IN_SITU_SQLFILE "INSERT INTO current_in_situ_prod (";  
     2378                print CURRENT_IN_SITU_SQLFILE "station_id,"; 
     2379                print CURRENT_IN_SITU_SQLFILE "time_stamp,";  
     2380                print CURRENT_IN_SITU_SQLFILE "z,"; 
     2381                print CURRENT_IN_SITU_SQLFILE "positive,"; 
     2382                print CURRENT_IN_SITU_SQLFILE "eastward_current,"; 
     2383                print CURRENT_IN_SITU_SQLFILE "northward_current,"; 
     2384                print CURRENT_IN_SITU_SQLFILE "surface_or_bottom,"; 
     2385                print CURRENT_IN_SITU_SQLFILE "the_geom"; 
     2386                print CURRENT_IN_SITU_SQLFILE ") ";  
     2387                print CURRENT_IN_SITU_SQLFILE "VALUES (";  
     2388                print CURRENT_IN_SITU_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     2389                print CURRENT_IN_SITU_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$k].'\''; 
     2390                if ($sea_surface_eastward_current[$i]{'height'} == $missing_value_value 
     2391                  || $sea_surface_eastward_current[$i]{'height'} == $Fill_value_value) { 
     2392                  print CURRENT_IN_SITU_SQLFILE ','.'\'\''; 
     2393                } 
     2394                else { 
     2395                  $this_val = sprintf("%.2f",$sea_surface_eastward_current[$i]{'height'}); 
     2396                  print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
     2397                   
     2398                } 
     2399                print CURRENT_IN_SITU_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     2400                $this_val = sprintf("%.2f",$sea_surface_eastward_current[$i]{'data'}[$k]); 
     2401                 
     2402                print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
     2403                $this_val = sprintf("%.2f",$sea_surface_northward_current[$j]{'data'}[$k]); 
     2404 
     2405                print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
     2406                print CURRENT_IN_SITU_SQLFILE ','.'\'surface\''; 
     2407                print CURRENT_IN_SITU_SQLFILE ",GeometryFromText('POINT(";  
     2408                print CURRENT_IN_SITU_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     2409                print CURRENT_IN_SITU_SQLFILE ")',-1));\n"; 
    17772410              } 
    1778               else { 
    1779                 $this_val = sprintf("%.2f",$sea_surface_eastward_current[$i]{'height'}); 
    1780                 print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
     2411               
     2412            } 
     2413          } 
     2414          print CURRENT_IN_SITU_SQLFILE "\n"; 
     2415        } 
     2416        close(CURRENT_IN_SITU_SQLFILE); 
     2417      } 
     2418      #DWR 4/5/2008 
     2419      if( $bWriteobsKMLFile ) 
     2420      { 
     2421        for my $i (0..$#sea_surface_eastward_current)  
     2422        { 
     2423          my $j = 0; 
     2424          while ($j <= $#sea_surface_northward_current 
     2425                && $sea_surface_northward_current[$j]{'height'} != $sea_surface_eastward_current[$i]{'height'})  
     2426          { 
     2427            $j++; 
     2428          } 
     2429          if ($j > $#sea_surface_northward_current)  
     2430          { 
     2431            die "ABORT!  Could not find matching sea_surface_northward_current for $sea_surface_eastward_current[$i]  {'var_name'}.\n"; 
     2432          } 
     2433          else  
     2434          { 
     2435            for my $k (0..$#this_sea_surface_eastward_current_data)  
     2436            { 
     2437              $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2438              $this_time_stamp = $time_formatted_values[$k]; 
     2439              $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2440               
     2441              my $ECurrentDataVal = 'NULL'; 
     2442              my $NCurrentDataVal = 'NULL'; 
     2443              my $Height = '';             
     2444               
     2445              if ($sea_surface_eastward_current[$i]{'data'}[$k] != $missing_value_value 
     2446                  && $sea_surface_eastward_current[$i]{'data'}[$k] != $Fill_value_value 
     2447                  && $sea_surface_northward_current[$j]{'data'}[$k] != $missing_value_value 
     2448                  && $sea_surface_northward_current[$j]{'data'}[$k] != $Fill_value_value 
     2449                  && $this_station_id_top_ts < $this_time_stamp_sec 
     2450                  && $this_time_stamp_sec > $oldest_ok_timestamp)  
     2451              { 
     2452                if ($sea_surface_eastward_current[$i]{'height'} != $missing_value_value 
     2453                    && $sea_surface_eastward_current[$i]{'height'} != $Fill_value_value)  
     2454                { 
     2455                  $Height = sprintf("%.2f",$sea_surface_eastward_current[$i]{'height'});                 
     2456                } 
     2457                $ECurrentDataVal = sprintf("%.2f",$sea_surface_eastward_current[$i]{'data'}[$k]); 
     2458                 
     2459                $NCurrentDataVal = sprintf("%.2f",$sea_surface_northward_current[$j]{'data'}[$k]); 
     2460 
    17812461              } 
    1782               print CURRENT_IN_SITU_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1783               $this_val = sprintf("%.2f",$sea_surface_eastward_current[$i]{'data'}[$k]); 
    1784               print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
    1785               $this_val = sprintf("%.2f",$sea_surface_northward_current[$j]{'data'}[$k]); 
    1786               print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
    1787               print CURRENT_IN_SITU_SQLFILE ','.'\'surface\''; 
    1788               print CURRENT_IN_SITU_SQLFILE ",GeometryFromText('POINT(";  
    1789               print CURRENT_IN_SITU_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1790               print CURRENT_IN_SITU_SQLFILE ")',-1));\n"; 
    1791             } 
     2462              my $strUnits; 
     2463              $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_surface_eastward_current[$i]{'units'}, $XMLControlFile );         
     2464              if( length( $strUnits ) == 0 ) 
     2465              { 
     2466               $strUnits = $sea_surface_eastward_current[$i]{'units'}; 
     2467              } 
     2468               
     2469              obsKMLSubRoutines::KMLAddObsToHash( 'eastward_current',  
     2470                                                  $time_formatted_values[$j], 
     2471                                                  $ECurrentDataVal, 
     2472                                                  1, 
     2473                                                  $strPlatformID, 
     2474                                                  $Height, 
     2475                                                  $strUnits, 
     2476                                                  $rObsHash ); 
     2477              $strUnits = ''; 
     2478              $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_surface_northward_current[$i]{'units'}, $XMLControlFile );         
     2479              if( length( $strUnits ) == 0 ) 
     2480              { 
     2481               $strUnits = $sea_surface_northward_current[$i]{'units'}; 
     2482              } 
     2483              obsKMLSubRoutines::KMLAddObsToHash( 'northward_current',  
     2484                                                  $time_formatted_values[$j], 
     2485                                                  $NCurrentDataVal, 
     2486                                                  1, 
     2487                                                  $strPlatformID, 
     2488                                                  $Height, 
     2489                                                  $strUnits, 
     2490                                                  $rObsHash ); 
    17922491          } 
    1793         } 
    1794         print CURRENT_IN_SITU_SQLFILE "\n"; 
    1795       } 
    1796       close(CURRENT_IN_SITU_SQLFILE); 
     2492        }                                             
     2493      }                   
     2494    } 
    17972495  } 
    17982496   
     
    18022500  # the metadata, use the station_id lookup, instead. 
    18032501  if ($#significant_wave_height > -1) { 
    1804     open(WAVE_SQLFILE,'>>../sql/wave_in_situ_prod_'.$institution_code_value.'_' 
    1805       .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1806     print WAVE_SQLFILE "-- format_category      = $format_category_value\n"; 
    1807     print WAVE_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1808     print WAVE_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1809     print WAVE_SQLFILE "-- package_code         = $package_code_value\n"; 
    1810     print WAVE_SQLFILE "-- title                = $title_value\n"; 
    1811     print WAVE_SQLFILE "-- institution          = $institution_value\n"; 
    1812     print WAVE_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1813     print WAVE_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1814     print WAVE_SQLFILE "-- source               = $source_value\n"; 
    1815     print WAVE_SQLFILE "-- references           = $references_value\n"; 
    1816     print WAVE_SQLFILE "-- contact              = $contact_value\n"; 
    1817     print WAVE_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1818     print WAVE_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1819     for my $i (0..$#significant_wave_height) { 
    1820       for my $j (0..$#this_significant_wave_height_data) { 
    1821         $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1822         $this_time_stamp = $time_formatted_values[$j]; 
    1823         $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2502    if( $bWriteSQLFiles ) 
     2503    { 
     2504      open(WAVE_SQLFILE,'>>../sql/wave_in_situ_prod_'.$institution_code_value.'_' 
     2505        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     2506      print WAVE_SQLFILE "-- format_category      = $format_category_value\n"; 
     2507      print WAVE_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     2508      print WAVE_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     2509      print WAVE_SQLFILE "-- package_code         = $package_code_value\n"; 
     2510      print WAVE_SQLFILE "-- title                = $title_value\n"; 
     2511      print WAVE_SQLFILE "-- institution          = $institution_value\n"; 
     2512      print WAVE_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     2513      print WAVE_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     2514      print WAVE_SQLFILE "-- source               = $source_value\n"; 
     2515      print WAVE_SQLFILE "-- references           = $references_value\n"; 
     2516      print WAVE_SQLFILE "-- contact              = $contact_value\n"; 
     2517      print WAVE_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     2518      print WAVE_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     2519      for my $i (0..$#significant_wave_height) { 
     2520        for my $j (0..$#this_significant_wave_height_data) { 
     2521          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2522          $this_time_stamp = $time_formatted_values[$j]; 
     2523          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2524           
     2525          my $DomWaveDataVal = 'NULL'; 
     2526          my $SigWaveHgtDataVal = 'NULL'; 
     2527          my $Height = ''; 
     2528           
     2529          if ($significant_wave_height[$i]{'data'}[$j] != $missing_value_value 
     2530            && $significant_wave_height[$i]{'data'}[$j] != $Fill_value_value 
     2531            && $this_station_id_top_ts < $this_time_stamp_sec 
     2532            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     2533            print WAVE_SQLFILE "INSERT INTO wave_in_situ_prod (";  
     2534            print WAVE_SQLFILE "station_id,"; 
     2535            print WAVE_SQLFILE "time_stamp,";  
     2536            print WAVE_SQLFILE "z,"; 
     2537            print WAVE_SQLFILE "positive,"; 
     2538            print WAVE_SQLFILE "significant_wave_height,"; 
     2539            print WAVE_SQLFILE "dominant_wave_period,"; 
     2540            print WAVE_SQLFILE "the_geom"; 
     2541            print WAVE_SQLFILE ") ";  
     2542            print WAVE_SQLFILE "VALUES (";  
     2543            print WAVE_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     2544            print WAVE_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     2545            if ($significant_wave_height[$i]{'height'} == $missing_value_value 
     2546              || $significant_wave_height[$i]{'height'} == $Fill_value_value) { 
     2547              print WAVE_SQLFILE ',NULL'; 
     2548            } 
     2549            else { 
     2550              $this_val = sprintf("%.2f",$significant_wave_height[$i]{'height'}); 
     2551              print WAVE_SQLFILE ','.$this_val; 
     2552 
     2553              #DWR 4/5/2008 
     2554              $Height = $this_val; 
     2555            } 
     2556            print WAVE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     2557            if ($significant_wave_height[$i]{'data'}[$j] == $missing_value_value 
     2558              || $significant_wave_height[$i]{'data'}[$j] == $Fill_value_value 
     2559              || $significant_wave_height[$i]{'data'}[$j] == '') { 
     2560              print WAVE_SQLFILE ',NULL'; 
     2561            } 
     2562            else { 
     2563              $this_val = sprintf("%.2f",$significant_wave_height[$i]{'data'}[$j]); 
     2564              print WAVE_SQLFILE ','.$this_val; 
     2565              #DWR 4/5/2008 
     2566              $SigWaveHgtDataVal = $this_val; 
     2567            } 
     2568            if ($dominant_wave_period[$i]{'data'}[$j] == $missing_value_value 
     2569              || $dominant_wave_period[$i]{'data'}[$j] == $Fill_value_value 
     2570              || $dominant_wave_period[$i]{'data'}[$j] == '') { 
     2571              print WAVE_SQLFILE ',NULL'; 
     2572            } 
     2573            else { 
     2574              $this_val = sprintf("%.2f",$dominant_wave_period[$i]{'data'}[$j]); 
     2575              print WAVE_SQLFILE ','.$this_val; 
     2576              #DWR 4/5/2008 
     2577              $DomWaveDataVal = $this_val; 
     2578            } 
     2579            print WAVE_SQLFILE ",GeometryFromText('POINT(";  
     2580            print WAVE_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     2581            print WAVE_SQLFILE ")',-1));\n"; 
     2582          } 
     2583        } 
     2584        print WAVE_SQLFILE "\n"; 
     2585        #DWR 4/3/2008 
     2586        my $strUnits; 
     2587        $strUnits = obsKMLSubRoutines::UnitsStringConversion( $dominant_wave_period[$i]{'units'}, $XMLControlFile );       
     2588        if( length( $strUnits ) == 0 ) 
     2589        { 
     2590           $strUnits = $dominant_wave_period[$i]{'units'}; 
     2591        } 
     2592           
     2593        obsKMLSubRoutines::KMLAddObsToHash( 'dominant_wave_period',  
     2594                                            $time_formatted_values[$j], 
     2595                                            $DomWaveDataVal, 
     2596                                            1, 
     2597                                            $strPlatformID, 
     2598                                            $Height, 
     2599                                            $strUnits, 
     2600                                            $rObsHash ); 
     2601        $strUnits = ''; 
     2602        $strUnits = obsKMLSubRoutines::UnitsStringConversion( $significant_wave_height[$i]{'units'}, $XMLControlFile );         
     2603        if( length( $strUnits ) == 0 ) 
     2604        { 
     2605           $strUnits = $significant_wave_height[$i]{'units'}; 
     2606        } 
     2607        obsKMLSubRoutines::KMLAddObsToHash( 'significant_wave_height',  
     2608                                            $time_formatted_values[$j], 
     2609                                            $SigWaveHgtDataVal, 
     2610                                            1, 
     2611                                            $strPlatformID, 
     2612                                            $Height, 
     2613                                            $strUnits, 
     2614                                            $rObsHash ); 
    18242615         
    1825         if ($significant_wave_height[$i]{'data'}[$j] != $missing_value_value 
    1826           && $significant_wave_height[$i]{'data'}[$j] != $Fill_value_value 
    1827           && $this_station_id_top_ts < $this_time_stamp_sec 
    1828           && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1829           print WAVE_SQLFILE "INSERT INTO wave_in_situ_prod (";  
    1830           print WAVE_SQLFILE "station_id,"; 
    1831           print WAVE_SQLFILE "time_stamp,";  
    1832           print WAVE_SQLFILE "z,"; 
    1833           print WAVE_SQLFILE "positive,"; 
    1834           print WAVE_SQLFILE "significant_wave_height,"; 
    1835           print WAVE_SQLFILE "dominant_wave_period,"; 
    1836           print WAVE_SQLFILE "the_geom"; 
    1837           print WAVE_SQLFILE ") ";  
    1838           print WAVE_SQLFILE "VALUES (";  
    1839           print WAVE_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1840           print WAVE_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1841           if ($significant_wave_height[$i]{'height'} == $missing_value_value 
    1842             || $significant_wave_height[$i]{'height'} == $Fill_value_value) { 
    1843             print WAVE_SQLFILE ',NULL'; 
     2616      } 
     2617      close(WAVE_SQLFILE); 
     2618    } 
     2619    #DWR 4/5/2008 
     2620    if( $bWriteobsKMLFile ) 
     2621    { 
     2622      for my $i (0..$#significant_wave_height)  
     2623      { 
     2624        for my $j (0..$#this_significant_wave_height_data)  
     2625        { 
     2626          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     2627          $this_time_stamp = $time_formatted_values[$j]; 
     2628          $this_time_stamp_sec = timelocal(substr($this_time_stamp,17,2),substr($this_time_stamp,14,2),substr($this_time_stamp,11,2),substr($this_time_stamp,8,2),(substr($this_time_stamp,5,2)-1),substr($this_time_stamp,0,4)); 
     2629           
     2630          my $DomWaveDataVal = 'NULL'; 
     2631          my $SigWaveHgtDataVal = 'NULL'; 
     2632          my $Height = ''; 
     2633           
     2634          if ($significant_wave_height[$i]{'data'}[$j] != $missing_value_value 
     2635              && $significant_wave_height[$i]{'data'}[$j] != $Fill_value_value 
     2636              && $this_station_id_top_ts < $this_time_stamp_sec 
     2637              && $this_time_stamp_sec > $oldest_ok_timestamp)  
     2638          { 
     2639            if ($significant_wave_height[$i]{'height'} != $missing_value_value 
     2640                && $significant_wave_height[$i]{'height'} != $Fill_value_value) 
     2641            { 
     2642              $Height = sprintf("%.2f",$significant_wave_height[$i]{'height'}); 
     2643            } 
     2644            print WAVE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     2645            if ($significant_wave_height[$i]{'data'}[$j] == $missing_value_value 
     2646                || $significant_wave_height[$i]{'data'}[$j] == $Fill_value_value 
     2647                || $significant_wave_height[$i]{'data'}[$j] == '')  
     2648            { 
     2649              $SigWaveHgtDataVal = sprintf("%.2f",$significant_wave_height[$i]{'data'}[$j]); 
     2650            } 
     2651            if ($dominant_wave_period[$i]{'data'}[$j] == $missing_value_value 
     2652              || $dominant_wave_period[$i]{'data'}[$j] == $Fill_value_value 
     2653              || $dominant_wave_period[$i]{'data'}[$j] == '')  
     2654            { 
     2655              $DomWaveDataVal = sprintf("%.2f",$dominant_wave_period[$i]{'data'}[$j]); 
     2656            } 
    18442657          } 
    1845           else { 
    1846             $this_val = sprintf("%.2f",$significant_wave_height[$i]{'height'}); 
    1847             print WAVE_SQLFILE ','.$this_val; 
    1848           } 
    1849           print WAVE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1850           if ($significant_wave_height[$i]{'data'}[$j] == $missing_value_value 
    1851             || $significant_wave_height[$i]{'data'}[$j] == $Fill_value_value 
    1852             || $significant_wave_height[$i]{'data'}[$j] == '') { 
    1853             print WAVE_SQLFILE ',NULL'; 
    1854           } 
    1855           else { 
    1856             $this_val = sprintf("%.2f",$significant_wave_height[$i]{'data'}[$j]); 
    1857             print WAVE_SQLFILE ','.$this_val; 
    1858           } 
    1859           if ($dominant_wave_period[$i]{'data'}[$j] == $missing_value_value 
    1860             || $dominant_wave_period[$i]{'data'}[$j] == $Fill_value_value 
    1861             || $dominant_wave_period[$i]{'data'}[$j] == '') { 
    1862             print WAVE_SQLFILE ',NULL'; 
    1863           } 
    1864           else { 
    1865             $this_val = sprintf("%.2f",$dominant_wave_period[$i]{'data'}[$j]); 
    1866             print WAVE_SQLFILE ','.$this_val; 
    1867           } 
    1868           print WAVE_SQLFILE ",GeometryFromText('POINT(";  
    1869           print WAVE_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1870           print WAVE_SQLFILE ")',-1));\n"; 
    1871         } 
    1872       } 
    1873       print WAVE_SQLFILE "\n"; 
    1874     } 
    1875     close(WAVE_SQLFILE); 
    1876   }   
     2658        } 
     2659        my $strUnits; 
     2660        $strUnits = obsKMLSubRoutines::UnitsStringConversion( $dominant_wave_period[$i]{'units'}, $XMLControlFile );       
     2661        if( length( $strUnits ) == 0 ) 
     2662        { 
     2663           $strUnits = $dominant_wave_period[$i]{'units'}; 
     2664        } 
     2665           
     2666        obsKMLSubRoutines::KMLAddObsToHash( 'dominant_wave_period',  
     2667                                            $time_formatted_values[$j], 
     2668                                            $DomWaveDataVal, 
     2669                                            1, 
     2670                                            $strPlatformID, 
     2671                                            $Height, 
     2672                                            $strUnits, 
     2673                                            $rObsHash ); 
     2674        $strUnits = ''; 
     2675        $strUnits = obsKMLSubRoutines::UnitsStringConversion( $significant_wave_height[$i]{'units'}, $XMLControlFile );         
     2676        if( length( $strUnits ) == 0 ) 
     2677        { 
     2678           $strUnits = $significant_wave_height[$i]{'units'}; 
     2679        } 
     2680        obsKMLSubRoutines::KMLAddObsToHash( 'significant_wave_height',  
     2681                                            $time_formatted_values[$j], 
     2682                                            $SigWaveHgtDataVal, 
     2683                                            1, 
     2684                                            $strPlatformID, 
     2685                                            $Height, 
     2686                                            $strUnits, 
     2687                                            $rObsHash ); 
     2688         
     2689      } 
     2690    }                     
     2691  } 
     2692  #DWR 4/5/2008 
     2693  if( $bWriteobsKMLFile ) 
     2694  { 
     2695    my $strXMLPath = '';  
     2696    my $strDate = `date  +%Y-%m-%dT%H-%M-%S`; 
     2697    chomp( $strDate );       
     2698    #$strXMLPath = "../sql/$strPlatformID-$strDate.kml"; 
     2699    $strXMLPath = "$strObsKMLFilePath/$strPlatformID-$strDate.kml"; 
     2700    #print( "$strXMLPath\n" ); 
     2701    obsKMLSubRoutines::BuildKMLFile( \%ObsHash, $strXMLPath ); 
     2702  } 
    18772703}