#!/usr/bin/perl -w

# In order to run this code, you must be logged in as the supper user. 
# To run the code type in the command prompt: 
# /usr/bin/perl /var/www/cgi-bin/CreFileInfo_2019-03-27.pl

# After you edit this code: put a copy in two places
#	/var/www/cgi-bin/
#	/etc/cron.daily/

use strict;
use Shell qw(ls);
use Carp;
use File::Find;

#-- Declare some global vars
my (%DataNames, %Station, %State, %StationType, %SubStation);
my $RecNo;
my %Interval;
my %DataType;
my %OneElArcDataType;

#	**************************************************************
#	Archival (updtaed format interval) files (1 month period)
#	**************************************************************
sub do_ArchiveUpdatedFormat()
{
    # Set all other fields that are static for this type of file
    my $FileType = "ArchiveUpdatedFormat";
    my $Format = "text";
    my $Path = "/var/www/html/download/ArchiveUpdatedFormat/";
    my $Day = "";

    # Open output file
    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_ArchivalUpdatedFormat.txt") or die "$!";
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
	# my $lsString2 = ls("-1sA", "/var/www/html/download/ArchiveUpdatedFormat/");
	# print STDERR "$lsString2\n";
	# print OUT_HANDLE "$lsString2\n";
	
	# create a blank list of file names to put the data in
	my @listfilelocation = ("");

	# Add the files to the list
	find sub { if(! -d){push @listfilelocation, "$File::Find::name\n"} }, "/var/www/html/download/ArchiveUpdatedFormat/";
	#print STDERR "@listfilelocation\n";
	
	# Sort the list of filenames
	@listfilelocation = sort {$a cmp $b} @listfilelocation;
	#print STDERR "@listfilelocation2\n";

	
	# Send the files to the outhandle
	#print OUT_HANDLE "@listfilelocation\n";

    # Convert string to list of lines
    #my @LineList = split /^/m, $lsString;
	#my @LineList = @listfilelocation;
	#	print STDERR "@LineList\n";

    # Get number of lines
    my $LineCount = @listfilelocation;
	#$LineCount = 5;
	#print STDERR "$LineCount\n";
	
		
		
		

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {

		# Increment record number
        $RecNo++;
	
        # Get the file location of one file
		my $filelocationtemporary = $listfilelocation[$i];
		
				
		#my $filesize = [stat $filelocationtemporary]->[7] or die "Ack $!";
		#$print STDERR "$filesize\n";
		
		
		$filelocationtemporary = substr($filelocationtemporary, 14, -1);
		#$filelocationtemporary = substr($filelocationtemporary, 44, -1);
		#print STDERR "$filelocationtemporary\n";

		#my $filesize = stat("$filelocationtemporary")->size;
		
		

		# Get the file name
		my $filename = (split /\//, $filelocationtemporary)[-1];
#		$filename = substr($filename, 0, -1);
        #print STDERR "$filename\n";



        # We'll assign several FileInfo fields based on the first three chars
        my $station = substr $filename, 0, 3;
		
	
		my $state = "Not Defined";
		my $stationname = "Not Defined";
		my $stationtype = "Not Defined";
		my $datatypes = "Not Defined";
		
		
		if ($station eq "ASO") {
		$state ="OR";
		$stationname = "Ashland";
		$stationtype = "SUB";
		$datatypes = "GHI; DNI; DHI; GHI_Tilt(15,180); Air_Temperature; Wind_Speed; PV_Power_5kW; PV_Power_15kW";
		};
		
		if ($station eq "BUO") {
		$state ="OR";
		$stationname = "Burns";
		$stationtype = "SUB";
		$datatypes = "GHI; DNI; DHI; Air_Temperature";
		};
		
		if ($station eq "DIM") {
		$state ="MT";
		$stationname = "Dillon";
		$stationtype = "SUB";
		$datatypes = "GHI; DNI; DHI; Air_Temperature";
		};
		
		if ($station eq "HEO") {
		$state ="OR";
		$stationname = "Hermiston";
		$stationtype = "SUB";
		$datatypes = "GHI; DNI; DHI; Air_Temperature";
		};
		
		if ($station eq "SIO") {
		$state ="OR";
		$stationname = "Silver Lake";
		$stationtype = "SUB";
		$datatypes = "GHI; DNI; DHI; GHI_Tilt(90,180); Air_Temperature";
		};

		if ($station eq "STW") {
		$state = "WA";
		$stationname = "Seattle_UW";
		$stationtype = "REF";
		$datatypes = "GHI; DNI; DHI; Air_Temperature";
		};
		
		if ($station eq "TFI") {
		$state = "ID";
		$stationname = "Twin Falls";
		$stationtype = "REF";
		$datatypes = "GHI; DNI; DHI; Air_Temperature";
		};
		
		# Define the year from filename
        my $year = substr $filename, 7, 4;
 
		# Define the month from filename
        my $month = substr $filename, 12, 2;

        # Get interval info from filename
       my $timeinterval = substr $filename, 4, 2;
	   

        # Write the information to FileInfo
		print OUT_HANDLE "$RecNo|$FileType|$state|$stationname|$stationtype|substation|$year|$month|day|$timeinterval|$Format|filesize|$filelocationtemporary|$filename|$datatypes\n";
    }
    close OUT_HANDLE;
}



#	**************************************************************
#	Archival (short interval) files (1 month period)
#	**************************************************************
sub do_archival()
{
    # Set all other fields that are static for this type of file
    my $FileType = "Archive";
    my $Format = "text";
    my $Path = "/var/www/html/download/Archive/";
    my $Day = "";

    # Open output file
    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_Archival.txt");
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
    my $lsString = ls("-1sA", "/var/www/html/download/Archive/");

    # Convert string to list of lines
    my @LineList = split /^/m, $lsString;

    # Get number of lines
    my $LineCount = @LineList;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {
        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineList[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }

        # We'll assign several FileInfo fields based on the first two chars
        # of the file name; e.g., $Station{$Nm}
        my $Nm = substr $FileName, 0, 2;

        # Create 4-digit year
        my $Yr = substr $FileName, 4, 2;
        my $Year;
        if ($Yr lt "40") {
            $Year = "20" . $Yr;
        }
        elsif ($Yr gt "60") {
            $Year = "19" . $Yr;
        }

        # Get month from filename
        my $Month = substr $FileName, 6, 2;

        # Get interval info from filename
        my $IntAbbrev = substr $FileName, 3, 1;

        # Get file head information by reading the data file
        open(IN_HANDLE, "</var/www/html/download/Archive/" . $FileName);
        my @Headers;
        if ( defined( my $InRec = <IN_HANDLE> ) ) {
            @Headers = split /\s+/, $InRec;
        }
        close IN_HANDLE;
        my $HeaderCnt = @Headers;
        my $DataTypes = "";
        for (my $j = 0; $j < $HeaderCnt; $j++) {
            if ($j > 2 && $Headers[$j-1] ne "0" && $j < $HeaderCnt-1) {
                $DataTypes .= "; ";
            }
            if (exists $DataNames{$Headers[$j]}) {
                $DataTypes .= $DataNames{$Headers[$j]};
            }
            elsif ($Headers[$j] ne "0" && $j > 1) {
                $DataTypes .= $Headers[$j];
            }
        }

        # Increment record number
        $RecNo++;

        # Write the information to FileInfo

        print OUT_HANDLE "$RecNo|$FileType|$State{$Nm}|$Station{$Nm}|$StationType{$Nm}|$SubStation{$Nm}|$Year|$Month|$Day|$Interval{$IntAbbrev}|$Format|$FileSize|$Path|$FileName|$DataTypes\n";
    }
    close OUT_HANDLE;
}

#	**************************************************************
#	Single element profile files (1 month period)
#	**************************************************************
sub do_single_el()
{
    # Set all other fields that are static for this type of file
    my $FileType = "OneElArc";
    my $Format = "text";
    my $Path = "/var/www/html/download/OneElArc/";
    my $Day = "";

   # Open output file
    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_OneElArc.txt");
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
    my $lsString = ls("-1sA", "/var/www/html/download/OneElArc/");

    # Convert string to list of lines
    my @LineList = split /^/m, $lsString;

    # Get number of lines
    my $LineCount = @LineList;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {

        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineList[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }

        # We'll assign several FileInfo fields based on the first two chars
        # of the file name; e.g. $Station{$Nm}
        my $Nm = substr $FileName, 0, 2;

        # Create 4-digit year
        my $Yr = substr $FileName, 7, 2;
        my $Year;
        if ($Yr lt "40") {
            $Year = "20" . $Yr;
        }
        elsif ($Yr gt "60") {
            $Year = "19" . $Yr;
        }

        # Get month from filename
        my $Month = substr $FileName, 9, 2;

        # Get interval info from filename
        my $IntAbbrev = substr $FileName, 3, 1;

        # Get data type from file name
        my $DataAbbrev = substr $FileName, 4, 2;

        # Get instrument number from filename and format with parens
        my $InstrumentNo = substr $FileName, 6, 1;
        $InstrumentNo = " (" . $InstrumentNo . ")";

        # Increment record number
        $RecNo++;

        # Write the information to FileInfo

        print OUT_HANDLE "$RecNo|$FileType|$State{$Nm}|$Station{$Nm}|$StationType{$Nm}|$SubStation{$Nm}|$Year|$Month|$Day|$Interval{$IntAbbrev}|$Format|$FileSize|$Path|$FileName|$OneElArcDataType{$DataAbbrev}$InstrumentNo\n";
    }
    close OUT_HANDLE;
}
#	**************************************************************
#	MFR files (1 day period)
#	**************************************************************
sub do_mfr()
{
    # Set all other fields that are static for this type of file
    my $FileType = "MFR";
    my $Format = "text";
    my $Path = "/var/www/html/download/MFR/";
    my $MFRDataType = "MFR data";

    # Open output file
    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_MFR.txt");
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
    my $lsString = ls("-1sA", "/var/www/html/download/MFR/");

    # Convert string to list of lines
    my @LineList = split /^/m, $lsString;

    # Get number of lines
    my $LineCount = @LineList;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {

        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineList[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }

        # We'll assign several FileInfo fields based on the first two chars
        # of the file name; e.g. $Station{$Nm}
        my $Nm = substr $FileName, 0, 2;

        # Get date info from filename
        my $Year = substr $FileName, 5, 4;
        my $Month = substr $FileName, 9, 2;
        my $Day = substr $FileName, 11, 2;

        # Increment record number
        $RecNo++;

        # Write the information to FileInfo
        print OUT_HANDLE "$RecNo|$FileType|$State{$Nm}|$Station{$Nm}|$StationType{$Nm}|$SubStation{$Nm}|$Year|$Month|$Day|$Interval{'O'}|$Format|$FileSize|$Path|$FileName|$MFRDataType\n";
    }
    close OUT_HANDLE;
}

#	**************************************************************
#	Spectral files (1 month period)
#	**************************************************************
sub do_spectral()
{
    
    # Open output file
#    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_Spectral.txt");
    open(OUT_HANDLE, ">/var/www/html/download/test/FileInfo_Spectral_test.txt");
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
#    my $lsString = ls("-1sA", "/var/www/html/download/Spectro-EKO/");
    my $lsString = ls("-1sA", "/var/www/html/download/test/");

    # Convert string to list of lines
    my @LineList = split /^/m, $lsString;

    # Get number of lines
    my $LineCount = @LineList;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {

        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineList[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }
		#Get Site initials
		my $Site = substr $FileName, 0, 2;

        # Get date info from filename
        my $Year = substr $FileName, 13, 4;
        #my $Month = substr $FileName, 18, 2;

        # Increment record number
        $RecNo++;

        # Write the information to FileInfo
		print OUT_HANDLE "$RecNo|Spectral|OR|Eugene|Ref||$Year|Month|||text|$FileSize|/var/www/html/download/Spectro-EKO/|$FileName|Spectral data\n";
		    }

# do the Lindenberg data set
	# Get file names and sizes in kilobytes into a string
    my $lsStringDE = ls("-1sA", "/var/www/html/download/Spectro-Lindenberg/");

    # Convert string to list of lines
    my @LineListDE = split /^/m, $lsStringDE;

    # Get number of lines
    my $LineCountDE = @LineListDE;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCountDE; $i++) {

        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineListDE[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }

        # Get date info from filename
        my $Year = substr $FileName, 13, 4;
        my $Month = substr $FileName, 18, 2;

        # Increment record number
        $RecNo++;

        # Write the information to FileInfo
        print OUT_HANDLE "$RecNo|Spectral|DE|Lindenberg|Ref||$Year|$Month|||text|$FileSize|/var/www/html/download/Spectro-Lindenberg/|$FileName|Spectral data\n";
	}
    close OUT_HANDLE;
}

