#!/usr/bin/perl -w
#
use File::Copy;
use Time::Local;

# This script captures data from an ATOVS daily data files and writes
# the data to ODS files.

print "\nExtracting a single day of data from an ATOVS RRODF\n";
system "date";
print "\n\n";


my $temp_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/atovs_m1";
chdir $temp_dir or warn "Could not chdir to the raw data directory: ".$raw_data_dir;

my $daily_dir     = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/";
my $byteswap_file = $daily_dir . "rrodf1000.m1.byteswap";
my $big_file      = $daily_dir . "rrodf1000.m1";
my $small_file    = $daily_dir . "rrodf1000_oneday.m1";


# Transfer the file from SATEPSANONE

chdir $daily_dir or warn "Could not chdir to the daily incoming directory: ".$raw_data_dir;

system "wget -4 --wait=5 -O $byteswap_file http://satepsanone.nesdis.noaa.gov/pub/atovsdev/rrodf/rrodf1000.m1";


# Byte-swap the words in the UNIX files so they can be read properly
# in a Linux environment.

if (-e $byteswap_file)
  {
  printf("\nByte swapping the file...\n");

  unlink $big_file;

  symlink $byteswap_file, "fort.20" or warn "Cannot link to fort.20";
  symlink $big_file, "fort.21" or warn "Cannot link to fort.21";

  system "/data/smcd/nprovs/source/byteswap/swaprrodf1000.x";

  unlink "fort.20" or warn "Cannot unlink fort.20";
  unlink "fort.21" or warn "Cannot unlink fort.21";

  system "chmod 664 $big_file";

  unlink $byteswap_file or warn "Could not delete the byte swap file";
  }


# Determine the date of the data to process

$data_day = time();

#$data_day -= (2 * 86400);
$data_day -= (1 * 86400);

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($data_day);
$year += 1900;
$mon++;
$sec = 0;
$min = 0;
$hour = 12;
$wday = 0;
$yday = 0;
$isdst = 0;

my $day_of_data = ($year * 10000) + ($mon * 100) + $mday;



# =============================================================================
# ATOVS M1

chdir $temp_dir or warn "Could not chdir to the raw data directory: ".$raw_data_dir;

unlink $small_file or warn "Could not delete the smaller file";


symlink $big_file, "in.file" or warn "Cannot link to in.file";
symlink $small_file, "out.file" or warn "Cannot link to out.file";

system "/data/data065/nprovs/source/data_transfer/atovs/ExtractDayFromRRODF1000.x " . $day_of_data;

unlink "in.file" or warn "Cannot unlink in.file";
unlink "out.file" or warn "Cannot unlink out.file";



#unlink $big_file;
system "mv " . $big_file . " " . $big_file . ".bkp";



system "mv " . $small_file . " " . $big_file;



# =============================================================================
# Run the ODS capture script

system "/data/data065/nprovs/scripts/graphics/run_atovs_m1_ods_capture";



#===================================================================
# Copy the daily file to the orbital archive and the backup archive

$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";
$archive_dir  = "/data/data599/orbital_archive";
$archive_dir2 = "/data/data215/nprovs/data/orbital_archive";
#$archive_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive";


printf("Copying the daily files to the archive directory\n\n");


# Get yesterday's date
$data_day = time();
$data_day -= 86400;

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($data_day);
$year += 1900;
$mon++;
$sec = 0;
$min = 0;
$hour = 0;
$wday = 0;
$yday = 0;
$isdst = 0;


# Copy and gzip the file to the first archive
$from_file = $nprovs_dir . "/rrodf1000.m1";
$to_file = $archive_dir . sprintf("/atovs_m1/atovs_rrodf1000_%d%02d%02d.m1", $year, $mon, $mday);

printf("Copying from: %s\n", $from_file);
printf("          to: %s\n\n", $to_file);

copy($from_file, $to_file);

system "gzip $to_file";


# Copy the gzipped file to the backup archive
$from_file = $to_file . ".gz";
$to_file = $archive_dir2 . sprintf("/atovs_m1/atovs_rrodf1000_%d%02d%02d.m1.gz", $year, $mon, $mday);

printf("Copying from: %s\n", $from_file);
printf("          to: %s\n\n", $to_file);

#copy($from_file, $to_file);

# end of file
