Load timed photon data
Loads the photon data around a source, subject to a GTI.
The function get_photon_data
loads a data dataset of photons in a cone about a specified source, with time, position and energy info, the latter two binned.
These are retrieved from a set of pickled dicts, one per week.
config = Config(verbose=3)
if config.valid:
source = PointSource('Geminga')
print(f'Test loading a photon data set, for source {source.name}')
photon_data = get_photon_data(config, source, key=None)
print(f'Head of the table, length {len(photon_data):,}:\n{photon_data.head()}')
else:
print('Not testing since no files.')
# #####################################################################################
# # Parquet code -- TODO just copied, need to test.
# # from notebooks/code development/parquet_writer.ipynb
# class ParquetConversion(object):
# import glob, pickle;
# import healpy
# import pyarrow as pa
# import pyarrow.parquet as pq
# def __init__(self,
# data_file_pattern ='$FERMI/data/P8_P305/time_info/month_*.pkl',
# dataset = '/nfs/farm/g/glast/u/burnett/analysis/lat_timing/data/photon_dataset'):
# self.files = sorted(glob.glob(os.path.expandvars(data_file_pattern)));
# print(f'Found {len(self.files)} monthly files with pattern {data_file_pattern}'\
# f'\nWill store parquet files here: {dataset}')
# if os.path.exists(dataset):
# print(f'Dataset folder {dataset} exists')
# else:
# os.makedirs(dataset)
# self.dataset=dataset
# def convert_all(self):
# files=self.files
# dataset=self.dataset
# nside=1024
# def convert(month):
# infile = files[month-1]
# print(month, end=',')
# #print(f'Reading file {os.path.split(infile)[-1]} size {os.path.getsize(infile):,}' )
# with open(infile, 'rb') as inp:
# t = pickle.load(inp,encoding='latin1')
# # convert to DataFrame, add month index as new column for partition, then make a Table
# df = pd.DataFrame(t['timerec'])
# tstart = t['tstart']
# df['month']= np.uint8(month)
# # add a columb with nest indexing -- makes the ring redundant, may remove later
# df['nest_index'] = healpy.ring2nest(nside, df.hpindex).astype(np.int32)
# table = pa.Table.from_pandas(df, preserve_index=False)
# # add to partitioned dataset
# pq.write_to_dataset(table, root_path=dataset, partition_cols=['month'] )
# for i in range(len(files)):
# convert(month=i+1)