#	**************************************************************
# 	Daily total files (1 year period)
#	**************************************************************
sub do_daily_total()
{
    # Set all other fields that are static for this type of file
    my $FileType = "DayTtl";
    my $Month = "";
    my $Day = "";
    my $Format = "text";
    my $Path = "/var/www/html/download/DayTtl/";

    # Open output file
    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_DayTtl.txt");
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
    my $lsString = ls("-1sA", "/var/www/html/download/DayTtl/");

    # Convert string to list of lines
    my @LineList = split /^/m, $lsString;

    # Get number of lines
    my $LineCount = @LineList;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {

        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineList[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }

        # We'll assign several FileInfo fields based on teh first two chars
        # of the file name; e.g., $Station{$Nm}
        my $Nm = substr $FileName, 0, 2;

        # Create 4-digit year
        my $Yr = substr $FileName, 4, 2;
        my $Year;
        if ($Yr lt "40") {
            $Year = "20" . $Yr;
        }
        elsif ($Yr gt "60") {
            $Year = "19" . $Yr;
        }

        # Get file head information by reading the data file
        open(IN_HANDLE, "</var/www/html/download/DayTtl/" . $FileName);
        my @Headers;
        if ( defined( my $InRec = <IN_HANDLE> ) ) {
            @Headers = split /\s+/, $InRec;
        }
        close IN_HANDLE;
        my $HeaderCnt = @Headers;
        my $DataTypes = "";
        for (my $j = 0; $j < $HeaderCnt; $j++) {
            if ($j > 2 && $Headers[$j-1] ne "0" && $j < $HeaderCnt-1) {
                $DataTypes .= "; ";
            }
            if (exists $DataNames{$Headers[$j]}) {
                $DataTypes .= $DataNames{$Headers[$j]};
            }
            elsif ($Headers[$j] ne "0" && $j > 1) {
                $DataTypes .= $Headers[$j];
            }
        }

        # Increment record number (had compile error when doing this usual way)
        if ($RecNo == 0 ) {
            $RecNo = 1;
        }
        else {
            $RecNo++;
        }

        # Write the information to FileInfo

        print OUT_HANDLE "$RecNo|$FileType|$State{$Nm}|$Station{$Nm}|$StationType{$Nm}|$SubStation{$Nm}|$Year|$Month|$Day|$Interval{'H'}|$Format|$FileSize|$Path|$FileName|$DataTypes\n";
    }
    close OUT_HANDLE;
}


