<jupyter_start><jupyter_text>Test code to exercise dataset handling with PSF event types.<jupyter_code>import os
os.environ['CALDB'] = os.path.expandvars('${BASE_DIR}/irfs/caldb/CALDB')
os.environ['TIMING_DIR'] = os.path.expandvars('${GLAST_EXT}/extFiles/v0r9/jplephem')

import numpy as np
from uw.like2 import configuration, bands, dataset, from_healpix
map(reload,(configuration,bands,dataset, from_healpix))
from uw.irfs import (caldb,irfman,psf,effective_area,exposure)
map(reload,(caldb,irfman,psf,effective_area,exposure))<jupyter_output><empty_output><jupyter_text>Specify a working directory and create it if needed<jupyter_code>cwd = os.path.expandvars('/tmp/test_psf_binning')
if not os.path.exists(cwd):
    os.mkdir(cwd)
os.chdir(cwd)<jupyter_output><empty_output><jupyter_text>Create configuration filesWrite out config.txt files in individual subdirectories. Configurations are set up to test:  * Loading a pass 7 dataset  * Loading a pass 8 dataset with front/back binning  * Loading a pass 8 dataset with PSF binning using the 'dataspec' config key   * Loading a pass 8 dataset with PSF binning using the 'datadict' config key     All configurations use a single month of data. Paths to data files<jupyter_code>cfg7 = dict(
    dataspec=dict(
        ft1files=os.path.expandvars('/afs/slac/g/glast/groups/catalog/P7_P202/Source/P7_P202_BASE_Source_001_zmax100.fits'),
        ft2files=os.path.expandvars('/afs/slac/g/glast/groups/catalog/P7_P202/ft2_4years.fits'),
        binfile=os.path.join(cwd,'P202_4bpd_fb.fits'),
        ltcube=os.path.join(cwd,'ltcube.fits'),
        binsperdec=4,
        
    ),
    irf= 'P7REP_SOURCE_V15',
    
    input_model = dict(path='$FERMI/skymodels/P202/uw30'),

    diffuse= dict(
        ring    =  dict(filename='template_4years_P7_v15_repro_v3_4bpd.zip',
                    correction='galactic_correction_v3.csv', 
                    systematic=0.0316), 
        isotrop = '4Y_SOURCE_**_V9_EGBfree.txt', 
        SunMoon = 'SunMoon_4years_zmax100.fits', 
        limb    = 'AziLimb:azi_limb_P7_v0_*.txt',
        ),
    
    extended= 'Extended_archive_v13',
)

cfg8_fb = dict(
    dataspec=dict(
        ft1files=os.path.expandvars('$FERMI/data/P8_P302/zmax105/P302_Source_001_zmax105.fits'),
        ft2files=os.path.expandvars('/afs/slac/g/glast/groups/catalog/P8_P302/ft2_2008.fits'),
        binfile=os.path.join(cwd,'P302_4bpd_fb.fits'),
        ltcube=os.path.join(cwd,'ltcube.fits'),
        binsperdec=4,
    ),
    irf= 'P8R2_SOURCE_V6',
    
    input_model = dict(path='$FERMI/skymodels/P302_7years/uw993'),

    diffuse = dict(
    ring    = dict(type='HealpixCube', 
            filename='/nfs/slac/g/ki/ki20/elliott/Pass8_GC/gcfit/results/P8_P302_ultraclean_veto_z90/galprop/models/'\
                'diffuse_model_flux_P8_P302_ultraclean_veto_z90_umap_ifilter_Galprop_5rings_IC123_geomLoopI_PS_P8uw963_adaptive_ps_mask_P8uw963_DM_Cusp_n2p5_ISO_plaw_pnorm_psfall.fits',
            correction='galactic_correction_uw1002A.csv', 
            systematic=0.0316), 
    isotrop = dict(type='IsotropicList', filename='isotropic_source_*_4years_P8V3.txt',
                   correction=None,
    ),
    limb    = None, 
    SunMoon = 'template_SunMoon_6years_zmax100.fits', 
    ),
    
    extended= 'Extended_archive_v14',
)

