Data IO

class cosipy.data_io.DataIO(input_yaml, pw=None)[source]

Handles main inputs and outputs.

class cosipy.data_io.UnBinnedData(input_yaml, pw=None)[source]

Handles unbinned data.

read_tra(output_name=None, run_test=False, use_ori=False, event_min=None, event_max=None)[source]

Reads MEGAlib .tra (or .tra.gz) file and creates cosi datset.

Parameters:
  • output_name (str, optional) – Prefix of output file (default is None, in which case no output is written).

  • run_test (bool, optional) – This is for unit testing only! Keep False unless comparing to MEGAlib calculations.

  • use_ori (bool, optional) – Option to get pointing information from the orientation file, based on event time-stamps (default is False, in which case the pointing information comes from the event file itself). Note: this is an option for now, but will later be the default.

  • event_min (int, optional) – Minimum event number to process (inclusive). All events below this will be skipped.

  • event_max (int, optional) –

    Maximum event number to process (non-inclusive). All events at and above this will be skipped.

    Note: event_min and event_max correspond to the total number of events in the file, which is not necessarily the same as the event ID number. The purpose of this is to allow the data to be read in chunks, in order to overcome memory limitations, depending on the user’s system.

Returns:

The returned dictionary contains the COSI dataset, which has the form: cosi_dataset = {‘Energies’:erg, ‘TimeTags’:tt, ‘Xpointings’:np.array([lonX,latX]).T, ‘Ypointings’:np.array([lonY,latY]).T, ‘Zpointings’:np.array([lonZ,latZ]).T, ‘Phi’:phi, ‘Chi local’:chi_loc, ‘Psi local’:psi_loc, ‘Distance’:dist, ‘Chi galactic’:chi_gal, ‘Psi galactic’:psi_gal} Arrays contain unbinned photon data.

Return type:

cosi_dataset, dict

Notes

The current code is only able to handle data with Compton events. It will need to be modified to handle single-site and pair events.

This method sets the instance attribute, cosi_dataset, but it does not explicitly return this.

instrument_pointing()[source]

Get pointing information from ori file.

Initializes interpolated functions for lonx, latx, lonz, latz in radians.

Returns:

  • xl_interp (scipy:interpolate:interp1d)

  • xb_interp (scipy:interpolate:interp1d)

  • zl_interp (scipy:interpolate:interp1d)

  • zb_interp (scipy:interpolate:interp1d)

Note

This method sets the instance attributes, but it does not explicitly return them.

construct_scy(scx_l, scx_b, scz_l, scz_b)[source]

Construct y-coordinate of instrument pointing given x and z directions.

Parameters:
  • scx_l (float) – Longitude of x direction in degrees.

  • scx_b (float) – Latitude of x direction in degrees.

  • scz_l (float) – Longitude of z direction in degrees.

  • scz_b (float) – Latitude of z direction in degrees.

Returns:

  • ra (float) – Right ascension (in degrees) for y-coordinate of instrument pointing.

  • dec (float) – Declination (in degrees) for y-coordinate of instrument pointing.

Note

Here, z is the optical axis.

polar2cart(ra, dec)[source]

Coordinate transformation of ra/dec (lon/lat) [phi/theta] polar/spherical coordinates into cartesian coordinates.

Parameters:
  • ra (float) – Right ascension in degrees.

  • dec (float) – Declination in degrees.

Returns:

x, y, and z cartesian coordinates in radians.

Return type:

array

cart2polar(vector)[source]

Coordinate transformation of cartesian x/y/z values into spherical (deg).

Parameters:

vector (vec) – Vector of x/y/z values.

Returns:

  • ra (float) – Right ascension in degrees.

  • dec (float) – Declination in degrees.

write_unbinned_output(output_name)[source]

Writes unbinned data file to either fits or hdf5.

Parameters:

output_name (str) – Name of output file. Only include prefix (not file type).

get_dict_from_fits(input_fits)[source]

Constructs dictionary from input fits file.

Parameters:

input_fits (str) – Name of input fits file.

Returns:

Dictionary constructed from input fits file.

Return type:

dict

get_dict_from_hdf5(input_hdf5)[source]

Constructs dictionary from input hdf5 file

Parameters:

input_hdf5 (str) – Name of input hdf5 file.

Returns:

Dictionary constructed from input hdf5 file.

Return type:

dict

select_data(output_name=None, unbinned_data=None)[source]

Applies cuts to unbinnned data dictionary.

Parameters:
  • unbinned_data (str, optional) – Name of unbinned dictionary file.

  • output_name (str, optional) – Prefix of output file (default is None, in which case no file is saved).

