Changeset 172

Show
Ignore:
Timestamp:
04/08/08 10:53:41
Author:
dan
Message:

--

Files:

Legend:

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

    r171 r172  
    2222use obsKMLSubRoutines; 
    2323 
     24#DWR 4/7/2008 Added constants for use with ARGV[1] to tell us what file(s) to write. 
    2425use constant { 
    2526  DEFAULTWRITESQLONLY      => 0, 
     
    184185} 
    185186 
    186 sub fixed_point () { 
    187  
    188   # top timestamp 
    189   $this_station_id_top_ts = $_[0]; 
    190   #DWR 4/7/2008 
    191   my $FileCreationOptions = $_[1]; 
    192    
    193   my $bWriteSQLFiles = 1; 
    194   my $bWriteobsKMLFile = 0; 
    195   if( $FileCreationOptions == 1 ) 
    196   { 
    197    $bWriteSQLFiles = 0; 
    198    $bWriteobsKMLFile = 1; 
    199   } 
    200   elsif( $FileCreationOptions == 2 ) 
    201   { 
    202    $bWriteobsKMLFile = 1;    
    203   } 
    204    
    205   #DWR 4/5/2008 
    206   #The XML file that has our obsKML units conversion. WE use it here to convert the units string names from the netcdf 
    207   #file into the strings we use in the obsKML and then in our database. 
    208   my $strUnitsXMLFilename; 
    209   $strUnitsXMLFilename = './UnitsConversion.xml'; 
    210   my $XMLControlFile = XML::LibXML->new->parse_file("$strUnitsXMLFilename"); 
    211    
    212   # 
    213   # Loop through variables looking for standard_name's 
    214    
    215   my $this_var_name = ''; 
    216   my $this_var_type = ''; 
    217   my $this_var_dims = ''; 
    218   my @this_var_dimid = ''; 
    219   my $this_var_natts = ''; 
    220   my $this_standard_name = ''; 
    221   my $j = 0; 
    222    
    223   for ($i = 0; $i < $nvars; $i++) { 
    224     my $varinq = NetCDF::varinq($ncid, $i, \$this_var_name,  
    225       \$this_var_type, \$this_var_dims, \@this_var_dimid, \$this_var_natts); 
    226     if ($varinq < 0) {die "ABORT!  Cannot get to variables.\n";} 
    227     my $attget = NetCDF::attget($ncid, $i, 'standard_name', \$this_standard_name); 
    228     if (substr($this_standard_name,length($this_standard_name)-1) eq chr(0))   {chop($this_standard_name);} 
    229     if ($attget >= 0) { 
    230       if ($this_standard_name =~ /^time$/) { 
    231         %time_dim = ( 
    232           ref_var_name => $this_var_name, 
    233           ref_var_id   => $i, 
    234           dim_id       => '', 
    235           dim_name     => '', 
    236           dim_size     => '' 
    237         ); 
    238         if ($this_var_dims != 1) {die "ABORT! Time has incorrect number of dimensions.\n";} 
    239         %time_var = ( 
    240           var_name => $this_var_name, 
    241           var_id   => $i, 
    242           dim_id   => $this_var_dimid[0] 
    243         ); 
    244       } 
    245       elsif ($this_standard_name =~ /^longitude$/) { 
    246         %longitude_dim = ( 
    247           ref_var_name => $this_var_name, 
    248           ref_var_id   => $i, 
    249           dim_id       => '', 
    250           dim_name     => '', 
    251           dim_size     => '' 
    252         ); 
    253         if ($this_var_dims != 1) {die "ABORT! Longitude has incorrect number of dimensions.\n";} 
    254         %longitude_var = ( 
    255           var_name => $this_var_name, 
    256           var_id   => $i, 
    257           dim_id   => $this_var_dimid[0] 
    258         ); 
    259       } 
    260       elsif ($this_standard_name =~ /^latitude$/) { 
    261         %latitude_dim = ( 
    262           ref_var_name => $this_var_name, 
    263           ref_var_id   => $i, 
    264           dim_id       => '', 
    265           dim_name     => '', 
    266           dim_size     => '' 
    267         ); 
    268         if ($this_var_dims != 1) {die "ABORT! Latitude has incorrect number of dimensions.\n";} 
    269         %latitude_var = ( 
    270           var_name => $this_var_name, 
    271           var_id   => $i, 
    272           dim_id   => $this_var_dimid[0] 
    273         ); 
    274       } 
    275       elsif ($this_standard_name =~ /^height$/) { 
    276         %height_dim = ( 
    277           ref_var_name => $this_var_name, 
    278           ref_var_id   => $i, 
    279           dim_id       => '', 
    280           dim_name     => '', 
    281           dim_size     => '' 
    282         ); 
    283         if ($this_var_dims != 1) {die "ABORT! Height has incorrect number of dimensions.\n";} 
    284         %height_var = ( 
    285           var_name => $this_var_name, 
    286           var_id   => $i, 
    287           dim_id   => $this_var_dimid[0], 
    288           positive => '' 
    289         ); 
    290       } 
    291       # push all water_level's onto a stack 
    292       elsif ($this_standard_name =~ /^water_level$/) { 
    293         %this_water_level = ( 
    294           var_name => $this_var_name, 
    295           var_id   => $i, 
    296           dim_id   => $this_var_dimid[0], 
    297           height   => '', 
    298           reference         => '', 
    299           reference_to_mllw => '', 
    300           reference_to_msl  => '', 
    301           reference_to_navd88  => '', 
    302           units => ''       #DWR 4/5/2008 
    303         ); 
    304         push @water_level, {%this_water_level}; 
    305       } 
    306       # push all sea_surface_temperature's onto a stack 
    307       elsif ($this_standard_name =~ /^sea_surface_temperature$/) { 
    308         %this_sea_surface_temperature = ( 
    309           var_name => $this_var_name, 
    310           var_id   => $i, 
    311           dim_id   => $this_var_dimid[0], 
    312           height   => '', 
    313           units => ''       #DWR 4/5/2008 
    314         ); 
    315         push @sea_surface_temperature, {%this_sea_surface_temperature}; 
    316       } 
    317       # push all bottom_water_temp's onto a stack 
    318       elsif ($this_standard_name =~ /^sea_bottom_temperature$/) { 
    319         %this_sea_bottom_temperature = ( 
    320           var_name => $this_var_name, 
    321           var_id   => $i, 
    322           dim_id   => $this_var_dimid[0], 
    323           height   => '', 
    324           units    => ''       #DWR 4/5/2008 
    325         ); 
    326         push @sea_bottom_temperature, {%this_sea_bottom_temperature}; 
    327       } 
    328       # push all air_temperature's onto a stack 
    329       elsif ($this_standard_name =~ /^air_temperature$/) { 
    330         %this_air_temperature = ( 
    331           var_name => $this_var_name, 
    332           var_id   => $i, 
    333           dim_id   => $this_var_dimid[0], 
    334           height   => '', 
    335           units    => ''       #DWR 4/5/2008 
    336         ); 
    337         push @air_temperature, {%this_air_temperature}; 
    338       } 
    339       # push all wind_speed's onto a stack 
    340       elsif ($this_standard_name =~ /^wind_speed$/) { 
    341         %this_wind_speed = ( 
    342           var_name          => $this_var_name, 
    343           var_id            => $i, 
    344           dim_id            => $this_var_dimid[0], 
    345           height            => '', 
    346           can_be_normalized => '', 
    347           units             => ''       #DWR 4/5/2008 
    348         ); 
    349         push @wind_speed, {%this_wind_speed}; 
    350       } 
    351       # push all wind_gust's onto a stack 
    352       elsif ($this_standard_name =~ /^wind_gust$/) { 
    353         %this_wind_gust = ( 
    354           var_name          => $this_var_name, 
    355           var_id            => $i, 
    356           dim_id            => $this_var_dimid[0], 
    357           height            => '', 
    358           can_be_normalized => '', 
    359           units             => ''       #DWR 4/5/2008 
    360         ); 
    361         push @wind_gust, {%this_wind_gust}; 
    362       } 
    363       # push all wind_from_direction's onto a stack 
    364       elsif ($this_standard_name =~ /^wind_from_direction$/) { 
    365         %this_wind_from_direction = ( 
    366           var_name => $this_var_name, 
    367           var_id            => $i, 
    368           dim_id            => $this_var_dimid[0], 
    369           height            => '', 
    370           can_be_normalized => '', 
    371           units             => ''       #DWR 4/5/2008 
    372            
    373         ); 
    374         push @wind_from_direction, {%this_wind_from_direction}; 
    375       } 
    376       # push all air_pressure's onto a stack 
    377       elsif ($this_standard_name =~ /^air_pressure$/) { 
    378         %this_air_pressure = ( 
    379           var_name => $this_var_name, 
    380           var_id   => $i, 
    381           dim_id   => $this_var_dimid[0], 
    382           height   => '', 
    383           units    => ''       #DWR 4/5/2008 
    384         ); 
    385         push @air_pressure, {%this_air_pressure}; 
    386       } 
    387       # push all salinity's onto a stack 
    388       elsif ($this_standard_name =~ /^salinity$/) { 
    389         %this_salinity = ( 
    390           var_name => $this_var_name, 
    391           var_id   => $i, 
    392           dim_id   => $this_var_dimid[0], 
    393           height   => '', 
    394           units    => ''       #DWR 4/5/2008 
    395         ); 
    396         push @salinity, {%this_salinity}; 
    397       } 
    398       # push all sea_surface_eastward_current's onto a stack 
    399       elsif ($this_standard_name =~ /^sea_surface_eastward_current$/) { 
    400         %this_sea_surface_eastward_current = ( 
    401           var_name          => $this_var_name, 
    402           var_id            => $i, 
    403           dim_id            => $this_var_dimid[0], 
    404           height            => '', 
    405           units             => ''       #DWR 4/5/2008 
    406         ); 
    407         push @sea_surface_eastward_current, {%this_sea_surface_eastward_current}; 
    408       } 
    409       # push all sea_surface_northward_current's onto a stack 
    410       elsif ($this_standard_name =~ /^sea_surface_northward_current$/) { 
    411         %this_sea_surface_northward_current = ( 
    412           var_name          => $this_var_name, 
    413           var_id            => $i, 
    414           dim_id            => $this_var_dimid[0], 
    415           height            => '', 
    416           units             => ''       #DWR 4/5/2008 
    417         ); 
    418         push @sea_surface_northward_current, {%this_sea_surface_northward_current}; 
    419       } 
    420       # push all significant_wave_height's onto a stack 
    421       elsif ($this_standard_name =~ /^significant_wave_height$/) { 
    422         %this_significant_wave_height = ( 
    423           var_name          => $this_var_name, 
    424           var_id            => $i, 
    425           dim_id            => $this_var_dimid[0], 
    426           height            => '', 
    427           units             => ''       #DWR 4/5/2008 
    428         ); 
    429         push @significant_wave_height, {%this_significant_wave_height}; 
    430       } 
    431       # push all dominant_wave_period's onto a stack 
    432       elsif ($this_standard_name =~ /^dominant_wave_period$/) { 
    433         %this_dominant_wave_period = ( 
    434           var_name          => $this_var_name, 
    435           var_id            => $i, 
    436           dim_id            => $this_var_dimid[0], 
    437           height            => '', 
    438           units             => ''       #DWR 4/5/2008 
    439         ); 
    440         push @dominant_wave_period, {%this_dominant_wave_period}; 
    441       } 
    442     } 
    443   } 
    444    
    445   # 
    446   # Required dimensions:  time, longitude, latitude, height 
    447   # Find out names through the variables. 
    448   # Abort if all are not found. 
    449    
    450   if (length($time_dim{'ref_var_name'}) < 1) { 
    451     $err .= "ABORT! No time dimension ref. found via variable.\n"; 
    452   } 
    453   if (length($longitude_dim{'ref_var_name'}) < 1) { 
    454     $err .= "ABORT! No longitude dimension ref. found via variable.\n"; 
    455   } 
    456   if (length($latitude_dim{'ref_var_name'}) < 1) { 
    457     $err .= "ABORT! No latitude dimension ref. found via variable.\n"; 
    458   } 
    459   if (length($height_dim{'ref_var_name'}) < 1) { 
    460     $err .= "ABORT! No height dimension ref. found via variable.\n"; 
    461   }  
    462   if (length $err > 0) { 
    463     die $err; 
    464   } 
    465  
    466   # 
    467   # Get the dimensions and their sizes. 
    468    
    469   # time 
    470   { 
    471     my $this_dim_size = ''; 
    472     my $this_dim_name = ''; 
    473     my $this_dim_id = NetCDF::dimid($ncid,$time_dim{'ref_var_name'}); 
    474     my $diminq = NetCDF::diminq($ncid,$this_dim_id,\$this_dim_name,\$this_dim_size); 
    475     if ($diminq >= 0) { 
    476       $time_dim{'dim_id'}   = $this_dim_id; 
    477       $time_dim{'dim_name'} = $this_dim_name; 
    478       $time_dim{'dim_size'} = $this_dim_size; 
    479     } 
    480     else { 
    481       die "ABORT! Error $diminq getting time dimension.\n"; 
    482     } 
    483   #  print "time dim_name [$time_dim{'dim_name'}] dim_size [$time_dim{'dim_size'}]\n"; 
    484   } 
    485    
    486   # longitude 
    487   { 
    488     my $this_dim_size = ''; 
    489     my $this_dim_name = ''; 
    490     my $this_dim_id = NetCDF::dimid($ncid,$longitude_dim{'ref_var_name'}); 
    491     my $diminq = NetCDF::diminq($ncid,$this_dim_id,\$this_dim_name,\$this_dim_size); 
    492     if ($diminq >= 0) { 
    493       $longitude_dim{'dim_id'}   = $this_dim_id; 
    494       $longitude_dim{'dim_name'} = $this_dim_name; 
    495       $longitude_dim{'dim_size'} = $this_dim_size; 
    496     } 
    497     else { 
    498       die "ABORT! Error $diminq getting longitude dimension.\n"; 
    499     } 
    500   #  print "longitude dim_name [$longitude_dim{'dim_name'}] dim_size [$longitude_dim{'dim_size'}]  \n"; 
    501   } 
    502    
    503   # latitude 
    504   { 
    505     my $this_dim_size = ''; 
    506     my $this_dim_name = ''; 
    507     my $this_dim_id = NetCDF::dimid($ncid,$latitude_dim{'ref_var_name'}); 
    508     my $diminq = NetCDF::diminq($ncid,$this_dim_id,\$this_dim_name,\$this_dim_size); 
    509     if ($diminq >= 0) { 
    510       $latitude_dim{'dim_id'}   = $this_dim_id; 
    511       $latitude_dim{'dim_name'} = $this_dim_name; 
    512       $latitude_dim{'dim_size'} = $this_dim_size; 
    513     } 
    514     else { 
    515       die "ABORT! Error $diminq getting latitude dimension.\n"; 
    516     } 
    517   #  print "latitude dim_name [$latitude_dim{'dim_name'}] dim_size [$latitude_dim{'dim_size'}]\n"; 
    518   } 
    519    
    520   # height 
    521   { 
    522     my $this_dim_size = ''; 
    523     my $this_dim_name = ''; 
    524     my $this_dim_id = NetCDF::dimid($ncid,$height_dim{'ref_var_name'}); 
    525     my $diminq = NetCDF::diminq($ncid,$this_dim_id,\$this_dim_name,\$this_dim_size); 
    526     if ($diminq >= 0) { 
    527       $height_dim{'dim_id'}   = $this_dim_id; 
    528       $height_dim{'dim_name'} = $this_dim_name; 
    529       $height_dim{'dim_size'} = $this_dim_size; 
    530     } 
    531     else { 
    532       die "ABORT! Error $diminq getting height dimension.\n"; 
    533     } 
    534   #  print "height dim_name [$height_dim{'dim_name'}] dim_size [$height_dim{'dim_size'}]\n"; 
    535   } 
    536    
    537   # 
    538   # Check to make sure that the required variables have 
    539   # the correct dimensions listed, e.g. time(time), longitude(longitude), etc. 
    540    
    541   # time 
    542   if ($time_var{'dim_id'} != $time_dim{'dim_id'}) { 
    543     die "ABORT! Time variable does not have correct time dimension.\n"; 
    544   } 
    545   # longitude 
    546   if ($longitude_var{'dim_id'} != $longitude_dim{'dim_id'}) { 
    547     die "ABORT! Longitude variable does not have correct longitude dimension.\n"; 
    548   } 
    549   # latitude 
    550   if ($latitude_var{'dim_id'} != $latitude_dim{'dim_id'}) { 
    551     die "ABORT! Latitude variable does not have correct latitude dimension.\n"; 
    552   } 
    553   # height 
    554   if ($height_var{'dim_id'} != $height_dim{'dim_id'}) { 
    555     die "ABORT! Height variable does not have correct height dimension.\n"; 
    556   } 
    557   
    558   # 
    559   # Get the data of the required elements. 
    560   @time_values           = ''; 
    561   @time_formatted_values = ''; 
    562   @longitude_value       = ''; 
    563   @latitude_value        = ''; 
    564   @height_value          = ''; 
    565   my $positive_value     = ''; 
    566    
    567   # time 
    568   # get all the values 
    569   my $units_value = ''; 
    570   my $varget = NetCDF::varget($ncid, $time_var{'var_id'}, (0), $time_dim{'dim_size'},   \@time_values); 
    571   if ($varget < 0) {die "ABORT! Cannot get time values.\n";} 
    572   # get the units 
    573   my $attget = NetCDF::attget($ncid, $time_var{'var_id'}, 'units', \$units_value); 
    574   if ($attget < 0) {die "ABORT! $time_var{'var_name'} has no units.\n";} 
    575   if (substr($units_value,length($units_value)-1) eq chr(0)) {chop($units_value);} 
    576   my $base_time = UDUNITS::scan($units_value) 
    577     || die "ABORT! Error with $time_var{'var_name'} units.\n"; 
    578   $base_time->istime() 
    579     || die "ABORT! Invalid units for $time_var{'var_name'}.\n"; 
    580    
    581   # format the time values for database insertion (no timezone right now) 
    582   for my $i (0..$#time_values) { 
    583     # convert the time value to new value based on the time units 
    584     my $this_time_value = $base_time->valtocal($time_values[$i],  
    585       $base_year, $base_month, $base_day, $base_hour, $base_minute, $base_second) == 0 
    586       || die "ABORT! Invalid units for $time_var{'var_name'}.\n"; 
    587     $time_formatted_values[$i] = $base_year.'-' 
    588       .sprintf("%02d",$base_month).'-' 
    589       .sprintf("%02d",$base_day).' ' 
    590       .sprintf("%02d",$base_hour).':' 
    591       .sprintf("%02d",$base_minute).':' 
    592       .sprintf("%02d",$base_second); 
    593   } 
    594    
    595   # longitude (scalar) 
    596   $varget = NetCDF::varget($ncid, $longitude_var{'var_id'}, (0), (1), \@longitude_value); 
    597   if ($varget < 0) {die "ABORT! Cannot get longitude value.\n";} 
    598    
    599   # latitude (scalar) 
    600   $varget = NetCDF::varget($ncid, $latitude_var{'var_id'}, (0), (1), \@latitude_value); 
    601   if ($varget < 0) {die "ABORT! Cannot get latitude value.\n";} 
    602    
    603   # height 
    604   #   if count(height) == 1 then this is for all variables 
    605   #   Otherwise, we can ignore the z values passed as parameters and need 
    606   #   to look at each variable's attributes 
    607   if ($height_dim{'dim_size'} == 1) { 
    608     $varget = NetCDF::varget($ncid, $height_var{'var_id'}, (0), (1), \@height_value); 
    609     if ($varget < 0) {die "ABORT! Cannot get height value.\n";} 
    610   } 
    611   # get the positive attribute 
    612   $attget = NetCDF::attget($ncid, $height_var{'var_id'}, 'positive', \$positive_value); 
    613   if (substr($positive_value,length($positive_value)-1) eq chr(0)) {chop($positive_value);} 
    614   $height_var{'positive'} = $positive_value; 
    615  
    616   # water_level's 
    617   @this_water_level_data = ''; 
    618   for $i (0..$#water_level) { 
    619     # this variable's dimension better be time 
    620     if ($water_level[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    621       die "ABORT!  $water_level[$i]{'var_name'} has wrong time dimension.\n"; 
    622     } 
    623     else { 
    624       # get all the variable goodies 
    625       $varget = NetCDF::varget($ncid, $water_level[$i]{'var_id'}, 
    626         (0), $time_dim{'dim_size'}, \@this_water_level_data); 
    627       if ($varget < 0) {die "ABORT! Cannot get $water_level[$i]{'var_name'} data.\n";} 
    628       # get all the attributes for this variable 
    629       my $name    = ''; 
    630       my $nc_type = ''; 
    631       my $ndims   = ''; 
    632       my @dimids  = ''; 
    633       my $natts   = ''; 
    634  
    635       #DWR 4/5/2008 
    636       my $units_value = ''; 
    637       my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, \$units_value); 
    638       if ($attget < 0)  
    639       { 
    640        die "ABORT! $water_level[$i]{'var_name'} has no units.\n"; 
    641       } 
    642       $water_level[$i]{'units'} = $units_value; 
    643        
    644       # we need to loop through the attributes, so find out how many there are 
    645       my $varinq = NetCDF::varinq($ncid, $water_level[$i]{'var_id'}, 
    646         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    647       if ($varinq < 0) {die "ABORT! Cannot get $water_level[$i]{'var_name'} attributes.\n";} 
    648       for my $k (0..$natts-1) { 
    649         # find out about each attribute 
    650         my $this_attname = ''; 
    651         my $attname = NetCDF::attname($ncid, $water_level[$i]{'var_id'}, $k, \$this_attname); 
    652         if ($attname < 0) {die "ABORT! Cannot get $water_level[$i]{'var_name'} $k attribute.\n";} 
    653         # is this a height? 
    654         if ($this_attname eq $height_dim{'dim_name'}) { 
    655           my $attval = ''; 
    656           my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, $this_attname, \$attval); 
    657           if ($attget < 0) {die "ABORT!  Cannot get $water_level[$i]{'var_name'} $k attribute.\n";} 
    658           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    659           $water_level[$i]{'height'} = $attval; 
    660         } 
    661         elsif ($this_attname =~ /^reference$/) { 
    662           my $attval = ''; 
    663           my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, $this_attname, \$attval); 
    664           if ($attget < 0) {die "ABORT!  Cannot get $water_level[$i]{'var_name'} $k attribute.\n";} 
    665           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    666           $water_level[$i]{'reference'} = $attval; 
    667         } 
    668         elsif ($this_attname =~ /^reference_to_MLLW$/) { 
    669           my $attval = ''; 
    670           my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, $this_attname, \$attval); 
    671           if ($attget < 0) {die "ABORT!  Cannot get $water_level[$i]{'var_name'} $k attribute.\n";} 
    672           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    673           $water_level[$i]{'reference_to_mllw'} = $attval; 
    674         } 
    675         elsif ($this_attname =~ /^reference_to_MSL$/) { 
    676           my $attval = ''; 
    677           my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, $this_attname, \$attval); 
    678           if ($attget < 0) {die "ABORT!  Cannot get $water_level[$i]{'var_name'} $k attribute.\n";} 
    679           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    680           $water_level[$i]{'reference_to_msl'} = $attval; 
    681         } 
    682         elsif ($this_attname =~ /^reference_to_NAVD88$/) { 
    683           my $attval = ''; 
    684           my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, $this_attname, \$attval); 
    685           if ($attget < 0) {die "ABORT!  Cannot get $water_level[$i]{'var_name'} $k attribute.\n";} 
    686           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    687           $water_level[$i]{'reference_to_navd88'} = $attval; 
    688         }; 
    689       } 
    690       # if we didn't have an height attribute, assign the global one to this var 
    691       if ($water_level[$i]{'height'} == '') { 
    692         $water_level[$i]{'height'} = $height_value[0]; 
    693       } 
    694       for my $j (0..$#this_water_level_data) { 
    695         push @{$water_level[$i]{'data'}}, $this_water_level_data[$j]; 
    696       } 
    697     } 
    698   } 
    699    
    700   # sea_surface_temperature's 
    701   @this_sea_surface_temperature_data = ''; 
    702   for $i (0..$#sea_surface_temperature) { 
    703     # this variable's dimension better be time 
    704     if ($sea_surface_temperature[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    705       die "ABORT!  $sea_surface_temperature[$i]{'var_name'} has wrong time dimension.\n"; 
    706     } 
    707     else { 
    708       # get all the variable goodies 
    709       $varget = NetCDF::varget($ncid, $sea_surface_temperature[$i]{'var_id'}, 
    710         (0), $time_dim{'dim_size'}, \@this_sea_surface_temperature_data); 
    711       if ($varget < 0) {die "ABORT! Cannot get $sea_surface_temperature[$i]{'var_name'} data.\n";} 
    712       # get all the attributes for this variable 
    713       my $name    = ''; 
    714       my $nc_type = ''; 
    715       my $ndims   = ''; 
    716       my @dimids  = ''; 
    717       my $natts   = ''; 
    718  
    719       #DWR 4/5/2008 
    720       my $units_value = ''; 
    721       my $attget = NetCDF::attget($ncid, $sea_surface_temperature[$i]{'var_id'}, 'units', \$units_value); 
    722       if ($attget < 0)  
    723       { 
    724        die "ABORT! $sea_surface_temperature[$i]{'var_name'} has no units.\n"; 
    725       } 
    726       $sea_surface_temperature[$i]{'units'} = $units_value; 
    727        
    728       # we need to loop through the attributes, so find out how many there are 
    729       my $varinq = NetCDF::varinq($ncid, $sea_surface_temperature[$i]{'var_id'}, 
    730         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    731       if ($varinq < 0) {die "ABORT! Cannot get $sea_surface_temperature[$i]{'var_name'}   attributes.\n";} 
    732       for my $k (0..$natts-1) { 
    733         # find out about each attribute 
    734         my $this_attname = ''; 
    735         my $attname = NetCDF::attname($ncid, $sea_surface_temperature[$i]{'var_id'}, $k,   \$this_attname); 
    736         if ($attname < 0) {die "ABORT! Cannot get $sea_surface_temperature[$i]{'var_name'} $k   attribute.\n";} 
    737         # is this a height? 
    738         if ($this_attname eq $height_dim{'dim_name'}) { 
    739           my $attval = ''; 
    740           my $attget = NetCDF::attget($ncid, $sea_surface_temperature[$i]{'var_id'}, $this_attname,   \$attval); 
    741           if ($attget < 0) {die "ABORT!  Cannot get $sea_surface_temperature[$i]{'var_name'} $k   attribute.\n";} 
    742           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    743           $sea_surface_temperature[$i]{'height'} = $attval; 
    744         }; 
    745       } 
    746       # if we didn't have an height attribute, assign the global one to this var 
    747       if ($sea_surface_temperature[$i]{'height'} == '') { 
    748         $sea_surface_temperature[$i]{'height'} = $height_value[0]; 
    749       } 
    750       for my $j (0..$#this_sea_surface_temperature_data) { 
    751         push @{$sea_surface_temperature[$i]{'data'}}, $this_sea_surface_temperature_data[$j]; 
    752       } 
    753     } 
    754   } 
    755  
    756   # sea_bottom_temperature's 
    757   @this_sea_bottom_temperature_data = ''; 
    758   for $i (0..$#sea_bottom_temperature) { 
    759     # this variable's dimension better be time 
    760     if ($sea_bottom_temperature[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    761       die "ABORT!  $sea_bottom_temperature[$i]{'var_name'} has wrong time dimension.\n"; 
    762     } 
    763     else { 
    764       # get all the variable goodies 
    765       $varget = NetCDF::varget($ncid, $sea_bottom_temperature[$i]{'var_id'}, 
    766         (0), $time_dim{'dim_size'}, \@this_sea_bottom_temperature_data); 
    767       if ($varget < 0) {die "ABORT! Cannot get $sea_bottom_temperature[$i]{'var_name'} data.\n";} 
    768       # get all the attributes for this variable 
    769       my $name    = ''; 
    770       my $nc_type = ''; 
    771       my $ndims   = ''; 
    772       my @dimids  = ''; 
    773       my $natts   = ''; 
    774  
    775       #DWR 4/5/2008 
    776       my $units_value = ''; 
    777       my $attget = NetCDF::attget($ncid, $sea_bottom_temperature[$i]{'var_id'}, 'units', \$units_value); 
    778       if ($attget < 0)  
    779       { 
    780        die "ABORT! $sea_bottom_temperature[$i]{'var_name'} has no units.\n"; 
    781       } 
    782       $sea_bottom_temperature[$i]{'units'} = $units_value; 
    783  
    784       # we need to loop through the attributes, so find out how many there are 
    785       my $varinq = NetCDF::varinq($ncid, $sea_bottom_temperature[$i]{'var_id'}, 
    786         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    787       if ($varinq < 0) {die "ABORT! Cannot get $sea_bottom_temperature[$i]{'var_name'}   attributes.\n";} 
    788       for my $k (0..$natts-1) { 
    789         # find out about each attribute 
    790         my $this_attname = ''; 
    791         my $attname = NetCDF::attname($ncid, $sea_bottom_temperature[$i]{'var_id'}, $k,   \$this_attname); 
    792         if ($attname < 0) {die "ABORT! Cannot get $sea_bottom_temperature[$i]{'var_name'} $k   attribute.\n";} 
    793         # is this a height? 
    794         if ($this_attname eq $height_dim{'dim_name'}) { 
    795           my $attval = ''; 
    796           my $attget = NetCDF::attget($ncid, $sea_bottom_temperature[$i]{'var_id'}, $this_attname,   \$attval); 
    797           if ($attget < 0) {die "ABORT!  Cannot get $sea_bottom_temperature[$i]{'var_name'} $k   attribute.\n";} 
    798           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    799           $sea_bottom_temperature[$i]{'height'} = $attval; 
    800         }; 
    801       } 
    802       # if we didn't have an height attribute, assign the global one to this var 
    803       if ($sea_bottom_temperature[$i]{'height'} == '') { 
    804         $sea_bottom_temperature[$i]{'height'} = $height_value[0]; 
    805       } 
    806       for my $j (0..$#this_sea_bottom_temperature_data) { 
    807         push @{$sea_bottom_temperature[$i]{'data'}}, $this_sea_bottom_temperature_data[$j]; 
    808       } 
    809     } 
    810   } 
    811  
    812   # air_temperature's 
    813   @this_air_temperature_data = ''; 
    814   for $i (0..$#air_temperature) { 
    815     # this variable's dimension better be time 
    816     if ($air_temperature[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    817       die "ABORT!  $air_temperature[$i]{'var_name'} has wrong time dimension.\n"; 
    818     } 
    819     else { 
    820       # get all the variable goodies 
    821       $varget = NetCDF::varget($ncid, $air_temperature[$i]{'var_id'}, 
    822         (0), $time_dim{'dim_size'}, \@this_air_temperature_data); 
    823       if ($varget < 0) {die "ABORT! Cannot get $air_temperature[$i]{'var_name'} data.\n";} 
    824       # get all the attributes for this variable 
    825       my $name    = ''; 
    826       my $nc_type = ''; 
    827       my $ndims   = ''; 
    828       my @dimids  = ''; 
    829       my $natts   = ''; 
    830        
    831       #DWR 4/5/2008 
    832       my $units_value = ''; 
    833       my $attget = NetCDF::attget($ncid, $air_temperature[$i]{'var_id'}, 'units', \$units_value); 
    834       if ($attget < 0)  
    835       { 
    836        die "ABORT! $air_temperature[$i]{'var_name'} has no units.\n"; 
    837       } 
    838       $air_temperature[$i]{'units'} = $units_value; 
    839        
    840       # we need to loop through the attributes, so find out how many there are 
    841       my $varinq = NetCDF::varinq($ncid, $air_temperature[$i]{'var_id'}, 
    842         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    843       if ($varinq < 0) {die "ABORT! Cannot get $air_temperature[$i]{'var_name'}   attributes.\n";} 
    844       for my $k (0..$natts-1) { 
    845         # find out about each attribute 
    846         my $this_attname = ''; 
    847         my $attname = NetCDF::attname($ncid, $air_temperature[$i]{'var_id'}, $k,   \$this_attname); 
    848         if ($attname < 0) {die "ABORT! Cannot get $air_temperature[$i]{'var_name'} $k   attribute.\n";} 
    849         # is this a height? 
    850         if ($this_attname eq $height_dim{'dim_name'}) { 
    851           my $attval = ''; 
    852           my $attget = NetCDF::attget($ncid, $air_temperature[$i]{'var_id'}, $this_attname,   \$attval); 
    853           if ($attget < 0) {die "ABORT!  Cannot get $air_temperature[$i]{'var_name'} $k   attribute.\n";} 
    854           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    855           $air_temperature[$i]{'height'} = $attval; 
    856         }; 
    857       } 
    858       # if we didn't have an height attribute, assign the global one to this var 
    859       if ($air_temperature[$i]{'height'} == '') { 
    860         $air_temperature[$i]{'height'} = $height_value[0]; 
    861       } 
    862       for my $j (0..$#this_air_temperature_data) { 
    863         push @{$air_temperature[$i]{'data'}}, $this_air_temperature_data[$j]; 
    864       } 
    865     } 
    866   } 
    867  
    868   # wind_speed's 
    869   @this_wind_speed_data = ''; 
    870   for $i (0..$#wind_speed) { 
    871     # this variable's dimension better be time 
    872     if ($wind_speed[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    873       die "ABORT!  $wind_speed[$i]{'var_name'} has wrong time dimension.\n"; 
    874     } 
    875     else { 
    876       # get all the variable goodies 
    877       $varget = NetCDF::varget($ncid, $wind_speed[$i]{'var_id'}, 
    878         (0), $time_dim{'dim_size'}, \@this_wind_speed_data); 
    879       if ($varget < 0) {die "ABORT! Cannot get $wind_speed[$i]{'var_name'} data.\n";} 
    880       # get all the attributes for this variable 
    881       my $name    = ''; 
    882       my $nc_type = ''; 
    883       my $ndims   = ''; 
    884       my @dimids  = ''; 
    885       my $natts   = ''; 
    886  
    887       #DWR 4/5/2008 
    888       my $units_value = ''; 
    889       my $attget = NetCDF::attget($ncid, $wind_speed[$i]{'var_id'}, 'units', \$units_value); 
    890       if ($attget < 0)  
    891       { 
    892        die "ABORT! $wind_speed[$i]{'var_name'} has no units.\n"; 
    893       } 
    894       $wind_speed[$i]{'units'} = $units_value; 
    895        
    896        
    897       # we need to loop through the attributes, so find out how many there are 
    898       my $varinq = NetCDF::varinq($ncid, $wind_speed[$i]{'var_id'}, 
    899         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    900       if ($varinq < 0) {die "ABORT! Cannot get $wind_speed[$i]{'var_name'} attributes.\n";} 
    901       for my $k (0..$natts-1) { 
    902         # find out about each attribute 
    903         my $this_attname = ''; 
    904         my $attname = NetCDF::attname($ncid, $wind_speed[$i]{'var_id'}, $k, \$this_attname); 
    905         if ($attname < 0) {die "ABORT! Cannot get $wind_speed[$i]{'var_name'} $k attribute.\n";} 
    906         # is this a height? 
    907         if ($this_attname eq $height_dim{'dim_name'}) { 
    908           my $attval = ''; 
    909           my $attget = NetCDF::attget($ncid, $wind_speed[$i]{'var_id'}, $this_attname, \$attval); 
    910           if ($attget < 0) {die "ABORT!  Cannot get $wind_speed[$i]{'var_name'} $k attribute.\n";} 
    911           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    912           $wind_speed[$i]{'height'} = $attval; 
    913         } 
    914         elsif ($this_attname eq 'can_be_normalized') { 
    915           my $attval = ''; 
    916           my $attget = NetCDF::attget($ncid, $wind_speed[$i]{'var_id'}, $this_attname, \$attval); 
    917           if ($attget < 0) {die "ABORT!  Cannot get $wind_speed[$i]{'var_name'} $k attribute.\n";} 
    918           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    919           $wind_speed[$i]{'can_be_normalized'} = $attval; 
    920         } 
    921       } 
    922       # if we didn't have an height attribute, assign the global one to this var 
    923       if ($wind_speed[$i]{'height'} == '') { 
    924         $wind_speed[$i]{'height'} = $height_value[0]; 
    925       } 
    926       # add a NULL where missing value 
    927       for my $j (0..$#this_wind_speed_data) { 
    928         push @{$wind_speed[$i]{'data'}}, $this_wind_speed_data[$j]; 
    929       } 
    930     } 
    931   } 
    932    
    933   # wind_gust's 
    934   @this_wind_gust_data = ''; 
    935   for $i (0..$#wind_gust) { 
    936     # this variable's dimension better be time 
    937     if ($wind_gust[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    938       die "ABORT!  $wind_gust[$i]{'var_name'} has wrong time dimension.\n"; 
    939     } 
    940     else { 
    941       # get all the variable goodies 
    942       $varget = NetCDF::varget($ncid, $wind_gust[$i]{'var_id'}, 
    943         (0), $time_dim{'dim_size'}, \@this_wind_gust_data); 
    944       if ($varget < 0) {die "ABORT! Cannot get $wind_gust[$i]{'var_name'} data.\n";} 
    945       # get all the attributes for this variable 
    946       my $name    = ''; 
    947       my $nc_type = ''; 
    948       my $ndims   = ''; 
    949       my @dimids  = ''; 
    950       my $natts   = ''; 
    951        
    952       #DWR 4/5/2008 
    953       my $units_value = ''; 
    954       my $attget = NetCDF::attget($ncid, $wind_gust[$i]{'var_id'}, 'units', \$units_value); 
    955       if ($attget < 0)  
    956       { 
    957        die "ABORT! $wind_gust[$i]{'var_name'} has no units.\n"; 
    958       } 
    959       $wind_gust[$i]{'units'} = $units_value; 
    960  
    961        
    962       # we need to loop through the attributes, so find out how many there are 
    963       my $varinq = NetCDF::varinq($ncid, $wind_gust[$i]{'var_id'}, 
    964         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    965       if ($varinq < 0) {die "ABORT! Cannot get $wind_gust[$i]{'var_name'} attributes.\n";} 
    966       for my $k (0..$natts-1) { 
    967         # find out about each attribute 
    968         my $this_attname = ''; 
    969         my $attname = NetCDF::attname($ncid, $wind_gust[$i]{'var_id'}, $k, \$this_attname); 
    970         if ($attname < 0) {die "ABORT! Cannot get $wind_gust[$i]{'var_name'} $k attribute.\n";} 
    971         # is this a height? 
    972         if ($this_attname eq $height_dim{'dim_name'}) { 
    973           my $attval = ''; 
    974           my $attget = NetCDF::attget($ncid, $wind_gust[$i]{'var_id'}, $this_attname, \$attval); 
    975           if ($attget < 0) {die "ABORT!  Cannot get $wind_gust[$i]{'var_name'} $k attribute.\n";} 
    976           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    977           $wind_gust[$i]{'height'} = $attval; 
    978         } 
    979         elsif ($this_attname eq 'can_be_normalized') { 
    980           my $attval = ''; 
    981           my $attget = NetCDF::attget($ncid, $wind_gust[$i]{'var_id'}, $this_attname, \$attval); 
    982           if ($attget < 0) {die "ABORT!  Cannot get $wind_gust[$i]{'var_name'} $k attribute.\n";} 
    983           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    984           $wind_gust[$i]{'can_be_normalized'} = $attval; 
    985         } 
    986       } 
    987       # if we didn't have an height attribute, assign the global one to this var 
    988       if ($wind_gust[$i]{'height'} == '') { 
    989         $wind_gust[$i]{'height'} = $height_value[0]; 
    990       } 
    991       # add a NULL where missing value 
    992       for my $j (0..$#this_wind_gust_data) { 
    993         push @{$wind_gust[$i]{'data'}}, $this_wind_gust_data[$j]; 
    994       } 
    995     } 
    996   } 
    997    
    998   # wind_from_direction's 
    999   @this_wind_from_direction_data = ''; 
    1000   for $i (0..$#wind_from_direction) { 
    1001     # this variable's dimension better be time 
    1002     if ($wind_from_direction[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    1003       die "ABORT!  $wind_from_direction[$i]{'var_name'} has wrong time dimension.\n"; 
    1004     } 
    1005     else { 
    1006       
    1007       #DWR 4/5/2008 
    1008       my $units_value = ''; 
    1009       my $attget = NetCDF::attget($ncid, $wind_from_direction[$i]{'var_id'}, 'units', \$units_value); 
    1010       if ($attget < 0)  
    1011       { 
    1012        die "ABORT! $wind_from_direction[$i]{'var_name'} has no units.\n"; 
    1013       } 
    1014       $wind_from_direction[$i]{'units'} = $units_value; 
    1015       
    1016       # get all the variable goodies 
    1017       $varget = NetCDF::varget($ncid, $wind_from_direction[$i]{'var_id'}, 
    1018         (0), $time_dim{'dim_size'}, \@this_wind_from_direction_data); 
    1019       if ($varget < 0) {die "ABORT! Cannot get $wind_from_direction[$i]{'var_name'} data.\n";} 
    1020       # get all the attributes for this variable 
    1021       my $name    = ''; 
    1022       my $nc_type = ''; 
    1023       my $ndims   = ''; 
    1024       my @dimids  = ''; 
    1025       my $natts   = ''; 
    1026       # we need to loop through the attributes, so find out how many there are 
    1027       my $varinq = NetCDF::varinq($ncid, $wind_from_direction[$i]{'var_id'}, 
    1028         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    1029       if ($varinq < 0) {die "ABORT! Cannot get $wind_from_direction[$i]{'var_name'} attributes.\n";} 
    1030       for my $k (0..$natts-1) { 
    1031         # find out about each attribute 
    1032         my $this_attname = ''; 
    1033         my $attname = NetCDF::attname($ncid, $wind_from_direction[$i]{'var_id'}, $k,   \$this_attname); 
    1034         if ($attname < 0) {die "ABORT! Cannot get $wind_from_direction[$i]{'var_name'} $k   attribute.\n";} 
    1035         # is this a height? 
    1036         if ($this_attname eq $height_dim{'dim_name'}) { 
    1037           my $attval = ''; 
    1038           my $attget = NetCDF::attget($ncid, $wind_from_direction[$i]{'var_id'}, $this_attname,   \$attval); 
    1039           if ($attget < 0) {die "ABORT!  Cannot get $wind_from_direction[$i]{'var_name'} $k   attribute.\n";} 
    1040           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1041           $wind_from_direction[$i]{'height'} = $attval; 
    1042         } 
    1043         elsif ($this_attname eq 'can_be_normalized') { 
    1044           my $attval = ''; 
    1045           my $attget = NetCDF::attget($ncid, $wind_speed[$i]{'var_id'}, $this_attname, \$attval); 
    1046           if ($attget < 0) {die "ABORT!  Cannot get $wind_speed[$i]{'var_name'} $k attribute.\n";} 
    1047           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1048           $wind_from_direction[$i]{'can_be_normalized'} = $attval; 
    1049         } 
    1050       } 
    1051       # if we didn't have an height attribute, assign the global one to this var 
    1052       if ($wind_from_direction[$i]{'height'} == '') { 
    1053         $wind_from_direction[$i]{'height'} = $height_value[0]; 
    1054       } 
    1055       # add a NULL where missing value 
    1056       for my $j (0..$#this_wind_from_direction_data) { 
    1057         push @{$wind_from_direction[$i]{'data'}}, $this_wind_from_direction_data[$j]; 
    1058       } 
    1059     } 
    1060   } 
    1061    
    1062   # air_pressure's 
    1063   @this_air_pressure_data = ''; 
    1064   for $i (0..$#air_pressure) { 
    1065     # this variable's dimension better be time 
    1066     if ($air_pressure[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    1067       die "ABORT!  $air_pressure[$i]{'var_name'} has wrong time dimension.\n"; 
    1068     } 
    1069     else { 
    1070           
    1071       # get all the variable goodies 
    1072       $varget = NetCDF::varget($ncid, $air_pressure[$i]{'var_id'}, 
    1073         (0), $time_dim{'dim_size'}, \@this_air_pressure_data); 
    1074       if ($varget < 0) {die "ABORT! Cannot get $air_pressure[$i]{'var_name'} data.\n";} 
    1075       # get all the attributes for this variable 
    1076       my $name    = ''; 
    1077       my $nc_type = ''; 
    1078       my $ndims   = ''; 
    1079       my @dimids  = ''; 
    1080       my $natts   = ''; 
    1081        
    1082       # check for units 
    1083       my $units_value    = ''; 
    1084       my $this_slope     = ''; 
    1085       my $this_intercept = ''; 
    1086       my $attget = NetCDF::attget($ncid, $air_pressure[$i]{'var_id'}, 'units', \$units_value); 
    1087       if ($attget < 0) {die "ABORT! $air_pressure[$i]{'var_name'} has no units.\n";} 
    1088       if (substr($units_value,length($units_value)-1) eq chr(0)) {chop($units_value);} 
    1089       my $base_units = UDUNITS::scan($units_value) 
    1090         || die "ABORT! Error with $air_pressure[$i]{'var_name'} units.\n"; 
    1091       my $dest_units = UDUNITS::scan('bar'); 
    1092       $base_units->convert($dest_units,$this_slope,$this_intercept); 
    1093        
    1094       #DWR 4/5/2008 
    1095       $air_pressure[$i]{'units'} = $units_value; 
    1096        
    1097       # we need to loop through the attributes, so find out how many there are 
    1098       my $varinq = NetCDF::varinq($ncid, $air_pressure[$i]{'var_id'}, 
    1099         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    1100       if ($varinq < 0) {die "ABORT! Cannot get $air_pressure[$i]{'var_name'}   attributes.\n";} 
    1101       for my $k (0..$natts-1) { 
    1102         # find out about each attribute 
    1103         my $this_attname = ''; 
    1104         my $attname = NetCDF::attname($ncid, $air_pressure[$i]{'var_id'}, $k,   \$this_attname); 
    1105         if ($attname < 0) {die "ABORT! Cannot get $air_pressure[$i]{'var_name'} $k   attribute.\n";} 
    1106         # is this a height? 
    1107         if ($this_attname eq $height_dim{'dim_name'}) { 
    1108           my $attval = ''; 
    1109           my $attget = NetCDF::attget($ncid, $air_pressure[$i]{'var_id'}, $this_attname, \$attval); 
    1110           if ($attget < 0) {die "ABORT!  Cannot get $air_pressure[$i]{'var_name'} $k   attribute.\n";} 
    1111           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1112           $air_pressure[$i]{'height'} = $attval; 
    1113         }; 
    1114       } 
    1115       # if we didn't have an height attribute, assign the global one to this var 
    1116       if ($air_pressure[$i]{'height'} == '') { 
    1117         $air_pressure[$i]{'height'} = $height_value[0]; 
    1118       } 
    1119       for my $j (0..$#this_air_pressure_data) { 
    1120         # target is millibars 
    1121         #cancel out conversion operation if missing value 
    1122         #also canceling conversion if air_pressure > 900 
    1123         #print "$this_air_pressure_data[$j]\n"; 
    1124         if ($this_air_pressure_data[$j] == $missing_value_value || $this_air_pressure_data[$j] == $Fill_value_value || $this_air_pressure_data[$j] > 900) { $this_slope = 1; $this_intercept = 0; } else { $this_slope = 1000; $this_intercept = 0;}  
    1125 print STATION_ID_SQLFILE "-- this_air_pressure_data[j]=".$this_air_pressure_data[$j]." missing_value_value=".$missing_value_value." Fill_value_value=".$Fill_value_value."\n"; 
    1126         push @{$air_pressure[$i]{'data'}}, 
    1127           ($this_air_pressure_data[$j] * $this_slope + $this_intercept); 
    1128         #print "$this_air_pressure_data[$j] * $this_slope + $this_intercept)\n"; 
    1129         #$test_val = ($this_air_pressure_data[$j] * $this_slope + $this_intercept); 
    1130         #print "$test_val\n"; 
    1131       } 
    1132     } 
    1133   } 
    1134    
    1135   # salinity's 
    1136   @this_salinity_data = ''; 
    1137   for $i (0..$#salinity) { 
    1138     # this variable's dimension better be time 
    1139     if ($salinity[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    1140       die "ABORT!  $salinity[$i]{'var_name'} has wrong time dimension.\n"; 
    1141     } 
    1142     else { 
    1143       
    1144       #DWR 4/5/2008 
    1145       my $units_value = ''; 
    1146       my $attget = NetCDF::attget($ncid, $salinity[$i]{'var_id'}, 'units', \$units_value); 
    1147       if ($attget < 0)  
    1148       { 
    1149        die "ABORT! $salinity[$i]{'var_name'} has no units.\n"; 
    1150       } 
    1151       $salinity[$i]{'units'} = $units_value; 
    1152       
    1153       # get all the variable goodies 
    1154       $varget = NetCDF::varget($ncid, $salinity[$i]{'var_id'}, 
    1155         (0), $time_dim{'dim_size'}, \@this_salinity_data); 
    1156       if ($varget < 0) {die "ABORT! Cannot get $salinity[$i]{'var_name'} data.\n";} 
    1157       # get all the attributes for this variable 
    1158       my $name    = ''; 
    1159       my $nc_type = ''; 
    1160       my $ndims   = ''; 
    1161       my @dimids  = ''; 
    1162       my $natts   = ''; 
    1163        
    1164       # we need to loop through the attributes, so find out how many there are 
    1165       my $varinq = NetCDF::varinq($ncid, $salinity[$i]{'var_id'}, 
    1166         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    1167       if ($varinq < 0) {die "ABORT! Cannot get $salinity[$i]{'var_name'}   attributes.\n";} 
    1168       for my $k (0..$natts-1) { 
    1169         # find out about each attribute 
    1170         my $this_attname = ''; 
    1171         my $attname = NetCDF::attname($ncid, $salinity[$i]{'var_id'}, $k,   \$this_attname); 
    1172         if ($attname < 0) {die "ABORT! Cannot get $salinity[$i]{'var_name'} $k   attribute.\n";} 
    1173         # is this a height? 
    1174         if ($this_attname eq $height_dim{'dim_name'}) { 
    1175           my $attval = ''; 
    1176           my $attget = NetCDF::attget($ncid, $salinity[$i]{'var_id'}, $this_attname, \$attval); 
    1177           if ($attget < 0) {die "ABORT!  Cannot get $salinity[$i]{'var_name'} $k   attribute.\n";} 
    1178           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1179           $salinity[$i]{'height'} = $attval; 
    1180         }; 
    1181       } 
    1182       # if we didn't have an height attribute, assign the global one to this var 
    1183       if ($salinity[$i]{'height'} == '') { 
    1184         $salinity[$i]{'height'} = $height_value[0]; 
    1185       } 
    1186       for my $j (0..$#this_salinity_data) { 
    1187         push @{$salinity[$i]{'data'}}, $this_salinity_data[$j]; 
    1188       } 
    1189     } 
    1190   } 
    1191    
    1192   # sea_surface_eastward_current's 
    1193   @this_sea_surface_eastward_current_data = ''; 
    1194   for $i (0..$#sea_surface_eastward_current) { 
    1195     # this variable's dimension better be time 
    1196     if ($sea_surface_eastward_current[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    1197       die "ABORT!  $sea_surface_eastward_current[$i]{'var_name'} has wrong time dimension.\n"; 
    1198     } 
    1199     else {              
    1200       # get all the variable goodies 
    1201       $varget = NetCDF::varget($ncid, $sea_surface_eastward_current[$i]{'var_id'}, 
    1202         (0), $time_dim{'dim_size'}, \@this_sea_surface_eastward_current_data); 
    1203       if ($varget < 0) {die "ABORT! Cannot get $sea_surface_eastward_current[$i]{'var_name'} data.\n";} 
    1204       # get all the attributes for this variable 
    1205       my $name    = ''; 
    1206       my $nc_type = ''; 
    1207       my $ndims   = ''; 
    1208       my @dimids  = ''; 
    1209       my $natts   = ''; 
    1210        
    1211       # check for units 
    1212       my $units_value    = ''; 
    1213       my $this_slope     = ''; 
    1214       my $this_intercept = ''; 
    1215       my $attget = NetCDF::attget($ncid, $sea_surface_eastward_current[$i]{'var_id'}, 'units', \$units_value); 
    1216       if ($attget < 0) {die "ABORT! $sea_surface_eastward_current[$i]{'var_name'} has no units.\n";} 
    1217       if (substr($units_value,length($units_value)-1) eq chr(0)) {chop($units_value);} 
    1218       my $base_units = UDUNITS::scan($units_value) 
    1219         || die "ABORT! Error with $sea_surface_eastward_current[$i]{'var_name'} units.\n"; 
    1220       my $dest_units = UDUNITS::scan('m s-1'); 
    1221       $base_units->convert($dest_units,$this_slope,$this_intercept); 
    1222  
    1223       #DWR 4/5/2008 
    1224       $sea_surface_eastward_current[$i]{'units'} = $units_value; 
    1225  
    1226       # we need to loop through the attributes, so find out how many there are 
    1227       my $varinq = NetCDF::varinq($ncid, $sea_surface_eastward_current[$i]{'var_id'}, 
    1228         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    1229       if ($varinq < 0) {die "ABORT! Cannot get $sea_surface_eastward_current[$i]{'var_name'} attributes.\n";} 
    1230       for my $k (0..$natts-1) { 
    1231         # find out about each attribute 
    1232         my $this_attname = ''; 
    1233         my $attname = NetCDF::attname($ncid, $sea_surface_eastward_current[$i]{'var_id'}, $k, \$this_attname); 
    1234         if ($attname < 0) {die "ABORT! Cannot get $sea_surface_eastward_current[$i]{'var_name'} $k attribute.\n";} 
    1235         # is this a height? 
    1236         if ($this_attname eq $height_dim{'dim_name'}) { 
    1237           my $attval = ''; 
    1238           my $attget = NetCDF::attget($ncid, $sea_surface_eastward_current[$i]{'var_id'}, $this_attname, \$attval); 
    1239           if ($attget < 0) {die "ABORT!  Cannot get $sea_surface_eastward_current[$i]{'var_name'} $k attribute.\n";} 
    1240           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1241           $sea_surface_eastward_current[$i]{'height'} = $attval; 
    1242         } 
    1243       } 
    1244       # if we didn't have an height attribute, assign the global one to this var 
    1245       if ($sea_surface_eastward_current[$i]{'height'} == '') { 
    1246         $sea_surface_eastward_current[$i]{'height'} = $height_value[0]; 
    1247       } 
    1248       # add a NULL where missing value 
    1249       for my $j (0..$#this_sea_surface_eastward_current_data) { 
    1250         #cancel out conversion operation if missing value 
    1251         if ($this_sea_surface_eastward_current_data[$j] == $missing_value_value || $this_sea_surface_eastward_current_data[$j] == $Fill_value_value) { $this_slope = 1; $this_intercept = 0; } 
    1252         push @{$sea_surface_eastward_current[$i]{'data'}}, 
    1253           ($this_sea_surface_eastward_current_data[$j] * $this_slope + $this_intercept); 
    1254       } 
    1255     } 
    1256   } 
    1257    
    1258   # sea_surface_northward_current's 
    1259   @this_sea_surface_northward_current_data = ''; 
    1260   for $i (0..$#sea_surface_northward_current) { 
    1261     # this variable's dimension better be time 
    1262     if ($sea_surface_northward_current[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    1263       die "ABORT!  $sea_surface_northward_current[$i]{'var_name'} has wrong time dimension.\n"; 
    1264     } 
    1265     else { 
    1266           
    1267       # get all the variable goodies 
    1268       $varget = NetCDF::varget($ncid, $sea_surface_northward_current[$i]{'var_id'}, 
    1269         (0), $time_dim{'dim_size'}, \@this_sea_surface_northward_current_data); 
    1270       if ($varget < 0) {die "ABORT! Cannot get $sea_surface_northward_current[$i]{'var_name'} data.\n";} 
    1271       # get all the attributes for this variable 
    1272       my $name    = ''; 
    1273       my $nc_type = ''; 
    1274       my $ndims   = ''; 
    1275       my @dimids  = ''; 
    1276       my $natts   = ''; 
    1277        
    1278       # check for units 
    1279       my $units_value    = ''; 
    1280       my $this_slope     = ''; 
    1281       my $this_intercept = ''; 
    1282       my $attget = NetCDF::attget($ncid, $sea_surface_northward_current[$i]{'var_id'}, 'units', \$units_value); 
    1283       if ($attget < 0) {die "ABORT! $sea_surface_northward_current[$i]{'var_name'} has no units.\n";} 
    1284       if (substr($units_value,length($units_value)-1) eq chr(0)) {chop($units_value);} 
    1285       my $base_units = UDUNITS::scan($units_value) 
    1286         || die "ABORT! Error with $sea_surface_northward_current[$i]{'var_name'} units.\n"; 
    1287       my $dest_units = UDUNITS::scan('m s-1'); 
    1288       $base_units->convert($dest_units,$this_slope,$this_intercept); 
    1289       #DWR 4/5/2008 
    1290       $sea_surface_northward_current[$i]{'units'} = $units_value; 
    1291        
    1292       # we need to loop through the attributes, so find out how many there are 
    1293       my $varinq = NetCDF::varinq($ncid, $sea_surface_northward_current[$i]{'var_id'}, 
    1294         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    1295       if ($varinq < 0) {die "ABORT! Cannot get $sea_surface_northward_current[$i]{'var_name'} attributes.\n";} 
    1296       for my $k (0..$natts-1) { 
    1297         # find out about each attribute 
    1298         my $this_attname = ''; 
    1299         my $attname = NetCDF::attname($ncid, $sea_surface_northward_current[$i]{'var_id'}, $k, \$this_attname); 
    1300         if ($attname < 0) {die "ABORT! Cannot get $sea_surface_northward_current[$i]{'var_name'} $k attribute.\n";} 
    1301         # is this a height? 
    1302         if ($this_attname eq $height_dim{'dim_name'}) { 
    1303           my $attval = ''; 
    1304           my $attget = NetCDF::attget($ncid, $sea_surface_northward_current[$i]{'var_id'}, $this_attname, \$attval); 
    1305           if ($attget < 0) {die "ABORT!  Cannot get $sea_surface_northward_current[$i]{'var_name'} $k attribute.\n";} 
    1306           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1307           $sea_surface_northward_current[$i]{'height'} = $attval; 
    1308         } 
    1309       } 
    1310       # if we didn't have an height attribute, assign the global one to this var 
    1311       if ($sea_surface_northward_current[$i]{'height'} == '') { 
    1312         $sea_surface_northward_current[$i]{'height'} = $height_value[0]; 
    1313       } 
    1314       # add a NULL where missing value 
    1315       for my $j (0..$#this_sea_surface_northward_current_data) { 
    1316         #cancel out conversion operation if missing value 
    1317         if ($this_sea_surface_northward_current_data[$j] == $missing_value_value || $this_sea_surface_northward_current_data[$j] == $Fill_value_value) { $this_slope = 1; $this_intercept = 0; } 
    1318         push @{$sea_surface_northward_current[$i]{'data'}}, 
    1319           ($this_sea_surface_northward_current_data[$j] * $this_slope + $this_intercept); 
    1320       } 
    1321     } 
    1322   } 
    1323    
    1324   # significant_wave_height's 
    1325   @this_significant_wave_height_data = ''; 
    1326   for $i (0..$#significant_wave_height) { 
    1327     # this variable's dimension better be time 
    1328     if ($significant_wave_height[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    1329       die "ABORT!  $significant_wave_height[$i]{'var_name'} has wrong time dimension.\n"; 
    1330     } 
    1331     else { 
    1332            
    1333       # get all the variable goodies 
    1334       $varget = NetCDF::varget($ncid, $significant_wave_height[$i]{'var_id'}, 
    1335         (0), $time_dim{'dim_size'}, \@this_significant_wave_height_data); 
    1336       if ($varget < 0) {die "ABORT! Cannot get $significant_wave_height[$i]{'var_name'} data.\n";} 
    1337       # get all the attributes for this variable 
    1338       my $name    = ''; 
    1339       my $nc_type = ''; 
    1340       my $ndims   = ''; 
    1341       my @dimids  = ''; 
    1342       my $natts   = ''; 
    1343        
    1344       # check for units 
    1345       my $units_value    = ''; 
    1346       my $this_slope     = ''; 
    1347       my $this_intercept = ''; 
    1348       my $attget = NetCDF::attget($ncid, $significant_wave_height[$i]{'var_id'}, 'units', \$units_value); 
    1349       if ($attget < 0) {die "ABORT! $significant_wave_height[$i]{'var_name'} has no units.\n";} 
    1350       if (substr($units_value,length($units_value)-1) eq chr(0)) {chop($units_value);} 
    1351       my $base_units = UDUNITS::scan($units_value) 
    1352         || die "ABORT! Error with $significant_wave_height[$i]{'var_name'} units.\n"; 
    1353       my $dest_units = UDUNITS::scan('m'); 
    1354       $base_units->convert($dest_units,$this_slope,$this_intercept); 
    1355       #DWR 4/5/2008 
    1356       $significant_wave_height[$i]{'units'} = $units_value; 
    1357        
    1358       # we need to loop through the attributes, so find out how many there are 
    1359       my $varinq = NetCDF::varinq($ncid, $significant_wave_height[$i]{'var_id'}, 
    1360         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    1361       if ($varinq < 0) {die "ABORT! Cannot get $significant_wave_height[$i]{'var_name'} attributes.\n";} 
    1362       for my $k (0..$natts-1) { 
    1363         # find out about each attribute 
    1364         my $this_attname = ''; 
    1365         my $attname = NetCDF::attname($ncid, $significant_wave_height[$i]{'var_id'}, $k, \$this_attname); 
    1366         if ($attname < 0) {die "ABORT! Cannot get $significant_wave_height[$i]{'var_name'} $k attribute.\n";} 
    1367         # is this a height? 
    1368         if ($this_attname eq $height_dim{'dim_name'}) { 
    1369           my $attval = ''; 
    1370           my $attget = NetCDF::attget($ncid, $significant_wave_height[$i]{'var_id'}, $this_attname, \$attval); 
    1371           if ($attget < 0) {die "ABORT!  Cannot get $significant_wave_height[$i]{'var_name'} $k attribute.\n";} 
    1372           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1373           $significant_wave_height[$i]{'height'} = $attval; 
    1374         } 
    1375       } 
    1376       # if we didn't have an height attribute, assign the global one to this var 
    1377       if (length $significant_wave_height[$i]{'height'} <= 0) { 
    1378         $significant_wave_height[$i]{'height'} = $height_value[0]; 
    1379       } 
    1380       # add a NULL where missing value 
    1381       for my $j (0..$#this_significant_wave_height_data) { 
    1382         #cancel out conversion operation if missing value 
    1383         if ($this_significant_wave_height_data[$j] == $missing_value_value || $this_significant_wave_height_data[$j] == $Fill_value_value) { $this_slope = 1; $this_intercept = 0; } 
    1384         push @{$significant_wave_height[$i]{'data'}}, 
    1385           ($this_significant_wave_height_data[$j] * $this_slope + $this_intercept); 
    1386       } 
    1387     } 
    1388   } 
    1389    
    1390   # dominant_wave_period's 
    1391   @this_dominant_wave_period_data = ''; 
    1392   for $i (0..$#dominant_wave_period) { 
    1393     # this variable's dimension better be time 
    1394     if ($dominant_wave_period[$i]{'dim_id'} != $time_dim{'dim_id'}) { 
    1395       die "ABORT!  $dominant_wave_period[$i]{'var_name'} has wrong time dimension.\n"; 
    1396     } 
    1397     else { 
    1398            
    1399       # get all the variable goodies 
    1400       $varget = NetCDF::varget($ncid, $dominant_wave_period[$i]{'var_id'}, 
    1401         (0), $time_dim{'dim_size'}, \@this_dominant_wave_period_data); 
    1402       if ($varget < 0) {die "ABORT! Cannot get $dominant_wave_period[$i]{'var_name'} data.\n";} 
    1403       # get all the attributes for this variable 
    1404       my $name    = ''; 
    1405       my $nc_type = ''; 
    1406       my $ndims   = ''; 
    1407       my @dimids  = ''; 
    1408       my $natts   = ''; 
    1409        
    1410       # check for units 
    1411       my $units_value    = ''; 
    1412       my $this_slope     = ''; 
    1413       my $this_intercept = ''; 
    1414       my $attget = NetCDF::attget($ncid, $dominant_wave_period[$i]{'var_id'}, 'units', \$units_value); 
    1415       if ($attget < 0) {die "ABORT! $dominant_wave_period[$i]{'var_name'} has no units.\n";} 
    1416       if (substr($units_value,length($units_value)-1) eq chr(0)) {chop($units_value);} 
    1417       my $base_units = UDUNITS::scan($units_value) 
    1418         || die "ABORT! Error with $dominant_wave_period[$i]{'var_name'} units.\n"; 
    1419       my $dest_units = UDUNITS::scan('second'); 
    1420       $base_units->convert($dest_units,$this_slope,$this_intercept); 
    1421       #DWR 4/5/2008 
    1422       $dominant_wave_period[$i]{'units'} = $units_value; 
    1423        
    1424       # we need to loop through the attributes, so find out how many there are 
    1425       my $varinq = NetCDF::varinq($ncid, $dominant_wave_period[$i]{'var_id'}, 
    1426         \$name, \$nc_type, $ndims, \@dimids, \$natts); 
    1427       if ($varinq < 0) {die "ABORT! Cannot get $dominant_wave_period[$i]{'var_name'} attributes.\n";} 
    1428       for my $k (0..$natts-1) { 
    1429         # find out about each attribute 
    1430         my $this_attname = ''; 
    1431         my $attname = NetCDF::attname($ncid, $dominant_wave_period[$i]{'var_id'}, $k, \$this_attname); 
    1432         if ($attname < 0) {die "ABORT! Cannot get $dominant_wave_period[$i]{'var_name'} $k attribute.\n";} 
    1433         # is this a height? 
    1434         if ($this_attname eq $height_dim{'dim_name'}) { 
    1435           my $attval = ''; 
    1436           my $attget = NetCDF::attget($ncid, $dominant_wave_period[$i]{'var_id'}, $this_attname, \$attval); 
    1437           if ($attget < 0) {die "ABORT!  Cannot get $dominant_wave_period[$i]{'var_name'} $k attribute.\n";} 
    1438           if (substr($attval,length($attval)-1) eq chr(0)) {chop($attval);} 
    1439           $dominant_wave_period[$i]{'height'} = $attval; 
    1440         } 
    1441       } 
    1442       # if we didn't have an height attribute, assign the global one to this var 
    1443       if (length $dominant_wave_period[$i]{'height'} <= 0) { 
    1444         $dominant_wave_period[$i]{'height'} = $height_value[0]; 
    1445       } 
    1446       # add a NULL where missing value 
    1447       for my $j (0..$#this_dominant_wave_period_data) { 
    1448         #cancel out conversion operation if missing value 
    1449         if ($this_dominant_wave_period_data[$j] == $missing_value_value || $this_dominant_wave_period_data[$j] == $Fill_value_value) { $this_slope = 1; $this_intercept = 0; } 
    1450         push @{$dominant_wave_period[$i]{'data'}}, 
    1451           ($this_dominant_wave_period_data[$j] * $this_slope + $this_intercept); 
    1452       } 
    1453     } 
    1454   } 
    1455    
    1456   # 
    1457   # write data to file(s) 
    1458  
    1459   # station_id 
    1460   $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1461   # 6/15/05 < changed to <= in next line by payne; when timestamp file doesn't exist (station has never been reported) 
    1462   # $this_station_id_top_ts has an empty value, which wasn't properly triggering the code to run, 
    1463   # so new stations weren't being created. 
    1464   if ($this_station_id_top_ts <= 0) { 
    1465     if( $bWriteSQLFiles ) 
    1466     { 
    1467       open(STATION_ID_SQLFILE,'>>../sql_in_situ_station_id/in_situ_station_id_'.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'.sql'); 
    1468       print STATION_ID_SQLFILE "-- format_category      = $format_category_value\n"; 
    1469       print STATION_ID_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1470       print STATION_ID_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1471       print STATION_ID_SQLFILE "-- package_code         = $package_code_value\n"; 
    1472       print STATION_ID_SQLFILE "-- title                = $title_value\n"; 
    1473       print STATION_ID_SQLFILE "-- institution          = $institution_value\n"; 
    1474       print STATION_ID_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1475       print STATION_ID_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1476       print STATION_ID_SQLFILE "-- source               = $source_value\n"; 
    1477       print STATION_ID_SQLFILE "-- references           = $references_value\n"; 
    1478       print STATION_ID_SQLFILE "-- contact              = $contact_value\n"; 
    1479       print STATION_ID_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1480       print STATION_ID_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1481       print STATION_ID_SQLFILE "INSERT INTO in_situ_station_id (";  
    1482       print STATION_ID_SQLFILE "station_id,"; 
    1483       print STATION_ID_SQLFILE "title,"; 
    1484       print STATION_ID_SQLFILE "institution,"; 
    1485       print STATION_ID_SQLFILE "institution_url,"; 
    1486       print STATION_ID_SQLFILE "institution_dods_url,"; 
    1487       print STATION_ID_SQLFILE "source,"; 
    1488       print STATION_ID_SQLFILE "refs,"; 
    1489       print STATION_ID_SQLFILE "contact"; 
    1490       print STATION_ID_SQLFILE ") ";  
    1491       print STATION_ID_SQLFILE "VALUES ("; 
    1492       print STATION_ID_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1493       print STATION_ID_SQLFILE ','.'\''.$title_value.'\''; 
    1494       print STATION_ID_SQLFILE ','.'\''.$institution_value.'\''; 
    1495       print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1496       print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1497       print STATION_ID_SQLFILE ','.'\''.$source_value.'\''; 
    1498       print STATION_ID_SQLFILE ','.'\''.$references_value.'\''; 
    1499       print STATION_ID_SQLFILE ','.'\''.$contact_value.'\''; 
    1500       print STATION_ID_SQLFILE ");\n"; 
    1501       close(STATION_ID_SQLFILE); 
    1502     } 
    1503   } 
    1504    
    1505   #DWR 4/5/2008 
    1506   #Implemented code to write the obsKML files.  
    1507   my %ObsHash; 
    1508   my $rObsHash = \%ObsHash; 
    1509   my $strPlatformID = $institution_code_value.'.'.$platform_code_value.'.'.$package_code_value; 
    1510   obsKMLSubRoutines::KMLAddPlatformHashEntry( $strPlatformID, $institution_url_value, $latitude_value[0], $longitude_value[0], $rObsHash ); 
    1511    
    1512   # water_level (water_level) 
    1513   if ($#water_level > -1) 
    1514   { 
    1515     if( $bWriteSQLFiles ) 
    1516     { 
    1517       open(WATER_LEVEL_SQLFILE,'>>../sql/water_level_prod_'.$institution_code_value.'_' 
    1518         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1519       print WATER_LEVEL_SQLFILE "-- format_category      = $format_category_value\n"; 
    1520       print WATER_LEVEL_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1521       print WATER_LEVEL_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1522       print WATER_LEVEL_SQLFILE "-- package_code         = $package_code_value\n"; 
    1523       print WATER_LEVEL_SQLFILE "-- title                = $title_value\n"; 
    1524       print WATER_LEVEL_SQLFILE "-- institution          = $institution_value\n"; 
    1525       print WATER_LEVEL_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1526       print WATER_LEVEL_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1527       print WATER_LEVEL_SQLFILE "-- source               = $source_value\n"; 
    1528       print WATER_LEVEL_SQLFILE "-- references           = $references_value\n"; 
    1529       print WATER_LEVEL_SQLFILE "-- contact              = $contact_value\n"; 
    1530       print WATER_LEVEL_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1531       print WATER_LEVEL_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1532       for my $i (0..$#water_level) { 
    1533         for my $j (0..$#this_water_level_data) { 
    1534           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1535           $this_time_stamp = $time_formatted_values[$j]; 
    1536           $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)); 
    1537  
    1538           if ($water_level[$i]{'data'}[$j] != $missing_value_value 
    1539             && $water_level[$i]{'data'}[$j] != $Fill_value_value 
    1540             && $this_station_id_top_ts < $this_time_stamp_sec 
    1541             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1542             print WATER_LEVEL_SQLFILE "INSERT INTO water_level_prod (";  
    1543             print WATER_LEVEL_SQLFILE "station_id,"; 
    1544             print WATER_LEVEL_SQLFILE "time_stamp,";  
    1545             print WATER_LEVEL_SQLFILE "z,"; 
    1546             print WATER_LEVEL_SQLFILE "positive,"; 
    1547             print WATER_LEVEL_SQLFILE "water_level,"; 
    1548             print WATER_LEVEL_SQLFILE "reference,"; 
    1549             print WATER_LEVEL_SQLFILE "reference_to_mllw,"; 
    1550             print WATER_LEVEL_SQLFILE "reference_to_msl,"; 
    1551             print WATER_LEVEL_SQLFILE "reference_to_navd88,"; 
    1552             print WATER_LEVEL_SQLFILE "title,"; 
    1553             print WATER_LEVEL_SQLFILE "institution,"; 
    1554             print WATER_LEVEL_SQLFILE "institution_url,"; 
    1555             print WATER_LEVEL_SQLFILE "institution_dods_url,"; 
    1556             print WATER_LEVEL_SQLFILE "source,"; 
    1557             print WATER_LEVEL_SQLFILE "refs,"; 
    1558             print WATER_LEVEL_SQLFILE "contact,"; 
    1559             print WATER_LEVEL_SQLFILE "the_geom"; 
    1560             print WATER_LEVEL_SQLFILE ") ";  
    1561             print WATER_LEVEL_SQLFILE "VALUES (";  
    1562             print WATER_LEVEL_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1563             print WATER_LEVEL_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1564             if ($water_level[$i]{'height'} == $missing_value_value 
    1565               || $water_level[$i]{'height'} == $Fill_value_value) { 
    1566               print WATER_LEVEL_SQLFILE ','.'\'\''; 
    1567             } 
    1568             else { 
    1569               $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
    1570               print WATER_LEVEL_SQLFILE ','.$this_val; 
    1571             } 
    1572             print WATER_LEVEL_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1573             $this_val = sprintf("%.2f",$water_level[$i]{'data'}[$j]); 
    1574             print WATER_LEVEL_SQLFILE ','.$this_val; 
    1575             print WATER_LEVEL_SQLFILE ','.'\''.$water_level[$i]{'reference'}.'\''; 
    1576              
    1577              
    1578             if (length $water_level[$i]{'reference_to_mllw'} > 0) { 
    1579               $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_mllw'}); 
    1580             } 
    1581             else { 
    1582               $this_val = 'NULL'; 
    1583             } 
    1584  
    1585             print WATER_LEVEL_SQLFILE ','.$this_val; 
    1586             if (length $water_level[$i]{'reference_to_msl'} > 0) { 
    1587               $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_msl'}); 
    1588             } 
    1589             else { 
    1590               $this_val = 'NULL'; 
    1591             } 
    1592  
    1593             print WATER_LEVEL_SQLFILE ','.$this_val; 
    1594             if (length $water_level[$i]{'reference_to_navd88'} > 0) { 
    1595               $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_navd88'}); 
    1596             } 
    1597             else { 
    1598               $this_val = 'NULL'; 
    1599             } 
    1600  
    1601             print WATER_LEVEL_SQLFILE ','.$this_val; 
    1602             print WATER_LEVEL_SQLFILE ','.'\''.$title_value.'\''; 
    1603             print WATER_LEVEL_SQLFILE ','.'\''.$institution_value.'\''; 
    1604             print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1605             print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1606             print WATER_LEVEL_SQLFILE ','.'\''.$source_value.'\''; 
    1607             print WATER_LEVEL_SQLFILE ','.'\''.$references_value.'\''; 
    1608             print WATER_LEVEL_SQLFILE ','.'\''.$contact_value.'\''; 
    1609             print WATER_LEVEL_SQLFILE ",GeometryFromText('POINT(";  
    1610             print WATER_LEVEL_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1611             print WATER_LEVEL_SQLFILE ")',-1));\n"; 
    1612           } 
    1613         } 
    1614          
    1615         print WATER_LEVEL_SQLFILE "\n"; 
    1616       } 
    1617       close(WATER_LEVEL_SQLFILE); 
    1618     } 
    1619     if( $bWriteobsKMLFile ) 
    1620     {        
    1621       my $MLLWDataVal = 'NULL'; 
    1622       my $MSLDataVal  = 'NULL'; 
    1623       my $NavD88DataVal = 'NULL'; 
    1624       my $Height = ''; 
    1625      
    1626       for my $i (0..$#water_level)  
    1627       { 
    1628         for my $j (0..$#this_water_level_data)  
    1629         { 
    1630           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1631           $this_time_stamp = $time_formatted_values[$j]; 
    1632           $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)); 
    1633  
    1634           if ($water_level[$i]{'data'}[$j] != $missing_value_value 
    1635             && $water_level[$i]{'data'}[$j] != $Fill_value_value 
    1636             && $this_station_id_top_ts < $this_time_stamp_sec 
    1637             && $this_time_stamp_sec > $oldest_ok_timestamp)  
    1638             { 
    1639      
    1640               #DWR 4/3/2008 
    1641               if ($water_level[$i]{'height'} != $missing_value_value 
    1642                 && $water_level[$i]{'height'} != $Fill_value_value)  
    1643               { 
    1644                 $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
    1645                 $Height = $this_val; 
    1646               } 
    1647               $this_val = sprintf("%.2f",$water_level[$i]{'data'}[$j]); 
    1648                
    1649               $DataVal = $water_level[$i]{'reference'}; 
    1650                
    1651               if (length $water_level[$i]{'reference_to_mllw'} > 0) { 
    1652                 $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_mllw'}); 
    1653               } 
    1654               else { 
    1655                 $this_val = 'NULL'; 
    1656               } 
    1657               $MLLWDataVal = $this_val; 
    1658  
    1659               if (length $water_level[$i]{'reference_to_msl'} > 0) { 
    1660                 $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_msl'}); 
    1661               } 
    1662               else { 
    1663                 $this_val = 'NULL'; 
    1664               } 
    1665               $MSLDataVal = $this_val; 
    1666  
    1667               if (length $water_level[$i]{'reference_to_navd88'} > 0) { 
    1668                 $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_navd88'}); 
    1669               } 
    1670               else { 
    1671                 $this_val = 'NULL'; 
    1672               } 
    1673               #DWR 4/5/2008 
    1674               $NavD88DataVal = $this_val; 
    1675           } 
    1676           obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
    1677                                             $time_formatted_values[$j], 
    1678                                             $MLLWDataVal, 
    1679                                             1, 
    1680                                             $strPlatformID, 
    1681                                             $Height, 
    1682                                             'm(MLLW)', 
    1683                                             $rObsHash ); 
    1684           obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
    1685                                             $time_formatted_values[$j], 
    1686                                             $MSLDataVal, 
    1687                                             2, 
    1688                                             $strPlatformID, 
    1689                                             $Height, 
    1690                                             'm(MSL)', 
    1691                                             $rObsHash ); 
    1692           obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
    1693                                             $time_formatted_values[$j], 
    1694                                             $NavD88DataVal, 
    1695                                             3, 
    1696                                             $strPlatformID, 
    1697                                             $Height, 
    1698                                             'm(NAVD88)', 
    1699                                             $rObsHash ); 
    1700         } 
    1701       }             
    1702     } 
    1703   } 
    1704   # sea_surface_temperature (sst) 
    1705   if ($#sea_surface_temperature > -1) { 
    1706     if( $bWriteSQLFiles ) 
    1707     { 
    1708       open(SST_SQLFILE,'>>../sql/sst_prod_'.$institution_code_value.'_' 
    1709         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1710       print SST_SQLFILE "-- format_category      = $format_category_value\n"; 
    1711       print SST_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1712       print SST_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1713       print SST_SQLFILE "-- package_code         = $package_code_value\n"; 
    1714       print SST_SQLFILE "-- title                = $title_value\n"; 
    1715       print SST_SQLFILE "-- institution          = $institution_value\n"; 
    1716       print SST_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1717       print SST_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1718       print SST_SQLFILE "-- source               = $source_value\n"; 
    1719       print SST_SQLFILE "-- references           = $references_value\n"; 
    1720       print SST_SQLFILE "-- contact              = $contact_value\n"; 
    1721       print SST_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1722       print SST_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1723        
    1724       my $DataVal = 'NULL'; 
    1725       my $Height = ''; 
    1726       for my $i (0..$#sea_surface_temperature) { 
    1727         for my $j (0..$#this_sea_surface_temperature_data) { 
    1728           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1729           $this_time_stamp = $time_formatted_values[$j]; 
    1730           $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)); 
    1731          
    1732           if ($sea_surface_temperature[$i]{'data'}[$j] != $missing_value_value 
    1733             && $sea_surface_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1734             && $this_station_id_top_ts < $this_time_stamp_sec 
    1735             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1736             print SST_SQLFILE "INSERT INTO sst_prod (";  
    1737             print SST_SQLFILE "station_id,"; 
    1738             print SST_SQLFILE "time_stamp,";  
    1739             print SST_SQLFILE "z,"; 
    1740             print SST_SQLFILE "positive,"; 
    1741             print SST_SQLFILE "temperature_celcius,"; 
    1742             print SST_SQLFILE "title,"; 
    1743             print SST_SQLFILE "institution,"; 
    1744             print SST_SQLFILE "institution_url,"; 
    1745             print SST_SQLFILE "institution_dods_url,"; 
    1746             print SST_SQLFILE "source,"; 
    1747             print SST_SQLFILE "refs,"; 
    1748             print SST_SQLFILE "contact,"; 
    1749             print SST_SQLFILE "the_geom"; 
    1750             print SST_SQLFILE ") ";  
    1751             print SST_SQLFILE "VALUES (";  
    1752             print SST_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1753             print SST_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1754             if ($sea_surface_temperature[$i]{'height'} == $missing_value_value 
    1755               || $sea_surface_temperature[$i]{'height'} == $Fill_value_value) { 
    1756               print SST_SQLFILE ','.'\'\''; 
    1757             } 
    1758             else { 
    1759               $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'height'}); 
    1760               print SST_SQLFILE ','.$this_val; 
    1761             } 
    1762  
    1763             print SST_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1764             $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'data'}[$j]); 
    1765              
    1766              
    1767             print SST_SQLFILE ','.$this_val; 
    1768             print SST_SQLFILE ','.'\''.$title_value.'\''; 
    1769             print SST_SQLFILE ','.'\''.$institution_value.'\''; 
    1770             print SST_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1771             print SST_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1772             print SST_SQLFILE ','.'\''.$source_value.'\''; 
    1773             print SST_SQLFILE ','.'\''.$references_value.'\''; 
    1774             print SST_SQLFILE ','.'\''.$contact_value.'\''; 
    1775             print SST_SQLFILE ",GeometryFromText('POINT(";  
    1776             print SST_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1777             print SST_SQLFILE ")',-1));\n"; 
    1778           } 
    1779            
    1780         } 
    1781         print SST_SQLFILE "\n"; 
    1782       } 
    1783       close(SST_SQLFILE); 
    1784     } 
    1785     #DWR 4/5/2008 
    1786     if( $bWriteobsKMLFile ) 
    1787     { 
    1788       my $DataVal = 'NULL'; 
    1789       my $Height = ''; 
    1790       for my $i (0..$#sea_surface_temperature)  
    1791       { 
    1792         for my $j (0..$#this_sea_surface_temperature_data)  
    1793         { 
    1794           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1795           $this_time_stamp = $time_formatted_values[$j]; 
    1796           $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)); 
    1797          
    1798           if ($sea_surface_temperature[$i]{'data'}[$j] != $missing_value_value 
    1799               && $sea_surface_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1800               && $this_station_id_top_ts < $this_time_stamp_sec 
    1801               && $this_time_stamp_sec > $oldest_ok_timestamp)  
    1802           { 
    1803  
    1804             if ($sea_surface_temperature[$i]{'height'} != $missing_value_value 
    1805               && $sea_surface_temperature[$i]{'height'} != $Fill_value_value)  
    1806             { 
    1807               $Height = sprintf("%.2f",$sea_surface_temperature[$i]{'height'}); 
    1808             } 
    1809             $DataVal = sprintf("%.2f",$sea_surface_temperature[$i]{'data'}[$j]); 
    1810                        
    1811           } 
    1812           my $strUnits; 
    1813           $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_surface_temperature[$i]{'units'}, $XMLControlFile ); 
    1814           if( length( $strUnits ) == 0 ) 
    1815           { 
    1816             $strUnits = $sea_surface_temperature[$i]{'units'}; 
    1817           } 
    1818           obsKMLSubRoutines::KMLAddObsToHash( 'water_temperature',  
    1819                                             $time_formatted_values[$j], 
    1820                                             $DataVal, 
    1821                                             1, 
    1822                                             $strPlatformID, 
    1823                                             $Height, 
    1824                                             $strUnits, 
    1825                                             $rObsHash ); 
    1826         } 
    1827       }                                             
    1828     } 
    1829   } 
    1830   # sea_bottom_temperature (sbt) 
    1831   if ($#sea_bottom_temperature > -1) { 
    1832     if( $bWriteSQLFiles ) 
    1833     { 
    1834       open(SBT_SQLFILE,'>>../sql/bottom_water_temp_prod_'.$institution_code_value.'_' 
    1835         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1836       print SBT_SQLFILE "-- format_category      = $format_category_value\n"; 
    1837       print SBT_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1838       print SBT_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1839       print SBT_SQLFILE "-- package_code         = $package_code_value\n"; 
    1840       print SBT_SQLFILE "-- title                = $title_value\n"; 
    1841       print SBT_SQLFILE "-- institution          = $institution_value\n"; 
    1842       print SBT_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1843       print SBT_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1844       print SBT_SQLFILE "-- source               = $source_value\n"; 
    1845       print SBT_SQLFILE "-- references           = $references_value\n"; 
    1846       print SBT_SQLFILE "-- contact              = $contact_value\n"; 
    1847       print SBT_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1848       print SBT_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1849        
    1850       for my $i (0..$#sea_bottom_temperature) { 
    1851         for my $j (0..$#this_sea_bottom_temperature_data) { 
    1852           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1853           $this_time_stamp = $time_formatted_values[$j]; 
    1854           $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)); 
    1855          
    1856           if ($sea_bottom_temperature[$i]{'data'}[$j] != $missing_value_value 
    1857             && $sea_bottom_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1858             && $this_station_id_top_ts < $this_time_stamp_sec 
    1859             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1860             print SBT_SQLFILE "INSERT INTO bottom_water_temp_prod (";  
    1861             print SBT_SQLFILE "station_id,"; 
    1862             print SBT_SQLFILE "time_stamp,";  
    1863             print SBT_SQLFILE "z,"; 
    1864             print SBT_SQLFILE "positive,"; 
    1865             print SBT_SQLFILE "temperature_celcius,"; 
    1866             print SBT_SQLFILE "title,"; 
    1867             print SBT_SQLFILE "institution,"; 
    1868             print SBT_SQLFILE "institution_url,"; 
    1869             print SBT_SQLFILE "institution_dods_url,"; 
    1870             print SBT_SQLFILE "source,"; 
    1871             print SBT_SQLFILE "refs,"; 
    1872             print SBT_SQLFILE "contact,"; 
    1873             print SBT_SQLFILE "the_geom"; 
    1874             print SBT_SQLFILE ") ";  
    1875             print SBT_SQLFILE "VALUES (";  
    1876             print SBT_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1877             print SBT_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1878             if ($sea_bottom_temperature[$i]{'height'} == $missing_value_value 
    1879               || $sea_bottom_temperature[$i]{'height'} == $Fill_value_value) { 
    1880               print SBT_SQLFILE ','.'\'\''; 
    1881             } 
    1882             else { 
    1883               $this_val = sprintf("%.2f",$sea_bottom_temperature[$i]{'height'}); 
    1884               print SBT_SQLFILE ','.$this_val; 
    1885             } 
    1886  
    1887             print SBT_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    1888             $this_val = sprintf("%.2f",$sea_bottom_temperature[$i]{'data'}[$j]); 
    1889              
    1890              
    1891             print SBT_SQLFILE ','.$this_val; 
    1892             print SBT_SQLFILE ','.'\''.$title_value.'\''; 
    1893             print SBT_SQLFILE ','.'\''.$institution_value.'\''; 
    1894             print SBT_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    1895             print SBT_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    1896             print SBT_SQLFILE ','.'\''.$source_value.'\''; 
    1897             print SBT_SQLFILE ','.'\''.$references_value.'\''; 
    1898             print SBT_SQLFILE ','.'\''.$contact_value.'\''; 
    1899             print SBT_SQLFILE ",GeometryFromText('POINT(";  
    1900             print SBT_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    1901             print SBT_SQLFILE ")',-1));\n"; 
    1902           } 
    1903  
    1904         } 
    1905         print SBT_SQLFILE "\n"; 
    1906       } 
    1907       close(SBT_SQLFILE); 
    1908     } 
    1909     #DWR 4/5/2008 
    1910     if( $bWriteobsKMLFile ) 
    1911     { 
    1912       my $DataVal = 'NULL'; 
    1913       my $Height = ''; 
    1914       for my $i (0..$#sea_bottom_temperature)  
    1915       { 
    1916         for my $j (0..$#sea_bottom_temperature)  
    1917         { 
    1918           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1919           $this_time_stamp = $time_formatted_values[$j]; 
    1920           $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));        
    1921           if ($sea_bottom_temperature[$i]{'data'}[$j] != $missing_value_value 
    1922               && $sea_bottom_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1923               && $this_station_id_top_ts < $this_time_stamp_sec 
    1924               && $this_time_stamp_sec > $oldest_ok_timestamp)  
    1925           { 
    1926  
    1927             if ($sea_bottom_temperature[$i]{'height'} != $missing_value_value 
    1928               && $sea_bottom_temperature[$i]{'height'} != $Fill_value_value)  
    1929             { 
    1930               $Height = sprintf("%.2f",$sea_bottom_temperature[$i]{'height'}); 
    1931             } 
    1932             $DataVal = sprintf("%.2f",$sea_bottom_temperature[$i]{'data'}[$j]); 
    1933                        
    1934           } 
    1935           my $strUnits; 
    1936           $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_bottom_temperature[$i]{'units'}, $XMLControlFile );         
    1937           if( length( $strUnits ) == 0 ) 
    1938           { 
    1939            $strUnits = $sea_bottom_temperature[$i]{'units'}; 
    1940           } 
    1941           obsKMLSubRoutines::KMLAddObsToHash( 'water_temperature',  
    1942                                             $time_formatted_values[$j], 
    1943                                             $DataVal, 
    1944                                             2, 
    1945                                             $strPlatformID, 
    1946                                             $Height, 
    1947                                             $strUnits, 
    1948                                             $rObsHash ); 
    1949         } 
    1950       }                                             
    1951     }     
    1952   } 
    1953    
    1954   # air_temperature 
    1955   # Going to do this right this time.  Instead of populating each row w/ all 
    1956   # the metadata, use the station_id lookup, instead. 
    1957   if ($#air_temperature > -1) { 
    1958     #DWR 4/5/2008  
    1959     if( $bWriteSQLFiles ) 
    1960     { 
    1961       open(AIR_TEMP_SQLFILE,'>>../sql/air_temperature_prod_'.$institution_code_value.'_' 
    1962         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    1963       print AIR_TEMP_SQLFILE "-- format_category      = $format_category_value\n"; 
    1964       print AIR_TEMP_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    1965       print AIR_TEMP_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    1966       print AIR_TEMP_SQLFILE "-- package_code         = $package_code_value\n"; 
    1967       print AIR_TEMP_SQLFILE "-- title                = $title_value\n"; 
    1968       print AIR_TEMP_SQLFILE "-- institution          = $institution_value\n"; 
    1969       print AIR_TEMP_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    1970       print AIR_TEMP_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    1971       print AIR_TEMP_SQLFILE "-- source               = $source_value\n"; 
    1972       print AIR_TEMP_SQLFILE "-- references           = $references_value\n"; 
    1973       print AIR_TEMP_SQLFILE "-- contact              = $contact_value\n"; 
    1974       print AIR_TEMP_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    1975       print AIR_TEMP_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    1976       for my $i (0..$#air_temperature) { 
    1977         for my $j (0..$#this_air_temperature_data) { 
    1978           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    1979           $this_time_stamp = $time_formatted_values[$j]; 
    1980           $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)); 
    1981  
    1982           my $DataValue = 'NULL'; 
    1983           my $Height    = ''; 
    1984           if ($air_temperature[$i]{'data'}[$j] != $missing_value_value 
    1985             && $air_temperature[$i]{'data'}[$j] != $Fill_value_value 
    1986             && $this_station_id_top_ts < $this_time_stamp_sec 
    1987             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    1988             print AIR_TEMP_SQLFILE "INSERT INTO air_temperature_prod (";  
    1989             print AIR_TEMP_SQLFILE "station_id,"; 
    1990             print AIR_TEMP_SQLFILE "time_stamp,";  
    1991             print AIR_TEMP_SQLFILE "z,"; 
    1992             print AIR_TEMP_SQLFILE "positive,"; 
    1993             print AIR_TEMP_SQLFILE "temperature_celcius,"; 
    1994             print AIR_TEMP_SQLFILE "the_geom"; 
    1995             print AIR_TEMP_SQLFILE ") ";  
    1996             print AIR_TEMP_SQLFILE "VALUES (";  
    1997             print AIR_TEMP_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    1998             print AIR_TEMP_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    1999             if ($air_temperature[$i]{'height'} == $missing_value_value 
    2000               || $air_temperature[$i]{'height'} == $Fill_value_value) { 
    2001               print AIR_TEMP_SQLFILE ',NULL'; 
    2002             } 
    2003             else { 
    2004               $this_val = sprintf("%.2f",$air_temperature[$i]{'height'}); 
    2005               print AIR_TEMP_SQLFILE ','.$this_val;               
    2006             } 
    2007             print AIR_TEMP_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    2008             $this_val = sprintf("%.2f",$air_temperature[$i]{'data'}[$j]); 
    2009             print AIR_TEMP_SQLFILE ','.$this_val; 
    2010             print AIR_TEMP_SQLFILE ",GeometryFromText('POINT(";  
    2011             print AIR_TEMP_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    2012             print AIR_TEMP_SQLFILE ")',-1));\n"; 
    2013                         
    2014           } 
    2015         } 
    2016         print AIR_TEMP_SQLFILE "\n"; 
    2017       } 
    2018       close(AIR_TEMP_SQLFILE); 
    2019     } 
    2020     #DWR 4/5/2008 
    2021     if( $bWriteobsKMLFile ) 
    2022     { 
    2023       my $DataVal = 'NULL'; 
    2024       my $Height = ''; 
    2025       for my $i (0..$#air_temperature)  
    2026       { 
    2027         for my $j (0..$#air_temperature)  
    2028         { 
    2029           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2030           $this_time_stamp = $time_formatted_values[$j]; 
    2031           $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)); 
    2032          
    2033           if ($air_temperature[$i]{'data'}[$j] != $missing_value_value 
    2034               && $air_temperature[$i]{'data'}[$j] != $Fill_value_value 
    2035               && $this_station_id_top_ts < $this_time_stamp_sec 
    2036               && $this_time_stamp_sec > $oldest_ok_timestamp)  
    2037           { 
    2038  
    2039             if ($air_temperature[$i]{'height'} != $missing_value_value 
    2040               && $air_temperature[$i]{'height'} != $Fill_value_value)  
    2041             { 
    2042               $Height = sprintf("%.2f",$air_temperature[$i]{'height'}); 
    2043             } 
    2044             $DataVal = sprintf("%.2f",$air_temperature[$i]{'data'}[$j]); 
    2045                        
    2046           } 
    2047           my $strUnits; 
    2048           $strUnits = obsKMLSubRoutines::UnitsStringConversion( $air_temperature[$i]{'units'}, $XMLControlFile );         
    2049           if( length( $strUnits ) == 0 ) 
    2050           { 
    2051            $strUnits = $air_temperature[$i]{'units'}; 
    2052           } 
    2053           obsKMLSubRoutines::KMLAddObsToHash( 'air_temperature',  
    2054                                             $time_formatted_values[$j], 
    2055                                             $DataVal, 
    2056                                             1, 
    2057                                             $strPlatformID, 
    2058                                             $Height, 
    2059                                             $strUnits, 
    2060                                             $rObsHash ); 
    2061         } 
    2062       }                                             
    2063     }         
    2064   } 
    2065   
    2066  
    2067   # wind_speed and wind_gust and wind_from_direction 
    2068   # Start w/ wind_speed and then look through the wind_from_direction to find its 
    2069   # pair by looking at the heights.  This index will also define the gust. 
    2070   # Assume that wind_speed controls everything (from_dir, gust, z, normalized) index-wise. 
    2071   if ($#wind_speed > -1) { 
    2072     #dwr 4/5/2008 
    2073     if( $bWriteSQLFiles ) 
    2074     { 
    2075       open(WIND_SQLFILE,'>>../sql/wind_prod_'.$institution_code_value.'_' 
    2076         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    2077       print WIND_SQLFILE "-- format_category      = $format_category_value\n"; 
    2078       print WIND_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    2079       print WIND_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    2080       print WIND_SQLFILE "-- package_code         = $package_code_value\n"; 
    2081       print WIND_SQLFILE "-- title                = $title_value\n"; 
    2082       print WIND_SQLFILE "-- institution          = $institution_value\n"; 
    2083       print WIND_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    2084       print WIND_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    2085       print WIND_SQLFILE "-- source               = $source_value\n"; 
    2086       print WIND_SQLFILE "-- references           = $references_value\n"; 
    2087       print WIND_SQLFILE "-- contact              = $contact_value\n"; 
    2088       print WIND_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    2089       print WIND_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    2090       for my $i (0..$#wind_speed) { 
    2091         my $j = 0; 
    2092         while ($j <= $#wind_from_direction 
    2093           && $wind_from_direction[$j]{'height'} != $wind_speed[$i]{'height'}) { 
    2094           $j++; 
    2095         } 
    2096         if ($j > $#wind_from_direction) { 
    2097           die "ABORT!  Could not find matching wind_from_direction for $wind_speed[$i]  {'var_name'}.\n"; 
    2098         } 
    2099         else { 
    2100           for my $k (0..$#this_wind_speed_data) { 
    2101             $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2102             $this_time_stamp = $time_formatted_values[$k]; 
    2103             $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)); 
    2104              
    2105             if ($wind_speed[$i]{'data'}[$k] != $missing_value_value 
    2106               && $wind_speed[$i]{'data'}[$k] != $Fill_value_value 
    2107               && $wind_from_direction[$j]{'data'}[$k] != $missing_value_value 
    2108               && $wind_from_direction[$j]{'data'}[$k] != $Fill_value_value 
    2109               && $this_station_id_top_ts < $this_time_stamp_sec 
    2110               && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    2111               print WIND_SQLFILE "INSERT INTO wind_prod (";  
    2112               print WIND_SQLFILE "station_id,"; 
    2113               print WIND_SQLFILE "time_stamp,";  
    2114               print WIND_SQLFILE "z,"; 
    2115               print WIND_SQLFILE "positive,"; 
    2116               print WIND_SQLFILE "wind_speed,"; 
    2117               print WIND_SQLFILE "wind_gust,"; 
    2118               print WIND_SQLFILE "wind_from_direction,"; 
    2119               print WIND_SQLFILE "can_be_normalized,"; 
    2120               print WIND_SQLFILE "title,"; 
    2121               print WIND_SQLFILE "institution,"; 
    2122               print WIND_SQLFILE "institution_url,"; 
    2123               print WIND_SQLFILE "institution_dods_url,"; 
    2124               print WIND_SQLFILE "source,"; 
    2125               print WIND_SQLFILE "refs,"; 
    2126               print WIND_SQLFILE "contact,"; 
    2127               print WIND_SQLFILE "the_geom"; 
    2128               print WIND_SQLFILE ") ";  
    2129               print WIND_SQLFILE "VALUES (";  
    2130               print WIND_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    2131               print WIND_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$k].'\''; 
    2132               if ($wind_speed[$i]{'height'} == $missing_value_value 
    2133                 || $wind_speed[$i]{'height'} == $Fill_value_value) { 
    2134                 print WIND_SQLFILE ','.'\'\''; 
    2135               } 
    2136               else { 
    2137                 $this_val = sprintf("%.2f",$wind_speed[$i]{'height'}); 
    2138                 print WIND_SQLFILE ','.$this_val; 
    2139               } 
    2140  
    2141               print WIND_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    2142               $this_val = sprintf("%.2f",$wind_speed[$i]{'data'}[$k]); 
    2143               print WIND_SQLFILE ','.$this_val; 
    2144  
    2145  
    2146               if ($wind_gust[$j]{'data'}[$k] == $missing_value_value 
    2147                 || $wind_gust[$j]{'data'}[$k] == $Fill_value_value 
    2148                 || $wind_gust[$j]{'data'}[$k] == '') { 
    2149                 print WIND_SQLFILE ',NULL'; 
    2150               } 
    2151               else { 
    2152                 $this_val = sprintf("%.2f",$wind_gust[$j]{'data'}[$k]); 
    2153                 print WIND_SQLFILE ','.$this_val; 
    2154               } 
    2155                
    2156               $this_val = sprintf("%.2f",$wind_from_direction[$j]{'data'}[$k]); 
    2157                
    2158               print WIND_SQLFILE ','.$this_val; 
    2159               print WIND_SQLFILE ','.'\''.$wind_speed[$i]{'can_be_normalized'}.'\''; 
    2160               print WIND_SQLFILE ','.'\''.$title_value.'\''; 
    2161               print WIND_SQLFILE ','.'\''.$institution_value.'\''; 
    2162               print WIND_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
    2163               print WIND_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
    2164               print WIND_SQLFILE ','.'\''.$source_value.'\''; 
    2165               print WIND_SQLFILE ','.'\''.$references_value.'\''; 
    2166               print WIND_SQLFILE ','.'\''.$contact_value.'\''; 
    2167               print WIND_SQLFILE ",GeometryFromText('POINT(";  
    2168               print WIND_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    2169               print WIND_SQLFILE ")',-1));\n"; 
    2170                              
    2171             } 
    2172           } 
    2173         } 
    2174         print WIND_SQLFILE "\n"; 
    2175       } 
    2176       close(WIND_SQLFILE); 
    2177     } 
    2178     #DWR 4/5/2008 
    2179     if( $bWriteobsKMLFile ) 
    2180     { 
    2181       for my $i (0..$#wind_speed)  
    2182       { 
    2183         my $j = 0; 
    2184         while ($j <= $#wind_from_direction 
    2185           && $wind_from_direction[$j]{'height'} != $wind_speed[$i]{'height'})  
    2186           { 
    2187           $j++; 
    2188         } 
    2189         if ($j > $#wind_from_direction)  
    2190         { 
    2191           die "ABORT!  Could not find matching wind_from_direction for $wind_speed[$i]  {'var_name'}.\n"; 
    2192         } 
    2193         else  
    2194         { 
    2195           for my $k (0..$#this_wind_speed_data)  
    2196           { 
    2197             $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2198             $this_time_stamp = $time_formatted_values[$k]; 
    2199             $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)); 
    2200              
    2201             my $WindSpdDataVal = 'NULL'; 
    2202             my $WindDirDataVal = 'NULL'; 
    2203             my $WindGstDataVal = 'NULL'; 
    2204             my $Height = ''; 
    2205             if ($wind_speed[$i]{'data'}[$k] != $missing_value_value 
    2206               && $wind_speed[$i]{'data'}[$k] != $Fill_value_value 
    2207               && $wind_from_direction[$j]{'data'}[$k] != $missing_value_value 
    2208               && $wind_from_direction[$j]{'data'}[$k] != $Fill_value_value 
    2209               && $this_station_id_top_ts < $this_time_stamp_sec 
    2210               && $this_time_stamp_sec > $oldest_ok_timestamp)  
    2211               { 
    2212                 if ($wind_speed[$i]{'height'} != $missing_value_value 
    2213                   && $wind_speed[$i]{'height'} != $Fill_value_value)  
    2214                 { 
    2215                   $Height = sprintf("%.2f",$wind_speed[$i]{'height'}); 
    2216                 } 
    2217  
    2218                 $this_val = sprintf("%.2f",$wind_speed[$i]{'data'}[$k]); 
    2219                 if( length( $this_val ) > 0 ) 
    2220                 { 
    2221                   $WindSpdDataVal = $this_val; 
    2222                 } 
    2223  
    2224                 if ($wind_gust[$j]{'data'}[$k] != $missing_value_value 
    2225                     && $wind_gust[$j]{'data'}[$k] != $Fill_value_value 
    2226                     && $wind_gust[$j]{'data'}[$k] != '')  
    2227                 { 
    2228                   $WindGstDataVal = sprintf("%.2f",$wind_gust[$j]{'data'}[$k]); 
    2229                 } 
    2230                  
    2231                 $this_val = sprintf("%.2f",$wind_from_direction[$j]{'data'}[$k]); 
    2232                 #DWR 4/5/2008 
    2233                 if( length( $this_val ) > 0 ) 
    2234                 { 
    2235                   $WindDirDataVal = $this_val; 
    2236                 }               
    2237               } 
    2238               my $strUnits; 
    2239               $strUnits = obsKMLSubRoutines::UnitsStringConversion( $wind_speed[$i]{'units'}, $XMLControlFile );         
    2240               if( length( $strUnits ) == 0 ) 
    2241               { 
    2242                $strUnits = $wind_speed[$i]{'units'}; 
    2243               } 
    2244                
    2245               obsKMLSubRoutines::KMLAddObsToHash( 'wind_speed',  
    2246                                                   $time_formatted_values[$j], 
    2247                                                   $WindSpdDataVal, 
    2248                                                   1, 
    2249                                                   $strPlatformID, 
    2250                                                   $Height, 
    2251                                                   $strUnits, 
    2252                                                   $rObsHash ); 
    2253               $strUnits = ''; 
    2254               $strUnits = obsKMLSubRoutines::UnitsStringConversion( $wind_from_direction[$i]{'units'}, $XMLControlFile );         
    2255               if( length( $strUnits ) == 0 ) 
    2256               { 
    2257                $strUnits = $wind_from_direction[$i]{'units'}; 
    2258               } 
    2259               obsKMLSubRoutines::KMLAddObsToHash( 'wind_from_direction',  
    2260                                                   $time_formatted_values[$j], 
    2261                                                   $WindDirDataVal, 
    2262                                                   1, 
    2263                                                   $strPlatformID, 
    2264                                                   $Height, 
    2265                                                   $strUnits, 
    2266                                                   $rObsHash ); 
    2267               $strUnits = ''; 
    2268               $strUnits = obsKMLSubRoutines::UnitsStringConversion( $wind_gust[$i]{'units'}, $XMLControlFile );         
    2269               if( length( $strUnits ) == 0 ) 
    2270               { 
    2271                $strUnits = $wind_gust[$i]{'units'}; 
    2272               } 
    2273               obsKMLSubRoutines::KMLAddObsToHash( 'wind_gust',  
    2274                                                   $time_formatted_values[$j], 
    2275                                                   $WindGstDataVal, 
    2276                                                   1, 
    2277                                                   $strPlatformID, 
    2278                                                   $Height, 
    2279                                                   $strUnits, 
    2280                                                   $rObsHash ); 
    2281           } 
    2282         } 
    2283       }                                             
    2284     }             
    2285   } 
    2286    
    2287   # air_pressure 
    2288   # Going to do this right this time.  Instead of populating each row w/ all 
    2289   # the metadata, use the station_id lookup, instead. 
    2290   if ($#air_pressure > -1) { 
    2291     if( $bWriteSQLFiles ) 
    2292     { 
    2293       open(AIR_PRESSURE_SQLFILE,'>>../sql/air_pressure_prod_'.$institution_code_value.'_' 
    2294         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    2295       print AIR_PRESSURE_SQLFILE "-- format_category      = $format_category_value\n"; 
    2296       print AIR_PRESSURE_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    2297       print AIR_PRESSURE_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    2298       print AIR_PRESSURE_SQLFILE "-- package_code         = $package_code_value\n"; 
    2299       print AIR_PRESSURE_SQLFILE "-- title                = $title_value\n"; 
    2300       print AIR_PRESSURE_SQLFILE "-- institution          = $institution_value\n"; 
    2301       print AIR_PRESSURE_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    2302       print AIR_PRESSURE_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    2303       print AIR_PRESSURE_SQLFILE "-- source               = $source_value\n"; 
    2304       print AIR_PRESSURE_SQLFILE "-- references           = $references_value\n"; 
    2305       print AIR_PRESSURE_SQLFILE "-- contact              = $contact_value\n"; 
    2306       print AIR_PRESSURE_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    2307       print AIR_PRESSURE_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    2308       for my $i (0..$#air_pressure) { 
    2309         for my $j (0..$#this_air_pressure_data) { 
    2310           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2311           $this_time_stamp = $time_formatted_values[$j]; 
    2312           $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)); 
    2313            
    2314           if ($air_pressure[$i]{'data'}[$j] != $missing_value_value 
    2315             && $air_pressure[$i]{'data'}[$j] != $Fill_value_value 
    2316             && $this_station_id_top_ts < $this_time_stamp_sec 
    2317             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    2318             print AIR_PRESSURE_SQLFILE "INSERT INTO air_pressure_prod (";  
    2319             print AIR_PRESSURE_SQLFILE "station_id,"; 
    2320             print AIR_PRESSURE_SQLFILE "time_stamp,";  
    2321             print AIR_PRESSURE_SQLFILE "z,"; 
    2322             print AIR_PRESSURE_SQLFILE "positive,"; 
    2323             print AIR_PRESSURE_SQLFILE "pressure,"; 
    2324             print AIR_PRESSURE_SQLFILE "the_geom"; 
    2325             print AIR_PRESSURE_SQLFILE ") ";  
    2326             print AIR_PRESSURE_SQLFILE "VALUES (";  
    2327             print AIR_PRESSURE_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    2328             print AIR_PRESSURE_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    2329             if ($air_pressure[$i]{'height'} == $missing_value_value 
    2330               || $air_pressure[$i]{'height'} == $Fill_value_value) { 
    2331               print AIR_PRESSURE_SQLFILE ',NULL'; 
    2332             } 
    2333             else { 
    2334               $this_val = sprintf("%.2f",$air_pressure[$i]{'height'}); 
    2335               print AIR_PRESSURE_SQLFILE ','.$this_val; 
    2336             } 
    2337              
    2338             print AIR_PRESSURE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    2339             $this_val = sprintf("%.2f",$air_pressure[$i]{'data'}[$j]); 
    2340              
    2341             print AIR_PRESSURE_SQLFILE ','.$this_val; 
    2342             print AIR_PRESSURE_SQLFILE ",GeometryFromText('POINT(";  
    2343             print AIR_PRESSURE_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    2344             print AIR_PRESSURE_SQLFILE ")',-1));\n"; 
    2345           } 
    2346            
    2347         } 
    2348         print AIR_PRESSURE_SQLFILE "\n"; 
    2349       } 
    2350       close(AIR_PRESSURE_SQLFILE); 
    2351     } 
    2352     #DWR 4/5/2008 
    2353     if( $bWriteobsKMLFile ) 
    2354     { 
    2355       my $DataVal = 'NULL'; 
    2356       my $Height = ''; 
    2357       for my $i (0..$#air_pressure)  
    2358       { 
    2359         for my $j (0..$#this_air_pressure_data)  
    2360         { 
    2361           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2362           $this_time_stamp = $time_formatted_values[$j]; 
    2363           $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)); 
    2364            
    2365           my $DataVal = 'NULL'; 
    2366           my $Height = ''; 
    2367           if ($air_pressure[$i]{'data'}[$j] != $missing_value_value 
    2368             && $air_pressure[$i]{'data'}[$j] != $Fill_value_value 
    2369             && $this_station_id_top_ts < $this_time_stamp_sec 
    2370             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    2371             if ($air_pressure[$i]{'height'} != $missing_value_value 
    2372               && $air_pressure[$i]{'height'} != $Fill_value_value)  
    2373             { 
    2374               $Height = sprintf("%.2f",$air_pressure[$i]{'height'}); 
    2375             } 
    2376             $DataVal = sprintf("%.2f",$air_pressure[$i]{'data'}[$j]); 
    2377           }            
    2378           #DWR 4/3/2008 
    2379           my $strUnits; 
    2380           $strUnits = obsKMLSubRoutines::UnitsStringConversion( $air_pressure[$i]{'units'}, $XMLControlFile );  
    2381           if( length( $strUnits ) == 0 ) 
    2382           { 
    2383            $strUnits =  $air_pressure[$i]{'units'}; 
    2384           } 
    2385                   
    2386           obsKMLSubRoutines::KMLAddObsToHash( 'air_pressure',  
    2387                                               $time_formatted_values[$j], 
    2388                                               $DataVal, 
    2389                                               1, 
    2390                                               $strPlatformID, 
    2391                                               $Height, 
    2392                                               $strUnits, 
    2393                                               $rObsHash ); 
    2394         } 
    2395       }                                             
    2396     }         
    2397   } 
    2398    
    2399   # salinity 
    2400   # Going to do this right this time.  Instead of populating each row w/ all 
    2401   # the metadata, use the station_id lookup, instead. 
    2402   if ($#salinity > -1) { 
    2403     if( $bWriteSQLFiles ) 
    2404     { 
    2405       open(SALINITY_SQLFILE,'>>../sql/salinity_prod_'.$institution_code_value.'_' 
    2406         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    2407       print SALINITY_SQLFILE "-- format_category      = $format_category_value\n"; 
    2408       print SALINITY_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    2409       print SALINITY_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    2410       print SALINITY_SQLFILE "-- package_code         = $package_code_value\n"; 
    2411       print SALINITY_SQLFILE "-- title                = $title_value\n"; 
    2412       print SALINITY_SQLFILE "-- institution          = $institution_value\n"; 
    2413       print SALINITY_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    2414       print SALINITY_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    2415       print SALINITY_SQLFILE "-- source               = $source_value\n"; 
    2416       print SALINITY_SQLFILE "-- references           = $references_value\n"; 
    2417       print SALINITY_SQLFILE "-- contact              = $contact_value\n"; 
    2418       print SALINITY_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    2419       print SALINITY_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    2420       for my $i (0..$#salinity) { 
    2421         for my $j (0..$#this_salinity_data) { 
    2422           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2423           $this_time_stamp = $time_formatted_values[$j]; 
    2424           $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)); 
    2425            
    2426           #print "debug: $salinity[$i]{'data'}[$k]\n";             
    2427           if ($salinity[$i]{'data'}[$j] != $missing_value_value 
    2428             && $salinity[$i]{'data'}[$j] != $Fill_value_value 
    2429             && $this_station_id_top_ts < $this_time_stamp_sec 
    2430             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    2431             print SALINITY_SQLFILE "INSERT INTO salinity_prod (";  
    2432             print SALINITY_SQLFILE "station_id,"; 
    2433             print SALINITY_SQLFILE "time_stamp,";  
    2434             print SALINITY_SQLFILE "z,"; 
    2435             print SALINITY_SQLFILE "positive,"; 
    2436             print SALINITY_SQLFILE "salinity,"; 
    2437             print SALINITY_SQLFILE "the_geom"; 
    2438             print SALINITY_SQLFILE ") ";  
    2439             print SALINITY_SQLFILE "VALUES (";  
    2440             print SALINITY_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    2441             print SALINITY_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    2442             if ($salinity[$i]{'height'} == $missing_value_value 
    2443               || $salinity[$i]{'height'} == $Fill_value_value) { 
    2444               print SALINITY_SQLFILE ',NULL'; 
    2445             } 
    2446             else { 
    2447               $this_val = sprintf("%.2f",$salinity[$i]{'height'}); 
    2448               print SALINITY_SQLFILE ','.$this_val; 
    2449             } 
    2450              
    2451             print SALINITY_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    2452             $this_val = sprintf("%.2f",$salinity[$i]{'data'}[$j]); 
    2453              
    2454             print SALINITY_SQLFILE ','.$this_val; 
    2455             print SALINITY_SQLFILE ",GeometryFromText('POINT(";  
    2456             print SALINITY_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    2457             print SALINITY_SQLFILE ")',-1));\n"; 
    2458           }           
    2459         } 
    2460         print SALINITY_SQLFILE "\n"; 
    2461       } 
    2462       close(SALINITY_SQLFILE); 
    2463     } 
    2464     #DWR 4/5/2008 
    2465     if( $bWriteobsKMLFile ) 
    2466     { 
    2467       my $DataVal = 'NULL'; 
    2468       my $Height = ''; 
    2469       for my $i (0..$#salinity)  
    2470       { 
    2471         for my $j (0..$#this_salinity_data)  
    2472         { 
    2473           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2474           $this_time_stamp = $time_formatted_values[$j]; 
    2475           $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)); 
    2476            
    2477           my $DataVal = 'NULL'; 
    2478           my $Height = ''; 
    2479           if ($salinity[$i]{'data'}[$j] != $missing_value_value 
    2480               && $salinity[$i]{'data'}[$j] != $Fill_value_value 
    2481               && $this_station_id_top_ts < $this_time_stamp_sec 
    2482               && $this_time_stamp_sec > $oldest_ok_timestamp)  
    2483           { 
    2484             if ($salinity[$i]{'height'} != $missing_value_value 
    2485               && $salinity[$i]{'height'} != $Fill_value_value)  
    2486             { 
    2487               $Height = sprintf("%.2f",$salinity[$i]{'height'}); 
    2488             } 
    2489             $DataVal = sprintf("%.2f",$salinity[$i]{'data'}[$j]); 
    2490           }            
    2491           my $strUnits; 
    2492           if( lc( $salinity[$i]{'units'} ) eq 'ppt' ) 
    2493           { 
    2494             $strUnits =  'psu';         
    2495           } 
    2496           if( length( $strUnits ) == 0 ) 
    2497           { 
    2498            $strUnits = $salinity[$i]{'units'}; 
    2499           } 
    2500            
    2501           obsKMLSubRoutines::KMLAddObsToHash( 'salinity',  
    2502                                               $time_formatted_values[$j], 
    2503                                               $DataVal, 
    2504                                               1, 
    2505                                               $strPlatformID, 
    2506                                               $Height, 
    2507                                               $strUnits, 
    2508                                               $rObsHash ); 
    2509         } 
    2510       }                                             
    2511     }             
    2512   } 
    2513    
    2514     # sea_surface_eastward_current and sea_surface_northward_current 
    2515     # Start w/ sea_surface_eastward_current and then look through the sea_surface_northward_current 
    2516     # to find its pair by looking at the heights.  (This is overkill for sea_surface_currents. 
    2517     # Assume that sea_surface_eastward_current controls everything index-wise. 
    2518     if ($#sea_surface_eastward_current > -1)  
    2519     { 
    2520       if( $bWriteSQLFiles ) 
    2521       { 
    2522         open(CURRENT_IN_SITU_SQLFILE,'>>../sql/current_in_situ_prod_'.$institution_code_value.'_' 
    2523           .$platform_code_value.'_'.$package_code_value.'.sql'); 
    2524         print CURRENT_IN_SITU_SQLFILE "-- format_category      = $format_category_value\n"; 
    2525         print CURRENT_IN_SITU_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    2526         print CURRENT_IN_SITU_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    2527         print CURRENT_IN_SITU_SQLFILE "-- package_code         = $package_code_value\n"; 
    2528         print CURRENT_IN_SITU_SQLFILE "-- title                = $title_value\n"; 
    2529         print CURRENT_IN_SITU_SQLFILE "-- institution          = $institution_value\n"; 
    2530         print CURRENT_IN_SITU_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    2531         print CURRENT_IN_SITU_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    2532         print CURRENT_IN_SITU_SQLFILE "-- source               = $source_value\n"; 
    2533         print CURRENT_IN_SITU_SQLFILE "-- references           = $references_value\n"; 
    2534         print CURRENT_IN_SITU_SQLFILE "-- contact              = $contact_value\n"; 
    2535         print CURRENT_IN_SITU_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    2536         print CURRENT_IN_SITU_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    2537         for my $i (0..$#sea_surface_eastward_current) { 
    2538           my $j = 0; 
    2539           while ($j <= $#sea_surface_northward_current 
    2540             && $sea_surface_northward_current[$j]{'height'} != $sea_surface_eastward_current[$i]{'height'}) { 
    2541             $j++; 
    2542           } 
    2543           if ($j > $#sea_surface_northward_current) { 
    2544             die "ABORT!  Could not find matching sea_surface_northward_current for $sea_surface_eastward_current[$i]  {'var_name'}.\n"; 
    2545           } 
    2546           else { 
    2547             for my $k (0..$#this_sea_surface_eastward_current_data) { 
    2548               $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2549               $this_time_stamp = $time_formatted_values[$k]; 
    2550               $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)); 
    2551                            
    2552         #print "debug: $sea_surface_eastward_current[$i]{'data'}[$k]\n";             
    2553               if ($sea_surface_eastward_current[$i]{'data'}[$k] != $missing_value_value 
    2554                 && $sea_surface_eastward_current[$i]{'data'}[$k] != $Fill_value_value 
    2555                 && $sea_surface_northward_current[$j]{'data'}[$k] != $missing_value_value 
    2556                 && $sea_surface_northward_current[$j]{'data'}[$k] != $Fill_value_value 
    2557                 && $this_station_id_top_ts < $this_time_stamp_sec 
    2558                 && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    2559                 print CURRENT_IN_SITU_SQLFILE "INSERT INTO current_in_situ_prod (";  
    2560                 print CURRENT_IN_SITU_SQLFILE "station_id,"; 
    2561                 print CURRENT_IN_SITU_SQLFILE "time_stamp,";  
    2562                 print CURRENT_IN_SITU_SQLFILE "z,"; 
    2563                 print CURRENT_IN_SITU_SQLFILE "positive,"; 
    2564                 print CURRENT_IN_SITU_SQLFILE "eastward_current,"; 
    2565                 print CURRENT_IN_SITU_SQLFILE "northward_current,"; 
    2566                 print CURRENT_IN_SITU_SQLFILE "surface_or_bottom,"; 
    2567                 print CURRENT_IN_SITU_SQLFILE "the_geom"; 
    2568                 print CURRENT_IN_SITU_SQLFILE ") ";  
    2569                 print CURRENT_IN_SITU_SQLFILE "VALUES (";  
    2570                 print CURRENT_IN_SITU_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    2571                 print CURRENT_IN_SITU_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$k].'\''; 
    2572                 if ($sea_surface_eastward_current[$i]{'height'} == $missing_value_value 
    2573                   || $sea_surface_eastward_current[$i]{'height'} == $Fill_value_value) { 
    2574                   print CURRENT_IN_SITU_SQLFILE ','.'\'\''; 
    2575                 } 
    2576                 else { 
    2577                   $this_val = sprintf("%.2f",$sea_surface_eastward_current[$i]{'height'}); 
    2578                   print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
    2579                    
    2580                 } 
    2581                 print CURRENT_IN_SITU_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    2582                 $this_val = sprintf("%.2f",$sea_surface_eastward_current[$i]{'data'}[$k]); 
    2583                  
    2584                 print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
    2585                 $this_val = sprintf("%.2f",$sea_surface_northward_current[$j]{'data'}[$k]); 
    2586  
    2587                 print CURRENT_IN_SITU_SQLFILE ','.$this_val; 
    2588                 print CURRENT_IN_SITU_SQLFILE ','.'\'surface\''; 
    2589                 print CURRENT_IN_SITU_SQLFILE ",GeometryFromText('POINT(";  
    2590                 print CURRENT_IN_SITU_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    2591                 print CURRENT_IN_SITU_SQLFILE ")',-1));\n"; 
    2592               } 
    2593                
    2594             } 
    2595           } 
    2596           print CURRENT_IN_SITU_SQLFILE "\n"; 
    2597         } 
    2598         close(CURRENT_IN_SITU_SQLFILE); 
    2599       } 
    2600       #DWR 4/5/2008 
    2601       if( $bWriteobsKMLFile ) 
    2602       { 
    2603         for my $i (0..$#sea_surface_eastward_current)  
    2604         { 
    2605           my $j = 0; 
    2606           while ($j <= $#sea_surface_northward_current 
    2607                 && $sea_surface_northward_current[$j]{'height'} != $sea_surface_eastward_current[$i]{'height'})  
    2608           { 
    2609             $j++; 
    2610           } 
    2611           if ($j > $#sea_surface_northward_current)  
    2612           { 
    2613             die "ABORT!  Could not find matching sea_surface_northward_current for $sea_surface_eastward_current[$i]  {'var_name'}.\n"; 
    2614           } 
    2615           else  
    2616           { 
    2617             for my $k (0..$#this_sea_surface_eastward_current_data)  
    2618             { 
    2619               $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2620               $this_time_stamp = $time_formatted_values[$k]; 
    2621               $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)); 
    2622                
    2623               my $ECurrentDataVal = 'NULL'; 
    2624               my $NCurrentDataVal = 'NULL'; 
    2625               my $Height = '';             
    2626                
    2627               if ($sea_surface_eastward_current[$i]{'data'}[$k] != $missing_value_value 
    2628                   && $sea_surface_eastward_current[$i]{'data'}[$k] != $Fill_value_value 
    2629                   && $sea_surface_northward_current[$j]{'data'}[$k] != $missing_value_value 
    2630                   && $sea_surface_northward_current[$j]{'data'}[$k] != $Fill_value_value 
    2631                   && $this_station_id_top_ts < $this_time_stamp_sec 
    2632                   && $this_time_stamp_sec > $oldest_ok_timestamp)  
    2633               { 
    2634                 if ($sea_surface_eastward_current[$i]{'height'} != $missing_value_value 
    2635                     && $sea_surface_eastward_current[$i]{'height'} != $Fill_value_value)  
    2636                 { 
    2637                   $Height = sprintf("%.2f",$sea_surface_eastward_current[$i]{'height'});                 
    2638                 } 
    2639                 $ECurrentDataVal = sprintf("%.2f",$sea_surface_eastward_current[$i]{'data'}[$k]); 
    2640                  
    2641                 $NCurrentDataVal = sprintf("%.2f",$sea_surface_northward_current[$j]{'data'}[$k]); 
    2642  
    2643               } 
    2644               my $strUnits; 
    2645               $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_surface_eastward_current[$i]{'units'}, $XMLControlFile );         
    2646               if( length( $strUnits ) == 0 ) 
    2647               { 
    2648                $strUnits = $sea_surface_eastward_current[$i]{'units'}; 
    2649               } 
    2650                
    2651               obsKMLSubRoutines::KMLAddObsToHash( 'eastward_current',  
    2652                                                   $time_formatted_values[$j], 
    2653                                                   $ECurrentDataVal, 
    2654                                                   1, 
    2655                                                   $strPlatformID, 
    2656                                                   $Height, 
    2657                                                   $strUnits, 
    2658                                                   $rObsHash ); 
    2659               $strUnits = ''; 
    2660               $strUnits = obsKMLSubRoutines::UnitsStringConversion( $sea_surface_northward_current[$i]{'units'}, $XMLControlFile );         
    2661               if( length( $strUnits ) == 0 ) 
    2662               { 
    2663                $strUnits = $sea_surface_northward_current[$i]{'units'}; 
    2664               } 
    2665               obsKMLSubRoutines::KMLAddObsToHash( 'northward_current',  
    2666                                                   $time_formatted_values[$j], 
    2667                                                   $NCurrentDataVal, 
    2668                                                   1, 
    2669                                                   $strPlatformID, 
    2670                                                   $Height, 
    2671                                                   $strUnits, 
    2672                                                   $rObsHash ); 
    2673           } 
    2674         }                                             
    2675       }                   
    2676     } 
    2677   } 
    2678    
    2679   # significant_wave_height and dominant_wave_period (assume they come as pairs -- 
    2680   # wave_height calls the shots over period 
    2681   # Going to do this right this time.  Instead of populating each row w/ all 
    2682   # the metadata, use the station_id lookup, instead. 
    2683   if ($#significant_wave_height > -1) { 
    2684     if( $bWriteSQLFiles ) 
    2685     { 
    2686       open(WAVE_SQLFILE,'>>../sql/wave_in_situ_prod_'.$institution_code_value.'_' 
    2687         .$platform_code_value.'_'.$package_code_value.'.sql'); 
    2688       print WAVE_SQLFILE "-- format_category      = $format_category_value\n"; 
    2689       print WAVE_SQLFILE "-- institution_code     = $institution_code_value\n"; 
    2690       print WAVE_SQLFILE "-- platform_code        = $platform_code_value\n"; 
    2691       print WAVE_SQLFILE "-- package_code         = $package_code_value\n"; 
    2692       print WAVE_SQLFILE "-- title                = $title_value\n"; 
    2693       print WAVE_SQLFILE "-- institution          = $institution_value\n"; 
    2694       print WAVE_SQLFILE "-- institution_url      = $institution_url_value\n"; 
    2695       print WAVE_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
    2696       print WAVE_SQLFILE "-- source               = $source_value\n"; 
    2697       print WAVE_SQLFILE "-- references           = $references_value\n"; 
    2698       print WAVE_SQLFILE "-- contact              = $contact_value\n"; 
    2699       print WAVE_SQLFILE "-- missing_value        = $missing_value_value\n"; 
    2700       print WAVE_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    2701       for my $i (0..$#significant_wave_height) { 
    2702         for my $j (0..$#this_significant_wave_height_data) { 
    2703           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2704           $this_time_stamp = $time_formatted_values[$j]; 
    2705           $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)); 
    2706            
    2707           my $DomWaveDataVal = 'NULL'; 
    2708           my $SigWaveHgtDataVal = 'NULL'; 
    2709           my $Height = ''; 
    2710            
    2711           if ($significant_wave_height[$i]{'data'}[$j] != $missing_value_value 
    2712             && $significant_wave_height[$i]{'data'}[$j] != $Fill_value_value 
    2713             && $this_station_id_top_ts < $this_time_stamp_sec 
    2714             && $this_time_stamp_sec > $oldest_ok_timestamp) { 
    2715             print WAVE_SQLFILE "INSERT INTO wave_in_situ_prod (";  
    2716             print WAVE_SQLFILE "station_id,"; 
    2717             print WAVE_SQLFILE "time_stamp,";  
    2718             print WAVE_SQLFILE "z,"; 
    2719             print WAVE_SQLFILE "positive,"; 
    2720             print WAVE_SQLFILE "significant_wave_height,"; 
    2721             print WAVE_SQLFILE "dominant_wave_period,"; 
    2722             print WAVE_SQLFILE "the_geom"; 
    2723             print WAVE_SQLFILE ") ";  
    2724             print WAVE_SQLFILE "VALUES (";  
    2725             print WAVE_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
    2726             print WAVE_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
    2727             if ($significant_wave_height[$i]{'height'} == $missing_value_value 
    2728               || $significant_wave_height[$i]{'height'} == $Fill_value_value) { 
    2729               print WAVE_SQLFILE ',NULL'; 
    2730             } 
    2731             else { 
    2732               $this_val = sprintf("%.2f",$significant_wave_height[$i]{'height'}); 
    2733               print WAVE_SQLFILE ','.$this_val; 
    2734  
    2735               #DWR 4/5/2008 
    2736               $Height = $this_val; 
    2737             } 
    2738             print WAVE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    2739             if ($significant_wave_height[$i]{'data'}[$j] == $missing_value_value 
    2740               || $significant_wave_height[$i]{'data'}[$j] == $Fill_value_value 
    2741               || $significant_wave_height[$i]{'data'}[$j] == '') { 
    2742               print WAVE_SQLFILE ',NULL'; 
    2743             } 
    2744             else { 
    2745               $this_val = sprintf("%.2f",$significant_wave_height[$i]{'data'}[$j]); 
    2746               print WAVE_SQLFILE ','.$this_val; 
    2747               #DWR 4/5/2008 
    2748               $SigWaveHgtDataVal = $this_val; 
    2749             } 
    2750             if ($dominant_wave_period[$i]{'data'}[$j] == $missing_value_value 
    2751               || $dominant_wave_period[$i]{'data'}[$j] == $Fill_value_value 
    2752               || $dominant_wave_period[$i]{'data'}[$j] == '') { 
    2753               print WAVE_SQLFILE ',NULL'; 
    2754             } 
    2755             else { 
    2756               $this_val = sprintf("%.2f",$dominant_wave_period[$i]{'data'}[$j]); 
    2757               print WAVE_SQLFILE ','.$this_val; 
    2758               #DWR 4/5/2008 
    2759               $DomWaveDataVal = $this_val; 
    2760             } 
    2761             print WAVE_SQLFILE ",GeometryFromText('POINT(";  
    2762             print WAVE_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
    2763             print WAVE_SQLFILE ")',-1));\n"; 
    2764           } 
    2765         } 
    2766         print WAVE_SQLFILE "\n"; 
    2767         #DWR 4/3/2008 
    2768         my $strUnits; 
    2769         $strUnits = obsKMLSubRoutines::UnitsStringConversion( $dominant_wave_period[$i]{'units'}, $XMLControlFile );       
    2770         if( length( $strUnits ) == 0 ) 
    2771         { 
    2772            $strUnits = $dominant_wave_period[$i]{'units'}; 
    2773         } 
    2774            
    2775         obsKMLSubRoutines::KMLAddObsToHash( 'dominant_wave_period',  
    2776                                             $time_formatted_values[$j], 
    2777                                             $DomWaveDataVal, 
    2778                                             1, 
    2779                                             $strPlatformID, 
    2780                                             $Height, 
    2781                                             $strUnits, 
    2782                                             $rObsHash ); 
    2783         $strUnits = ''; 
    2784         $strUnits = obsKMLSubRoutines::UnitsStringConversion( $significant_wave_height[$i]{'units'}, $XMLControlFile );         
    2785         if( length( $strUnits ) == 0 ) 
    2786         { 
    2787            $strUnits = $significant_wave_height[$i]{'units'}; 
    2788         } 
    2789         obsKMLSubRoutines::KMLAddObsToHash( 'significant_wave_height',  
    2790                                             $time_formatted_values[$j], 
    2791                                             $SigWaveHgtDataVal, 
    2792                                             1, 
    2793                                             $strPlatformID, 
    2794                                             $Height, 
    2795                                             $strUnits, 
    2796                                             $rObsHash ); 
    2797          
    2798       } 
    2799       close(WAVE_SQLFILE); 
    2800     } 
    2801     #DWR 4/5/2008 
    2802     if( $bWriteobsKMLFile ) 
    2803     { 
    2804       for my $i (0..$#significant_wave_height)  
    2805       { 
    2806         for my $j (0..$#this_significant_wave_height_data)  
    2807         { 
    2808           $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
    2809           $this_time_stamp = $time_formatted_values[$j]; 
    2810           $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)); 
    2811            
    2812           my $DomWaveDataVal = 'NULL'; 
    2813           my $SigWaveHgtDataVal = 'NULL'; 
    2814           my $Height = ''; 
    2815            
    2816           if ($significant_wave_height[$i]{'data'}[$j] != $missing_value_value 
    2817               && $significant_wave_height[$i]{'data'}[$j] != $Fill_value_value 
    2818               && $this_station_id_top_ts < $this_time_stamp_sec 
    2819               && $this_time_stamp_sec > $oldest_ok_timestamp)  
    2820           { 
    2821             if ($significant_wave_height[$i]{'height'} != $missing_value_value 
    2822                 && $significant_wave_height[$i]{'height'} != $Fill_value_value) 
    2823             { 
    2824               $Height = sprintf("%.2f",$significant_wave_height[$i]{'height'}); 
    2825             } 
    2826             print WAVE_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
    2827             if ($significant_wave_height[$i]{'data'}[$j] == $missing_value_value 
    2828                 || $significant_wave_height[$i]{'data'}[$j] == $Fill_value_value 
    2829                 || $significant_wave_height[$i]{'data'}[$j] == '')  
    2830             { 
    2831               $SigWaveHgtDataVal = sprintf("%.2f",$significant_wave_height[$i]{'data'}[$j]); 
    2832             } 
    2833             if ($dominant_wave_period[$i]{'data'}[$j] == $missing_value_value 
    2834               || $dominant_wave_period[$i]{'data'}[$j] == $Fill_value_value 
    2835               || $dominant_wave_period[$i]{'data'}[$j] == '')  
    2836             { 
    2837               $DomWaveDataVal = sprintf("%.2f",$dominant_wave_period[$i]{'data'}[$j]); 
    2838             } 
    2839           } 
    2840         } 
    2841         my $strUnits; 
    2842         $strUnits = obsKMLSubRoutines::UnitsStringConversion( $dominant_wave_period[$i]{'units'}, $XMLControlFile );       
    2843         if( length( $strUnits ) == 0 ) 
    2844         { 
    2845            $strUnits = $dominant_wave_period[$i]{'units'}; 
    2846         } 
    2847            
    2848         obsKMLSubRoutines::KMLAddObsToHash( 'dominant_wave_period',  
    2849                                             $time_formatted_values[$j], 
    2850                                             $DomWaveDataVal, 
    2851                                             1, 
    2852                                             $strPlatformID, 
    2853                                             $Height, 
    2854                                             $strUnits, 
    2855                                             $rObsHash ); 
    2856         $strUnits = ''; 
    2857         $strUnits = obsKMLSubRoutines::UnitsStringConversion( $significant_wave_height[$i]{'units'}, $XMLControlFile );         
    2858         if( length( $strUnits ) == 0 ) 
    2859         { 
    2860            $strUnits = $significant_wave_height[$i]{'units'}; 
    2861         } 
    2862         obsKMLSubRoutines::KMLAddObsToHash( 'significant_wave_height',  
    2863                                             $time_formatted_values[$j], 
    2864                                             $SigWaveHgtDataVal, 
    2865                                             1, 
    2866                                             $strPlatformID, 
    2867                                             $Height, 
    2868                                             $strUnits, 
    2869                                             $rObsHash ); 
    2870          
    2871       } 
    2872     }                     
    2873   } 
    2874   #DWR 4/5/2008 
    2875   if( $bWriteobsKMLFile ) 
    2876   { 
    2877     my $strXMLPath = '';  
    2878     my $strDate = `date  +%Y-%m-%dT%H-%M-%S`; 
    2879     chomp( $strDate );       
    2880     $strXMLPath = "../sql/$strPlatformID-$strDate.kml"; 
    2881     obsKMLSubRoutines::BuildKMLFile( \%ObsHash, $strXMLPath ); 
    2882   } 
    2883 } 
  • scout/branches/cdl_fixed_point.pl

    r170 r172  
    55  # top timestamp 
    66  $this_station_id_top_ts = $_[0]; 
     7  #DWR 4/7/2008 
     8  my $FileCreationOptions = $_[1]; 
     9   
     10  my $bWriteSQLFiles = 1; 
     11  my $bWriteobsKMLFile = 0; 
     12  if( $FileCreationOptions == WRITEKMLONLY ) 
     13  { 
     14   $bWriteSQLFiles = 0; 
     15   $bWriteobsKMLFile = 1; 
     16  } 
     17  elsif( $FileCreationOptions == WRITEBOTH ) 
     18  { 
     19   $bWriteobsKMLFile = 1;    
     20  } 
     21   
     22  #DWR 4/5/2008 
     23  #The XML file that has our obsKML units conversion. WE use it here to convert the units string names from the netcdf 
     24  #file into the strings we use in the obsKML and then in our database. 
     25  my $strUnitsXMLFilename; 
     26  $strUnitsXMLFilename = './UnitsConversion.xml'; 
     27  my $XMLControlFile = XML::LibXML->new->parse_file("$strUnitsXMLFilename"); 
    728   
    829  # 
     
    95116          reference_to_mllw => '', 
    96117          reference_to_msl  => '', 
    97           reference_to_navd88  => '' 
     118          reference_to_navd88  => '', 
     119          units => ''       #DWR 4/5/2008 
    98120        ); 
    99121        push @water_level, {%this_water_level}; 
     
    105127          var_id   => $i, 
    106128          dim_id   => $this_var_dimid[0], 
    107           height   => '' 
     129          height   => '', 
     130          units => ''       #DWR 4/5/2008 
    108131        ); 
    109132        push @sea_surface_temperature, {%this_sea_surface_temperature}; 
     
    115138          var_id   => $i, 
    116139          dim_id   => $this_var_dimid[0], 
    117           height   => '' 
     140          height   => '', 
     141          units    => ''       #DWR 4/5/2008 
    118142        ); 
    119143        push @sea_bottom_temperature, {%this_sea_bottom_temperature}; 
     
    125149          var_id   => $i, 
    126150          dim_id   => $this_var_dimid[0], 
    127           height   => '' 
     151          height   => '', 
     152          units    => ''       #DWR 4/5/2008 
    128153        ); 
    129154        push @air_temperature, {%this_air_temperature}; 
     
    136161          dim_id            => $this_var_dimid[0], 
    137162          height            => '', 
    138           can_be_normalized => '' 
     163          can_be_normalized => '', 
     164          units             => ''       #DWR 4/5/2008 
    139165        ); 
    140166        push @wind_speed, {%this_wind_speed}; 
     
    147173          dim_id            => $this_var_dimid[0], 
    148174          height            => '', 
    149           can_be_normalized => '' 
     175          can_be_normalized => '', 
     176          units             => ''       #DWR 4/5/2008 
    150177        ); 
    151178        push @wind_gust, {%this_wind_gust}; 
     
    158185          dim_id            => $this_var_dimid[0], 
    159186          height            => '', 
    160           can_be_normalized => '' 
     187          can_be_normalized => '', 
     188          units             => ''       #DWR 4/5/2008 
     189           
    161190        ); 
    162191        push @wind_from_direction, {%this_wind_from_direction}; 
     
    168197          var_id   => $i, 
    169198          dim_id   => $this_var_dimid[0], 
    170           height   => '' 
     199          height   => '', 
     200          units    => ''       #DWR 4/5/2008 
    171201        ); 
    172202        push @air_pressure, {%this_air_pressure}; 
     
    178208          var_id   => $i, 
    179209          dim_id   => $this_var_dimid[0], 
    180           height   => '' 
     210          height   => '', 
     211          units    => ''       #DWR 4/5/2008 
    181212        ); 
    182213        push @salinity, {%this_salinity}; 
     
    188219          var_id            => $i, 
    189220          dim_id            => $this_var_dimid[0], 
    190           height            => '' 
     221          height            => '', 
     222          units             => ''       #DWR 4/5/2008 
    191223        ); 
    192224        push @sea_surface_eastward_current, {%this_sea_surface_eastward_current}; 
     
    198230          var_id            => $i, 
    199231          dim_id            => $this_var_dimid[0], 
    200           height            => '' 
     232          height            => '', 
     233          units             => ''       #DWR 4/5/2008 
    201234        ); 
    202235        push @sea_surface_northward_current, {%this_sea_surface_northward_current}; 
     
    208241          var_id            => $i, 
    209242          dim_id            => $this_var_dimid[0], 
    210           height            => '' 
     243          height            => '', 
     244          units             => ''       #DWR 4/5/2008 
    211245        ); 
    212246        push @significant_wave_height, {%this_significant_wave_height}; 
     
    218252          var_id            => $i, 
    219253          dim_id            => $this_var_dimid[0], 
    220           height            => '' 
     254          height            => '', 
     255          units             => ''       #DWR 4/5/2008 
    221256        ); 
    222257        push @dominant_wave_period, {%this_dominant_wave_period}; 
     
    414449      my @dimids  = ''; 
    415450      my $natts   = ''; 
     451 
     452      #DWR 4/5/2008 
     453      my $units_value = ''; 
     454      my $attget = NetCDF::attget($ncid, $water_level[$i]{'var_id'}, \$units_value); 
     455      if ($attget < 0)  
     456      { 
     457       die "ABORT! $water_level[$i]{'var_name'} has no units.\n"; 
     458      } 
     459      $water_level[$i]{'units'} = $units_value; 
     460       
    416461      # we need to loop through the attributes, so find out how many there are 
    417462      my $varinq = NetCDF::varinq($ncid, $water_level[$i]{'var_id'}, 
     
    488533      my @dimids  = ''; 
    489534      my $natts   = ''; 
     535 
     536      #DWR 4/5/2008 
     537      my $units_value = ''; 
     538      my $attget = NetCDF::attget($ncid, $sea_surface_temperature[$i]{'var_id'}, 'units', \$units_value); 
     539      if ($attget < 0)  
     540      { 
     541       die "ABORT! $sea_surface_temperature[$i]{'var_name'} has no units.\n"; 
     542      } 
     543      $sea_surface_temperature[$i]{'units'} = $units_value; 
     544       
    490545      # we need to loop through the attributes, so find out how many there are 
    491546      my $varinq = NetCDF::varinq($ncid, $sea_surface_temperature[$i]{'var_id'}, 
     
    534589      my @dimids  = ''; 
    535590      my $natts   = ''; 
     591 
     592      #DWR 4/5/2008 
     593      my $units_value = ''; 
     594      my $attget = NetCDF::attget($ncid, $sea_bottom_temperature[$i]{'var_id'}, 'units', \$units_value); 
     595      if ($attget < 0)  
     596      { 
     597       die "ABORT! $sea_bottom_temperature[$i]{'var_name'} has no units.\n"; 
     598      } 
     599      $sea_bottom_temperature[$i]{'units'} = $units_value; 
     600 
    536601      # we need to loop through the attributes, so find out how many there are 
    537602      my $varinq = NetCDF::varinq($ncid, $sea_bottom_temperature[$i]{'var_id'}, 
     
    580645      my @dimids  = ''; 
    581646      my $natts   = ''; 
     647       
     648      #DWR 4/5/2008 
     649      my $units_value = ''; 
     650      my $attget = NetCDF::attget($ncid, $air_temperature[$i]{'var_id'}, 'units', \$units_value); 
     651      if ($attget < 0)  
     652      { 
     653       die "ABORT! $air_temperature[$i]{'var_name'} has no units.\n"; 
     654      } 
     655      $air_temperature[$i]{'units'} = $units_value; 
     656       
    582657      # we need to loop through the attributes, so find out how many there are 
    583658      my $varinq = NetCDF::varinq($ncid, $air_temperature[$i]{'var_id'}, 
     
    626701      my @dimids  = ''; 
    627702      my $natts   = ''; 
     703 
     704      #DWR 4/5/2008 
     705      my $units_value = ''; 
     706      my $attget = NetCDF::attget($ncid, $wind_speed[$i]{'var_id'}, 'units', \$units_value); 
     707      if ($attget < 0)  
     708      { 
     709       die "ABORT! $wind_speed[$i]{'var_name'} has no units.\n"; 
     710      } 
     711      $wind_speed[$i]{'units'} = $units_value; 
     712       
     713       
    628714      # we need to loop through the attributes, so find out how many there are 
    629715      my $varinq = NetCDF::varinq($ncid, $wind_speed[$i]{'var_id'}, 
     
    680766      my @dimids  = ''; 
    681767      my $natts   = ''; 
     768       
     769      #DWR 4/5/2008 
     770      my $units_value = ''; 
     771      my $attget = NetCDF::attget($ncid, $wind_gust[$i]{'var_id'}, 'units', \$units_value); 
     772      if ($attget < 0)  
     773      { 
     774       die "ABORT! $wind_gust[$i]{'var_name'} has no units.\n"; 
     775      } 
     776      $wind_gust[$i]{'units'} = $units_value; 
     777 
     778       
    682779      # we need to loop through the attributes, so find out how many there are 
    683780      my $varinq = NetCDF::varinq($ncid, $wind_gust[$i]{'var_id'}, 
     
    724821    } 
    725822    else { 
     823      
     824      #DWR 4/5/2008 
     825      my $units_value = ''; 
     826      my $attget = NetCDF::attget($ncid, $wind_from_direction[$i]{'var_id'}, 'units', \$units_value); 
     827      if ($attget < 0)  
     828      { 
     829       die "ABORT! $wind_from_direction[$i]{'var_name'} has no units.\n"; 
     830      } 
     831      $wind_from_direction[$i]{'units'} = $units_value; 
     832      
    726833      # get all the variable goodies 
    727834      $varget = NetCDF::varget($ncid, $wind_from_direction[$i]{'var_id'}, 
     
    778885    } 
    779886    else { 
     887          
    780888      # get all the variable goodies 
    781889      $varget = NetCDF::varget($ncid, $air_pressure[$i]{'var_id'}, 
     
    801909      $base_units->convert($dest_units,$this_slope,$this_intercept); 
    802910       
     911      #DWR 4/5/2008 
     912      $air_pressure[$i]{'units'} = $units_value; 
     913       
    803914      # we need to loop through the attributes, so find out how many there are 
    804915      my $varinq = NetCDF::varinq($ncid, $air_pressure[$i]{'var_id'}, 
     
    847958    } 
    848959    else { 
     960      
     961      #DWR 4/5/2008 
     962      my $units_value = ''; 
     963      my $attget = NetCDF::attget($ncid, $salinity[$i]{'var_id'}, 'units', \$units_value); 
     964      if ($attget < 0)  
     965      { 
     966       die "ABORT! $salinity[$i]{'var_name'} has no units.\n"; 
     967      } 
     968      $salinity[$i]{'units'} = $units_value; 
     969      
    849970      # get all the variable goodies 
    850971      $varget = NetCDF::varget($ncid, $salinity[$i]{'var_id'}, 
     
    8931014      die "ABORT!  $sea_surface_eastward_current[$i]{'var_name'} has wrong time dimension.\n"; 
    8941015    } 
    895     else { 
     1016    else {              
    8961017      # get all the variable goodies 
    8971018      $varget = NetCDF::varget($ncid, $sea_surface_eastward_current[$i]{'var_id'}, 
     
    9161037      my $dest_units = UDUNITS::scan('m s-1'); 
    9171038      $base_units->convert($dest_units,$this_slope,$this_intercept); 
    918        
     1039 
     1040      #DWR 4/5/2008 
     1041      $sea_surface_eastward_current[$i]{'units'} = $units_value; 
     1042 
    9191043      # we need to loop through the attributes, so find out how many there are 
    9201044      my $varinq = NetCDF::varinq($ncid, $sea_surface_eastward_current[$i]{'var_id'}, 
     
    9571081    } 
    9581082    else { 
     1083          
    9591084      # get all the variable goodies 
    9601085      $varget = NetCDF::varget($ncid, $sea_surface_northward_current[$i]{'var_id'}, 
     
    9791104      my $dest_units = UDUNITS::scan('m s-1'); 
    9801105      $base_units->convert($dest_units,$this_slope,$this_intercept); 
     1106      #DWR 4/5/2008 
     1107      $sea_surface_northward_current[$i]{'units'} = $units_value; 
    9811108       
    9821109      # we need to loop through the attributes, so find out how many there are 
     
    10201147    } 
    10211148    else { 
     1149           
    10221150      # get all the variable goodies 
    10231151      $varget = NetCDF::varget($ncid, $significant_wave_height[$i]{'var_id'}, 
     
    10421170      my $dest_units = UDUNITS::scan('m'); 
    10431171      $base_units->convert($dest_units,$this_slope,$this_intercept); 
     1172      #DWR 4/5/2008 
     1173      $significant_wave_height[$i]{'units'} = $units_value; 
    10441174       
    10451175      # we need to loop through the attributes, so find out how many there are 
     
    10831213    } 
    10841214    else { 
     1215           
    10851216      # get all the variable goodies 
    10861217      $varget = NetCDF::varget($ncid, $dominant_wave_period[$i]{'var_id'}, 
     
    11051236      my $dest_units = UDUNITS::scan('second'); 
    11061237      $base_units->convert($dest_units,$this_slope,$this_intercept); 
     1238      #DWR 4/5/2008 
     1239      $dominant_wave_period[$i]{'units'} = $units_value; 
    11071240       
    11081241      # we need to loop through the attributes, so find out how many there are 
     
    11471280  # so new stations weren't being created. 
    11481281  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   } 
     1282    if( $bWriteSQLFiles ) 
     1283    { 
     1284      open(STATION_ID_SQLFILE,'>>../sql_in_situ_station_id/in_situ_station_id_'.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'.sql'); 
     1285      print STATION_ID_SQLFILE "-- format_category      = $format_category_value\n"; 
     1286      print STATION_ID_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1287      print STATION_ID_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1288      print STATION_ID_SQLFILE "-- package_code         = $package_code_value\n"; 
     1289      print STATION_ID_SQLFILE "-- title                = $title_value\n"; 
     1290      print STATION_ID_SQLFILE "-- institution          = $institution_value\n"; 
     1291      print STATION_ID_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1292      print STATION_ID_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1293      print STATION_ID_SQLFILE "-- source               = $source_value\n"; 
     1294      print STATION_ID_SQLFILE "-- references           = $references_value\n"; 
     1295      print STATION_ID_SQLFILE "-- contact              = $contact_value\n"; 
     1296      print STATION_ID_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1297      print STATION_ID_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     1298      print STATION_ID_SQLFILE "INSERT INTO in_situ_station_id (";  
     1299      print STATION_ID_SQLFILE "station_id,"; 
     1300      print STATION_ID_SQLFILE "title,"; 
     1301      print STATION_ID_SQLFILE "institution,"; 
     1302      print STATION_ID_SQLFILE "institution_url,"; 
     1303      print STATION_ID_SQLFILE "institution_dods_url,"; 
     1304      print STATION_ID_SQLFILE "source,"; 
     1305      print STATION_ID_SQLFILE "refs,"; 
     1306      print STATION_ID_SQLFILE "contact"; 
     1307      print STATION_ID_SQLFILE ") ";  
     1308      print STATION_ID_SQLFILE "VALUES ("; 
     1309      print STATION_ID_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1310      print STATION_ID_SQLFILE ','.'\''.$title_value.'\''; 
     1311      print STATION_ID_SQLFILE ','.'\''.$institution_value.'\''; 
     1312      print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1313      print STATION_ID_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1314      print STATION_ID_SQLFILE ','.'\''.$source_value.'\''; 
     1315      print STATION_ID_SQLFILE ','.'\''.$references_value.'\''; 
     1316      print STATION_ID_SQLFILE ','.'\''.$contact_value.'\''; 
     1317      print STATION_ID_SQLFILE ");\n"; 
     1318      close(STATION_ID_SQLFILE); 
     1319    } 
     1320  } 
     1321   
     1322  #DWR 4/5/2008 
     1323  #Implemented code to write the obsKML files.  
     1324  my %ObsHash; 
     1325  my $rObsHash = \%ObsHash; 
     1326  my $strPlatformID = $institution_code_value.'.'.$platform_code_value.'.'.$package_code_value; 
     1327  obsKMLSubRoutines::KMLAddPlatformHashEntry( $strPlatformID, $institution_url_value, $latitude_value[0], $longitude_value[0], $rObsHash ); 
    11861328   
    11871329  # 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 ','.'\'\''; 
     1330  if ($#water_level > -1) 
     1331  { 
     1332    if( $bWriteSQLFiles ) 
     1333    { 
     1334      open(WATER_LEVEL_SQLFILE,'>>../sql/water_level_prod_'.$institution_code_value.'_' 
     1335        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     1336      print WATER_LEVEL_SQLFILE "-- format_category      = $format_category_value\n"; 
     1337      print WATER_LEVEL_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1338      print WATER_LEVEL_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1339      print WATER_LEVEL_SQLFILE "-- package_code         = $package_code_value\n"; 
     1340      print WATER_LEVEL_SQLFILE "-- title                = $title_value\n"; 
     1341      print WATER_LEVEL_SQLFILE "-- institution          = $institution_value\n"; 
     1342      print WATER_LEVEL_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1343      print WATER_LEVEL_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1344      print WATER_LEVEL_SQLFILE "-- source               = $source_value\n"; 
     1345      print WATER_LEVEL_SQLFILE "-- references           = $references_value\n"; 
     1346      print WATER_LEVEL_SQLFILE "-- contact              = $contact_value\n"; 
     1347      print WATER_LEVEL_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1348      print WATER_LEVEL_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
     1349      for my $i (0..$#water_level) { 
     1350        for my $j (0..$#this_water_level_data) { 
     1351          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1352          $this_time_stamp = $time_formatted_values[$j]; 
     1353          $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)); 
     1354 
     1355          if ($water_level[$i]{'data'}[$j] != $missing_value_value 
     1356            && $water_level[$i]{'data'}[$j] != $Fill_value_value 
     1357            && $this_station_id_top_ts < $this_time_stamp_sec 
     1358            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     1359            print WATER_LEVEL_SQLFILE "INSERT INTO water_level_prod (";  
     1360            print WATER_LEVEL_SQLFILE "station_id,"; 
     1361            print WATER_LEVEL_SQLFILE "time_stamp,";  
     1362            print WATER_LEVEL_SQLFILE "z,"; 
     1363            print WATER_LEVEL_SQLFILE "positive,"; 
     1364            print WATER_LEVEL_SQLFILE "water_level,"; 
     1365            print WATER_LEVEL_SQLFILE "reference,"; 
     1366            print WATER_LEVEL_SQLFILE "reference_to_mllw,"; 
     1367            print WATER_LEVEL_SQLFILE "reference_to_msl,"; 
     1368            print WATER_LEVEL_SQLFILE "reference_to_navd88,"; 
     1369            print WATER_LEVEL_SQLFILE "title,"; 
     1370            print WATER_LEVEL_SQLFILE "institution,"; 
     1371            print WATER_LEVEL_SQLFILE "institution_url,"; 
     1372            print WATER_LEVEL_SQLFILE "institution_dods_url,"; 
     1373            print WATER_LEVEL_SQLFILE "source,"; 
     1374            print WATER_LEVEL_SQLFILE "refs,"; 
     1375            print WATER_LEVEL_SQLFILE "contact,"; 
     1376            print WATER_LEVEL_SQLFILE "the_geom"; 
     1377            print WATER_LEVEL_SQLFILE ") ";  
     1378            print WATER_LEVEL_SQLFILE "VALUES (";  
     1379            print WATER_LEVEL_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1380            print WATER_LEVEL_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     1381            if ($water_level[$i]{'height'} == $missing_value_value 
     1382              || $water_level[$i]{'height'} == $Fill_value_value) { 
     1383              print WATER_LEVEL_SQLFILE ','.'\'\''; 
     1384            } 
     1385            else { 
     1386              $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
     1387              print WATER_LEVEL_SQLFILE ','.$this_val; 
     1388            } 
     1389            print WATER_LEVEL_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     1390            $this_val = sprintf("%.2f",$water_level[$i]{'data'}[$j]); 
     1391            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1392            print WATER_LEVEL_SQLFILE ','.'\''.$water_level[$i]{'reference'}.'\''; 
     1393             
     1394             
     1395            if (length $water_level[$i]{'reference_to_mllw'} > 0) { 
     1396              $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_mllw'}); 
     1397            } 
     1398            else { 
     1399              $this_val = 'NULL'; 
     1400            } 
     1401 
     1402            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1403            if (length $water_level[$i]{'reference_to_msl'} > 0) { 
     1404              $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_msl'}); 
     1405            } 
     1406            else { 
     1407              $this_val = 'NULL'; 
     1408            } 
     1409 
     1410            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1411            if (length $water_level[$i]{'reference_to_navd88'} > 0) { 
     1412              $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_navd88'}); 
     1413            } 
     1414            else { 
     1415              $this_val = 'NULL'; 
     1416            } 
     1417 
     1418            print WATER_LEVEL_SQLFILE ','.$this_val; 
     1419            print WATER_LEVEL_SQLFILE ','.'\''.$title_value.'\''; 
     1420            print WATER_LEVEL_SQLFILE ','.'\''.$institution_value.'\''; 
     1421            print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1422            print WATER_LEVEL_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1423            print WATER_LEVEL_SQLFILE ','.'\''.$source_value.'\''; 
     1424            print WATER_LEVEL_SQLFILE ','.'\''.$references_value.'\''; 
     1425            print WATER_LEVEL_SQLFILE ','.'\''.$contact_value.'\''; 
     1426            print WATER_LEVEL_SQLFILE ",GeometryFromText('POINT(";  
     1427            print WATER_LEVEL_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     1428            print WATER_LEVEL_SQLFILE ")',-1));\n"; 
    12391429          } 
    1240           else { 
    1241             $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
    1242             print WATER_LEVEL_SQLFILE ','.$this_val; 
     1430        } 
     1431         
     1432        print WATER_LEVEL_SQLFILE "\n"; 
     1433      } 
     1434      close(WATER_LEVEL_SQLFILE); 
     1435    } 
     1436    if( $bWriteobsKMLFile ) 
     1437    {        
     1438      my $MLLWDataVal = 'NULL'; 
     1439      my $MSLDataVal  = 'NULL'; 
     1440      my $NavD88DataVal = 'NULL'; 
     1441      my $Height = ''; 
     1442     
     1443      for my $i (0..$#water_level)  
     1444      { 
     1445        for my $j (0..$#this_water_level_data)  
     1446        { 
     1447          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1448          $this_time_stamp = $time_formatted_values[$j]; 
     1449          $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)); 
     1450 
     1451          if ($water_level[$i]{'data'}[$j] != $missing_value_value 
     1452            && $water_level[$i]{'data'}[$j] != $Fill_value_value 
     1453            && $this_station_id_top_ts < $this_time_stamp_sec 
     1454            && $this_time_stamp_sec > $oldest_ok_timestamp)  
     1455            { 
     1456     
     1457              #DWR 4/3/2008 
     1458              if ($water_level[$i]{'height'} != $missing_value_value 
     1459                && $water_level[$i]{'height'} != $Fill_value_value)  
     1460              { 
     1461                $this_val = sprintf("%.2f",$water_level[$i]{'height'}); 
     1462                $Height = $this_val; 
     1463              } 
     1464              $this_val = sprintf("%.2f",$water_level[$i]{'data'}[$j]); 
     1465               
     1466              $DataVal = $water_level[$i]{'reference'}; 
     1467               
     1468              if (length $water_level[$i]{'reference_to_mllw'} > 0) { 
     1469                $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_mllw'}); 
     1470              } 
     1471              else { 
     1472                $this_val = 'NULL'; 
     1473              } 
     1474              $MLLWDataVal = $this_val; 
     1475 
     1476              if (length $water_level[$i]{'reference_to_msl'} > 0) { 
     1477                $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_msl'}); 
     1478              } 
     1479              else { 
     1480                $this_val = 'NULL'; 
     1481              } 
     1482              $MSLDataVal = $this_val; 
     1483 
     1484              if (length $water_level[$i]{'reference_to_navd88'} > 0) { 
     1485                $this_val = sprintf("%.2f",$water_level[$i]{'reference_to_navd88'}); 
     1486              } 
     1487              else { 
     1488                $this_val = 'NULL'; 
     1489              } 
     1490              #DWR 4/5/2008 
     1491              $NavD88DataVal = $this_val; 
    12431492          } 
    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    
     1493          obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
     1494                                            $time_formatted_values[$j], 
     1495                                            $MLLWDataVal, 
     1496                                            1, 
     1497                                            $strPlatformID, 
     1498                                            $Height, 
     1499                                            'm(MLLW)', 
     1500                                            $rObsHash ); 
     1501          obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
     1502                                            $time_formatted_values[$j], 
     1503                                            $MSLDataVal, 
     1504                                            2, 
     1505                                            $strPlatformID, 
     1506                                            $Height, 
     1507                                            'm(MSL)', 
     1508                                            $rObsHash ); 
     1509          obsKMLSubRoutines::KMLAddObsToHash( 'water_level',  
     1510                                            $time_formatted_values[$j], 
     1511                                            $NavD88DataVal, 
     1512                                            3, 
     1513                                            $strPlatformID, 
     1514                                            $Height, 
     1515                                            'm(NAVD88)', 
     1516                                            $rObsHash ); 
     1517        } 
     1518      }             
     1519    } 
     1520  } 
    12861521  # sea_surface_temperature (sst) 
    12871522  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)); 
     1523    if( $bWriteSQLFiles ) 
     1524    { 
     1525      open(SST_SQLFILE,'>>../sql/sst_prod_'.$institution_code_value.'_' 
     1526        .$platform_code_value.'_'.$package_code_value.'.sql'); 
     1527      print SST_SQLFILE "-- format_category      = $format_category_value\n"; 
     1528      print SST_SQLFILE "-- institution_code     = $institution_code_value\n"; 
     1529      print SST_SQLFILE "-- platform_code        = $platform_code_value\n"; 
     1530      print SST_SQLFILE "-- package_code         = $package_code_value\n"; 
     1531      print SST_SQLFILE "-- title                = $title_value\n"; 
     1532      print SST_SQLFILE "-- institution          = $institution_value\n"; 
     1533      print SST_SQLFILE "-- institution_url      = $institution_url_value\n"; 
     1534      print SST_SQLFILE "-- institution_dods_url = $institution_dods_url_value\n"; 
     1535      print SST_SQLFILE "-- source               = $source_value\n"; 
     1536      print SST_SQLFILE "-- references           = $references_value\n"; 
     1537      print SST_SQLFILE "-- contact              = $contact_value\n"; 
     1538      print SST_SQLFILE "-- missing_value        = $missing_value_value\n"; 
     1539      print SST_SQLFILE "-- _FillValue           = $Fill_value_value\n"; 
    13081540       
    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 ','.'\'\''; 
     1541      my $DataVal = 'NULL'; 
     1542      my $Height = ''; 
     1543      for my $i (0..$#sea_surface_temperature) { 
     1544        for my $j (0..$#this_sea_surface_temperature_data) { 
     1545          $this_station_id = $institution_code_value.'_'.$platform_code_value.'_'.$package_code_value; 
     1546          $this_time_stamp = $time_formatted_values[$j]; 
     1547          $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)); 
     1548         
     1549          if ($sea_surface_temperature[$i]{'data'}[$j] != $missing_value_value 
     1550            && $sea_surface_temperature[$i]{'data'}[$j] != $Fill_value_value 
     1551            && $this_station_id_top_ts < $this_time_stamp_sec 
     1552            && $this_time_stamp_sec > $oldest_ok_timestamp) { 
     1553            print SST_SQLFILE "INSERT INTO sst_prod (";  
     1554            print SST_SQLFILE "station_id,"; 
     1555            print SST_SQLFILE "time_stamp,";  
     1556            print SST_SQLFILE "z,"; 
     1557            print SST_SQLFILE "positive,"; 
     1558            print SST_SQLFILE "temperature_celcius,"; 
     1559            print SST_SQLFILE "title,"; 
     1560            print SST_SQLFILE "institution,"; 
     1561            print SST_SQLFILE "institution_url,"; 
     1562            print SST_SQLFILE "institution_dods_url,"; 
     1563            print SST_SQLFILE "source,"; 
     1564            print SST_SQLFILE "refs,"; 
     1565            print SST_SQLFILE "contact,"; 
     1566            print SST_SQLFILE "the_geom"; 
     1567            print SST_SQLFILE ") ";  
     1568            print SST_SQLFILE "VALUES (";  
     1569            print SST_SQLFILE   '\''.$institution_code_value.'_'.$platform_code_value.'_'.$package_code_value.'\''; 
     1570            print SST_SQLFILE ','.'timestamp without time zone \''.$time_formatted_values[$j].'\''; 
     1571            if ($sea_surface_temperature[$i]{'height'} == $missing_value_value 
     1572              || $sea_surface_temperature[$i]{'height'} == $Fill_value_value) { 
     1573              print SST_SQLFILE ','.'\'\''; 
     1574            } 
     1575            else { 
     1576              $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'height'}); 
     1577              print SST_SQLFILE ','.$this_val; 
     1578            } 
     1579 
     1580            print SST_SQLFILE ','.'\''.$height_var{'positive'}.'\''; 
     1581            $this_val = sprintf("%.2f",$sea_surface_temperature[$i]{'data'}[$j]); 
     1582             
     1583             
     1584            print SST_SQLFILE ','.$this_val; 
     1585            print SST_SQLFILE ','.'\''.$title_value.'\''; 
     1586            print SST_SQLFILE ','.'\''.$institution_value.'\''; 
     1587            print SST_SQLFILE ','.'\''.'<a href='.$institution_url_value.' target=  _blank>'.$institution_url_value.'</a>'.'\''; 
     1588            print SST_SQLFILE ','.'\''.'<a href='.$institution_dods_url_value.' target=  _blank>'.$institution_dods_url_value.'</a>'.'\''; 
     1589            print SST_SQLFILE ','.'\''.$source_value.'\''; 
     1590            print SST_SQLFILE ','.'\''.$references_value.'\''; 
     1591            print SST_SQLFILE ','.'\''.$contact_value.'\''; 
     1592            print SST_SQLFILE ",GeometryFromText('POINT(";  
     1593            print SST_SQLFILE $longitude_value[0].' '.$latitude_value[0];  
     1594            print SST_SQLFILE ")',-1));\n"; 
    13341595          } 
    1335           else { 
    1336             $this_val