#	**************************************************************
#	Monthly average files (1 year period)
#	**************************************************************
sub do_monthly_avg()
{
    # Set all other fields that are static for this type of file
    my $FileType = "MthAvg";
    my $Format = "text";
    my $Path = "/var/www/html/download/MthAvg/";
    my $Month = "";
    my $Day = "";

    # Open output file
    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_MthAvg.txt");
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
    my $lsString = ls("-1sA", "/var/www/html/download/MthAvg/");

    # Convert string to list of lines
    my @LineList = split /^/m, $lsString;

    # Get number of lines
    my $LineCount = @LineList;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {

        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineList[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }

        # We'll assign several FileInfo fields based on the first two chars
        # of the file name; e.g., $Station{$Nm}
        my $Nm = substr $FileName, 0, 2;

        # Create 4-digit year
        my $Yr = substr $FileName, 5, 2;
        my $Year;
        if ($Yr lt "40") {
            $Year = "20" . $Yr;
        }
        elsif ($Yr gt "60") {
            $Year = "19" . $Yr;
        }

        # Get interval info from filename
        my $IntAbbrev = substr $FileName, 4, 1;

        # Get file head information by reading the data file
        open(IN_HANDLE, "</var/www/html/download/MthAvg/" . $FileName);
        my @Headers;
        if ( defined( my $InRec = <IN_HANDLE> ) ) {
            @Headers = split /\s+/, $InRec;
        }
        close IN_HANDLE;
        my $HeaderCnt = @Headers;
        my $DataTypes = "";
        for (my $j = 0; $j < $HeaderCnt; $j++) {
            if ($j > 2 && $Headers[$j-1] ne "0" && $j < $HeaderCnt-1) {
                $DataTypes .= "; ";
            }
            if (exists $DataNames{$Headers[$j]}) {
                $DataTypes .= $DataNames{$Headers[$j]};
            }
            elsif ($Headers[$j] ne "0" && $j > 1) {
                $DataTypes .= $Headers[$j];
            }
        }

        # Increment record number
        $RecNo++;

        # Write the information to FileInfo
        print OUT_HANDLE "$RecNo|$FileType|$State{$Nm}|$Station{$Nm}|$StationType{$Nm}|$SubStation{$Nm}|$Year|$Month|$Day|$Interval{$IntAbbrev}|$Format|$FileSize|$Path|$FileName|$DataTypes\n";
    }
    close OUT_HANDLE;
}

