#meta_data.pm
#Time-stamp: <2005-03-15 10:47:56 lstearns>
#
#Abstract: This module will read in tables of tower related meta data
#  and output hash arrays of the information based on param id.  Hacked
#  together from the subroutine in tower_parse.perl and perl_defs.pm,
#  a piece of code from Brian Blanton (Thanks Brian!!).

# perl defs and subroutines common to quoddy nc/fc runs
# v11/07/2001 B.O.B.
# v02/09/2002 B.O.B. to work out of dmf
# v06/04/2002 B.O.B. added initial da stuff
# v19/08/2002 B.O.B. fixes for changes to "edu" from "com"
# v13/09/2002 B.O.B. added truxton stuff
# v13/12/2002 B.O.B. version for casco4b inclusion/ ssh assim
# vxx/09/2003 B.O.B. modified for SEACOOS
# v15/02/2004 B.O.B. turned into perl package provider (seacoos_sab_defs)


package meta_data;  # assumes seacoos_sab_defs.pm

use strict;
use warnings;
use vars qw();


BEGIN{
   use Exporter ();
   
   our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);

   $VERSION="1.0";

   @ISA = qw(Exporter);

   @EXPORT = qw(
		%parse_info %parse_output
		%T1_conversion_coeff %T1_conversion_formula
		%LSRB_conversion_coeff %LSRB_conversion_formula
		%R4_conversion_coeff %R4_conversion_formula
		%conversion_coeff %conversion_formula
		%range %precision %derived %units 
		%format %long_name %standard_name 
		%description %sub_vars %dimensions
		%global_format %global_value
                %tower_lon %tower_lat
		@vars @globals
    );

   @EXPORT_OK=();
}


our(%parse_info,%parse_output,
    %conversion_coeff,%conversion_formula,
    %T1_conversion_coeff,%T1_conversion_formula,
    %LSRB_conversion_coeff,%LSRB_conversion_formula,
    %R4_conversion_coeff,%R4_conversion_formula,
    %range,%precision,%derived,%units,
    %format,%long_name,%standard_name,
    %description,%sub_vars,%dimensions,
    %global_format,%global_value,
    %tower_lon,%tower_lat,
    @vars,@globals
   );

my($param_id,$parse_info,$parse_output,
   $conversion_coeff,$conversion_formula,
   $range,$precision,$derived,$units, 
   $format,$long_name,$standard_name,
   $description,$sub_vars,@sub_vars,
   $dimensions,$meta_data_dir,
   $g_a_format,$g_a_value,$g_a_name,
   $tower_id,$tower_lon,$tower_lat
  );

#$meta_data_dir = '/afs/isis.unc.edu/depts/marine/workspace/hseim/'.
#    'sablam4/lstearns/perl/test_nemo/meta_data/';
$meta_data_dir = '/opt/local/seacoos/bin/meta_data/';

######################################################################
### Initialize Info ##################################################
#sub initialize_info {

