Download

package SrcMerge;
use strict;
use English;
use Carp;
use vars
  qw(@ISA $VERSION $name $author $date $version @instList $numberOfStreams);
@ISA = qw(Module);
use ModuleResources;

# Declare identity, version, author, date, etc.
$name    = __PACKAGE__;
$VERSION = '2.18';
$version = $VERSION;
$author  = 'Richard West,Dean Hinshaw,Duncan John Fyfe,Ian Stewart,AnjaSchroeder, Duncan Law-Green, Jose V Perea';
$date    = '2014-12-17';

#	DEVELOPERS NOTE:
#	The evaluateRules differ between production and catalogue pipelines.
#	Remeber this when copying this module between pipelines.
#
# ChangeLog
# =========
#
# version 2.18 - 2014-12-17 JVP
# ------------
#
# + Ignore module if there is no source list or the list is empty. SendACDS will be ignored in that case.
#
# version 2.17 - 2012-12-21 DLG
# ------------
#
# + added astcorr=Y to catcorr call to  perform 'field stretch' astrometric correction
#
# version 2.16 - 2012-07-10 DLG
# ------------
#
# + check for non-empty OM source list in srcmatch call
#
# version 2.15 - 2012-04-25 DLG
# ------------
#
# + disable dpssflag to prevent flag overwrite
#
## version 2.14 - 2012-03-05 DLG
# ------------
#
# + disable evalcorr
#
# version 2.13 - 2012-03-02 DLG
# ------------
# 
# + add calls to CGP catalogue xcorr routines catprep/catcorr
# 
# version 2.12 - 2006-08-09 DJF
# ------------
# 
# + evalcorr intercept changed from 5 to 9.
#
# version 2.11 - 2006-03-07 DJF
# ------------
# 
# + dpssflag had unquoted strings as parameter values.
# + change evalcorr invocation to use new parameters and test
#
# version 2.10 - 2006-03-01 ACS
# ------------
# 
# + Changed dpssflag parameters
# from dpssflag  srclisttable=$mlSrcList  mfraclimit=0.5  brcolumn=SCTS  brfactor=0.0003 extfactor=1
# to dpssflag srclisttable=$mlSrcList extlimit=1.5 mfraclimit=0.5 brcolumn=RATE brfactor=65 op1=sqrt 
#    op2=mul minrad=10 maxrad=400 extfactor=3 maxextrad=200 brlimit=1000 fixext=160 brexfraction=0.4 
#    detmlfraction=0.9 badcolumn=CUTRAD badfactor=1 pnclosetobadon=no 
# this should be the final version
#
# version 2.09 - 2006-01-20 DJF 
# ------------
#
# + Added test to make sure OM observation source list exists before passing it as a parameter to srcmatch.
# 
# version 2.08 - 2005-12-07 DJF 
# ------------
# 
# + Changed dpssflag parameters
# from dpssflag  srclisttable=$mlSrcList  mfraclimit=0.5  brcolumn=SCTS  brfactor=0.0003
# to dpssflag  srclisttable=$mlSrcList  mfraclimit=0.5  brcolumn=SCTS  brfactor=0.0003
#
# version 2.07 - 2005-12-01 IMS
# ------------
# + Added invocation of dpssflag.
# + Thinned out commented-out code a bit.
#
# version 2.06 - 2005-11-18 IMS
# ------------
# + No longer generates the USNO extract. This job is taken over by module USNOExtract.
# + 'start' dependency can therefore lose ImageMerge but must gain USNOExtract.
#
# version 2.05 - 2005-11-04 IMS
# ------------
#
# + The module no longer skips source lists with BKGDSCRN=T - non-flare-screened exposures are now screened out at an earlier stage.
# + Implemented DJF's shortened intermediate file names.
#
# version 2.04 - 2005-10-14 IMS
# ------------
#
# + Added 'allComplete' to the list of methods called from ModuleResources.
# + RGW reinstated the XID src list in the call to srcmatch.
#
# version 2.03 - 2005-09-20 RGW
# ------------
#
# + don't pass XID source list to srcmatch (this is a hack / temporary workaround)
#
# version 2.02 - 2005-08-31 RGW
# ------------
#
# + updated trigger conditions (requires ImageMerge to have completed)
#
# version 2.01 - 2005-08-02 IMS
# ------------
#
# + In evaluateRules, allIgnored and allComplete tests on ExpDetect changed to ignored and complete (because ExpDetect now has only 1 instrument and stream).
# + ExpDetect now creates just a single emldetect source list for the observation. SrcMerge has been altered to use this list (plus the matching list with XID data) instead of finding the longest-duration exposure-level source list.
# + The observation-level image is now also used instead of the appropriate instrument fov image.
#
# version 2.00 - 2005-05-09 DJF
# ------------
#
# + Add explicit perl module headers.  Previously these were supplied
#   at compile time.  This will make debugging and extending the modules
#   through additional perl libraries easier.
#
# + Code layout made more uniform with perltidy
# + Standerdized date format (ccyy-mm-dd)
# + Standerdized author list to use comma separator
# + Make use of perl $VERSION magic.  Now $Version = version = ''
##
# Version 1.13 - 2003-12-10 (DJF)
# ------------
#
# + Adapted doCommand to use anonymous lists for list parameters
#
# Version 1.12 - 2002-07-02 (DJF)
# ------------
#
# + Changed srcmatch systerr from 4.0 to 2.5
#
# Version 1.11 - 2002-05-30 (DJF)
# ------------
#
# + Catalogue version copied into production pipeline.
#   See Version 1.10.2 - 1.10.7 ChangeLogs for further details
#
#
# Version 1.10.7 - 2002-05-17 (DJF)
# --------------
#
# + Added evalcorr task
#
# Version 1.10.6 - 2002-05-15 (DJF)
# --------------
#
# + Amended eposcorr maxoffset (was 7, now 10) and maxdist (was 20 now 15) parameters
#
# Version 1.10.5 - 2002-04-30 (DH)
# --------------
#
# + Add option to write out likelyhood image in eposcorr.
#
# Version 1.10.4 - 2002-04-30 (DH)
# --------------
#
# + Specify input source lists as being of format FITS.
#
# Version 1.10.3 - 2002-04-23 (DH)
# --------------
#
# + Don't use source lists in srcmatch if they have not been
#   background screened.
#
# Version 1.10.2 - 2002-03-29 (DH)
# --------------
#
# + Add in call to eposcorr to correct positions of combined
#   source list.
#
# Version 1.10.1 - 2002-03-28 (DH)
# --------------
#
# + Remove dependency on OMSourceCombine - Catalogue pipeline only.
#
# Version 1.10 - 2002-03-22 (DH)
# ------------
#
# + Don't use source lists that have not entries.
#
# Version 1.09 - 2002-02-25 (DH)
# ------------
#
# + Bug fix for 1.08 .
#
# Version 1.08 - 2002-02-19 (DH)
# ------------
#
# + Add in the correlation of the om observation soure list
#   to srcmatch, if it is available.
#
# Version 1.07 - 2001-05-22 (DH)
# ------------
#
# + Change srcmatch parameter syserr, to its proper
#   spelling systerr.
#
# Version 1.06 - 2001-05-21 (DH)
# ------------
#
# + Change srcmatch parameters maxerr from 5 to 4, #   and syserr from default to 4 .
#
# Version 1.05 - 2001-03-16 (DH)
# ------------
#
# + Print out version number in performAction() for
#   tracking purposes.
#
# Version 1.04 - 2001-01-16 (DH)
# ------------
#
# + Remove ignore rule on number of exposures, as it was
#   redundant and causing unnecessary DB access.
#
# Version 1.03 - 2001-01-11 (DH)
# ------------
#
# + Test for existence of source list file before trying to
#   use it.
#
# Version 1.02 - 2001-01-09 (DH)
# ------------
#
# + Change ignore conditions evaluateRules.
#
# Version 1.01 - 2000-11-28 (DH)
# ------------
#
# + First production version.
#
# Declare list of instruments this module is interested in
@instList = qw(all);

