Last active
December 28, 2015 19:39
-
-
Save guymac/7551990 to your computer and use it in GitHub Desktop.
Line-for-line (or as close to it as possible) port of a Perl CLI utility to Groovy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#!/usr/bin/env groovy | |
CVS_ID = 'HiRISE_Data_Location $Revision: 1.1 $ $Date: 2013/11/01 19:47:41 $' | |
println CVS_ID | |
Command_Name = ('$RCSfile: HiRISE_Data_Location,v $' =~ /(\w+),v/)[0][1] | |
// Number of orbits per directory. | |
DEFAULT_ORBITS_PER_DIRECTORY = 100 | |
ORBITS_PER_DIRECTORY_BY_PHASE = | |
[ | |
'CRU': 10 | |
] | |
// Prefix for otherwise numeric directory names. | |
YYMM_DIRECTORY_NAME_PREFIX = 'YRM_' | |
DEFAULT_DIRECTORY_NAME_PREFIX = 'ORB_' | |
DIRECTORY_NAME_PREFIX_BY_PHASE = | |
[ | |
'CRU': 'SEQ_' | |
] | |
cli = new CliBuilder(usage: Command_Name + ' [options] <ID>', header:'options') | |
cli.n(args: 1, argName: 'Number', 'orbits per directory') | |
cli.p(args: 1, argName: 'Prefix', 'directory name prefix') | |
cli.h('help') | |
// print usage to stderr instead of stdout | |
cli.writer = new java.io.PrintWriter(System.err) | |
def opts = cli.parse(args) | |
if (!opts.arguments()) | |
{ | |
cli.usage() | |
println("An Observation ID must be provided.") | |
System.exit(1) // explicit return code | |
} | |
if (opts.arguments().size() > 1) | |
{ | |
cli.usage() | |
println("Multiple Observation IDs specified.") | |
System.exit(1) | |
} | |
if (opts.n && !opts.n.isNumber()) | |
{ | |
println "Number expected for ${cli.options.getOption('n')}, but ${opts.n} found instead." | |
} | |
Directory_Name_Prefix = opts.p | |
Orbits_per_Directory = opts.n | |
ID = opts.arguments()[0] | |
// strip off any extension or suffix | |
ID = ID.replaceAll("[\\.-]+.*", "") | |
// break the string down into fields | |
fields = ID.split('_') | |
if (fields.size() < 2 || fields.size() > 5) | |
{ | |
cli.usage() | |
println("Couldn't recognize an Observation ID in '$ID'") | |
System.exit(2) | |
} | |
Phase = fields[0] | |
if (fields[1].size() == 15 && fields[1][8] == "T") | |
{ | |
def ( YYYYMMDD, HHMMSS ) = fields[1].split('T') | |
YYMM = YYYYMMDD[2..5] | |
ID = "${fields[0]}_${fields[1]}" | |
Directory_Name_Prefix = (Directory_Name_Prefix ? Directory_Name_Prefix : YYMM_DIRECTORY_NAME_PREFIX) | |
Pathname = "$Phase/$Directory_Name_Prefix$YYMM/$ID" | |
} | |
else | |
if ( | |
fields.size() > 2 && fields[1].size() == 6 && fields[1].isNumber() && | |
fields[2].size() == 4 && fields[2].isNumber() | |
) | |
{ | |
if (! Orbits_per_Directory) | |
{ | |
Orbits_per_Directory = ORBITS_PER_DIRECTORY_BY_PHASE[Phase] | |
Orbits_per_Directory = (Orbits_per_Directory ? Orbits_per_Directory : DEFAULT_ORBITS_PER_DIRECTORY) | |
} | |
def lower = (int) (fields[1].toInteger() / Orbits_per_Directory) * Orbits_per_Directory | |
def upper = lower + Orbits_per_Directory - 1 | |
Orbit_Range = String.format("%06d_%06d", lower.toInteger(), upper.toInteger()) | |
// Reassemble the Observation ID fields. | |
ID = "${fields[0]}_${fields[1]}_${fields[2]}" | |
// Directory name prefix. | |
if (! Directory_Name_Prefix) | |
{ | |
Directory_Name_Prefix = DIRECTORY_NAME_PREFIX_BY_PHASE[Phase] | |
Directory_Name_Prefix = (Directory_Name_Prefix ? Directory_Name_Prefix : DEFAULT_DIRECTORY_NAME_PREFIX) | |
} | |
// Assemble the pathname. | |
Pathname = "$Phase/$Directory_Name_Prefix$Orbit_Range/$ID" | |
} | |
else | |
{ | |
cli.usage() | |
println("Couldn't find a proper orbit number or date field in '${ fields[1] }'") | |
System.exit(2) | |
} | |
println Pathname | |
/** | |
* @copy 2013 Arizona Board of Regents on behalf of the Planetary | |
* Image Research Laboratory, Lunar and Planetary Laboratory at the | |
* University of Arizona. | |
* | |
* This program is free software; you can redistribute it and/or modify it | |
* under the terms of the GNU General Public License, version 2, as | |
* published by the Free Software Foundation. | |
* | |
* This program is distributed in the hope that it will be useful, but | |
* WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
* General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License along | |
* with this program; if not, write to the Free Software Foundation, Inc., | |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment