from history import *
from history.record import *

import unittest
import warnings 

warnings.simplefilter("ignore", Warning)

class TestHistoryRecord(unittest.TestCase):

    def test_01(self):

        # check if Record is truly an Abstract Base Class (i.e. should be unable to instantiate it)
        try:
            rr = Record()
        except TypeError:
            pass
        else:
            raise Exception("Record ABC FAILED")

        # check that HistoryRecord is a subclass and instance of Record
        if issubclass(HistoryRecord, Record) is not True:
            raise Exception("issubclass FAILED")

        # create empty HistoryRecord
        try:
            hr = HistoryRecord()
        except Exception as ex:
            if "input content -OR- individual arguments are required" not in str(ex):
                raise Exception("create empty HistoryRecord FAILED")


    def test_02(self):
        # test creating an ASC-FITS compliant history record with parameter stacks

        content = ['HISTORY  TOOL  :dmhedit  2016-08-11T20:43:33                            ASC00870', 
                   'HISTORY  PARM  :infile=@/this/is/a/super/loooooooooooooong/directory/namASC00871', 
                   'HISTORY  CONT  :e/but/wait/there/is/lots/lots/lots/mooooooooooooore/infiASC00872', 
                   'HISTORY  CONT  :le.lis                                                  ASC00873', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack1ASC00874', 
                   'HISTORY  CONT  :.fits                                                   ASC00875', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack2ASC00876', 
                   'HISTORY  CONT  :.fits                                                   ASC00877', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack3ASC00878', 
                   'HISTORY  CONT  :.fits                                                   ASC00879', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name//but/wASC00880', 
                   'HISTORY  CONT  :ait/there/is/lots/lots/lots/mooooooooooooore/stack4.fitsASC00881', 
                   'HISTORY  CONT  :                                                        ASC00882', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack5ASC00883', 
                   'HISTORY  CONT  :.fits                                                   ASC00884', 
                   'HISTORY  PARM  :outfile=/tmp/dir/outfile.fits                           ASC00885']
        

        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create HistoryRecord from ASC-FITS compliant record FAILED")
        
        tmpstr = "Tool:  dmhedit\n" + "Date:  2016-08-11T20:43:33\n" + "Params:\n" + "   infile = @/this/is/a/super/loooooooooooooong/directory/name/but/wait/there/is/lots/lots/lots/mooooooooooooore/infile.lis\n" + "      > /this/is/a/super/loooooooooooooong/directory/name/stack1.fits\n" + "      > /this/is/a/super/loooooooooooooong/directory/name/stack2.fits\n" + "      > /this/is/a/super/loooooooooooooong/directory/name/stack3.fits\n" + "      > /this/is/a/super/loooooooooooooong/directory/name//but/wait/there/is/lots/lots/lots/mooooooooooooore/stack4.fits \n" + "      > /this/is/a/super/loooooooooooooong/directory/name/stack5.fits\n" + "   outfile = /tmp/dir/outfile.fits\n" + "ASCNUM:  870\n"

        # check string representation