# Number of streams
sub numberOfStreams
{
    return 1;
}

# Rules method
sub evaluateRules
{
    return ignore() if ignored( module => 'ExpDetect'
        , instrument => 'epic'
        , stream => 1
    );

    start()
      if complete( module => 'ExpDetect'
        , instrument => 'epic'
        , stream => 1
      )
      and complete( module => 'USNOExtract'
        , instrument => 'all'
        , stream => 1
     )
      and allComplete(
        module => 'OMSourceCombine'
        , instrument => 'om'
        , stream => 1
      );
}

# Action method
sub performAction
{
    info("Module version number: $version");

    my (@extraopts);

    my $mlSrcList = findFile(
        class => 'product'
        , instrument => 'epic'
        , content => 'EPIC observation ml source list'
    );

#    return success()
#      unless (
#        fileExists( file => $mlSrcList )
#        && numberFITSRows(
#            file => $mlSrcList
#            , extension => 'SRCLIST'
#        ) > 0
#      );

    if ( fileExists( file => $mlSrcList ) && numberFITSRows( file => $mlSrcList, extension => 'SRCLIST') > 0 ) 
       {
       info("Source list found and has non-zero rows. Processing...");
       } else {
       info("Source list not found or it has zero rows. Ignoring.");
       return ignore();
       }


    # Pre-flagging for screeners:
#    doCommand(
#        'dpssflag'
#        , srclisttable => $mlSrcList
#        , extlimit => 1.5 
#        , mfraclimit => 0.5
#        , brcolumn => 'RATE'
#        , brfactor => 65
#        , op1 => 'sqrt' 
#        , op2 => 'mul' 
#        , minrad => 10 
#        , maxrad => 400 
#        , extfactor => 3 
#        , maxextrad => 200 
#        , brlimit => 1000 
#        , fixext => 160 
#        , brexfraction => 0.4 
#        , detmlfraction => 0.9 
#        , badcolumn => 'CUTRAD' 
#        , badfactor => 1 
#      )
#      or return exception();

#	info("***DPSSFLAG disabled because it is broken***");

    my $mlXidSrcList = findFile(
        class => 'intermediate'
        , instrument => 'epic'
        , content => 'ml xid source list'
    );

    # Get om summary source list, if it exists, and has non-zero rows
    my $omList = findFile(
        class => 'product'
        , instrument => 'om'
        , content => 'OM OBSERVATION SOURCE LIST'
    );
    if ( $omList && fileExists( file => $omList ) && numberFITSRows( file => $omList, extension => 'SRCLIST' ) > 0 )
    {
        @extraopts = (
            useomlistset => 'Y'
            , omlistset => $omList
        );
    }
	else
	{
		info('No OM observation source list available.');
        @extraopts = (
            useomlistset => 'N'
        );
	}

    # Run srcmatch to form temporary summary source lists
    my $tmpfitsSummary = newFile(
        class => 'intermediate'
        , instrument => 'epic'
        , content => 'summary source list'
        , format => 'FITS'
    );
    my $htmlSummary = newFile(
        class => 'product'
        , instrument => 'epic'
        , content => 'EPIC summary source list'
        , format => 'HTML'
    );
    doCommand(
        'srcmatch'
        , inputlistsets => "$mlSrcList $mlXidSrcList"
        , outputlistset => $tmpfitsSummary
        , htmloutput => $htmlSummary
#        , maxerr => 4
#        , systerr => 2.5
        , @extraopts
        , allepicmode => 'yes'
      )
      or return exception();

    # Run catcorr to correct source positions

    my $usnoList = findFile(
			    class => 'product'
			    , instrument => 'all'
			    , content => 'Reference catalogue'
			    , format => 'FITS'
			    );

    info("No catalogue extract to work from.  Unable to continue.") and ignore() unless defined ($usnoList);

    doCommand(
	      'catcorr'
	      , srclistset => $tmpfitsSummary
	      , catset => $usnoList
	      , mingood => 10
	      , minfit => 5.0
	      , maxoffset => 10.0
	      , astcorr => 'Y'
	      ) or return exception();

    my $fitsSummary = newFile(
        class => 'product'
        , instrument => 'epic'
        , content => 'EPIC summary source list'
        , format => 'FITS'
    );

#
# insert copyFile to create final summary source list since evalcorr disabled

    copyFile(
	     source => $tmpfitsSummary
	     , destination => $fitsSummary
	     );

    # evalcorr generates final summary source lists by cloning the eposcorr output
    #doCommand(
    #    'evalcorr'
    #    , input => $tmpfitsSummary
    #    , output => $fitsSummary
#		, mode => 'CLONE'
#		, expression => '2XMM'
#		, intercept => 9
#    );

    #
    return success();
}
1;