# **************************************************************
#	Cumultative summary files
# **************************************************************
sub do_cumulative_sum()
{
    # Set all other fields that are static for this type of file
    my $FileType = "CumSum";
    my $Year = "";
    my $Month = "";
    my $Day = "";
    my $NoInterval = "";
    my $Format = "text";
    my $Path = "/var/www/html/download/CumSum/";

    # Open output file
    open(OUT_HANDLE, ">/var/www/cgi-bin/FileInfo_CumSum.txt");
    $RecNo = 0;

    # Get file names and sizes in kilobytes into a string
    my $lsString = ls("-1sA", "/var/www/html/download/CumSum/");

    # Convert string to list of lines
    my @LineList = split /^/m, $lsString;

    # Get number of lines
    my $LineCount = @LineList;

    # Process each line, starting with second record to skip total size info
    for (my $i = 1; $i < $LineCount; $i++) {

        # Get size and name of file, stripped of blanks
        (my $junk, my $FileSize, my $FileName) = split /\s+/, $LineList[$i];

        # No leading blank? Can cause fields to be assigned wrong. Fix here.
        if ($junk ne "") {
            $FileName = $FileSize;
            $FileSize = $junk;
        }

        # We'll assign several FileInfo fields based on the first two chars
        # of the file name; e.g., $Station{$Nm}
        my $Nm = substr $FileName, 0, 2;

        next if substr($FileName, 2, 3) ne 'SM_';

        my $yyyy = substr($FileName, 5, 4);
        next if $yyyy lt '1970';
        next if $yyyy gt '2050';

        next if substr($FileName, 9, 1) ne '_';

        my $code = substr($FileName, 10, 4);

        my $cumsum_datatype = '';
        if (exists $DataNames{$code})
        {
            $cumsum_datatype .= $DataNames{$code};
        }

        # Increment record number (had compile error when doing this usual way)
        if ($RecNo == 0 ) {
            $RecNo = 1;
        }
        else {
            $RecNo++;
        }

        # Write the information to FileInfo
        print OUT_HANDLE "$RecNo|$FileType|$State{$Nm}|$Station{$Nm}|$StationType{$Nm}|$SubStation{$Nm}|$Year|$Month|$Day|$NoInterval|$Format|$FileSize|$Path|$FileName|$cumsum_datatype\n";
    }
    close OUT_HANDLE;
}




















