#!/usr/bin/perl # Routine to change the name of a series of files and log the correspondences. # # Both FITS and label files will require changing. In the label file, all # pointer must be updated, as should the PRODUCT_ID. # # Recurses down through current working directory. # # Format: fixfeb "date" # # where "date" is a 4-character string with the month/day digits to be used # in the file names. Note that this must be quoted. # #============================================================================== use File::Find; # Save the argument: die "Usage: fixfeb \"date\". \n" if (@ARGV != 1); $date = $ARGV[0]; # Open the translation list: open(LIST,"| sort >names$date.tab") || die "Could not open names$date.tab for writing ($!)"; # Run: find({wanted => \&fixit},"."); # That's it for the main routine. #------------------------------------------------------------------------------ sub fixit # Routine to test the current file name, change it if needed, and update # label files as encountered. We are cd'ed to the local directory when # this routine is called. { my ($filename,$line); my ($newname,$NEWNAME); my ($suffix,$prefix,$PREFIX,$newprefix,$NEWPREFIX); my ($number); $filename = $_; # This is just the file name # We only need to worry about file names like "nb0087", so we can ignore # anything else. And we can't rename the data file until we get the date # info from the label file, so we'll skip FITS files as well: return if ($filename !~ /^(n[a-z])0([0-9]{3})\.lbl$/); # We've got a label, so we'll create the strings we'll need for making # the changes, then open files for processing: $number = $2; $prefix = $1."0".$number; $PREFIX = $prefix; $PREFIX =~ tr/a-z/A-Z/; $newprefix = "kp05${date}_$number"; $NEWPREFIX = "KP05${date}_$number"; # Open the old label for reading and the new label for writing: open(OLD,"varlen -rmcr $filename | ") || die "Could not open $File::Find::name pipe for reading ($!)."; open(NEW,"| fixlen -c > $newprefix.lbl") || die "Could not open $file::Find::dir/$newprefix.lbl for writing ($!)."; # Now step through the label: while ($line=) { $line =~ s/$prefix/$newprefix/; # This changes pointers $line =~ s/$PREFIX/$NEWPREFIX/; # This changes PRODUCT_ID $line =~ s/_FIT//; # These shorten the PRODUCT_ID $line =~ s/_2005_/_05_/; printf NEW $line; } # Close the labels and rename the files: close(NEW); close(OLD); unlink $filename; rename "$prefix.fit", "$newprefix.fit"; # And write the line to the output list: printf LIST "$newprefix $prefix\n"; # And we're done: return 1; }