Note

Only cuts in time are allowed for now.

combine_unbinned_data(input_files, output_name=None)[source]

Combines input unbinned data files.

Parameters:
  • input_files (list) – List of file names to combine.

  • output_name (str, optional) – Prefix of output file.

class cosipy.data_io.BinnedData(input_yaml, pw=None)[source]

Handles binned data.

get_binned_data(unbinned_data=None, output_name=None, make_binning_plots=False, psichi_binning='galactic', event_range=None)[source]

Bin the data using histpy and mhealpy.

Parameters:
  • unbinned_data (str, optional) – Name of unbinned data file to use. Input file is either .fits or .hdf5 as specified in the unbinned_output parameter in inputs.yaml.

  • output_name (str, optional) – Prefix of output file.

  • make_binning_plots (bool, optional) – Option to make basic plots of the binning (default is False).

  • psichi_binning (str, optional) – ‘galactic’ for binning psichi in Galactic coordinates, or ‘local’ for binning in local coordinates. Default is Galactic.

  • event_range (list of integers, optional) – min and max event to use for the binning.

Returns:

binned_data – Data is binned in four axes: time, measured energy, Compton scattering angle (phi), and scattering direction (PsiChi).

Return type:

histpy:Histogram

Note

This method constructs the instance attribute, binned_data, but it does not explicitly return it.

load_binned_data_from_hdf5(binned_data)[source]

Loads binned histogram from hdf5 file.

Parameters:

binned_data (str) – Name of binned data file to load.

Returns:

binned_data – Data is binned in four axes: time, measured energy, Compton scattering angle (phi), and scattering direction (PsiChi).

Return type:

histpy:Histogram

Note

This method sets the instance attribute, binned_data, but it does not explicitly return it.

get_binning_info(binned_data=None)[source]

Get binning information from Histpy histogram.

Parameters:

binned_data (str) – Name of binned data hdf5 file to use.

plot_binned_data(binned_data=None)[source]

Plot binnned data for all axes.

Parameters:

binned_data (histpy:Histogram, optional) – Name of binned histogram to use.

plot_psichi_map()[source]

Plot psichi healpix map.

plot_psichi_map_slices(Em, phi, output, binned_data=None, coords=None)[source]

Plot psichi map in slices of Em and phi.

Parameters:
  • Em (int) – Bin of energy slice.

  • phi (int) – Bin of phi slice.

  • output (str) – Prefix of output plot.

  • binned_data (histpy:Histogram, optional) – Name of binned histogram to use.

  • coords (list, optional) – Coordinates of source position. Galactic longitude and latidude for Galactic coordinates. Azimuthal and latitude for local coordinates.

get_raw_spectrum(binned_data=None, time_rate=False, output_name=None)[source]

Calculates raw spectrum of binned data, plots, and writes to file.

Parameters:
  • binned_data (str, optional) – Name of binnned hdf5 data file.

  • output_name (str, optional) – Prefix of output files. Writes both pdf and dat file.

  • time_rate (bool, optional) – If True, calculates ct/keV/s. The defualt is ct/keV.

get_raw_lightcurve(binned_data=None, output_name=None)[source]

Calculates raw lightcurve of binned data, plots, and writes data to file.

Parameters:
  • binned_data (str, optional) – Name of binnned hdf5 data file to use.

  • output_name (str, optional) – Prefix of output files. Writes both pdf and dat file.

class cosipy.data_io.ReadTraTest(input_yaml, pw=None)[source]

Old method for reading tra file, used for unit testing.

read_tra_old(make_plots=True)[source]

Reads in MEGAlib .tra (or .tra.gz) file.

This method uses MEGAlib to read events from the tra file. This is used to compare to the new event reader, which is independent of MEGAlib.

Parameters:

make_plots (bool, optional) – Option to make binning plot.

Returns:

cosi_dataset – Returns COSI dataset as a dictionary of the form: cosi_dataset = {‘Full filename’:self.data_file, ‘Energies’:erg, ‘TimeTags’:tt, ‘Xpointings’:np.array([lonX,latX]).T, ‘Ypointings’:np.array([lonY,latY]).T, ‘Zpointings’:np.array([lonZ,latZ]).T, ‘Phi’:phi, ‘Chi local’:chi_loc, ‘Psi local’:psi_loc, ‘Distance’:dist, ‘Chi galactic’:chi_gal, ‘Psi galactic’:psi_gal}

Return type:

dict

Note

This method sets the instance attribute, cosi_dataset, but it does not explicitly return this.