#****************************************************************************
#
#   M A I N   P R O G R A M
#
#****************************************************************************

open INI_FILE, "/var/www/cgi-bin/CreFileInfo.ini" or die "Can't open /var/www/cgi-bin/CreFileInfo.ini: $!\n";

my $ini_rec_no = 0;

while ( defined( my $ini_rec = <INI_FILE> ) )
{
    $ini_rec_no++;
    chomp $ini_rec;

    #-- skip comments
    next if substr( $ini_rec, 0, 1 ) eq '#';

    #-- skip records with only whitespace
    next unless $ini_rec =~ /\S/o;

    #-- split record on tabs
    my @ini_fld = split "\t", $ini_rec;

    #-- must have 3 fields in each record
    die "Invalid record in .ini file at line $ini_rec_no\n" if ! defined( $ini_fld[ 2 ] );

    #-- only 3rd field can have mixed upper/lower chars
    my $op_type = uc( $ini_fld[ 0 ] );
    my $key = uc( $ini_fld[ 1 ] );
    my $value = $ini_fld[ 2 ];

    #-- do something intelligent with each kind of record
    if ( $op_type eq 'DATA_CODE' )
    {
        if ( $key =~ /^7\d\d\d$/o )
        {
            #-- 'spectral' codes need no further work
            $DataNames{ $key } = $value;
        }
        elsif ( $key =~ /^\d\d\d$/o )
        {
            #-- otherwise, append an instrument number (10 times!)
            for ( my $i = 0; $i < 10; $i++ )
            {
                #-- these suffixes are tacked on the data name, with zero just blank
                my $name_suffix = " $i";
                $name_suffix = "" if $i == 0;
                $DataNames{ "$key$i" } = "$value$name_suffix";
            }
        }
        else
        {
            die "Unknown data code in .ini file at line $ini_rec_no\n";
        }
    }
    elsif ( $op_type eq 'STA_NAME' )
    {
        die "Invalid station abbreviation in .ini file at line $ini_rec_no\n" if ! $key =~ /^[A-Z][A-Z0-9]$/o;
        $Station{ $key } = $value;
    }
    elsif ( $op_type eq 'STA_STATE' )
    {
        die "Invalid station abbreviation in .ini file at line $ini_rec_no\n" if ! $key =~ /^[A-Z][A-Z0-9]$/o;
        die "Invalid state abbreviation in .ini file at line $ini_rec_no\n" if ! $value =~ /^[A-Z][A-Z]$/o;
        $State{ $key } = $value;
    }
    elsif ( $op_type eq 'STA_TYPE' )
    {
        die "Invalid station abbreviation in .ini file at line $ini_rec_no\n" if ! $key =~ /^[A-Z][A-Z0-9]$/o;
        die "Invalid station type in .ini file at line $ini_rec_no\n" if ! lc( $value ) =~ /^(sub|ref)$/o;
        $StationType{ $key } = lc( $value );
    }
    elsif ( $op_type eq 'STA_SUBSTA' )
    {
        die "Invalid station abbreviation in .ini file at line $ini_rec_no\n" if ! $key =~ /^[A-Z][A-Z0-9]$/o;
        $SubStation{ $key } = $value;
    }
    elsif ( $op_type eq 'INTERVAL' )
    {
        die "Invalid interval code in .ini file at line $ini_rec_no\n" if ! $key =~ /^[A-Z]$/o;
        $Interval{ uc( $key ) } = $value;
    }
    elsif ( $op_type eq '1_ELEMENT_DATATYPE' )
    {
        die "Invalid single element archive datatype code in .ini file at line $ini_rec_no\n" if ! $key =~ /^[A-Z][A-Z0-9]$/o;
        $OneElArcDataType{ uc( $key ) } = $value;
    }
    elsif ( $op_type eq 'SUMMARY_DATATYPE' )
    {
        #-- TODO add validation code here once we know what these things all look like
        $DataType{ $key } = $value;
    }
    else
    {
        die "Unknown operator type in .ini file at record $ini_rec_no\n";
    }
}

#-- set substations to blank if currently undefined
foreach my $s ( keys %Station )
{
    $SubStation{ $s } = "" if ! defined $SubStation{ $s };
}

#**************************************************************
# Call subs to handle various file types
#**************************************************************

#do_ArchiveUpdatedFormat();
#do_archival();
#do_single_el();
#do_mfr();
do_spectral();
#do_daily_total();
#do_monthly_avg();
#do_cumulative_sum();
