#!/usr/bin/perl -w
# Add or subtract a model from the visibilities corresponding to viscode.
# Takes care to leave the history in the right state (so that ptsrcs can be run
# right after it) and preserve the original files (so that the operation can be
# backed out of if necessary).
use DRAORed;
$use = q {
Use: vismath viscode +/- model
};
unless($#ARGV == 2){
die $use;
}
my ($srcvc, $op, $modfn) = @ARGV;
if($op eq "+"){
$opl = "P";
}
elsif($op eq "-"){
$opl = "M";
}
else{
die $use;
}
$srcvc = "\U${srcvc}\E";
my $srcfn = <*.V21$srcvc>;
my $destnum = DRAORed::get_first_slot();
my $srcnum = DRAORed::fileindex($srcfn);
my $modnum = DRAORed::fileindex($modfn);
#die "destnum: $destnum, $srcfn: $srcnum, $modfn: $modnum";
my $modvc = substr($modfn, -2);
#$modvc =~ s/.*_MODEL//;
#die "modvc: $modvc";
my $destfile = "${srcvc}${opl}${modvc}MOD.V21";
print "$destfile (madr index $destnum) = $srcfn $op $modfn\n";
# Set up logging
my $coline = "vismath $srcvc $op $modfn";
my $vlog = "vismath_$srcvc$op${modfn}.log";
my $runnum = checklogd($vlog); # Create the logs directory if necessary.
$vlog = "$vlog$runnum";
open(HLOG, ">>logs/index.html")
or warn "Could not add entry to logs/index.html";
print HLOG "$coline
\n";
close(HLOG) or warn "Error closing logs/index.html";
open(VLOG, ">logs/$vlog") or warn "Could not log to logs/$vlog";
print VLOG "vismath\n";
print VLOG "-------\n\n";
# Copy the *model* def to the dest def instead of the src def. This is
# counterintuitive but correct. to_stokes sets the observation epoch of its
# output to 0, and some other tasks will not accept that. Amazingly subsequent
# images and uv files (including models) have the correct epoch.
open(MADR, "|madr >/dev/null 2>&1") or die "Could not |madr >/dev/null 2>&1";
print MADR "copy f$modnum $destnum\n";
print MADR "def f$destnum\n";
print MADR "$destfile\n";
for ($i = 0; $i < 60; ++$i) {
print MADR "\n"; # Cheat by giving lots of \n's.
}
print MADR "m\n"; # ?> m
# Roland says the way to avoid the history problem is M> f1m = f1 - f10,
# i.e. write the result over the input file. Yuck! Not gonna do that...
print MADR "f$destnum = f$srcnum $op f$modnum\n"; # M> f11 = f1 - f10
print MADR "exit\n"; # M> exit
print MADR "exit\n"; # ?> exit
close(MADR) or die "vismath bungled";
print VLOG "Copied ${modfn}'s def ($modnum) to ${destnum} and named it ${destfile}.\n";
sleep 2;
# I prefer to solve the history problem by renaming the input visibilities
# and linking the old visibility filename to $destfile.
madrrename($srcfn, "ORIG$srcfn");
print VLOG "madrrenamed $srcfn to ORIG$srcfn,\n";
sleep 1;
# ln -s the file to make it obvious what it really is.
unless(symlink($destfile, $srcfn)){
print VLOG "Error in symlink($destfile, $srcfn)!\nDying...\n";
close(VLOG) or warn "Error closing logs/$vlog";
die "Could not ln -s $destfile $srcfn!\n";
};
print VLOG "then did symlink($destfile, $srcfn).\n";
my $linkind = get_first_slot();
open(MADR, "|madr >/dev/null 2>&1") or die "Could not |madr >/dev/null 2>&1";
print MADR "copy f$destnum $linkind\n";
print MADR "def f$linkind\n";
print MADR "$srcfn\n";
for ($i = 0; $i < 60; ++$i) {
print MADR "\n"; # Cheat by giving lots of \n's.
}
close(MADR)
or die "Could not copy def $destnum to $linkind and name it ${srcfn}";
print VLOG "Copied def $destnum to $linkind and named it ${srcfn}.\n";
close(VLOG) or warn "Error closing logs/$vlog";