#!/usr/arch/bin/perl $dependCheck = "ProcessHeaderInfo.pl"; if (!(-e $dependCheck)) { print "ProcessHeaderInfo.pl is not present. \n"; print "Master.pl shouldn't be run without this other script... \n"; print "Try looking for the file in /local/ice... Exiting... \n"; exit(1); } $dependCheck = "SarImageProcess.pro"; if (!(-e $dependCheck)) { print "SarImageProcess.pro is not present. \n"; print "Master.pl shouldn't be run without this other script... \n"; print "Try looking for the file in /local/ice... Exiting... \n"; exit(1); } $user = `whoami`; chomp $user; if ($user ne "ice" && $user ne "bppresto") { print "ERROR: Script MUST be run as user 'ice'... \n"; exit (1); } # ************************* # settings to use in script # ************************* # the length of the file before the extensions such a .D / .L / .T $mainFileNameLength = 42; # the default location to get the files from to process $defaultFileLocation = "/local/ice/saa_radar/"; # different file extensions used throughout the script $fileNameSearchPattern = ".A.D"; $dataExt = ".D"; $correctDataExt = ".dat"; $leaderExt = ".L"; $correctLeaderExt = ".lea"; $trailerExt = ".T"; $zipExt = ".gz"; $zipCmd = "gzip"; $unzipCmd = "gunzip"; $holdingDir = "/local/ice/RADARSAT_Holding/"; $processedDir = "/local/ice/RADARSAT_Processed/"; # web dir to write the jpeg's to $baseWebDir = "/local/ht-geo/great_lakes/icegroup/pics/"; # name of IDL script to output $idlScriptFile = "automatic"; # .pro will be added later to the file # if they specified another location, use that instead of the default if (@ARGV[0] ne "") { $fileLocation = @ARGV[0]; #make sure it ends in a / if (!($fileLocation =~ /\/$/)) { $fileLocation = $fileLocation . "/"; } # if it's on a CD, we want to use /tmp to process files in... # (Since we can't write to a CD...) $onCdrom = 0; # if 'cdrom' is in the path, get the data from the cdrom if ($fileLocation =~ /cdrom/) { $onCdrom = 1; } } else { # if they didn't specify another location, use the default $fileLocation = $defaultFileLocation; } # if the directory doesn't exist, we can't process anything so quit if (!(-e $fileLocation)) { print "\nERROR: Can not access directory: \n"; print " $fileLocation \n"; print "Exiting Script... \n\n"; exit(1); } # open the directory to process files in opendir(CWD, $fileLocation); # get the list of all the files in the directory @dirListing = grep{/$fileNameSearchPattern/} readdir(CWD); # close the directory closedir(CWD); print "****************************************** \n"; print "Processing Images from $fileLocation \n"; print "****************************************** \n"; # for each file in the directory, get the base file name (before any extensions like .D / .L / .T # Assumption: Name is in format: 'NSS.R1.GT.D00114.T101926.P44N066.P43N060.A' foreach $item(@dirListing) { chomp $item; $mainFileName = substr($item, 0, $mainFileNameLength); # check to make sure the corresponding .L and .T files exist for the .D file $check = triadCheck($mainFileName); if ($check == 1) { # if they do, add the file to the list of files to process push @listToConvert, $mainFileName; } elsif ($check == 0) { print "\n"; print "WARNING: Corresponding .L and .T files not found for: \n"; print "$fileLocation$mainFileName\n"; print "Not Converting to jpeg... \n"; print "\n"; } } print "\nWriting IDL Script -> $idlScriptFile.pro \n\n"; # open the IDL script file to write to open (OUTPUT, ">$idlScriptFile.pro"); # start writing stuff to the file... print OUTPUT "pro $idlScriptFile \n"; print OUTPUT "\n"; print OUTPUT "print, 'Opening envi' \n"; print OUTPUT "envi, /restore_base_save_files \n"; print OUTPUT "envi_init, /batch_mode, batch_unit=unit \n"; print OUTPUT "envi_check_save, /filter \n"; print OUTPUT "restore, 'envi_ext.sav' \n"; print OUTPUT "\n"; # loop to process each file in the list of files to convert $sizeToConvert = @listToConvert; $iteration = 1; foreach $item(@listToConvert) { chomp $item; print OUTPUT "print, '***************************************' \n"; print OUTPUT "print, 'Processing File $iteration out of $sizeToConvert:' \n"; $iteration++; # if the file is on a CDROM, we have to use /tmp for our staging directory # (staging meaning directory to write tmp files to...) if ($onCdrom) { $dataFileSet = $fileLocation . $item . ".*"; $dataFile = "/tmp/" . $item . $dataExt; $correctDataFile = "/tmp/" . $item . $correctDataExt; $leadFile = "/tmp/" . $item . $leaderExt; $correctLeadFile = "/tmp/" . $item . $correctLeaderExt; $zipFile = "/tmp/" . $item . $dataExt . $zipExt; print OUTPUT "print, 'Copying file from CDROM to /tmp' \n"; print OUTPUT "SPAWN, 'cp $dataFileSet /tmp' \n"; } else { $dataFile = $fileLocation . $item . $dataExt; $correctDataFile = $fileLocation . $item . $correctDataExt; $leadFile = $fileLocation . $item . $leaderExt; $correctLeadFile = $fileLocation . $item . $correctLeaderExt; $zipFile = $dataFile . $zipExt; } print OUTPUT "print, ' $dataFile' \n"; # if the file is zipped, upzip it if (-e $zipFile) { print OUTPUT "SPAWN, '$unzipCmd $zipFile ' \n"; } # rename the files to have .dat and .lea extensions so that ENVI won't prompt for the leader file print OUTPUT "SPAWN, 'mv $dataFile $correctDataFile' \n"; print OUTPUT "SPAWN, 'mv $leadFile $correctLeadFile' \n"; # set up the name of the trailer file to get data from to add to picture $trailer = $fileLocation . $item . $trailerExt; # find which lake the image is from $dirToWrite = findWhichLake($dataFile); $jpegToWrite = $item . ".D"; # find out if it's ascending or descending (for which way to rotate it) $direction = directionCheck($trailer); # process the file print OUTPUT "cmd = 'ProcessHeaderInfo.pl $trailer ' \n"; print OUTPUT "SPAWN, cmd \n"; print OUTPUT "RC = ProcessImageFile('$correctDataFile', '$dirToWrite', '$jpegToWrite', $direction) \n"; # if it's on the CDROM, clean up our temp files from /tmp if ($onCdrom) { # delete the tmp file we made print OUTPUT "SPAWN, 'rm /tmp/$item.*' \n"; print OUTPUT "\n"; } # else move the files back to their original states else { print OUTPUT "print, 'Zipping $dataFile...' \n"; print OUTPUT "SPAWN, 'mv $correctDataFile $dataFile' \n"; print OUTPUT "SPAWN, 'mv $correctLeadFile $leadFile' \n"; print OUTPUT "SPAWN, '$zipCmd $dataFile' \n"; print OUTPUT "print, 'Done Zipping file...' \n"; } $mainFile = substr($dataFile, 0, length($dataFile) -2); $mainFile = $mainFile . ".*"; # if we could open the data file, then add the text data from it from the Trailer file print OUTPUT "if (RC[0] ne 0) then begin \n"; print OUTPUT " SPAWN, 'mv $mainFile $processedDir' \n"; print OUTPUT "endif else begin \n"; print OUTPUT " SPAWN, 'mv $mainFile $holdingDir' \n"; print OUTPUT "endelse \n"; print OUTPUT "\n"; } # we're done processing all the files so close up shop print OUTPUT "print, 'Done processing images...' \n"; print OUTPUT "print, 'Calling UpdateWebPage.pl script to update web page with newly processed pictures' \n"; print OUTPUT "SPAWN, '/local/ht-geo/great_lakes/icegroup/UpdateWebPage.pl' \n"; print OUTPUT "envi_exit \n"; print OUTPUT "free_lun, unit \n"; print OUTPUT "end \n"; close (OUTPUT); print " ********************\n"; print " Script Complete... \n"; print " ********************\n\n"; print "IDL Script '$idlScriptFile' has been created.\n"; print "To start converting the files, run IDL.\n"; print "After IDL is running, type '.compile $idlScriptFile'\n"; print "Then, simply type '$idlScriptFile' and go from there. \n"; # ********************************************************************* # Functions used in script # ********************************************************************* # this function makes sure this is a corresponding .L and .T file for each .D sub triadCheck() { my $fileCheck = @_[0]; $return = 1; $lCheck = $fileLocation . $fileCheck . $leaderExt; $tCheck = $fileLocation . $fileCheck . $trailerExt; # if the leader file doesn't exist, return false if (!(-e $lCheck)) { $return = 0; } # if the trailer file doesn't exist, return false if (-e $tCheck) { # if filesize of trailer is 720, return error since # the data in there is not complete $size = (stat($tCheck))[7]; if ($size == 720) { $fileToMove = $fileLocation . $fileCheck . "*"; print "\n"; print "ERROR: .T file does not contain all of the needed"; print " information... \n"; print "Not Converting to jpeg... \n"; print "\n"; print "Moving $fileCheck to holding directory... \n"; `mv $fileToMove $holdingDir`; $return = -1; } } else { $return = 0; } return $return; } # see if the file is ascending or descending sub directionCheck() { my $fileCheck = @_[0]; open (TRAILER, $fileCheck); while () { $ascending = index($_, "ASCENDING"); } close (TRAILER); # if it's ascending, it will return the position it was found. If not, it will return -1. # that way we can tell if it's ascending or not. return $ascending; } # Find which lake the image is viewing sub findWhichLake() { # set the file name to the first (and only) argument passed in $fileName = @_[0]; chomp $fileName; # split the file name at the "."'s @sepParts = (split /\./, $fileName); # the upper left coord. will be the fifth element $upLeft = @sepParts[5]; # get the lat and long from the upper left $lat = substr($upLeft, 1, 2); $long = substr($upLeft, 4, 3); # parse it accordingly if ($lat >= 47 && $lat <= 52 && $long >= 87) { $lakeDir = "Superior/"; } elsif ($lat <= 47 && $long >= 87) { $lakeDir = "Michigan/"; } elsif ($lat <= 47 && $long <= 87 && $long >= 83) { $lakeDir = "Huron_Erie/"; } elsif ($long <= 83) { $lakeDir = "Ontario_Erie_StLaw/"; } else { $lakeDir = "Other/"; } @parts = split(/\//, $fileName); $length = @parts; # get the year from the file name so we know which year to put it under for the lake $year = substr(@parts[$length-1], 11, 2); $jpegName = @parts[$length-1]; # if year < 90, then year = 20 + year? if ($year == "00") { $year = "2000"; } elsif ($year == "01") { $year = "2001"; } else { $year = "19" . $year; } $year = $year . "/"; # this is the absolute path of where to write the jpeg to $location = $baseWebDir . $year . $lakeDir . $jpegName . "/"; return $location; }