cfg8_psf = dict(
    dataspec=dict(
        ft1files=[os.path.expandvars('$FERMI/data/P8_P302/zmax105/P302_Source_001_zmax105.fits')],
        ft2files=[os.path.expandvars('/afs/slac/g/glast/groups/catalog/P8_P302/ft2_2008.fits')],
        binfile=os.path.join(cwd,'P302_4bpd_psf.fits'),
        ltcube=os.path.join(cwd,'ltcube.fits'),
        binsperdec=4,
        psf_event_types=True,
    ),
    irf= 'P8R2_SOURCE_V6',
    
    input_model = dict(path='$FERMI/skymodels/P302_7years/uw993'),

    diffuse = dict(
    ring    = dict(type='HealpixCube', 
            filename='/nfs/slac/g/ki/ki20/elliott/Pass8_GC/gcfit/results/P8_P302_ultraclean_veto_z90/galprop/models/'\
                'diffuse_model_flux_P8_P302_ultraclean_veto_z90_umap_ifilter_Galprop_5rings_IC123_geomLoopI_PS_P8uw963_adaptive_ps_mask_P8uw963_DM_Cusp_n2p5_ISO_plaw_pnorm_psfall.fits',
            correction='galactic_correction_uw1002A.csv', 
            systematic=0.0316), 
    isotrop = dict(type='IsotropicList', filename='isotropic_source_*_4years_P8V3.txt',
                   correction=None,
    #correction='isotropic_correction_*_uw965.csv'),
                   ),
    limb    = None, 
    SunMoon = 'template_SunMoon_6years_zmax100.fits', 
    ),
    
    extended= 'Extended_archive_v14',
)
dataspec_dict = dict(P302_psf_test=dict(data_name="Pass8, P302, 1 month, PSF event types",
                 ft1files = '$FERMI/data/P8_P302/zmax105/P302_Source_001_zmax105.fits',
                 ft2files = '/afs/slac/g/glast/groups/catalog/P8_P302/ft2_2008.fits',
                 binsperdec=4,
                 binfile = 'P302_4bpd_psf.fits',
                 ltcube =  'ltcube.fits', 
                 psf_event_types = True,
                 legacy = True,))
cfg8_psf_datadict = dict(
    datadict=dict(
        dataname='P302_psf_test',
        ft1files=[os.path.expandvars('$FERMI/data/P8_P302/zmax105/P302_Source_001_zmax105.fits')],
        ft2files=[os.path.expandvars('/afs/slac/g/glast/groups/catalog/P8_P302/ft2_2008.fits')],
        binfile=os.path.join(cwd,'P302_4bpd_psf.fits'),
        ltcube=os.path.join(cwd,'ltcube.fits'),
        binsperdec=4,
        psf_event_types=True,
    ),
    irf= 'P8R2_SOURCE_V6',
    
    input_model = dict(path='$FERMI/skymodels/P302_7years/uw993'),

    diffuse = dict(
    ring    = dict(type='HealpixCube', 
            filename='/nfs/slac/g/ki/ki20/elliott/Pass8_GC/gcfit/results/P8_P302_ultraclean_veto_z90/galprop/models/'\
                'diffuse_model_flux_P8_P302_ultraclean_veto_z90_umap_ifilter_Galprop_5rings_IC123_geomLoopI_PS_P8uw963_adaptive_ps_mask_P8uw963_DM_Cusp_n2p5_ISO_plaw_pnorm_psfall.fits',
            correction='galactic_correction_uw1002A.csv', 
            systematic=0.0316), 
    isotrop = dict(type='IsotropicList', filename='isotropic_source_*_4years_P8V3.txt',
                   correction=None,
    ),
                   #correction='isotropic_correction_*_uw965.csv'),
    limb    = None, 
    SunMoon = 'template_SunMoon_6years_zmax100.fits', 
    ),
    
    extended= 'Extended_archive_v14',
)


for cfg,cfgdir in zip([cfg7,cfg8_fb,cfg8_psf],
                      ['P202_4bpd_fb','P302_4bpd_fb','P302_4bpd_psf']):
    if not os.path.exists(cfgdir):
        os.mkdir(cfgdir)
    with open(os.path.join(cfgdir,'config.txt'),'w') as f:
        f.write(str(cfg))
cfg,cfgdir = cfg8_psf_datadict,'P302_4bpd_psf_datadict'
if not os.path.exists(cfgdir):
    os.mkdir(cfgdir)
