#!/usr/bin/perl -w # Automatically clean, from Roland Kothes' polproc.step2 script for reducing # CGPS 21cm polarization data. # # Souped up by RR to be a bit faster and more robust, and to add an entry to # logs/index.html. # # Nov. 25, 2004: Now guesses the values of unspecified arguments. Consider # yourself warned. use DRAORed; my $use = q { Use: autoclean [dirty.map] [dirtybeam.P##] [limit (mJy)] If dirty.map is not specified, it defaults to CLEANing the youngest image. If dirtybeam.P## is not specified, it defaults to using the youngest beam. If limit is not specified, it defaults to using 1.0. }; if(@ARGV == 0){ print "Using defaults! If you do not want that...\n"; print $use; } elsif($ARGV[0] =~ /-h/){ die $use; } my $clean_prog = "/home/rreid/drao/unappl/clean/clean"; #/usr/local/export/bin/clean my $dmap; my $dbeam; my $clean_limit = 1.0; foreach(@ARGV){ if(/\.P(IV)?(21|74)$/){ $dbeam = $_; } elsif(/^[0-9.]+$/){ $clean_limit = $_; } else{ $dmap = $_; } } unless(defined($dmap)){ my @images; if(defined($dbeam)){ if($dbeam =~ /21$/){ @images = latestfile("*.M*21"); } else{ @images = latestfile("*.M*74"); } } else{ @images = latestfile("*.M*[27][14]"); } $dmap = $images[-1]; } unless(defined($dbeam)){ my @beams; if(defined($dmap)){ if($dmap =~ /21$/){ @beams = latestfile("*.P21"); } else{ @beams = latestfile("*.P74"); } } else{ @beams = latestfile("*.P[27][14]"); } $dbeam = $beams[-1]; } $dmapind = fileindex($dmap); $dbeamind = fileindex($dbeam); #print "dmapind = $dmapind, dbeamind = $dbeamind\n"; my ($stokes, $band) = get_stokesandbandfromdir(); #print "stokes = $stokes, band = $band\n"; my ($max, $min) = getextrema($dmapind); #print "max = $max, min = $min\n"; # Find the number of spacings used, in order to predict the quality of the map. my $minspac = 3; # Default my $maxspac = 144; # Default my $nant = 7; # Default open(PH2LOG, `grep -l $dmap ph2*.LOG logs/ph2*.LOG 2>/dev/null`); while(){ if(/Minimum spacing:[ \t]+([0-9]+)[ \t]+Maximum spacing:[ \t]+([0-9]+)/){ $minspac = $1; $maxspac = $2; print "minspac: $minspac maxspac: $maxspac\n"; } if(/Antennas Used:\s+(.*)/){ $nant = $1; $nant =~ s/\s//g; $nant = length($nant); } } close(PH2LOG); my $spacspread = $maxspac - $minspac; # Check if there are already CLEAN files and remove them. foreach (){ remove_file($_); } $min = -$min; if($min > $max){$max = $min}; $clean_limit /= $max; open(CLEAN, "|$clean_prog"); print CLEAN "\n"; #! Do you wish to run in batch mo print CLEAN "\n"; #! Do you want to create a log? [ print CLEAN "\n"; #! Is log file to be printed on e print CLEAN "\n"; #! Run CLEAN in debug mode? [N]: print CLEAN "\n"; #! Is a previous CLEAN being rest print CLEAN "n\n"; #! Do you wish to receive this pr print CLEAN "$dmapind\n"; #! File definition number [DEFI print CLEAN "$dbeamind\n"; #! File definition number [DEFI print CLEAN "\n"; #! Do you want to use this as the print CLEAN "\n"; #! File definition number [DEFI print CLEAN "CLN${band}.M${stokes}21\n"; #! File name (max 52 chars) [] (" print CLEAN "\n"; #! Are the parameters that you en print CLEAN "\n"; #! Do you want a comment? [N]: print CLEAN "\n"; #! Do you want to save this defin print CLEAN "\n"; #! File definition number [DEFI print CLEAN "CC${band}.M${stokes}21\n"; #! File name (max 52 chars) [] (" print CLEAN "\n"; #! Are the parameters that you en print CLEAN "\n"; #! Do you want a comment? [N]: print CLEAN "\n"; #! Do you want to save this defin print CLEAN "\n"; #! Do you want the residual map(s print CLEAN "\n"; #! Are extra CLEAN components to print CLEAN "n\n"; #! OK? [Y]: print CLEAN "1\n"; #! Item number [0]: print CLEAN "200\n"; #! Maximum TOTAL number of iterat print CLEAN "2\n"; #! Item number [0]: print CLEAN "$clean_limit\n"; #! Limiting fraction of initial p print CLEAN "3\n"; #! Item number [0]: my $sdifract = 11969.3368 / ($max * $maxspac * sqrt($spacspread) * $nant / 7); if($#ARGV > 1){ $sdifract *= sqrt($ARGV[2]); } if($sdifract > 0.75){ # 0.75 is a guess. Clark CLEAN can cause a failure $sdifract = 1.0; # if used when SDI should have been used right away. } print CLEAN "$sdifract\n"; #! Fraction of initial peak for s print CLEAN "4\n"; #! Item number [0]: print CLEAN "199999\n"; #! Maximum number of components [ print CLEAN "\n"; #! Item number [0]: if($sdifract < 1.0){ # The number of questions depends on whether print CLEAN "\n"; #(! OK? [Y]:) Clark CLEAN is used. } print CLEAN "\n"; #! OK? [Y]: print CLEAN "\n"; #! OK? [Y]: print CLEAN "\n"; #! Is residual map to be displaye close(CLEAN) or warn "Error closing clean!"; # Add an entry to index.html my $runnum = checklogd("clean*.LOG"); my $cline = join(" ", @ARGV); $cline = "autoclean $cline"; my $aclog = "clean${runnum}.html"; open(LOG, ">>logs/index.html") or die "Could not add entry to logs/index.html"; print LOG "$cline
\n"; close(LOG) or die "Could not close logs/index.html!"; open(LOG, ">logs/$aclog") or die "Could not open logs/$aclog"; print LOG "\n$cline\n\n\n"; print LOG "

$cline

\n"; print LOG "|max|: $max  \t  "; print LOG "SDI switch level: ", $sdifract * $max, "
\n"; print LOG "minspac: $minspac  \t  maxspac: $maxspac

\n"; my $clog = `ls -1t clean*.LOG | head -1`; chomp $clog; unless(system("gzip $clog")){ $clog .= ".gz"; } print LOG "Clean log: $clog\n"; print LOG "\n"; close(LOG); mvlogww($clog);