#!/bin/tcsh -f #+ # # koa_translator [directory] [-test] # # looks through the specified directory (or the current directory if # there is no argument) and all subdirectories for file names of the # form II.dddddddd.xxxxx.fits, where II is a KOA instrument designator # (see list in the case statement below), dddddddd is an eight-digit # date number, and xxxxx is a five-digit "KOAID" index number. # # Files ending in ".fits.gz" will also be processed. These filenames are # assumed to be filenames from KOA (Keck Observatory Archive). # # The "-test" argument creates a file (translate.csh) that can be executed to # perform the translation. Without this argument, the script will be created, # run, and then deleted. # # The translator will look through each such file's FITS header and # reconstruct the original filename (including a parent directory # derived from the UT date, and possibly appropriate subdirectories) and # write to a file "translate.csh" the appropriate shell commands to # rename the KOA file to the original filename. # # This is meant to reconstruct the original directory and filename # structure as observed. The reconstruction will not always be perfect. # Observers may have used nonstandard directory structure, or KOA # might have recorded multiple versions of the same filename. The # latter might occur, for example, if observers recorded some files # then deleted them and reset the file number to write new files # into the same filenames. # # Note that if there are duplicate original filenames, the program will # use the original filename for the first version it finds, then create # second (and subsequent, if necessary) versions with ".v2", ".v3", etc. # added to the filename. THIS DOES NOT GUARANTEE THAT FILES ALL USING # ".v2", FOR EXAMPLE, ARE REALLY FROM THE SAME DATASET. You may have # to do some checking to determine which files belong in which dataset. # # Written by: R. W. Goodrich, 2013-2014. # #- # Disclaimer about "Input error" lines. echo "" echo Ignore any \"Input error\" lines that you see from this script. echo They occur in some cases but are benign. echo "" # Loop through the current directory or the directory provided and create # a list of KOAID-like filenames. set koadir = "." set test = 0 while ($#argv != 0) if ("$1" == "-test") then set test = 1 else if (-d "$1") then set koadir = "$1" else echo The argument "$1" Is not a directory nor a valid flag. exit 1 endif shift end # Three-letter abbreviations for months. set month = ("jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec") # Create list of KOA files. set format = '[A-Z][A-Z,0-9].[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].[0-9][0-9][0-9][0-9][0-9]' set koafiles = (`find $koadir -name "$format.fits" -print` `find $koadir -name "$format.fits.gz" -print`) if ("$koafiles" == "") then echo "No relevant files found to translate." echo "KOA files are of the form OS.20111230.05089.fits with a possible" echo ".gz extension." exit 1 endif # Create the translation file. set transfile = "$koadir/translate.csh" # If the translate.csh file exists, ask whether to overwrite or delete. if (-e $transfile) then echo "The translation file, $transfile, exists." echo -n "Do you want to overwrite it [y/n, default is "y"] ? " set answer = $< if ("$answer" == "" || "$answer" == "y" || "$answer" == "Y" || "$answer" == "yes" || "$answer" == "YES") then echo "Overwriting translate.csh..." cat /dev/null >! $transfile endif endif # Loop through the file list. foreach file ($koafiles) # If the file ends in "gz", use "gunzip -c" to list the file. # Note that "zcat" does not work properly on some Macs. set gztest = `echo $file | grep "\.gz"` if ("$gztest" == "") then set catcmd = cat set gzext = "" else set catcmd = "gunzip -c" set gzext = ".gz" endif # Create the directory name from the KOAID's UT date. # The month is replaced by a three-letter abbreviation. set explodedName = (`echo $file | sed "s|/| |g"`) set filename = $explodedName[$#explodedName] set koainst = `echo $filename | cut -c1-2` set koaUTyear = `echo $filename | cut -c4-7` set koaUTmonth = `echo $filename | cut -c8-9` set koaUTday = `echo $filename | cut -c10-11` # Translate the KOAID instrument abbreviation to a longer string. switch ($koainst) case DE: set inst = DEIMOS breaksw case ES: set inst = ESI breaksw case HI: set inst = HIRES breaksw case LB: case LR: set inst = LRIS breaksw case LW: set inst = LWS breaksw case MF: set inst = MOSFIRE breaksw case N1: set inst = NIRC breaksw case N2: set inst = NIRC2 breaksw case NC: case NS: set inst = NIRSPEC breaksw case OI: case OS: set inst = OSIRIS breaksw default: echo Don\'t understand the two-letter KOAID code \"$koainst\" endsw set outdir = $inst/$koaUTyear$month[$koaUTmonth]$koaUTday/ # Relevant keywords for different instruments. # The default output directory is the UT date derived from the KOA # filename. # Different keywords for different instruments. switch ($koainst) case OS: case OI: case MF: set origfile = `$catcmd $file | fold | head -100 | grep "^DATAFILE=" | cut -f2 -d\'`.fits breaksw case ES: case DE: case LR: case LB: case HI: set prefix = `$catcmd $file | fold | head -350 | grep "^OUTFILE =" | cut -f2 -d\'` set frame = `$catcmd $file | fold | head -350 | grep "^FRAMENO =" | cut -f2 -d=` set paddedframe = `printf "%4.4i" $frame[1]` set origfile = $prefix[1]$paddedframe.fits breaksw case N1: case LW: echo "Cannot yet translate LWS, NIRC files." exit 1 breaksw case N2: set origfile = `$catcmd $file | fold | head -100 | grep "^FILENAME=" | cut -f2 -d\'` breaksw case NS: set outdir = "$outdir/spec" set origfile = `$catcmd $file | fold | head -100 | grep "^FILENAME=" | cut -f2 -d\'` breaksw case NC: set outdir = "$outdir/scam" set origfile = `$catcmd $file | fold | head -100 | grep "^FILENAME=" | cut -f2 -d\'` breaksw default: echo Could not determine the instrument \"$koainst\" exit 1 endsw # Filter for double "/" marks in outdir, then write out the translation line. set outdir = `echo $outdir | sed "s://:/:g"` # Check to see if the output file is in the translation file yet. @ i=1 set test = `grep $outdir$origfile$gzext $transfile` while ("$test" != "") @ i++ set origfile = `echo $origfile | sed "s/\.fits/.v$i.fits/"` set test = `grep $outdir$origfile$gzext $transfile` end echo "mkdir -p $outdir ; mv $file $outdir$origfile$gzext" >>! $transfile end # Make the shell script executable. chmod a+x $transfile # Run the shell script if this is not in test mode. if ("$test" == 0) then source $transfile \rm $transfile endif