#!/usr/bin/perl -I/nwprod/lib/incmod/perl

# $Id: bsky_main.pl,v 1.7 2004/06/18 14:52:22 smo Exp $
################################################################################
# This is the main program for blue sky.
#
# Usage is:
#    (optional) setenv BSKY_HOME <path>
#    (optional) setenv FCAMMS <NWRMC|CANSAC|RMC|SHRMC|EAMC|EPA>
#    (optional) export BSKY_HOME FCAMMS
#
#    bsky_main.pl [YYYYMMDDHH]
#
#  USDA Forest Service - Pacific Northwest Wildland Fire Labratory
#  Copyright (C) 2003-2004
#
################################################################################

print (" luke- in smoke_bsky_main.pl - begining the processing \n");

# Get bluesky home dir from env variable or use default
$FCAMMS   = exists $ENV{FCAMMS} ? $ENV{FCAMMS} : 'EPA';
$DIR_HOME = exists $ENV{BSKY_HOME} ? $ENV{BSKY_HOME} : '/bluesky';

print (" luke- in smoke_bsky_main.pl - DIR_HOME is $DIR_HOME \n");

chdir($DIR_HOME);

$FIXsmoke = $ENV{FIXsmoke};
$USHsmoke = $ENV{USHsmoke};

require "$USHsmoke/smoke_sub_global.pl";

$cwd = `pwd`;
#$cwd = system(pwd);
print (" luke- in smoke_bsky_main.pl the cwd is $cwd \n");

################################################################################
# Set forecast start time variables
# set from command line or using todays date by default

$START_TIME = time();

if ($#ARGV >= 0) {
    $FORECAST_START_TIME = convert_datehr_to_seconds($ARGV[0]);
} else {
    $FORECAST_START_TIME = zero_zulu($START_TIME);
}
$FORECAST_DATEHR_STR = make_date_string($FORECAST_START_TIME, "%04d%02d%02d%02d", "gmt");
$FORECAST_DATE_STR   = make_date_string($FORECAST_START_TIME, "%04d%02d%02d"    , "gmt");

################################################################################
# Now, read in parameters for run.  File is default.param
require "$FIXsmoke/smoke_default_".$FCAMMS.".param";

################################################################################
# Set other time variables

# Account for met model spinup.  FORECAST_START_TIME in seconds, SPINUP in hours
$MODEL_START_TIME = $FORECAST_START_TIME + $SPINUP*3600;

$MODEL_DATEHR_STR     = make_date_string($MODEL_START_TIME,
                                         "%04d%02d%02d%02d", "gmt");
$MODEL_END_DATEHR_STR = make_date_string($MODEL_START_TIME+3600*$HOURS_TO_RUN, 
                                         "%04d%02d%02d%02d", "gmt");

# Set the execution date string.  YYYYMMDDHHMMSS of current time
$EXEC_DATE_STR = make_date_string($START_TIME, "%04d%02d%02d%02d%02d%02d", 
                                  "local");

################################################################################
# CREATE AND MOVE TO WORKING DIRECTORY
# set other pathnames dependent on $WORKING_DIR

#$WORKING_DIR = "$DIR_HOME/working";
#$MODELS_BURN_DIR = "$DIR_HOME/models/burns";
#$MODELS_EMISSIONS_DIR = "$DIR_HOME/models/emissions/$EMISSIONS_TYPE";
#$MODELS_FUELLOAD_DIR = "$DIR_HOME/models/fuelload";

$WORKING_DIR = "$DIR_HOME/working";
$MODELS_BURN_DIR = "$DIR_HOME/working";
$MODELS_EMISSIONS_DIR = "$DIR_HOME/working"; 
$MODELS_FUELLOAD_DIR = "$FIXsmoke";

use lib "./";
#create_working_dir();
 if ( ! -e $WORKING_DIR ) {
    mkdir $WORKING_DIR, 0755 or
    die ("FATAL ERROR: can't create directory $WORKING_DIR\n");
 }
print ("luke- cd to $WORKING_DIR\n");
chdir("$WORKING_DIR");

$cwd = `pwd`;
#$cwd = system(pwd);
print ("luke-  in smoke_bsky_main.pl the cwd is $cwd \n");


################################################################################
# INCLUDE SUB-ROUTINES LOCATED IN OTHER FILES

require "$USHsmoke/smoke_sub_global.pl";  # needs to be required earlier, but doesn't hurt to put here as well
require "$USHsmoke/smoke_sub_burn_data.pl";

################################################################################
# RUN THE PROGRAM!

print ("\n\n\n-----> STARTING BLUESKY RUN <-----\n");
print (" start time      : ".make_date_string($START_TIME, "%04d/%02d/%02d %02d:%02d:%02d", "local"));
print ("\n\n");
print (" runid           : $EXEC_DATE_STR\n");
print (" forecast datetm : $FORECAST_DATEHR_STR\n");
print (" model datetm    : $MODEL_DATEHR_STR\n");
print (" model end       : $MODEL_END_DATEHR_STR\n");
print (" hours           : $HOURS_TO_RUN\n");
print (" home dir        : $DIR_HOME\n");
print (" working dir     : $WORKING_DIR\n");
print (" param file      : $PARAM_FILE\n"); 
print ("\n\n");
print (" model MODEL_START_TIME : $MODEL_START_TIME\n");

#------------------------------------------------------------------------------
# Get the burn files.  Set up $BURN variable and BSKY_NUMFIRES.
# BURN will contain data as in $BURN[firenumber]{data hash}.
#------------------------------------------------------------------------------
%foo;
$BURN[0] = \%foo;
$BSKY_NUMFIRES = 0;  
require "$USHsmoke/smoke_sub_burn_system_baseclass.pl";
foreach $bsys (@BURN_SYSTEMS) {
    require "$USHsmoke/smoke_sub_burn_system_$bsys.pl";
    my $burn_obj;
    $burn_obj = $bsys->new();
    #chdir "$BURN_INPUT_DIR/$bsys";
    chdir "$BURN_INPUT_DIR";

     $cwd = `pwd`;
     #$cwd = system(pwd);
     print ("luke-  after chdir BURN_INPUT_DIR the cwd is $cwd \n");

    $burn_obj->get_load_burn_data();
}
chdir("$WORKING_DIR");

$cwd = `pwd`;
#$cwd = system(pwd);
print ("luke-  in smoke_bsky_main.pl the cwd is $cwd \n");

#------------------------------------------------------------------------------
# Process the Burn data.
#------------------------------------------------------------------------------
require "$USHsmoke/smoke_sub_fuelload_HARDY.pl";
$HARDY_FUELLOAD = HARDY->new();
require "$USHsmoke/smoke_sub_fuelload_NFDRS.pl";
$NFDRS_FUELLOAD = NFDRS->new();
fill_burn_data();    # in sub_burn_data.pl
require "$USHsmoke/smoke_sub_write_burns.pl";
write_burn_output(); # in sub_burn_data.pl
