#!/usr/bin/env perl
# +-======-+ 
#  Copyright (c) 2003-2007 United States Government as represented by 
#  the Admistrator of the National Aeronautics and Space Administration.  
#  All Rights Reserved.
#  
#  THIS OPEN  SOURCE  AGREEMENT  ("AGREEMENT") DEFINES  THE  RIGHTS  OF USE,
#  REPRODUCTION,  DISTRIBUTION,  MODIFICATION AND REDISTRIBUTION OF CERTAIN 
#  COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE UNITED STATES GOVERNMENT AS 
#  REPRESENTED BY THE GOVERNMENT AGENCY LISTED BELOW ("GOVERNMENT AGENCY").  
#  THE UNITED STATES GOVERNMENT, AS REPRESENTED BY GOVERNMENT AGENCY, IS AN 
#  INTENDED  THIRD-PARTY  BENEFICIARY  OF  ALL  SUBSEQUENT DISTRIBUTIONS OR 
#  REDISTRIBUTIONS  OF THE  SUBJECT  SOFTWARE.  ANYONE WHO USES, REPRODUCES, 
#  DISTRIBUTES, MODIFIES  OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED 
#  HEREIN, OR ANY PART THEREOF,  IS,  BY THAT ACTION, ACCEPTING IN FULL THE 
#  RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT.
#  
#  Government Agency: National Aeronautics and Space Administration
#  Government Agency Original Software Designation: GSC-15354-1
#  Government Agency Original Software Title:  GEOS-5 GCM Modeling Software
#  User Registration Requested.  Please Visit http://opensource.gsfc.nasa.gov
#  Government Agency Point of Contact for Original Software:  
#  			Dale Hithon, SRA Assistant, (301) 286-2691
#  
# +-======-+ 
#=======================================================================
# name - setup_gaas_obs
# purpose - organize GAAS observation data into subdirectories expected
#           by the reading program
#
# runtime parameter
# => $workdir: directory location of modis data
#
# runtime flags (choose at least one)
# => $avhrr: organize avhrr patmosx data into subdirectories
# => $modis: organize modis Level2 HDF data into subdirectories
#=======================================================================
use strict;
use warnings;

# global variables
#-----------------
my ($workdir, $modis, $avhrr, %flags, $verbose);

# main program
#-------------
{
    use File::Basename ("basename");
    use File::Copy ("move");
    use File::Path ("mkpath");

    my ($file, $base, $adflg);
    my ($label, $Adate, $time, $version);
    my ($type, $jdoy, $subdir);
    my ($year, $month, $day);

    init();
    if ($modis) {
        foreach $file (<$workdir/M?D04_L2.*.*.hdf>) {
            $base = basename $file;
            ($label, $Adate, $time, $version) = split /[\.]/, $base;

            $type = substr($label, 0, 5);
            $year = substr($Adate, 1, 4);
            $jdoy = substr($Adate, 5, 3);

            $subdir = "$workdir/$version/$type/$year/$jdoy";
            unless (-d $subdir) {
                mkpath($subdir, \%flags) or die "Error making dir: $subdir;";
                system("touch $subdir/.no_archiving");
            }
            print "mv $file $subdir\n" if $verbose;
            move($file, $subdir);
        }
    }

    if ($avhrr) {
        foreach $file (<$workdir/patmosx_v05r02.*.npz>) {
            $base = basename $file;
            ($label, $adflg, $Adate) = split /[\.]/, $base;

            $year  = substr($Adate, 0, 4);
            $month = substr($Adate, 4, 2);
            $day   = substr($Adate, 6, 2);

            $subdir = "$workdir/Y$year/M$month/D$day";
            unless (-d $subdir) {
                mkpath($subdir, \%flags) or die "Error making dir: $subdir;";
                system("touch $subdir/.no_archiving");
            }
            print "mv $file $subdir\n" if $verbose;
            move($file, $subdir);
        }
    }
            
}

#=======================================================================
# name - init
# purpose - get input parameters and flags
#=======================================================================
sub init {
    use Getopt::Long;

    GetOptions( "v"     => \$verbose,
                "modis" => \$modis,
                "avhrr" => \$avhrr );
    $workdir = shift @ARGV;

    $flags{"verbose"} = 1 if $verbose;
}
