#!/usr/bin/perl -w

#------------------------------------------------------------------------------
#BOP
#
# !ROUTINE: copy_loop
#
# !DESCRIPTION: Perl script COPY\_LOOP copies GEOS-Chem original files from 
# the "../loop" subdirectory to this directory.  Files in "../loop" are only
# copied if they are newer.
#\\
#\\
# !CALLING SEQUENCE:
#    copy_loop ROOTDIR
#
#  For example:  
#    copy_loop /home/bmy/gc_geos5/Code.bmy
#
# !REMARKS:
#  The file loop/schem_mod_loop.f will get renamed to ./schem.f for 
#  backwards compatibility w/ existing GEOS-Chem filenames.
#
# !REVISION HISTORY: 
#  23 Jul 2009 - R. Yantosca - Initial version
#  14 Dec 2009 - R. Yantosca - Now look for files with *.F extensions in loop/
#  31 Aug 2010 - R. Yantosca - Modified for GEOS-4 data
#EOP
#------------------------------------------------------------------------------
#BOC


# Define local variables
my $loopDir   = ".";
my @loopFiles = ();
my $loopFile  = "";
my $codeFile  = "";
my $cmd       = "";
my $result    = "";
my $file      = "";

# Read column files
opendir( D, "." ) or die "Cannot open $loopDir\n";
@loopFiles = readdir( D );
closedir( D );

# Loop over all files
foreach $file ( @loopFiles ) {

  # Remove the newline
  chomp( $file );

  # Columnized file
  $loopFile =  "$file";

  if ( $file =~ /GEOS5_47L/ ) {
 
    # Replace GEOS-5 file extensions
    $codeFile =  $loopFile;
    $codeFile =~ s/_GEOS5_47L_4x5.bpch/.geos5.4x5.47L/g;
    
  } elsif ( $file =~ /GEOS4_30L/ ) {

    # Replace GEOS-4 file extensions
    $codeFile =  $loopFile;
    $codeFile =~ s/.GEOS4_30L.4x5/.geos4.4x5.30L/g;

  }

  $cmd = "mv -f $loopFile $codeFile";
  print "$cmd\n";
  qx( $cmd );
}		

# Return normally
exit(0);
