#!/usr/bin/perl -w
#
# This script processes GEOCAT images provided by CIMSS/SSEC.
#
# The input file name will have a form similar to:
#  SSEC_AWIPS_GEOCAT-GEOW_CONUS_4KM_MVFRPROB_20110107_2242.55779862
# This file arrives as a gzip-compressed netCDF file.
#
# Created by
# Jordan Gerth
# Last modified September 6, 2011
# CIMSS/SSEC - Madison, Wisconsin
# http://www.ssec.wisc.edu/~jordang
# jordang@ssec.wisc.edu
#
# Thanks to Jay Smith for assistance

# Create a system to log results of this script
$datedir=`date -u +%Y%m%d`;
chomp $datedir;
$logfile="/preProcessGOESR.log";
open (LOGGOESR, ">/data/logs/fxa/$datedir$logfile");


# Record initiation time
print LOGGOESR "Started ";
print LOGGOESR `date`;
print LOGGOESR "\n";

# Remove directory structure from file name
use File::Basename;
$shortname = basename($ARGV[0],"");

# Indicate the attempt to process
print LOGGOESR "Processing $shortname";
print LOGGOESR "\n";

# Add proper suffix and gunzip file
$filenmgz = "$ARGV[0]".".gz";
print LOGGOESR `mv $ARGV[0] $filenmgz`;
print LOGGOESR `gunzip $filenmgz`;

# Determine AWIPS filename, additional directories
$nextus0 = index($shortname,'_',12);
$dir0 = substr($shortname,11,$nextus0-11);
$nextus1 = index($shortname,'_',$nextus0+1);
$dir1 = substr($shortname,$nextus0+1,$nextus1-$nextus0-1);
$nextus2 = index($shortname,'_',$nextus1+1);
$dir2 = substr($shortname,$nextus1+1,$nextus2-$nextus1-1);
$nextus3 = index($shortname,'_',$nextus2+1);
$dir3 = substr($shortname,$nextus2+1,$nextus3-$nextus2-1);
$periloc = index($shortname,'.');
$filenmout = substr($shortname,$nextus3+1,$periloc-$nextus3-1);
$prodkey = substr($shortname,$periloc+1,8);

# Place file into location for it to be viewed using the D-2D
#print LOGGOESR `mv $ARGV[0] /data/fxa/sat/SSEC/netCDF/$dir0/$dir1/$dir2/$dir3/$filenmout`;
print LOGGOESR `mv $ARGV[0] /awips2/edex/data/manual/`;

# Force the notificationServer to recognize the new file and
# update the time stamp for the image in the AWIPS menu system
# print LOGGOESR `ssh -x -l fxa dx3 testFileNotify k $prodkey $filenmout`;

# Close and exit
close (LOGGOESR);
exit(0);