with open(os.path.join(cfgdir,'config.txt'),'w') as f:
    f.write(str(cfg))
with open(os.path.join(cfgdir,'dataspec.py'),'w') as f:
    f.write(str(dataspec_dict))
    
if not os.path.basename(os.getcwd())=='P202_4bpd_fb':
    os.chdir(os.path.join(cwd,'P202_4bpd_fb'))
config_p7 = configuration.Configuration(quiet=True)

if not os.path.basename(os.getcwd())=='P302_4bpd_fb':
    os.chdir(os.path.join(cwd,'P302_4bpd_fb'))

config_fb = configuration.Configuration(quiet=True)

if not os.path.basename(os.getcwd())=='P302_4bpd_psf':
    os.chdir(os.path.join(cwd,'P302_4bpd_psf'))

config_spec = configuration.Configuration(quiet=True)

if not os.path.basename(os.getcwd())=='P302_4bpd_psf_datadict':
    os.chdir(os.path.join(cwd,'P302_4bpd_psf_datadict'))
config_dict = configuration.Configuration(quiet=True)<jupyter_output>Warning: PROC_VER not found in /afs/slac/g/glast/groups/catalog/P7_P202/Source/P7_P202_BASE_Source_001_zmax100.fits header
processing cuts:  ZENITH_ANGLE zenith_cut
ft1_cut DSTYP3: ZENITH_ANGLE
DSUNI3: deg
DSVAL3: 0:100
DSREF3: None
processing cuts:  THETA theta_cut
ft1_cut None
processing cuts:  EVENT_CLASS event_class_cut
ft1_cut None
Failed dss comparison:
 ltcube DSTYP1: BIT_MASK(EVENT_CLASS,128,P8R2)
DSUNI1: DIMENSIONLESS
DSVAL1: 1:1
DSREF1: None
DSTYP2: TIME
DSUNI2: s
DSVAL2: TABLE
DSREF2: :GTI
DSTYP3: ENERGY
DSUNI3: MeV
DSVAL3: 10:1000000
DSREF3: None
DSTYP4: ZENITH_ANGLE
DSUNI4: deg
DSVAL4: 0:105
DSREF4: None
DSTYP5: THETA
DSUNI5: deg
DSVAL5: :66.4
DSREF5: None,
 FT1 DSTYP1: TIME
DSUNI1: s
DSVAL1: TABLE
DSREF1: :GTI
DSTYP2: ENERGY
DSUNI2: MeV
DSVAL2: 30:1000000
DSREF2: None
DSTYP3: ZENITH_ANGLE
DSUNI3: deg
DSVAL3: 0:100
DSREF3: None
DSTYP4: THETA
DSUNI4: deg
DSVAL4: :66.4
DSREF4: None
DSTYP5: BIT_MASK(EVENT_CLASS,2)
DSUNI5: DIMENSIONLESS
DSVAL5: 1:1
DSREF5: None
on iteration 0
[...]<jupyter_text>Test loading dataCreate a BandSet for each Configuration, and load the data.<jupyter_code>from uw.like2 import from_healpix, bands
roi_index = 840
bandsets = []
for config in (config_p7, config_fb, config_spec,config_dict):
    bandsets += [bands.BandSet(config,roi_index,load=True)]
#psf_info = config_p7.irfs.caldb('psf',version = config_p7.irfs.irf_version,
#                        event_class=config_p7.irfs.event_class,
#                       event_type = config_p7.irfs.event_types)
iman = config.irfs
iman.event_types
roi_sources = from_healpix.ROImodelFromHealpix(config_spec,
                                                roi_index,
                                                load_kw=dict(rings=2))

from uw.like2 import bandlike
for roi_bands in bandsets:
    blikes = bandlike.BandLikeList(roi_bands,roi_sources)
    print(blikes)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11834319526627218, 'fraction_numerical': 0.05885460693153001, 'mean_word_length': 3.708955223880597, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '564265', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 4139, 'n_tokens_neox': 3780, 'n_words': 477, 'path': 'python/uw/like2/notebooks/PSF binning test.ipynb', 'repo_name': 'coclar/pointlike', 'size': '9464'}