#        self.assertEqual(str(hr), tmpstr, "str/repr FAILED")
        self.assertEqual(str(hr), tmpstr)

        # check that parameters were read in correctly
        params = hr.params

        pnames = "['infile', 'outfile']"
        pvalues = "['@/this/is/a/super/loooooooooooooong/directory/name/but/wait/there/is/lots/lots/lots/mooooooooooooore/infile.lis', '/tmp/dir/outfile.fits']"
        pstacks = "[['/this/is/a/super/loooooooooooooong/directory/name/stack1.fits', '/this/is/a/super/loooooooooooooong/directory/name/stack2.fits', '/this/is/a/super/loooooooooooooong/directory/name/stack3.fits', '/this/is/a/super/loooooooooooooong/directory/name//but/wait/there/is/lots/lots/lots/mooooooooooooore/stack4.fits ', '/this/is/a/super/loooooooooooooong/directory/name/stack5.fits'], []]"

        self.assertEqual(str(params['pnames']), pnames, "get param names FAILED")
        self.assertEqual(str(params['pvalues']), pvalues, "get param values FAILED")
        self.assertEqual(str(params['pstacks']), pstacks, "get param stacks FAILED")
        
        # try to alter parameters
        try:
            hr._params.pkey  = ["filein", "fileout"]
        except Exception as ex:
            if "can't set attribute" not in str(ex):
                raise Exception("params immmutabilty check 1 FAILED")

        try:
            hr._params['pstack'] = [["stack1", "stack2"], []]
        except Exception as ex:
            if "'Parameters' object does not support item assignment" not in str(ex):
                raise Exception("params immmutabilty check 2 FAILED")

        # should be ASC-FITS compliant
        self.assertTrue(hr.is_ascfits_compliant(), "is_ascfits_compliant() check FAILED")

        # check if ascstring output is the same as content input
        self.assertEqual(hr.as_ASC_FITS(with_tag=True, stk_expand=True), '\n'.join(content), "ASC-FITS as_ASC_FITS(with_tag=True, stk_expand=True) FAILED")
        #self.assertEqual(hr.as_ASC_FITS(with_tag=True, stk_expand=True), '\n'.join(content))

        # check FITS string output
        as_fits = "HISTORY dmhedit infile=@/this/is/a/super/loooooooooooooong/directory/name/but/wa\n" + "HISTORY it/there/is/lots/lots/lots/mooooooooooooore/infile.lis outfile=/tmp/dir/\n" + "HISTORY outfile.fits                                                            "
        self.assertEqual(hr.as_FITS(True), as_fits, "ASC-FITS as_FITS(with_tag=True) FAILED")
       
        # check string output 
        as_str = "HISTORY dmhedit infile=@/this/is/a/super/loooooooooooooong/directory/name/but/wait/there/is/lots/lots/lots/mooooooooooooore/infile.lis outfile=/tmp/dir/outfile.fits "
        self.assertEqual(hr.as_string(True), as_str, "ASC-FITS as_string(with_tag=True) FAILED")

        # check command output
        as_cmd = "dmhedit infile=@/this/is/a/super/loooooooooooooong/directory/name/but/wait/there/is/lots/lots/lots/mooooooooooooore/infile.lis outfile=/tmp/dir/outfile.fits "
        self.assertEqual(hr.as_command(), as_cmd, "ASC-FITS as_command() FAILED")


    def test_03(self):
        # test creating a pixlib history record
        content = [ "HISTORY  PARM  :PIXLIB Version: 4.0.0                                   ASC00015",
                    "HISTORY  PARM  :******     PIXLIB Parameter File      *****             ASC00016",
                    "HISTORY  PARM  :/home/ascds/DS.l3/param/geom.par:                       ASC00017",
                    "HISTORY  PARM  :instruments  = /export/CALDB/level3/data/chandra/defaultASC00018",
                    "HISTORY  CONT  :/geom/telD1999-07-23geomN0006.fits(CALDB)               ASC00019",
                    "HISTORY  PARM  :aimpoints    = /export/CALDB/level3/data/chandra/defaultASC00020",
                    "HISTORY  CONT  :/aimpts/telD1999-07-23aimptsN0002.fits(CALDB)           ASC00021",
                    "HISTORY  PARM  :tdet         = /export/CALDB/level3/data/chandra/defaultASC00022",
                    "HISTORY  CONT  :/tdet/telD1999-07-23tdetN0001.fits(CALDB)               ASC00023",
                    "HISTORY  PARM  :sky          = /export/CALDB/level3/data/chandra/defaultASC00024",
                    "HISTORY  CONT  :/sky/telD1999-07-23skyN0002.fits(CALDB)                 ASC00025",
                    "HISTORY  PARM  :shell        = /export/CALDB/level3/data/chandra/defaultASC00026",
                    "HISTORY  CONT  :/sgeom/telD1999-07-23sgeomN0001.fits(CALDB)             ASC00027",
                    "HISTORY  PARM  :obsfile      = nowT00:00:00()                           ASC00028"]

        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create HistoryRecord from PIXLIB record FAILED")
        
        # check if ASC-FITS compliant
        if hr.is_ascfits_compliant() is True:
            raise Exception("pixlib compliancy check FAILED")

        # check if ASC-FITS string output is the same as content input
        if hr.as_ASC_FITS(with_tag=True) not in '\n'.join(content):
            raise Exception("pixlib as_ASC_FITS(with_tag=True) FAILED")

        # check if as_FITS() prints the same string as as_ASC_FITS()
        if hr.as_FITS(with_tag=True) not in '\n'.join(content):
            raise Exception("pixlib as_FITS(with_tag=True) FAILED")

        as_str = "HISTORY PIXLIB Version: 4.0.0\n" + "HISTORY ******     PIXLIB Parameter File      *****\n" + "HISTORY /home/ascds/DS.l3/param/geom.par:\n" + "HISTORY instruments   =  /export/CALDB/level3/data/chandra/default/geom/telD1999-07-23geomN0006.fits(CALDB)\n" + "HISTORY aimpoints     =  /export/CALDB/level3/data/chandra/default/aimpts/telD1999-07-23aimptsN0002.fits(CALDB)\n" + "HISTORY tdet          =  /export/CALDB/level3/data/chandra/default/tdet/telD1999-07-23tdetN0001.fits(CALDB)\n" + "HISTORY sky           =  /export/CALDB/level3/data/chandra/default/sky/telD1999-07-23skyN0002.fits(CALDB)\n" + "HISTORY shell         =  /export/CALDB/level3/data/chandra/default/sgeom/telD1999-07-23sgeomN0001.fits(CALDB)\n" + "HISTORY obsfile       =  nowT00:00:00()"
        
        # check as_string()
        if as_str not in hr.as_string(True):
            raise Exception("pixlib as_string(with_tag=True) FAILED")

        # should not be unable to print pixlib record as a command
        try:
            hr.as_command()
        except Exception as ex:
            if "Unable to print PIXLIB history as command" not in str(ex):
                raise Exception("pixlib as_command() FAILED")


    def test_04(self):
        # create HistoryRecord without a date timestamp and @@ in filenames

        content = [ "HISTORY  TOOL  :ade                                                     ASC00001",
                    "HISTORY  PARM  :infile=/home/ascdsrap/ap/sdp/opus/prs_run/tmp//TP_ADE___ASC00002",
                    "HISTORY  CONT  :_086490403n255/input/acisf059969572N002_SR0.strip       ASC00003",
                    "HISTORY  PARM  :template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_sASC00004",
                    "HISTORY  CONT  :trip_file_info.dat@@/main/2                             ASC00005",
                    "HISTORY  PARM  :template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_fASC00006",
                    "HISTORY  CONT  :ile_info.dat@@/main/12                                  ASC00007",
                    "HISTORY  PARM  :template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_cASC00008",
                    "HISTORY  CONT  :onfig_file.dat@@/main/2                                 ASC00009",
                    "HISTORY  PARM  :template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_gASC00010",
                    "HISTORY  CONT  :roup_info.dat@@/main/12                                 ASC00011",
                    "HISTORY  PARM  :template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_dASC00012",
                    "HISTORY  CONT  :p_info.dat@@/main/19                                    ASC00013",
                    "HISTORY  PARM  :template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_cASC00014",
                    "HISTORY  CONT  :ommon_dp_info.dat@@/main/9                              ASC00015"]
    
        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create HistoryRecord without date timestamp FAILED") 
    
        tmpstr = "Tool:  ade\n" + "Date:  \n" + "Params:\n" + "   infile = /home/ascdsrap/ap/sdp/opus/prs_run/tmp//TP_ADE____086490403n255/input/acisf059969572N002_SR0.strip\n" + "   template = /vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_strip_file_info.dat@@/main/2\n" + "   template = /vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_file_info.dat@@/main/12\n" + "   template = /vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_config_file.dat@@/main/2\n" + "   template = /vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_group_info.dat@@/main/12\n" +"   template = /vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_dp_info.dat@@/main/19\n" + "   template = /vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_common_dp_info.dat@@/main/9\n" + "ASCNUM:  1\n"

        # check string representation
        if tmpstr not in str(hr):
            raise Exception("str/repr FAILED")

        # check if ascstring output is the same as content input
        if hr.as_ASC_FITS(with_tag=True) not in '\n'.join(content):
            raise Exception("as_ASC_FITS(with_tag=True) FAILED")

        as_cmd = "ade infile=/home/ascdsrap/ap/sdp/opus/prs_run/tmp//TP_ADE____086490403n255/input/acisf059969572N002_SR0.strip template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_strip_file_info.dat@@/main/2 template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_file_info.dat@@/main/12 template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_config_file.dat@@/main/2 template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_group_info.dat@@/main/12 template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_dp_info.dat@@/main/19 template=/vobs/ASC_DR_TLM/src/dr/tlm/template_dir/acis_common_dp_info.dat@@/main/9"
        if as_cmd not in hr.as_command():
            raise Exception("as_command() FAILED")


    def test_05(self):
        # test creating an ASC-FITS compliant history record with multiple parameter stacks

        content =[ "HISTORY  TOOL  :acis_process_events  2000-09-30T00:34:41                ASC00083",
                   "HISTORY  PARM  :infile=/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1ASC00084",
                   "HISTORY  CONT  :_086557597n455/output/acisf01463_000N002_tim1.fits      ASC00085",
                   "HISTORY  PARM  :outfile=/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_LASC00086",
                   "HISTORY  CONT  :1_086557597n455/output/acisf01463_000N002_evt1.fits     ASC00087",
                   "HISTORY  PARM  :acaofffile=@/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACISASC00088",
                   "HISTORY  CONT  :_F_L1_086557597n455/input/pcadf01463_000N002_asol1.lis  ASC00089",
                   "HISTORY  STCK  :/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557ASC00090",
                   "HISTORY  CONT  :597n455/input/pcadf059968984N002_asol1.fits[time=5996879ASC00091",
                   "HISTORY  CONT  :7.4852730:59993398.5111620]                             ASC00092",
                   "HISTORY  PARM  :alignmentfile=@/home/ascdsrap/ap/sdp/opus/prs_run/tmp//AASC00093",
                   "HISTORY  CONT  :CIS_F_L1_086557597n455/input/pcadf01463_000N002_asol1.liASC00094",
                   "HISTORY  CONT  :s                                                       ASC00095",
                   "HISTORY  STCK  :/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557ASC00096",
                   "HISTORY  CONT  :597n455/input/pcadf059968984N002_asol1.fits[time=5996879ASC00097",
                   "HISTORY  CONT  :7.4852730:59993398.5111620]                             ASC00098",
                   "HISTORY  PARM  :obsfile=/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_LASC00099",
                   "HISTORY  CONT  :1_086557597n455/input/axaff01463_000N002_obs0a.par      ASC00100",
                   "HISTORY  PARM  :logfile=/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_LASC00101",
                   "HISTORY  CONT  :1_086557597n455/output/acis_process_events.log          ASC00102",
                   "HISTORY  PARM  :gradefile=/home/ascds/DS.rap/data/asca_grades.fits      ASC00103",
                   "HISTORY  PARM  :gainfile=/data/CALDB/data/chandra/acis/bcf/gain/acisD199ASC00104",
                   "HISTORY  CONT  :9-09-16gainN0004.fits                                   ASC00105",
                   "HISTORY  PARM  :badpixfile=/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_ASC00106",
                   "HISTORY  CONT  :F_L1_086557597n455/output/acisf01463_000N002_bpix1.fits ASC00107",
                   "HISTORY  PARM  :threshfile=NONE                                         ASC00108",
                   "HISTORY  PARM  :eventdef={d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdeASC00109",
                   "HISTORY  CONT  :t,f:det,f:sky,s:phas,l:pha,f:energy,l:pi,s:fltgrade,s:grASC00110",
                   "HISTORY  CONT  :ade,x:status}                                           ASC00111",
                   "HISTORY  PARM  :doevtgrade=yes                                          ASC00112",
                   "HISTORY  PARM  :spthresh=13                                             ASC00113",
                   "HISTORY  PARM  :time_offset=0                                           ASC00114",
                   "HISTORY  PARM  :docentroid=no                                           ASC00115",
                   "HISTORY  PARM  :calculate_pi=yes                                        ASC00116",
                   "HISTORY  PARM  :pi_bin_width=14.6                                       ASC00117",
                   "HISTORY  PARM  :pi_num_bins=1024                                        ASC00118",
                   "HISTORY  PARM  :cc_chipy=564.1341161932397                              ASC00119",
                   "HISTORY  PARM  :tstart=TSTART                                           ASC00120",
                   "HISTORY  PARM  :tstop=TSTOP                                             ASC00121",
                   "HISTORY  PARM  :clobber=no                                              ASC00122",
                   "HISTORY  PARM  :verbose=0                                               ASC00123",
                   "HISTORY  PARM  :stop=sky                                                ASC00124",
                   "HISTORY  PARM  :instrume=acis                                           ASC00125",
                   "HISTORY  PARM  :rand_seed=1                                             ASC00126",
                   "HISTORY  PARM  :rand_pix_size=0.5                                       ASC00127",
                   "HISTORY  PARM  :stdlev1={d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdetASC00128",
                   "HISTORY  CONT  :,f:det,f:sky,s:phas,l:pha,f:energy,l:pi,s:fltgrade,s:graASC00129",
                   "HISTORY  CONT  :de,x:status}                                            ASC00130",
                   "HISTORY  PARM  :grdlev1={d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdetASC00131",
                   "HISTORY  CONT  :,f:det,f:sky,l:pha,s:corn_pha,f:energy,l:pi,s:fltgrade,sASC00132",
                   "HISTORY  CONT  ::grade,x:status}                                        ASC00133",
                   "HISTORY  PARM  :cclev1={d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdet,ASC00134",
                   "HISTORY  CONT  :f:det,f:sky,f:sky_1d,s:phas,l:pha,f:energy,l:pi,s:fltgraASC00135",
                   "HISTORY  CONT  :de,s:grade,x:status}                                    ASC00136",
                   "HISTORY  PARM  :ccgrdlev1={d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdASC00137",
                   "HISTORY  CONT  :et,f:det,f:sky,f:sky_1d,l:pha,f:energy,l:pi,s:fltgrade,sASC00138",
                   "HISTORY  CONT  ::grade,x:status}                                        ASC00139"
                   ]
 
        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create HistoryRecord with multi parameter stacks FAILED") 

        tmpstr = "Tool:  acis_process_events\n" + "Date:  2000-09-30T00:34:41\n" + "Params:\n" + "   infile = /home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/output/acisf01463_000N002_tim1.fits\n" + "   outfile = /home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/output/acisf01463_000N002_evt1.fits\n" + "   acaofffile = @/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/input/pcadf01463_000N002_asol1.lis\n" + "      > /home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/input/pcadf059968984N002_asol1.fits[time=59968797.4852730:59993398.5111620]\n" + "   alignmentfile = @/home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/input/pcadf01463_000N002_asol1.lis\n" + "      > /home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/input/pcadf059968984N002_asol1.fits[time=59968797.4852730:59993398.5111620]\n" + "   obsfile = /home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/input/axaff01463_000N002_obs0a.par\n" + "   logfile = /home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/output/acis_process_events.log\n" + "   gradefile = /home/ascds/DS.rap/data/asca_grades.fits\n" + "   gainfile = /data/CALDB/data/chandra/acis/bcf/gain/acisD1999-09-16gainN0004.fits\n" + "   badpixfile = /home/ascdsrap/ap/sdp/opus/prs_run/tmp//ACIS_F_L1_086557597n455/output/acisf01463_000N002_bpix1.fits\n" + "   threshfile = NONE\n" + "   eventdef = {d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdet,f:det,f:sky,s:phas,l:pha,f:energy,l:pi,s:fltgrade,s:grade,x:status}\n" + "   doevtgrade = yes\n" + "   spthresh = 13\n" + "   time_offset = 0\n" + "   docentroid = no\n" + "   calculate_pi = yes\n" + "   pi_bin_width = 14.6\n" + "   pi_num_bins = 1024\n" + "   cc_chipy = 564.1341161932397\n" + "   tstart = TSTART\n" + "   tstop = TSTOP\n" + "   clobber = no\n" + "   verbose = 0\n" +  "   stop = sky\n" + "   instrume = acis\n" + "   rand_seed = 1\n" + "   rand_pix_size = 0.5\n" + "   stdlev1 = {d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdet,f:det,f:sky,s:phas,l:pha,f:energy,l:pi,s:fltgrade,s:grade,x:status}\n" + "   grdlev1 = {d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdet,f:det,f:sky,l:pha,s:corn_pha,f:energy,l:pi,s:fltgrade,s:grade,x:status}\n" + "   cclev1 = {d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdet,f:det,f:sky,f:sky_1d,s:phas,l:pha,f:energy,l:pi,s:fltgrade,s:grade,x:status}\n" + "   ccgrdlev1 = {d:time,s:ccd_id,s:node_id,i:expno,s:chip,s:tdet,f:det,f:sky,f:sky_1d,l:pha,f:energy,l:pi,s:fltgrade,s:grade,x:status}\n" + "ASCNUM:  83"
        
        if tmpstr not in str(hr):
            raise Exception("str/repr FAILED")

        # check that the ascstring output is the same as the content input 
        if hr.as_ASC_FITS(with_tag=True, stk_expand=True) not in '\n'.join(content):
            raise Exception("as_ASC_FITS(with_tag=True, stk_expand=True) FAILED")

        

    def test_06(self):
        # test creating a non-ASC-FITS compliant HistoryRecord
        content = "This is a non-ASC-FITS compliant record"

        hr = HistoryRecord(content)
        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create non-ASC-FITS compliant HistoryRecord FAILED") 

        if hr.is_ascfits_compliant() is True:
            raise Exception("is_ascfits_compliant() check FAILED")

        try:
            hr.as_ASC_FITS()
        except Exception as ex:
            if "Unable to print non-ASC-FITS compliant history in ASC-FITS format" not in str(ex):
                raise Exception("non-ASC-FITS as_ASC_FITS() FAILED")

            
        if "HISTORY This is a non-ASC-FITS compliant record" not in hr.as_FITS(True):
            raise Exception("non-ASC-FITS as_FITS(true) FAILED")

        if content not in hr.as_string():
            raise Exception("non-ASC-FITS as_string() FAILED")

        try:
            hr.as_command()
        except Exception as ex:
            if "Unable to print non-ASC-FITS compliant history as command" not in str(ex):
                raise Exception("non-ASC-FITS as_command() FAILED")


            
    def test_07(self):
        # test creating a very long non-ASC-FITS compliant HistoryRecord
        content = "dmcopy infile=/proj/sciops/test/master/datamodel/indt001m001.fits[sky=mask(/proj/sciops/test/master/datamodel/mymask.fits)[pha=1:100] outfile=/pool14/mcd/temp/myout.fits cl+"

        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create very long non-ASC-FITS compliant HistoryRecord FAILED") 

        try:
            hr.as_ASC_FITS()
        except Exception as ex:
            if "Unable to print non-ASC-FITS compliant history in ASC-FITS format" not in str(ex):
                raise Exception("non-ASC-FITS as_ASC_FITS() FAILED")

        as_fits = "HISTORY dmcopy infile=/proj/sciops/test/master/datamodel/indt001m001.fits[sky=ma\n" + "HISTORY sk(/proj/sciops/test/master/datamodel/mymask.fits)[pha=1:100] outfile=/p\n" + "HISTORY ool14/mcd/temp/myout.fits cl+"

        if as_fits not in hr.as_FITS(True):
            raise Exception("non-ASC-FITS as_FITS(True) FAILED")

        as_fits2 = "dmcopy infile=/proj/sciops/test/master/datamodel/indt001m001.fits[sky=ma\n" + "sk(/proj/sciops/test/master/datamodel/mymask.fits)[pha=1:100] outfile=/p\n" + "ool14/mcd/temp/myout.fits cl+"

        if as_fits2 not in hr.as_FITS():
            raise Exception("non-ASC-FITS as_FITS(False) FAILED")

        if content not in hr.as_string():
            raise Exception("non-ASC-FITS as_string() FAILED")

        try:
            hr.as_command()
        except Exception as ex:
            if "Unable to print non-ASC-FITS compliant history as command" not in str(ex):
                raise Exception("non-ASC-FITS as_command() FAILED")


    def test_08(self):
        # test creating a HistoryRecord object with an empty line

        content = [ "HISTORY  "]
        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create HistoryRecord with an empty line FAILED") 

        try:
            hr.as_ASC_FITS(with_tag=True, stk_expand=True)
        except Exception as ex:
            if "Unable to print non-ASC-FITS compliant history in ASC-FITS format" not in str(ex):
                raise Exception("as_ASC_FITS() with empty line FAILED")

        as_fits = "HISTORY                                                                         "
        if as_fits not in hr.as_FITS(True):
            raise Exception("as_FITS(True) with empty line FAILED")

        as_str = "HISTORY  "
        if as_str not in hr.as_string(True):
            raise Exception("as_string(True) with empty line FAILED")

        try:
            hr.as_command()
        except Exception as ex:
            if "Unable to print non-ASC-FITS compliant history as command" not in str(ex):
                raise Exception("as_command() with empty line FAILED")


    def test_09(self):
        # test creating HistoryRecord object with various missing args, with all args set, and with varying datatype

        # all input args set
        try:
            h1 = HistoryRecord("srcextent infile=my_events.fits", "srcextent", "now", ["outfile"], ["kj332nz.fits"] )
        except Exception as ex:
            if "Cannot create HistoryRecord from both content and individual arguments" not in str(ex):
                raise Exception("input content and individual arguments FAILED")

        # missing param stacks
        try:
            h1 = HistoryRecord(None, "srcextract", "now", param_names=["a"], param_values=["b"])
        except Exception as ex:
            if "Parameter stacks list must be the same length as names and values lists" not in str(ex):
                raise Exception("missing param stacks test FAILED")

        # missing param values
        try:
            h1 = HistoryRecord(None, "srcextract", "now", param_names=["a"], param_stacks=["b"])
        except Exception as ex:
            if "Parameter values list is required" not in str(ex):
                raise Exception("missing param values test FAILED")

        # missing param names
        try:
            h1 = HistoryRecord(None, "srcextract", "now", param_values=["a"], param_stacks=["b"])
        except Exception as ex:
            if "Parameter names list is required" not in str(ex):
                raise Exception("missing param names test FAILED")

        # missing tool name
        try:
            h1 = HistoryRecord(None,  date="now", param_names=["a"], param_values=["b"], param_stacks=["c"])
        except Exception as ex:
            if "Cannot create a HistoryRecord without a tool name" not in str(ex):
                raise Exception("missing tool name test FAILED")

        # param stacks not a List of Lists
        try:
            h2 = HistoryRecord(None, tool="srcextract", date="now", param_names=["infile","outfile","verbose"], param_values=["@myfiles.lis","out.fits","0"], param_stacks=["a.fits","b.fits","c.fits"])
        except Exception as ex:
            if "Parameter stacks list must be a List of Lists" not in str(ex):
                raise Exception("param stacks not a List of Lists test FAILED")

        # input has invalid datatype
        try:
            h1 = HistoryRecord(None, tool="srcextract", date="now", param_names=["infile","outfile",1000], param_values=["evt.fits","out.fits","0"], param_stacks=["x","y","z"])
        except Exception as ex:
            if "All parameter names must be strings" not in str(ex):
                raise Exception("input param name has an invalid datatype test FAILED")

        try:
            h1 = HistoryRecord(None, tool="srcextract", date="now", param_names=["infile","outfile","verbose"], param_values=["evt.fits","out.fits","0"], param_stacks=[["x","y"],["z"], [0]])
        except Exception as ex:
            if "All parameter stacks must be strings" not in str(ex):
                raise Exception("input param stack has an invalid datatype test FAILED")

        # Auto-conversion of parameteter value list to strings.
        try:
            h1 = HistoryRecord(None, tool="srcextract", date="now", param_names=["infile","outfile","verbose"], param_values=["@files.lis","out.fits",0], param_stacks=[["x","y","z"],[],[]])
        except Exception as ex:
            print(str(ex))
            raise Exception("input param value has an varied datatype test FAILED")
        expected = "srcextract infile=@files.lis outfile=out.fits verbose=0 "
        self.assertEqual(h1.as_string(False), expected, "input param value has an varied datatype test. FAILED")



    def test_10(self):

        content = ['HISTORY  TOOL  :dmhedit  2016-08-11T20:43:33                            ASC00870', 
                   'HISTORY  PARM  :infile=@/this/is/a/super/loooooooooooooong/directory/namASC00871', 
                   'HISTORY  CONT  :e/but/wait/there/is/lots/lots/lots/mooooooooooooore/infiASC00872', 
                   'HISTORY  CONT  :le.lis                                                  ASC00873', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack1ASC00874', 
                   'HISTORY  CONT  :.fits                                                   ASC00875', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack2ASC00876', 
                   'HISTORY  CONT  :.fits                                                   ASC00877', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack3ASC00878', 
                   'HISTORY  CONT  :.fits                                                   ASC00879', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name//but/wASC00880', 
                   'HISTORY  CONT  :ait/there/is/lots/lots/lots/mooooooooooooore/stack4.fitsASC00881', 
                   'HISTORY  CONT  :                                                        ASC00882', 
                   'HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack5ASC00883', 
                   'HISTORY  CONT  :.fits                                                   ASC00884', 
                   'HISTORY  PARM  :outfile=/tmp/dir/outfile.fits                           ASC00885']

        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create another ASC-FITS compliant HistoryRecord FAILED") 

        # try to set HistoryRecord attribute; only getter methods are available for public attributes
        try:
            hr.tool = "foo"
        except AttributeError:
            pass
        else:
            raise Exception("record immutability FAILED")

        # set HistoryRecord values using protected attributes
        hr._tool = "dmhedit"
        
        if "dmhedit" not in hr.tool:
            raise Exception("set tool name using protected attributes FAILED")

        # try to set datetime to incorrectly formatted time
        try:
            hr._datetime = "2016-08-11"
        except ValueError:
            pass
        else:
            raise Exception("bad time format check FAILED")

        hr._datetime = None
        if "" not in hr._datetime:
            raise Exception("datetime set to None value FAILED")

        hr._datetime = "2016-08-11T20:43:33"
        if "2016-08-11T20:43:33" not in hr.datetime:
            raise Exception("set datetime using protected attributes FAILED")
        
        # try to set HistoryRecord attribute; only getter methods are available for public attributes
        try:
            hr.asc_start = 87
        except AttributeError:
            pass
        else:
            raise Exception("asc_start immutability FAILED")

        hr._asc_start = 870
        if hr.asc_start != 870:
            raise Exception("set asc_start number using protected attributes FAILED")

        # try to set asc_start to an invalid value; valid values are between 1 and 99999
        try:
            hr._asc_start = 870000
        except ValueError:
            pass
        else:
            raise Exception("asc_start value check FAILED ")
        
        # set  afterkey
        hr._afterkey = "REGIONID"
        if "REGIONID" not in hr.afterkey:
            raise Exception("set afterkey using protected attributes FAILED")

    def test_11(self):
        # test for creating ARDLIB history record
        
        content= ["HISTORY  PARM  :ARDLIB Version: 0.7.1-4                                 ASC00028",
                  "HISTORY  PARM  :File used for CALDB info: /state/partition1/tmp.ascdsl3/ASC00029",
                  "HISTORY  CONT  :L3ASRC____338268443n247/output/acisf01878_000N001_r0100_ASC00030",
                  "HISTORY  CONT  :wmap3.fits                                              ASC00031",
                  "HISTORY  PARM  :Note: CTI_APP = PPPPPBPBPP                              ASC00032",
                  "HISTORY  PARM  :Parameter file: /state/partition1/tmp.ascdsl3/L3ASRC____ASC00033",
                  "HISTORY  CONT  :338268443n247/output/acisf01878_000N001_r0100_ardlib3.paASC00034",
                  "HISTORY  CONT  :r                                                       ASC00035",
                  "HISTORY  PARM  :Vignetting File: /state/partition1/local/install.CAT3.0/ASC00036",
                  "HISTORY  CONT  :CALDB/data/chandra/tel/hrma/bcf/vignet/hrmaD1996-12-20viASC00037",
                  "HISTORY  CONT  :gnetN0003.fits[AXAF_VIGNET1]                            ASC00038",
                  "HISTORY  PARM  :Onaxis Area File: /state/partition1/local/install.CAT3.0ASC00039",
                  "HISTORY  CONT  :/CALDB/data/chandra/tel/hrma/bcf/effarea/hrmaD1996-12-20ASC00040",
                  "HISTORY  CONT  :axeffaN0007.fits[AXAF_AXEFFA1]                          ASC00041",
                  "HISTORY  PARM  :ACIS QE File: /state/partition1/local/install.CAT3.0/CALASC00042",
                  "HISTORY  CONT  :DB/data/chandra/acis/bcf/qe/acisD1997-04-17qeN0006.fits[ASC00043",
                  "HISTORY  CONT  :AXAF_QE3]                                               ASC00044",
                  "HISTORY  PARM  :ACIS Uniformity File: /state/partition1/local/install.CAASC00045",
                  "HISTORY  CONT  :T3.0/CALDB/data/chandra/acis/bcf/qe/acisD2000-01-29qeuN0ASC00046",
                  "HISTORY  CONT  :006.fits[AXAF_QEU3]                                     ASC00047",
                  "HISTORY  PARM  :ACIS Contamination File: /state/partition1/local/installASC00048",
                  "HISTORY  CONT  :.CAT3.0/CALDB/data/chandra/acis/bcf/contam/acisD1999-08-ASC00049",
                  "HISTORY  CONT  :13contamN0004.fits[AXAF_CONTAM3]                        ASC00050",
                  "HISTORY  PARM  :ACIS 2 Badpix File: /state/partition1/tmp.ascdsl3/L3ASRCASC00051",
                  "HISTORY  CONT  :____338268443n247/input/acisf01878_000N001_bpix3.fits[BAASC00052",
                  "HISTORY  CONT  :DPIX2]                                                  ASC00053",
                  "HISTORY  PARM  :File used for CALDB info: /state/partition1/tmp.ascdsl3/ASC00054",
                  "HISTORY  CONT  :L3ASRC____338268443n247/output/acisf01878_000N001_r0100_ASC00055",
                  "HISTORY  CONT  :wmap3.fits                                              ASC00056",
                  "HISTORY  PARM  :Note: CTI_APP = PPPPPBPBPP                              ASC00057",
                  "HISTORY  PARM  :File used for CALDB info: /state/partition1/tmp.ascdsl3/ASC00058",
                  "HISTORY  CONT  :L3ASRC____338268443n247/output/acisf01878_000N001_r0100_ASC00059",
                  "HISTORY  CONT  :wmap3.fits                                              ASC00060",
                  "HISTORY  PARM  :Note: CTI_APP = PPPPPBPBPP                              ASC00061"]

        try:
            hr = HistoryRecord(content)
        except Exception as ex:
            print(str(ex))
            raise Exception("create ARDLIB HistoryRecord FAILED") 

        # check if ASC-FITS compliant
        if hr.is_ascfits_compliant() is True:
            raise Exception("ardlib compliancy check FAILED")

        # check if ASC-FITS string output is the same as content input
        self.assertEqual(hr.as_ASC_FITS(with_tag=True), '\n'.join(content), "ardlib as_ASC_FITS(with_tag=True) FAILED")

        # check if as_FITS() prints the same string as as_ASC_FITS()
        self.assertEqual(hr.as_FITS(with_tag=True), '\n'.join(content), "ardlib as_FITS(with_tag=True) FAILED")

        as_str = "ARDLIB Version: 0.7.1-4\n" +"File used for CALDB info: /state/partition1/tmp.ascdsl3/L3ASRC____338268443n247/output/acisf01878_000N001_r0100_wmap3.fits\n" + "Note: CTI_APP = PPPPPBPBPP\n" + "Parameter file: /state/partition1/tmp.ascdsl3/L3ASRC____338268443n247/output/acisf01878_000N001_r0100_ardlib3.par\n" + "Vignetting File: /state/partition1/local/install.CAT3.0/CALDB/data/chandra/tel/hrma/bcf/vignet/hrmaD1996-12-20vignetN0003.fits[AXAF_VIGNET1]\n" + "Onaxis Area File: /state/partition1/local/install.CAT3.0/CALDB/data/chandra/tel/hrma/bcf/effarea/hrmaD1996-12-20axeffaN0007.fits[AXAF_AXEFFA1]\n" + "ACIS QE File: /state/partition1/local/install.CAT3.0/CALDB/data/chandra/acis/bcf/qe/acisD1997-04-17qeN0006.fits[AXAF_QE3]\n" + "ACIS Uniformity File: /state/partition1/local/install.CAT3.0/CALDB/data/chandra/acis/bcf/qe/acisD2000-01-29qeuN0006.fits[AXAF_QEU3]\n" + "ACIS Contamination File: /state/partition1/local/install.CAT3.0/CALDB/data/chandra/acis/bcf/contam/acisD1999-08-13contamN0004.fits[AXAF_CONTAM3]\n" + "ACIS 2 Badpix File: /state/partition1/tmp.ascdsl3/L3ASRC____338268443n247/input/acisf01878_000N001_bpix3.fits[BADPIX2]\n" + "File used for CALDB info: /state/partition1/tmp.ascdsl3/L3ASRC____338268443n247/output/acisf01878_000N001_r0100_wmap3.fits\n" + "Note: CTI_APP = PPPPPBPBPP\n" + "File used for CALDB info: /state/partition1/tmp.ascdsl3/L3ASRC____338268443n247/output/acisf01878_000N001_r0100_wmap3.fits\n" + "Note: CTI_APP = PPPPPBPBPP\n"
        
        # check as_string()
        self.assertEqual(hr.as_string(False), as_str, "ardlib as_string(with_tag=False) FAILED")

        # should not be unable to print ardlib record as a command
        try:
            hr.as_command()
        except Exception as ex:
            if "Unable to print ARDLIB history as command" not in str(ex):
                raise Exception("ardlib as_command() FAILED")


    def test_11(self):
        # tests for bad ASC-FITS compliant, PIXLIB, and ARDLIB content

        # non-compliant card in ASC-FITS compliant block
        content = ["HISTORY  TOOL  :dmcopy  2016-08-11T20:27:22                             ASC00041",
                   "HISTORY  PARM  :infile=/export/proc/tmp.ascdsl3/prs_run/tmp//L3AMRF____5ASC00042",
                   "REVISION=                    2 / Revision number of processed data              ",  # << non-compliant card
                   "HISTORY  CONT  :ts[sky=bounds(region(/export/proc/tmp.ascdsl3/prs_run/tmASC00044",
                   "HISTORY  CONT  :p//L3AMRF____587231988n944/input/acisfJ0331514m274138_00ASC00045",
                   "HISTORY  CONT  :1N021_r0001_srbreg3.fits[bkgreg]))][bin sky=1][opt type=ASC00046",
                   "HISTORY  CONT  :i4]                                                     ASC00047",
                   "HISTORY  PARM  :outfile=/export/proc/tmp.ascdsl3/prs_run/tmp//L3AMRF____ASC00048",
                   "HISTORY  CONT  :587231988n944/output/acisf05017_000N022_r0001b_fbinpsf3.ASC00049",
                   "HISTORY  CONT  :fits                                                    ASC00050",
                   "HISTORY  PARM  :kernel=default                                          ASC00051",
                   "HISTORY  PARM  :option=image                                            ASC00052",
                   "HISTORY  PARM  :verbose=0                                               ASC00053",
                   "HISTORY  PARM  :clobber=yes                                             ASC00054"]    
        hr = CXCHistory()
        
        try:
            hr.add_record("HISTORY",content)
        except Exception as ex:
            if "non-compliant card found in ASC-FITS content" not in str(ex):
                raise Exception("invalid ASC-FITS tag test FAILED")


        # try adding multiple Standard HISTORY records at once
        content = ['HISTORY dmcopy infile=/export/proc/tmp.ascdsl3/prs_run/tmp//L3AMRF____587231988n',
                   'HISTORY 944/output/acisf05017_000N022_r0001b_rayevt3.fits[sky=bounds(region(/exp',
                   'HISTORY ort/proc/tmp.ascdsl3/prs_run/tmp//L3AMRF____587231988n944/input/acisfJ03',
                   'HISTORY 31514m274138_001N021_r0001_srbreg3.fits[bkgreg]))][bin sky=1][opt type=i',
                   'HISTORY 4] outfile=/export/proc/tmp.ascdsl3/prs_run/tmp//L3AMRF____587231988n944',
                   'HISTORY /output/acisf05017_000N022_r0001b_fbinpsf3.fits kernel=default option=im',
                   'HISTORY age verbose=0 clobber=yes                                               ']
        try:
            hr.add_record("HISTORY",content)
        except Exception as ex:
            if "Cannot create more than one history record" not in str(ex):
                raise Exception("add multiple non-ASC-FITS compliant records test FAILED")

            
        content = ["HISTORY  TOOL  :dmhedit  2016-08-11T20:43:33                            ASC00870",
                   "HISTORY  PARM  :infile=@/this/is/a/super/loooooooooooooong/directory/namASC00871",
                   "HISTORY  CONT  :e/but/wait/there/is/lots/lots/lots/mooooooooooooore/infiASC00872",
                   "HISTORY  CONT  :le.lis                                                  ASC00873",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack1ASC00874",
                   "HISTORY  CONT  :.fits                                                   ASC00875",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack2ASC00876",
                   "HISTORY  CONT  :.fits                                                   ASC00877",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack3ASC00878",
                   "HISTORY  CONT  :.fits                                                   ASC00879",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name//but/wASC00880",
                   "HISTORY  CONT  :ait/there/is/lots/lots/lots/mooooooooooooore/stack4.fitsASC00881",
                   "HISTORY  CONT  :                                                        ASC00882",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack5ASC00883",
                   "HISTORY  CONT  :.fits                                                   ASC00884",
                   "HISTORY  PARM  :outfile=/tmp/dir/outfile.fits                           ASC00885",
                   "HISTORY  This is a non-ASCFITS compliant history record                         "]
        try:
            hr.add_record("HISTORY",content)
        except Exception as ex:
            if "non-compliant card found in ASC-FITS content" not in str(ex):
                print(str(ex))
                raise Exception("add one ASC-FITS record and one non-ASC-FITS compliant record test FAILED")

        
        content = ["HISTORY  This is a non-ASCFITS compliant history record                         ",
                   "HISTORY  TOOL  :dmhedit  2016-08-11T20:43:33                            ASC00870",
                   "HISTORY  PARM  :infile=@/this/is/a/super/loooooooooooooong/directory/namASC00871",
                   "HISTORY  CONT  :e/but/wait/there/is/lots/lots/lots/mooooooooooooore/infiASC00872",
                   "HISTORY  CONT  :le.lis                                                  ASC00873",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack1ASC00874",
                   "HISTORY  CONT  :.fits                                                   ASC00875",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack2ASC00876",
                   "HISTORY  CONT  :.fits                                                   ASC00877",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack3ASC00878",
                   "HISTORY  CONT  :.fits                                                   ASC00879",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name//but/wASC00880",
                   "HISTORY  CONT  :ait/there/is/lots/lots/lots/mooooooooooooore/stack4.fitsASC00881",
                   "HISTORY  CONT  :                                                        ASC00882",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack5ASC00883",
                   "HISTORY  CONT  :.fits                                                   ASC00884",
                   "HISTORY  PARM  :outfile=/tmp/dir/outfile.fits                           ASC00885",
                   "HISTORY  This is a non-ASCFITS compliant history record                         "]
        try:
            hr.add_record("HISTORY",content)
        except Exception as ex:
            if "Cannot create more than one history record" not in str(ex):
                raise Exception("add one non-ASC-FITS record and one ASC-FITS compliant record test FAILED")


        content = ["HISTORY  PARM  :PIXLIB Version: 4.0.0                                   ASC00015",
                   "HISTORY  PARM  :******     PIXLIB Parameter File      *****             ASC00016",
                   "HISTORY  PARM  :/home/ascds/DS.l3/param/geom.par:                       ASC00017",
                   "HISTORY  PARM  :instruments  = /export/CALDB/level3/data/chandra/defaultASC00018",
                   "HISTORY  CONT  :/geom/telD1999-07-23geomN0006.fits(CALDB)               ASC00019",
                   "HISTORY  TOOL  :dmhedit  2016-08-11T20:43:33                            ASC00870",
                   "HISTORY  PARM  :infile=@/this/is/a/super/loooooooooooooong/directory/namASC00871",
                   "HISTORY  CONT  :e/but/wait/there/is/lots/lots/lots/mooooooooooooore/infiASC00872",
                   "HISTORY  CONT  :le.lis                                                  ASC00873",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack1ASC00874",
                   "HISTORY  CONT  :.fits                                                   ASC00875",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack2ASC00876",
                   "HISTORY  CONT  :.fits                                                   ASC00877",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack3ASC00878",
                   "HISTORY  CONT  :.fits                                                   ASC00879",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name//but/wASC00880",
                   "HISTORY  CONT  :ait/there/is/lots/lots/lots/mooooooooooooore/stack4.fitsASC00881",
                   "HISTORY  CONT  :                                                        ASC00882",
                   "HISTORY  STCK  :/this/is/a/super/loooooooooooooong/directory/name/stack5ASC00883",
                   "HISTORY  CONT  :.fits                                                   ASC00884",
                   "HISTORY  PARM  :outfile=/tmp/dir/outfile.fits                           ASC00885"]
        try:
            hr.add_record("HISTORY",content)
        except Exception as ex:
            if "Cannot create more than one history record" not in str(ex):
                raise Exception("add one PIXLIB record and one ASC-FITS compliant record test FAILED")


        content= ["HISTORY  PARM  :ARDLIB Version: 0.7.1-4                                 ASC00028",
                  "HISTORYxxPARM  :File used for CALDB info: /state/partition1/tmp.ascdsl3/ASC00029",
                  "HISTORY  CONT  :L3ASRC____338268443n247/output/acisf01878_000N001_r0100_ASC00030",
                  "HISTORY  CONT  :wmap3.fits                                              ASC00031"]

        try:
            hr.add_record("HISTORY",content)
        except Exception as ex:
            if "ASC-FITS content chars at position 8, 9, 14, and 15 must be blank" not in str(ex):
                raise Exception("add ASC-FITS record with non-blank spaces 8 and 9 test FAILED")

        content= ["HISTORY  PARM  :ARDLIB Version: 0.7.1-4                                 ASC00028",
                  "HISTORY  PARM  :File used for CALDB info: /state/partition1/tmp.ascdsl3/ASC00029",
                  "HISTORY  CONTxx:L3ASRC____338268443n247/output/acisf01878_000N001_r0100_ASC00030",
                  "HISTORY  CONT  :wmap3.fits                                              ASC00031"]

        try:
            hr.add_record("HISTORY",content)
        except Exception as ex:
            if "ASC-FITS content chars at position 8, 9, 14, and 15 must be blank" not in str(ex):
                raise Exception("add ASC-FITS record with non-blank spaces 14 and 15 test FAILED")

    def test_12(self):
        # test creating an ASC-FITS compliant history record with only a tool name

        try:
            hr = HistoryRecord(tool='bob')
        except Exception as ex:
            print(str(ex))
            raise Exception("create HistoryRecord tool='bob' FAILED")
        
        # check if ASC-FITS compliant
        self.assertTrue(hr.is_ascfits_compliant(), "tool='bob' compliancy check FAILED")

        # check if ASC-FITS string output is the same as content input
        self.assertEqual( hr.as_ASC_FITS(with_tag=True)[0:20], "HISTORY  TOOL  :bob ", "tool='bob' as_ASC_FITS(with_tag=True) FAILED")

        # check as_FITS()
        self.assertEqual(hr.as_FITS(), "bob                                                                     ", "tool='bob' as FITS FAILED")



if __name__ == '__main__':
    unittest.main(verbosity=5)
