Changeset 233

Show
Ignore:
Timestamp:
08/26/08 09:07:01
Author:
dan
Message:

--

Files:

Legend:

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

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