#$meta_data_dir = "/afs/isis.unc.edu/depts/marine/workspace/hseim/".
#  "sablam3/seacoos/meta_data/";
  #This section is fairly consistant, in fact I'd like to pull it out
  #and put it in a seperate script that gets loaded.  This is to 
  #read in all of the tables of information that relate to parsing, 
  #conversion, and ancillary information about all of the variables.

  ####################################################################
  # Read Parsing Information Table
  open(PARSING_INFO,"${meta_data_dir}parse.table");

  #Loop through the lines of the file
  while (<PARSING_INFO>) {
    #Get rid of the carriage return.
    chop;

    #Ignore comments, that is, any line starting with a '#' character
    #This is done by setting a comment line to an empty line
    $_=~s/^^\#.*//;

    #Consider only non-empty lines
    if ($_) {
      #Split lines of the table using 2 or more spaces (which 
      #allows for single spaces in the entries)
      ($param_id,$parse_info,$parse_output) = split('\s{2,}');
      
      #Store information in hash arrays with the param id as the key
      $parse_info{$param_id}=$parse_info;
      $parse_output{$param_id}=$parse_output;
    }
  }
  close(PARSING_INFO);

  ####################################################################
  # Read Conversions Table
  open(T1_CONVERSION_INFO,"${meta_data_dir}T1_conversions.table");
  while (<T1_CONVERSION_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
      ($param_id,$conversion_coeff,$conversion_formula) = split('\s{2,}');
      $T1_conversion_coeff{$param_id}=$conversion_coeff;
      $T1_conversion_formula{$param_id}=$conversion_formula;
    }
  }
  close(T1_CONVERSION_INFO);

  ####################################################################
  # Read Conversions Table
  open(LSRB_CONVERSION_INFO,"${meta_data_dir}LSRB_conversions.table");
  while (<LSRB_CONVERSION_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
      ($param_id,$conversion_coeff,$conversion_formula) = split('\s{2,}');
      $LSRB_conversion_coeff{$param_id}=$conversion_coeff;
      $LSRB_conversion_formula{$param_id}=$conversion_formula;
    }
  }
  close(LSRB_CONVERSION_INFO);

  ####################################################################
  # Read Conversions Table
  open(R4_CONVERSION_INFO,"${meta_data_dir}R4_conversions.table");
  while (<R4_CONVERSION_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
      ($param_id,$conversion_coeff,$conversion_formula) = split('\s{2,}');
      $R4_conversion_coeff{$param_id}=$conversion_coeff;
      $R4_conversion_formula{$param_id}=$conversion_formula;
    }
  }
  close(R4_CONVERSION_INFO);

  ####################################################################
  # Read Conversions Table
  open(CONVERSION_INFO,"${meta_data_dir}conversions.table");
  while (<CONVERSION_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
      ($param_id,$conversion_coeff,$conversion_formula) = split('\s{2,}');
      $conversion_coeff{$param_id}=$conversion_coeff;
      $conversion_formula{$param_id}=$conversion_formula;
    }
  }
  close(CONVERSION_INFO);

  ####################################################################
  # Read Variables Table
  open(VARIABLE_INFO,"${meta_data_dir}variable.table");
  while (<VARIABLE_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
      ($param_id,$range,$precision,$derived,$units,$format,$dimensions,
       $long_name,$standard_name,$description) = split('\s{2,}');
      $range{$param_id}=$range;
      $precision{$param_id}=$precision;
      $derived{$param_id}=$derived;
      $units{$param_id}=$units;
      $format{$param_id} = $format;
      $dimensions{$param_id} = $dimensions;
      $long_name{$param_id}=$long_name;
      $standard_name{$param_id}=$standard_name;
      $description{$param_id}=$description;
      push(@vars,"$param_id");
    }
  }
  close(VARIABLE_INFO);

  ####################################################################
  # Read Sub_Vars Table
  open(SUB_VAR_INFO,"${meta_data_dir}subvars.table");
  while (<SUB_VAR_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
      ($param_id,$sub_vars) = split('\s{2,}');
      @sub_vars = split(',',$sub_vars);
      @{$sub_vars{$param_id}}=@sub_vars;
    }
  }
  close(SUB_VAR_INFO);

  ####################################################################
  # Read Globals Table
  open(GLOBAL_INFO,"${meta_data_dir}globals.table");
  while (<GLOBAL_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
      ($g_a_name,$g_a_format,$g_a_value) = split('\s{2,}');
      $global_format{$g_a_name}=$g_a_format;
      $global_value{$g_a_name}=$g_a_value;
      push(@globals,"$g_a_name");

    }
  }
  close(GLOBAL_INFO);

  ####################################################################
  # Read Locations Table
  open(LOCATION_INFO,"${meta_data_dir}locations.table");
  while (<LOCATION_INFO>) {
    chop;
    $_=~s/^\#.*//;
    if ($_) {
	($tower_id,$tower_lat,$tower_lon) = split('\s{2,}');
	$tower_lat{$tower_id}=$tower_lat;
	$tower_lon{$tower_id}=$tower_lon;
    }
  }
  close(LOCATION_INFO);

#}



1;

