package Subs::UvotProduct;
##############################################################################
#
# DESCRIPTION: Produce UVOT products: 2 gif lightcurve files
#
#
# HISTORY:
#
# VERSION: 0.0
#
#
#
##############################################################################
use Subs::Sub;
use Util::Log;
#@ISA = ("Subs::Sub","Util::Log");
@ISA = ("Subs::Sub");
use strict;
sub new {
my $proto=shift;
my $self=$proto->SUPER::new();
$self->{DESCRIPTION}="Make UVOT products: 2 gif lightcurves";
return $self;
}
##################
# METHODS:
##################
sub body {
my $self=shift;
my $log =$self->log();
my $filename=$self->filename();
my $jobpar =$self->jobpar();
##################################
# make uvot 2 lightcurve gif files
##################################
$self->make_uvot_gif_files();
} # end of body method
#############################################################################
# produce 2 uvot grb light curve gif files
#############################################################################
sub make_uvot_gif_files {
my $self=shift;
my $log =$self->log();
my $filename=$self->filename();
my $jobpar =$self->jobpar();
my $procpar = $self->procpar();
my $watchers = $procpar->read("watchers");
my $trigtime = $jobpar->read('burst_time');
###########FIND GRB only
my $trigid =$jobpar->read('sequence');
$trigid=~s/\d{3}$//;
$trigid = $trigid*1;
my $key1=0;
my $key2=0;
if (20000<=$trigid && $trigid<30000) {
$key1=1;
}
if ($trigid>=100000) {
$key2=1;
}
if ( ! ($key1 + $key2) ) {
$log->error(1, "This is not GRB, not making UVOT GIF files: exit 1");
return;
}
########CHECK IF WE HAVE ALL FILES NEEDED##############
my @imglist1=$filename->get('filterimg', 'uvot', '*', '*');
if (! @imglist1) {
$log->error(1, "No Uvot image file not exist, exit 1");
return ;
}
my @imglist2=();
foreach my $f (@imglist1) {
if ($f =~m/\S+ugu*/) {
next;
} #exclude grizm files
push(@imglist2, $f);
}
#################################ADD KEYWORDS
$self->Subs::XrtGrbLc::write_standard_keyword(@imglist2);
#################################ADD KEYWORDS
my $rv = $self->AddTrigtime(\@imglist2, $trigtime, $trigid);
my $trigstr = 'TRIGTIME';
if($rv == 0){
$log->error(1, "TRIGTIME not defined, Unable to proceed");
my $enddate = $jobpar->read("enddate");
my $endtime = $jobpar->read("endtime");
my $dateobj = Util::Date->new($enddate, $endtime);
my $emjd = $dateobj->mjd();
my $ndateobj = Util::Date->new();
my $nmjd = $ndateobj->mjd();
my $alerT = $procpar->read("alerTime");
if (!defined $alerT ) {
$alerT = 2.0;
}
my $dt = $nmjd - $emjd;
if ($dt > $alerT) {
my $object = $jobpar->read('object');
my $trigid = $jobpar->read('sequence');
my $target = $jobpar->read('target');
my $subject = "Unable to obtain TRIGTIME from database or GRB catalog";
my $msg = "Unable to find info about TRIGTIME (burst_time) in either database\nor GRB catalogs for the following GRB:\n\nobject= $object\nsequence= $trigid\ntarget= $target\n\n\tPlease, make sure that such info is actually available.\n\n\tYou can add the required info to the local GRB catalog\nby using\n\n/data/sdc/local/data/sdc4/apsop/Processman/PMT/bin/AddGRB.pl\n\n";
$self->Subs::XrtGrbLc::sendEmail($subject, $msg, $watchers);
}
return;
}
my $imglist = join(',', @imglist2);
my ($batpos,$xrtpos,$uvotpos,$groundpos)=0;
my($t,$t1,@table,@t8)=0;
my($i,$x,$xx,$y,$z,$zz)=0;
my ($batra,$batdec,$baterr,$xrtra,$xrtdec,$xrterr,$uvotra,$uvotdec,$uvoterr)=0;
##########GET GRB COORD
# First try Lorella catalog, if it fails,
# then try JD catalog, then try local catalog,
# and finally send an email.
my $refPL = $self->Subs::UvotProduct::get_grb_coord_local(); # read local catalog
if(defined $refPL and $refPL == -1){
return;
}
my $refP = $self->Subs::UvotProduct::get_swiftgrb_coord(); # Lorella catalog
if(defined $refP and $refP == -1){
return;
}
if (!defined $refP){
$refP = $self->Subs::UvotProduct::get_grb_coord(); # JD catalog
}
if (!defined $refP and !defined $refPL) {
my $enddate = $jobpar->read("enddate");
my $endtime = $jobpar->read("endtime");
my $dateobj = Util::Date->new($enddate, $endtime);
my $emjd = $dateobj->mjd();
my $ndateobj = Util::Date->new();
my $nmjd = $ndateobj->mjd();
my $alerT = $procpar->read("alerTime");
if (!defined $alerT ) {
$alerT = 2.0;
}
my $dt = $nmjd - $emjd;
if ($dt > $alerT) {
my $object = $jobpar->read('object');
my $trigid = $jobpar->read('sequence');
my $target = $jobpar->read('target');
my $subject = "GRB not found in catalogs";
my $msg = "Unable to find GRB info in all catalogs for:\nobject= $object\nsequence= $trigid\ntarget= $target\n\n\tPlease, make sure that such info is actually available.\n\n\tYou can add the required info to the local GRB catalog\nby using\n\n/local/data/sdc4/apsop/Processman/PMT/bin/AddGRB.pl\n\n";
$self->Subs::XrtGrbLc::sendEmail($subject, $msg, $watchers);
}
$log->error(1, "Unable to find GRB in catalogs. Email notice sent to $watchers");
return;
}
my @rItems = qw/bat_ra bat_dec bat_pos_err xrt_ra xrt_dec xrt_pos_err uvot_ra uvot_dec uvot_pos_err ot_ra ot_dec ot_pos_err/;
if(defined $refPL){
if(!defined $refP){
foreach my $it (@rItems){
$refP->{$it} = $refPL->{$it};
}
} else {
if((defined $refPL->{bat_over} and $refPL->{bat_over} == 1) or
(!defined $refP->{bat_ra} or !defined $refP->{bat_dec})){
$refP->{bat_ra} = $refPL->{bat_ra};
$refP->{bat_dec} = $refPL->{bat_dec};
$refP->{bat_pos_err} = $refPL->{bat_pos_err};
$log->entry("Using BAT coordinates from Local Catalog");
}
if((defined $refPL->{xrt_over} and $refPL->{xrt_over} == 1) or
(!defined $refP->{xrt_ra} or !defined $refP->{xrt_dec})){
$refP->{xrt_ra} = $refPL->{xrt_ra};
$refP->{xrt_dec} = $refPL->{xrt_dec};
$refP->{xrt_pos_err} = $refPL->{xrt_pos_err};
$log->entry("Using XRT coordinates from Local Catalog");
}
if((defined $refPL->{ot_over} and $refPL->{ot_over} == 1) or
(!defined $refP->{ot_ra} or !defined $refP->{ot_dec})){
$refP->{ot_ra} = $refPL->{ot_ra};
$refP->{ot_dec} = $refPL->{ot_dec};
$refP->{ot_pos_err} = $refPL->{ot_pos_err};
$log->entry("Using OPT coordinates from Local Catalog");
}
if((defined $refPL->{uvot_over} and $refPL->{uvot_over} == 1) or
(!defined $refP->{uvot_ra} or !defined $refP->{uvot_dec})){
$refP->{uvot_ra} = $refPL->{uvot_ra};
$refP->{uvot_dec} = $refPL->{uvot_dec};
$refP->{uvot_pos_err} = $refPL->{uvot_pos_err};
$log->entry("Using UVOT coordinates from Local Catalog");
}
}
}
############ARRAY OF ALL GRB COORD AND ERRORS @t8:
# 0 1 2 3 4 5 6 7 8
@t8=($batra,$batdec,$baterr,$xrtra,$xrtdec,$xrterr,$uvotra,$uvotdec,$uvoterr);
############INIT :
$batpos=$xrtpos=$uvotpos="";$groundpos='NONE';
###########IF XRT COORD NOT AVAILABLE THEN EXIT :
if ((!defined $refP->{xrt_ra} and !defined $refP->{xrt_dec} and !defined $refP->{xrt_pos_err}) or
(defined $refPL->{xrt_bad_data} and $refPL->{xrt_bad_data} == 1)){
$log->error(1, "BAT coordinates only not enough: exit 1");
return ;
}
#########DEC>90 <-90
if((defined $refP->{bat_dec} and abs($refP->{bat_dec}) > 90) ||
(defined $refP->{xrt_dec} and abs($refP->{xrt_dec}) > 90) ||
(defined $refP->{uvot_dec} and abs($refP->{uvot_dec}) > 90)){
$log->error(2, "DEC coordinates >90, <-90 need fix: exit 1");
return ;
}
###########ERR TOO BIG
if ((defined $refP->{xrt_pos_err} and $refP->{xrt_pos_err} > 10) or (defined $refP->{uvot_pos_err} and $refP->{uvot_pos_err} > 5)) {
$log->error(2, "XRT err>10, UVOT err>5 need fix: exit 1");
return ;
}
###########ERR TOO BIG >3
# if($t8[2]>3 || $t8[5]>3 || $t8[8]>3){#or >4 ?
# $log->error(2, "BAT,XRT,UVOT err>3, uvotproduct not ran: exit 1");
# return ;
# }
###########IF UVOT COORD NOT AVAILABLE REPLACE WITH NONE :
if ((!defined $refP->{uvot_ra} or !defined $refP->{uvot_dec} or !defined $refP->{uvot_pos_err}) or
(defined $refPL->{uvot_bad_data} and $refPL->{uvot_bad_data} == 1)){
$uvotpos='NONE';
} else {
$uvotpos='FOUND';
}
if ((!defined $refP->{ot_ra} or !defined $refP->{ot_dec} or !defined $refP->{ot_pos_err}) or
(defined $refPL->{ot_bad_data} and $refPL->{ot_bad_data} == 1)){
$groundpos='NONE';
} else {
$groundpos='FOUND';
}
###########PREPARE GRB COORD TO UVOTPRODUCT :
# 0 1 2 3 4 5 6 7 8
#@t8=($batra,$batdec,$baterr,$xrtra,$xrtdec,$xrterr,$uvotra,$uvotdec,$uvoterr);
my $srcreg='';
if($uvotpos ne 'NONE'){
$uvotpos=($refP->{uvot_dec}>0 ? $refP->{uvot_ra}.'+'.$refP->{uvot_dec}.'~'.$refP->{uvot_pos_err} : $refP->{uvot_ra}.$refP->{uvot_dec}.'~'.$refP->{uvot_pos_err});
if($srcreg eq ''){ $srcreg="fk5;circle\(".$refP->{uvot_ra}.','.$refP->{uvot_dec}.','.$refP->{uvot_pos_err}."\")"; }
}
#my($gr,$grra,$grdec,$grerr,$grb)=0;
if ($groundpos ne 'NONE') {
$groundpos=($refP->{ot_dec}>0 ? $refP->{ot_ra}.'+'.$refP->{ot_dec}.'~'.$refP->{ot_pos_err} : $refP->{ot_ra}.$refP->{ot_dec}.'~'.$refP->{ot_pos_err} );
if ($srcreg eq '') {
$srcreg="fk5;circle\(".$refP->{ot_ra}.','.$refP->{ot_dec}.','.$refP->{ot_pos_err}."\")";
}
}
$xrtpos =($refP->{xrt_dec}>0 ? $refP->{xrt_ra}.'+'.$refP->{xrt_dec}.'~'.$refP->{xrt_pos_err} : $refP->{xrt_ra}.$refP->{xrt_dec}.'~'.$refP->{xrt_pos_err});
if (!$srcreg) {
$srcreg="fk5;circle\(".$refP->{xrt_ra}.','.$refP->{xrt_dec}.','.$refP->{xrt_pos_err}."\")";
}
if (defined $refP->{bat_ra} and
defined $refP->{bat_dec} and
defined $refP->{bat_pos_err}) {
$batpos =($refP->{bat_dec}>0 ? $refP->{bat_ra}.'+'.$refP->{bat_dec}.'~'.$refP->{bat_pos_err} : $refP->{bat_ra}.$refP->{bat_dec}.'~'.$refP->{bat_pos_err});
} else {
$batpos = 'NONE';
}
if ($uvotpos eq 'NONE' && $refP->{xrt_pos_err} >3) {
my $xer = $refP->{xrt_pos_err};
$srcreg=~s/(\S+\d+\.\d+),(\d+\.\d+),(\d+\.\d+)\"\)/$1,$2,$xer\"\)/;
}
open(F,">src.reg");
print F "$srcreg\n";
close(F);
########MAKE UVOT GRB LIGHT CURVE 2 GIF FILES##############
################NEW DEVELOP UVOTPRODUCT :
#my $cmd="uvotproduct infile=$imglist outfile=maghist.fits plotfile=maghist.gif batpos=$batpos xrtpos=$xrtpos uvotpos=$uvotpos reportfile=summary.txt chatter=5 groundpos=$groundpos, srcreg=src.reg bkgreg=DEFAULT ";
# system("source /software/lheasoft/develop/headas.sh;$cmd");
#################RELEASE UVOTPRODUCT :
my $plotfile = $filename->get("lcplot", "uvot", "sr", 0);
my $maghist = $filename->get("lightcurve","uvot","sr",0);
my $uvotproduct=Util::HEAdas->new("uvotproduct")->is_script(1);
$uvotproduct->params({
infile => $imglist,
outfile => $maghist,
plotfile => $plotfile,
srcreg => 'src.reg',
bkreg => 'DEFAULT',
batpos => $batpos,
xrtpos => $xrtpos,
uvotpos => $uvotpos,
groundpos=> $groundpos,
reportfile => 'report.txt',
timezero => $trigstr,
plotmag => 'yes',
fcprefix => 'NONE',
zerofile => 'CALDB',
coinfile => 'CALDB',
psffile => 'CALDB',
lssfile => 'NONE',
# apercorr => 'NONE',
apercorr => 'CURVEOFGROWTH',
nsigma => 3.0,
exclude => 'DEFAULT',
frametime => 'DEFAULT',
centroid => 'no',
# fwhmsig => 3,
fwhmsig => -1,
rebin => 'DEFAULT',
clobber => 'yes',
cleanup => 'yes',
chatter => 5,
qdpfile => 'NONE',
mode => 'ql'
});
$uvotproduct->verbose( 2 );
$uvotproduct->seriousness( 1 );
$uvotproduct->run();
my $plotfile1 = $filename->get("lcplot", 'uvot', 'sr', 1);
my $plotfile2 = $filename->get("lcplot", 'uvot', 'sr', 2);
my $prefix = (split/\./, $plotfile)[0];
my $p1 = $prefix.'1.gif';
my $p2 = $prefix.'2.gif';
if(-e $p1){
rename $p1, $plotfile1;
}
if(-e $p2){
rename $p2, $plotfile2;
}
}# end of make_uvot_gif_files method
##########################################################
# convertRAStringToDegrees -
#
# converts RA in HH:MM:SS.SSSS, HH MM SS.SSSS, HHhMMmSS.SSSSs
# or DDD.DDDD to degrees
#
# Inputs:
# - input RA string
#
# Outputs:
# - output scalar RA in degrees
# - status variable
#
##########################################################
sub convertRAStringToDegrees {
my $inRa = shift;
my $ra = -1;
my $status = 0;
if ( $inRa =~ /^(\d{2})([: h]|h\s+)(\d{2})([: m]|m\s+)(\d{2}(\.\d*)?)s?$/ ) {
my $h = $1 * 3600.;
my $m = $3 * 60.;
my $s = $5 * 1.;
$ra = ( $h + $m + $s ) / (60. * 4.);
# debug( "converted $inRa to $ra degrees\n" );
} elsif ( $inRa =~ /^(\d+\.?\d*|\.\d+|\d+\.)$/ ) {
$ra = $inRa * 1.0;
} else {
$status ="$inRa is invalid RA string in convertRAStringToDegrees\n".
"Use HH:MM:SS.SS, HHhMMmSS.SSs, HH MM SS.SS or DDD.DD\n";
# $status = -1;
}
return ( $status, $ra );
}
##########################################################
#
# convertDecStringToDegrees -
#
# converts Dec in [+-]DD:MM:SS.SSSS, [+-]DD MM SS.SSSS, [+-]DDdMMmSS.SSSSs
# or DDD.DDDD to degrees
#
# Inputs:
# - input Dec string
#
# Outputs:
# - output scalar Dec in degrees
# - status variable
#
##########################################################
sub convertDecStringToDegrees {
my $inDec = shift;
my $dec = -1;
my $status = 0;
if ( $inDec =~ /^([\+-]?\d{2})([: d]|d\s+)(\d{2})([: m]|m\s+)(\d{2}(\.\d*)?)s?$/ ) {
my $d = $1;
my $m = $3 / 60.;
my $s = $5 / 3600.;
my $sign = ( $d < 0 || $d =~ /-00/ ) ? -1. : 1.;
$dec = $sign * ( $d * $sign + $m + $s );
# debug( "converted $inDec to $dec degrees\n" );
} elsif ( $inDec =~ /^[\+-]?(\d+\.?\d*)|(\.\d+)|(\d+\.)$/ ) {
$dec = $inDec * 1.0;
} else {
$status ="$inDec is invalid Dec string in convertDecStringToDegrees\n".
"Use HH:MM:SS.SS, HHhMMmSS.SSs, HH MM SS.SS or DDD.DD\n";
# $status = -1;
}
return ( $status, $dec );
}
#####################################################################
#sdc:apsop$ crontab -l|grep grb -A6 -B 1
###### get GRB coordinates from heasarc web site every night 1AM ######
#0 1 * * * /aps/scripts/grb_coord.pl >/aps/db/grb_coord/grb.txt
#0 1 * * * /aps/tools/headas/current/i686-pc-linux-gnu-libc2.2.5/bin/xwebquery.pl url=/docs/swift/archive/grbsummary/grb_pages/swiftgrb.tdat host=heasarc.gsfc.nasa.gov >/aps/db/grb_coord/swiftgrb.tdat
#sdc:apsop$ uname -n
#sdc
#-sh-3.00$ crontab -l
#11 1 * * * cp -pf /Heasarc_dev/dba_dbase/work/swiftgrb/hdb/swiftgrb.tdat /www.prod/htdocs/docs/swift/archive/grbsummary/grb_pages/swiftgrb.tdat
#-sh-3.00$ uname -n
#heasarcdev
#######################GET LORELLA GRB COORD##########################
sub get_swiftgrb_coord{
my $self=shift;
my $log =$self->log();
my $filename=$self->filename();
my $jobpar =$self->jobpar();
my $GRB = $jobpar->read('object');
my $target = undef;
my $swiftcat = '/aps/db/grb_coord/swiftgrb.tdat';
my $hostname = `hostname`;
chomp $hostname;
if($hostname =~ /^sdcdev/){
$swiftcat = '/data/prodsdc/local/data/sdc1/apsop/db/grb_coord/swiftgrb.tdat';
}
if($GRB !~ /^GRB\s*\d{6}[A-Z]*/){
$target = $jobpar->read('target');
$target =~ s/^0//;
$target =~ s/^0//;
}
my $trigid =$jobpar->read('sequence');
$trigid=~s/\d{3}$//; $trigid=~s/^00//;
$GRB=~s/GRB//g; $GRB=uc($GRB);
my $refP = undef;
###########FIND GRB only
my $key1=0;
my $key2=0;
if (20000<=$trigid && $trigid<30000) {
$key1=1;
}
if ($trigid>=100000) {
$key2=1;
}
my $prog = (split/\./,(split/\//,(caller())[1])[-1])[0];
if ( ! ($key1 + $key2) ) {
if($prog ne 'XrtEvents' and $prog ne 'XrtProducts'){
$log->error(1, "This is not GRB, $prog not ran: exit 1");
}
return $refP;
}
######################## COPY swiftgrb.tdat file from /aps/db/grb_coord to here
system("\$LOCKIT $swiftcat;cp $swiftcat .;\$UNLOCKIT $swiftcat");#for sdc2
if(! -e "swiftgrb.tdat"){
system("\$LOCKIT $swiftcat;cp $swiftcat .;\$UNLOCKIT $swiftcat");#for sdc2;
}
my $refItems = undef;
my @Items = qw/name target_id bat_ra bat_dec bat_pos_err xrt_ra xrt_dec xrt_pos_err uvot_ra uvot_dec uvot_pos_err ot_ra ot_dec ot_pos_err/;
my @rItems = qw/bat_ra bat_dec bat_pos_err xrt_ra xrt_dec xrt_pos_err uvot_ra uvot_dec uvot_pos_err ot_ra ot_dec ot_pos_err/;
foreach my $it (@Items){
$refItems->{$it} = -1;
}
my $strer = '';
my $flgR = 0;
if (open INF, "swiftgrb.tdat"){
while (my $line = <INF>){
chomp $line;
next if ($line =~ /^\#/);
if ($line =~ /^\s*line\[1\]\s*\=/){
my @tar = split /\s+/, (split/\s*\=\s*/, $line)[1];
for(my $i=0; $i<scalar(@tar); $i++){
if(exists $refItems->{$tar[$i]}){
my $j = $i;
$refItems->{$tar[$i]} = $j;
}
}
$flgR = 1;
}elsif ($line =~ /^\s*\<DATA\>/ and $flgR == 1){
$flgR = 2;
} elsif ($flgR == 2){
if (!defined $target and $line =~ /^\s*GRB\s*$GRB/) {
my @art = split/\|/, $line;
my $tgrb = $art[0];
$tgrb =~ s/^\s*GRB\s*//;
$tgrb = uc($tgrb);
my $gf = 0;
if($GRB =~ /\d{6}[A-Z]/ and $GRB eq $tgrb){
$gf = 1;
} else {
if($GRB =~ /$tgrb.'[A]*'/){
$gf = 1;
}
}
if ($gf == 1) {
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
if ($idx != -1) {
if ($art[$idx] eq '' or $art[$idx] eq 'n/a') {
$art[$idx] = undef;
}
$refItems->{$key} = $art[$idx];
} else {
$strer .= "Unable to find value for $key. ";
}
}
$flgR = 3;
last;
}
} elsif(defined $target) {
my @art = split/\|/, $line;
my $tidx = $refItems->{target_id};
if(defined $tidx && $target eq $art[$tidx]){
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
if ($idx != -1) {
if ($art[$idx] eq '' or $art[$idx] eq 'n/a') {
$art[$idx] = undef;
}
$refItems->{$key} = $art[$idx];
} else {
$strer .= "Unable to find value for $key. ";
}
}
$flgR = 3;
last;
}
}
}
} # end of while loop
close INF;
} # end of if (open INF, "swiftgrb.tdat"){
if($flgR != 3){
$log->error(1, "GRB $GRB not found in catalog swiftgrb.tdat");
$refP = undef;
return $refP;
} elsif ($strer ne ''){
$log->error(1, "$strer, exit 1");
$refP = undef;
return $refP;
}
foreach my $it (@rItems) {
$refP->{$it} = $refItems->{$it};
}
return $refP;;
} # sub get_swiftgrb_coord
#######################GET JD GRB COORD##########################
sub get_grb_coord{
my $self=shift;
my $log =$self->log();
my $filename=$self->filename();
my $jobpar =$self->jobpar();
my $refP = undef;
my $GRB = $jobpar->read('object');
my $trigid =$jobpar->read('sequence');
$trigid=~s/\d{3}$//; $trigid=~s/^00//;
my $grbOut = '/tmp/GRB_'.$GRB.'.txt';
my $TI = undef;
if($GRB !~ /^GRB\s*\d{6}[A-Z]*/){
$TI = $trigid;
$grbOut = '/tmp/GRB_'.$TI.'.txt';
}
$GRB=~s/GRB//g;
$GRB=uc($GRB);
###########FIND GRB only
my $key1=0;
my $key2=0;
if (20000<=$trigid && $trigid<30000) {
$key1=1;
}
if ($trigid>=100000) {
$key2=1;
}
my $prog = (split/\./,(split/\//,(caller())[1])[-1])[0];
if ( ! ($key1 + $key2) ) {
$log->error(1, "This is not GRB, $prog not ran: exit 1");
return $refP;
}
# Remove file if it exists
# if(-e $grbOut){
# unlink $grbOut;
# }
# Make new file by querying catalog with specific ID for the given GRB
my $ssh = 'ssh -nq sdc ';
my $hostname = `hostname`;
chomp $hostname;
if($hostname =~ /^sdc/ or $hostname =~ /sdcproc/){
$ssh = '';
}
my $cmd = $ssh.' wget http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/grb_lookupLC.php?grb_name='.$GRB.' -O '.$grbOut." >& /dev/null";
if(defined $TI){
$cmd = $ssh.' wget http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/grb_lookupLC.php?trig_id='.$TI.' -O '.$grbOut." >& /dev/null";
}
my @GRBvals = ();
my $retval = system($cmd);
if($retval != 0){
$log->error(1, "Unable to reach catalog http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/: exit 1");
return $refP;
} else{
if($ssh ne ''){
my $cmd2 = "ssh -nq sdc cat $grbOut; rm -f $grbOut";
@GRBvals = `$cmd2`;
} else {
@GRBvals = `cat $grbOut`;
unlink $grbOut;
}
}
my $refGRB = undef;
my $flgNF = -1;
foreach my $line (@GRBvals){
chomp $line;
next if ($line =~ /^\s*$/);
if($line =~ /^\s*GRB\snot\sfound/){
$flgNF = 1;
last;
} elsif ($line =~ /No\s+such\s+file\s+or\s+directory/){
last;
}
if ($line =~ /\s*\=\s*/) {
my ($name, $value) = split/\s*\=\s*/, $line;
if ($value =~ /n\/a/i or !defined $value or $value eq '') {
$value = undef;
}
$refGRB->{$name} = $value;
if (defined $value) {
if ($value =~ /\d+\:\d+/) {
if ($name =~ /ra$/) {
my ($status, $ra) = convertRAStringToDegrees($value);
if ($status == 0) {
$refGRB->{$name} = $ra;
} else {
$refGRB->{$name} = undef;
}
} elsif ($name =~ /dec$/) {
my ($status, $dec) = convertDecStringToDegrees($value);
if ($status == 0) {
$refGRB->{$name} = $dec;
} else {
$refGRB->{$name} = undef;
}
}
$flgNF = 0;
}
}
}
}
if ($flgNF == 1){
$log->error(1, "GRB $GRB not found in JD catalog; exit 1");
return $refP;
} elsif ($flgNF == -1){
$log->error(1, "GRB $GRB not found: command $cmd must have failed; exit 1");
return $refP;
}
my @items = qw/batra batdec baterr xrtra xrtdec xrterr uvotra uvotdec uvoterr/;
my @rItems = qw/bat_ra bat_dec bat_pos_err xrt_ra xrt_dec xrt_pos_err uvot_ra uvot_dec uvot_pos_err/;
#Some sanity check while also writing out the data
my $erIt = '';
my $i = 0;
foreach my $it (@items) {
if (!exists $refGRB->{$it}) {
$refP->{$rItems[$i]} = undef;
} else {
$refP->{$rItems[$i]} = $refGRB->{$it};
}
$i++;
}
return $refP;
} #end of get_grb_coord
#######################GET LOCAL GRB COORD##########################
sub get_grb_coord_local{
my $self=shift;
my $log =$self->log();
my $filename=$self->filename();
my $jobpar =$self->jobpar();
my $procpar = $self->procpar();
my $refP = undef;
my $catalog = $procpar->read('LGrbCat');
if(!-e $catalog){
$log->error(1, "Local GRB catalog $catalog does NOT exists.");
return $refP;
}
my @Cat = split/\//, $catalog;
my $Lcat = $Cat[-1];
my $GRB = $jobpar->read('object');
my $target = undef;
if($GRB !~ /^GRB\s*\d{6}[A-Z]*/){
$target = $jobpar->read('target');
$target =~ s/^0//;
$target =~ s/^0//;
}
my $trigid =$jobpar->read('sequence');
$trigid=~s/\d{3}$//; $trigid=~s/^00//;
$GRB=~s/GRB//g; $GRB=uc($GRB);
###########FIND GRB only
my $key1=0;
my $key2=0;
if (20000<=$trigid && $trigid<30000) {
$key1=1;
}
if ($trigid>=100000) {
$key2=1;
}
my $prog = (split/\./,(split/\//,(caller())[1])[-1])[0];
if ( ! ($key1 + $key2) ) {
if($prog ne 'XrtEvents' and $prog ne 'XrtProducts'){
$log->error(1, "This is not GRB, $prog not ran: exit 1");
}
return -1;
}
######################## COPY swiftgrb.tdat file from /aps/db/grb_coord to here
system("\$LOCKIT $catalog;cp $catalog .;\$UNLOCKIT $catalog");#for sdc2
my $refItems = undef;
# my @Items = qw/name sequence target_id bat_ra bat_dec bat_pos_err xrt_ra xrt_dec xrt_pos_err uvot_ra uvot_dec uvot_pos_err ot_ra ot_dec ot_pos_err/;
# my @rItems = qw/bat_ra bat_dec bat_pos_err xrt_ra xrt_dec xrt_pos_err uvot_ra uvot_dec uvot_pos_err ot_ra ot_dec ot_pos_err/;
# foreach my $it (@Items){
# $refItems->{$it} = -1;
# }
my $strer = '';
my $flgR = 0;
if (open INF, "<$Lcat"){
while (my $line = <INF>){
chomp $line;
next if ($line =~ /^\#/);
if ($line =~ /^\s*line\[1\]\s*\=/){
my @tar = split /\s+/, (split/\s*\=\s*/, $line)[1];
for(my $i=0; $i<scalar(@tar); $i++){
$refItems->{$tar[$i]} = $i;
}
$flgR = 1;
}elsif ($line =~ /^\s*\<DATA\>/ and $flgR == 1){
$flgR = 2;
} elsif ($flgR == 2){
if (!defined $target and $line =~ /^\s*GRB\s+$GRB/) {
my @art = split/\|/, $line;
my $tgrb = $art[0];
$tgrb =~ s/^\s*GRB\s*//;
$tgrb = uc($tgrb);
my $gf = 0;
if($GRB =~ /\d{6}[A-Z]/ and $GRB eq $tgrb){
$gf = 1;
} else {
if($GRB =~ /$tgrb.'[A]*'/){
$gf = 1;
}
}
if ($gf == 1) {
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
my $it = $art[$idx];
if (!defined $it or $it eq '' or $it eq 'n/a') {
$it = undef;
}
$refItems->{$key} = $it;
}
$flgR = 3;
last;
}
} else {
my @art = split/\|/, $line;
my $sidx = $refItems->{target_id};
if($trigid eq $art[$sidx]){
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
my $it = $art[$idx];
if(!defined $it or $it eq '' or $it eq 'n/a'){
$it = undef;
}
$refItems->{$key} = $it;
}
$flgR = 3;
last;
}
}
}
} # end of while loop
close INF;
} # end of if (open INF, "swiftgrb.tdat"){
if($flgR != 3){
$log->error(1, "GRB $GRB not found in Local catalog.");
return $refP;
}
return $refItems;
} # sub get_grb_coord_local
sub AddTrigtime {
my $self=shift;
my ($refA, $trigtime, $trigid) = @_;
if(!defined $trigtime or $trigtime == 0){
$trigtime = $self->getTrigFromDB();
if(!defined $trigtime){
$trigtime = $self->getTrigFromSWIFTCatalog();
if(!defined $trigtime){
$trigtime = $self->getTrigFromJDCatalog();
if(!defined $trigtime){
$trigtime = $self->getTrigFromLocalCatalog();
}
}
}
}
my $rv = 0;
if(defined $trigtime){
foreach my $file (@$refA ) {
my $fits = Util::FITSfile->new($file);
my $nhdus = $fits->nhdus();
unless ($nhdus) {
next;
}
my $hdu;
for($hdu=0; $hdu<$nhdus; $hdu++) {
$fits->ext($hdu);
my $extname = $hdu==0 ? '' : $fits->keyword('EXTNAME');
################################
# write keywords to the file
################################
$fits->begin_many_keywords();
$fits->keyword('TRIGTIME', $trigtime, '[s] MET TRIGger Time for Automatic Target');
$fits->keyword('TARG_ID', $trigid, 'Target ID');
$fits->end_many_keywords();
}
$rv = 1;
}
}
return $rv;
}
sub getTrigFromDB {
my $self =shift;
my $log =$self->log();
my $filename=$self->filename();
my $jobpar =$self->jobpar();
my $targetid =$jobpar->read('sequence');
$targetid=~s/\d{3}$//;
my $RDB_BIN = '/aps/tools/rdb';
if(defined $ENV{RDB_BIN}){
$RDB_BIN = $ENV{RDB_BIN};
}
my $RDB_TABLES_DIR = '/aps/db/rdb/tables';
if(defined $ENV{RDB_TABLES_DIR}){
$RDB_TABLES_DIR = $ENV{RDB_TABLES_DIR};
}
my $db = "$RDB_TABLES_DIR/sw/swifttdrss.rdb";
my $host = `/bin/hostname`;
chomp $host;
if($host =~ /^sdcdev/i){
$db = "/data/sdc/local/data/sdc1/apsop/db/rdb/tables/sw/swifttdrss.rdb";
}
my $cmdb = "$RDB_BIN/row < $db target_id eq $targetid | $RDB_BIN/column time_seconds1 ufcf_corr -a utime 23N | $RDB_BIN/compute utime = time_seconds1 - ufcf_corr | $RDB_BIN/column utime | $RDB_BIN/headchg -del | head -1";
my $btime = `$cmdb`;
chomp $btime;
if($btime =~ /^\s*$/ or $btime == 0){
$btime = undef;
}
return $btime;
}
sub getTrigFromSWIFTCatalog {
my $self = shift;
my $log = $self->log();
my $filename = $self->filename();
my $jobpar = $self->jobpar();
my $GRB = $jobpar->read('object');
my $target = undef;
my $swiftcat = '/aps/db/grb_coord/swiftgrb.tdat';
my $hostname = `hostname`;
chomp $hostname;
if ($hostname =~ /^sdcdev/) {
$swiftcat = '/data/prodsdc/local/data/sdc1/apsop/db/grb_coord/swiftgrb.tdat';
}
if ($GRB !~ /^GRB\s*\d{6}[A-Z]*/) {
$target = $jobpar->read('target');
$target =~ s/^0//;
$target =~ s/^0//;
}
my $trigid =$jobpar->read('sequence');
$trigid=~s/\d{3}$//; $trigid=~s/^00//;
$GRB=~s/GRB//g; $GRB=uc($GRB);
my $refP = undef;
###########FIND GRB only
my $key1=0;
my $key2=0;
if (20000<=$trigid && $trigid<30000) {
$key1=1;
}
if ($trigid>=100000) {
$key2=1;
}
my $prog = (split/\./,(split/\//,(caller())[1])[-1])[0];
if ( ! ($key1 + $key2) ) {
$log->error(1, "This is not GRB, call from $prog not ran: exit 1");
return $refP;
}
######################## COPY swiftgrb.tdat file from /aps/db/grb_coord to here
system("\$LOCKIT $swiftcat;cp $swiftcat .;\$UNLOCKIT $swiftcat"); #for sdc2
if (! -e "swiftgrb.tdat") {
system("\$LOCKIT $swiftcat;cp $swiftcat .;\$UNLOCKIT $swiftcat"); #for sdc2;
}
my $refItems = undef;
my @Items = qw/name target_id trigger_time/;
my @rItems = qw/trigger_time/;
foreach my $it (@Items) {
$refItems->{$it} = -1;
}
my $strer = '';
my $flgR = 0;
if (open INF, "swiftgrb.tdat") {
while (my $line = <INF>) {
chomp $line;
next if ($line =~ /^\#/);
if ($line =~ /^\s*line\[1\]\s*\=/) {
my @tar = split /\s+/, (split/\s*\=\s*/, $line)[1];
for (my $i=0; $i<scalar(@tar); $i++) {
if (exists $refItems->{$tar[$i]}) {
my $j = $i;
$refItems->{$tar[$i]} = $j;
}
}
$flgR = 1;
} elsif ($line =~ /^\s*\<DATA\>/ and $flgR == 1) {
$flgR = 2;
next;
} elsif ($flgR == 2) {
if (!defined $target and $line =~ /^\s*GRB\s*$GRB/) {
my @art = split/\|/, $line;
my $tgrb = $art[0];
$tgrb =~ s/^\s*GRB\s*//;
$tgrb = uc($tgrb);
my $gf = 0;
if ($GRB =~ /\d{6}[A-Z]/ and $GRB eq $tgrb) {
$gf = 1;
} else {
if ($GRB =~ /$tgrb.'[A]*'/) {
$gf = 1;
}
}
if ($gf == 1) {
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
if ($idx != -1) {
if ($art[$idx] eq '' or $art[$idx] eq 'n/a') {
$art[$idx] = undef;
}
$refItems->{$key} = $art[$idx];
} else {
$strer .= "Unable to find value for $key. ";
}
}
$flgR = 3;
last;
}
} elsif (defined $target) {
my @art = split/\|/, $line;
my $tidx = $refItems->{target_id};
if (defined $tidx && $target eq $art[$tidx]) {
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
if ($idx != -1) {
if ($art[$idx] eq '' or $art[$idx] eq 'n/a') {
$art[$idx] = undef;
}
$refItems->{$key} = $art[$idx];
} else {
$strer .= "Unable to find value for $key. ";
}
}
$flgR = 3;
last;
}
}
}
} # end of while loop
close INF;
} # end of if (open INF, "swiftgrb.tdat"){
if ($flgR != 3) {
$log->error(1, "GRB $GRB not found in catalog swiftgrb.tdat");
$refP = undef;
} elsif ($strer ne '') {
$log->error(1, "$strer, exit 1");
$refP = undef;
}
foreach my $it (@rItems) {
$refP = $refItems->{$it};
}
return $refP;
}
sub getTrigFromJDCatalog {
my $self = shift;
my $log = $self->log();
my $filename = $self->filename();
my $jobpar = $self->jobpar();
my $GRB = $jobpar->read('object');
my $target = undef;
if ($GRB !~ /^GRB\s*\d{6}[A-Z]*/) {
$target = $jobpar->read('target');
$target =~ s/^0//;
$target =~ s/^0//;
}
my $trigid =$jobpar->read('sequence');
$trigid=~s/\d{3}$//; $trigid=~s/^00//;
$GRB=~s/GRB//g; $GRB=uc($GRB);
my $refP = undef;
my $grbOut = '/tmp/GRB_'.$GRB.'.txt';
my $TI = undef;
if($GRB !~ /^GRB\s*\d{6}[A-Z]*/){
$TI = $trigid;
$grbOut = '/tmp/GRB_'.$TI.'.txt';
}
$GRB=~s/GRB//g;
$GRB=uc($GRB);
# Remove file if it exists
if (-e $grbOut) {
unlink $grbOut;
}
# Make new file by querying catalog with specific ID for the given GRB
my $ssh = 'ssh -nq sdc ';
my $hostname = `/bin/hostname`;
chomp $hostname;
if($hostname =~ /^sdc/ or $hostname =~ /sdcproc/){
$ssh = '';
}
my $cmd = $ssh.' wget http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/grb_lookupLC2.php?grb_name='.$GRB.' -O '.$grbOut." >& /dev/null";
if(defined $TI){
$cmd = $ssh.' wget http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/grb_lookupLC2.php?trig_id='.$TI.' -O '.$grbOut." >& /dev/null";
}
my @GRBvals = ();
my $retval = system($cmd);
if($retval != 0){
$log->error(1, "Unable to reach catalog http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/: exit 1");
return $refP;
} else{
if($ssh ne ''){
my $cmd2 = "ssh -nq sdc cat $grbOut; rm -f $grbOut";
@GRBvals = `$cmd2`;
} else {
@GRBvals = `cat $grbOut`;
unlink $grbOut;
}
}
my $refGRB = undef;
my $flgNF = -1;
foreach my $line (@GRBvals) {
chomp $line;
next if ($line =~ /^\s*$/);
if ($line =~ /^\s*GRB\snot\sfound/) {
$flgNF = 1;
last;
} elsif ($line =~ /No\s+such\s+file\s+or\s+directory/) {
last;
}
if ($line =~ /\s*\=\s*/) {
my ($name, $value) = split/\s*\=\s*/, $line;
if ($value =~ /n\/a/i or !defined $value or $value eq '') {
$value = undef;
}
$refGRB->{$name} = $value;
if (defined $value) {
if ($value =~ /\d+\:\d+/) {
if ($name =~ /ra$/) {
my ($status, $ra) = convertRAStringToDegrees($value);
if ($status == 0) {
$refGRB->{$name} = $ra;
} else {
$refGRB->{$name} = undef;
}
} elsif ($name =~ /dec$/) {
my ($status, $dec) = convertDecStringToDegrees($value);
if ($status == 0) {
$refGRB->{$name} = $dec;
} else {
$refGRB->{$name} = undef;
}
}
$flgNF = 0;
}
}
}
}
if ($flgNF == 1) {
$log->error(1, "GRB $GRB not found in JD catalog; exit 1");
return $refP;
} elsif ($flgNF == -1) {
$log->error(1, "GRB $GRB not found: command $cmd must have failed; exit 1");
return $refP;
}
#Some sanity check while also writing out the data
if(exists $refGRB->{trigger_time} and
defined $refGRB->{trigger_time} and
$refGRB->{trigger_time} =~ /\d{2}\:\d{2}\:\d{2}/ and
defined $GRB){
my $y = substr($GRB, 0, 2);
$y += 2000;
my $m = substr($GRB, 2, 2);
my $d = substr($GRB, 4, 2);
my $t = "$y\-$m\-$d".'T'.$refGRB->{trigger_time};
$refP = $t;
}
return $refP;
}
sub getTrigFromLocalCatalog {
my $self=shift;
my $log =$self->log();
my $filename=$self->filename();
my $jobpar =$self->jobpar();
my $procpar = $self->procpar();
my $refP = undef;
my $catalog = $procpar->read('LGrbCat');
if (!-e $catalog) {
$log->error(1, "Local GRB catalog $catalog does NOT exists.");
return $refP;
}
my @Cat = split/\//, $catalog;
my $Lcat = $Cat[-1];
my $GRB = $jobpar->read('object');
my $target = undef;
if ($GRB !~ /^GRB\s*\d{6}[A-Z]*/) {
$target = $jobpar->read('target');
$target =~ s/^0//;
$target =~ s/^0//;
}
my $trigid =$jobpar->read('sequence');
$trigid=~s/\d{3}$//; $trigid=~s/^00//;
$GRB=~s/GRB//g; $GRB=uc($GRB);
######################## COPY swiftgrb.tdat file from /aps/db/grb_coord to here
system("\$LOCKIT $catalog;cp $catalog .;\$UNLOCKIT $catalog"); #for sdc2
my $refItems = undef;
my $strer = '';
my $flgR = 0;
if (open INF, "<$Lcat") {
while (my $line = <INF>) {
chomp $line;
next if ($line =~ /^\#/);
if ($line =~ /^\s*line\[1\]\s*\=/) {
my @tar = split /\s+/, (split/\s*\=\s*/, $line)[1];
for (my $i=0; $i<scalar(@tar); $i++) {
$refItems->{$tar[$i]} = $i;
}
$flgR = 1;
} elsif ($line =~ /^\s*\<DATA\>/ and $flgR == 1) {
$flgR = 2;
} elsif ($flgR == 2) {
if (!defined $target and $line =~ /^\s*GRB\s+$GRB/) {
my @art = split/\|/, $line;
my $tgrb = $art[0];
$tgrb =~ s/^\s*GRB\s*//;
$tgrb = uc($tgrb);
my $gf = 0;
if ($GRB =~ /\d{6}[A-Z]/ and $GRB eq $tgrb) {
$gf = 1;
} else {
if ($GRB =~ /$tgrb.'[A]*'/) {
$gf = 1;
}
}
if ($gf == 1) {
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
my $it = $art[$idx];
if (!defined $it or $it eq '' or $it eq 'n/a') {
$it = undef;
}
$refItems->{$key} = $it;
}
$flgR = 3;
last;
}
} else {
my @art = split/\|/, $line;
my $sidx = $refItems->{target_id};
if ($trigid eq $art[$sidx]) {
foreach my $key (keys %$refItems) {
my $idx = $refItems->{$key};
my $it = $art[$idx];
if (!defined $it or $it eq '' or $it eq 'n/a') {
$it = undef;
}
$refItems->{$key} = $it;
}
$flgR = 3;
last;
}
}
}
} # end of while loop
close INF;
} # end of if (open INF, "swiftgrb.tdat"){
if ($flgR != 3) {
$log->error(1, "GRB $GRB not found in Local catalog.");
return undef;
}
my $trigtime = undef;
if(exists $refItems->{trigger_time} and
($refItems->{trigger_time} =~ /\d{4}\-\d{2}\-\d{2}T\d{2}\:\d{2}\:\d{2}/ or
$refItems->{trigger_time} =~ /\d+\.*\d*/)){
$trigtime = $refItems->{trigger_time};
}
return $trigtime;
}
##########cat /aps/scripts/grb_coord.pl #################
#crontab -l|grep grb
#0 * * * * /aps/scripts/grb_coord.pl >/aps/db/grb_coord/grb.txt
#
# this script is run by cronjob every hour
#
##!/usr1/local/bin/perl -w
#use LWP::Simple;
#
############URL FOR ROOT GRB TABLE :
#
#$URL="http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/grb_table.php?obs_swift=1&obs_ipn=1&obs_integral=1&obs_hete=1&obs_agile=1&obs_fermi=1&restrict=none&grb_time=1&grb_trigger=1&burst_advocate=1&other_obs=1&redshift=1&host=1&comments=1&references=1&bat_location=1&bat_err_radius=1&bat_t90=1&bat_fluence=1&bat_err_fluence=1&bat_1s_peak_flux=1&bat_err_1s_peak_flux=1&bat_photon_index=1&bat_err_photon_index=1&xrt_location=1&xrt_err_radius=1&xrt_first_obs=1&xrt_early_flux=1&xrt_24hr_flux=1&xrt_lc_index=1&xrt_gamma=1&xrt_nh=1&uvot_location=1&uvot_err_radius=1&uvot_first_obs=1&uvot_vmag=1&uvot_filters=1";
#
# $t=get($URL);
#
# $t1=join(" ",split(/">/,"$t"));
# $t=join(" ",split(/="/,"$t1"));
# @table=split(/ /,"$t");
#
#############URL FOR GRB TABLE EXTENSION grb_table.txt:
#
# $y=0;
# foreach $x (@table){
# if($x=~m/.txt/) { $y=$x; last; }
# }
#
#$t=get("http://heasarc.gsfc.nasa.gov/docs/swift/archive/grb_table/$y");
#
## open(F,">/aps/db/grb_coord/grb.txt");
## print F $t;
## close(F);
# print $t;
#