|
1 |
| -import csv |
2 | 1 | import logging
|
3 | 2 |
|
4 | 3 | import astropy.units as u
|
| 4 | +import numpy as np |
5 | 5 | import yaml
|
| 6 | +from astropy.table import Table |
6 | 7 |
|
7 | 8 | import v2dl5.light_curves.orbital_period as orbit
|
8 | 9 |
|
@@ -66,65 +67,11 @@ def _read_fluxes_from_file(self, data_config):
|
66 | 67 | if data_config["file_name"].endswith(".csv") or data_config["file_name"].endswith(
|
67 | 68 | ".ecsv"
|
68 | 69 | ):
|
69 |
| - return self._read_fluxes_from_csv_file(data_config) |
| 70 | + return self._read_fluxes_from_ecsv_file(data_config) |
70 | 71 | except KeyError:
|
71 | 72 | self._logger.error("File name not found in configuration file")
|
72 | 73 | raise KeyError
|
73 | 74 |
|
74 |
| - def _read_fluxes_from_csv_file(self, data_config, TimeMinMax=True, MJD_min=-1.0, MJD_max=-1.0): |
75 |
| - """ |
76 |
| - Read gamma-ray fluxes from csv file (open gamma-ray format) |
77 |
| - - ignore all lines with '#' |
78 |
| - - accept a random number of spaces as delimiter |
79 |
| -
|
80 |
| - Parameters: |
81 |
| - ----------- |
82 |
| - data_config: dict |
83 |
| - configuration dictionary |
84 |
| - TimeMinMax: bool |
85 |
| - flag to read time_min and time_max |
86 |
| - MJD_min: float |
87 |
| - MJD min value for MJD cut |
88 |
| - MJD_max: float |
89 |
| - MJD max value for MJD cut |
90 |
| -
|
91 |
| - """ |
92 |
| - fp = open(data_config["file_name"]) |
93 |
| - rdr = csv.DictReader( |
94 |
| - filter(lambda row: row[0] != "#", fp), delimiter=" ", skipinitialspace=True |
95 |
| - ) |
96 |
| - f = {} |
97 |
| - f["time_min"] = [] |
98 |
| - f["time_max"] = [] |
99 |
| - f["flux"] = [] |
100 |
| - f["flux_err"] = [] |
101 |
| - for row in rdr: |
102 |
| - if TimeMinMax: |
103 |
| - t_min = float(row["time_min"]) |
104 |
| - t_max = float(row["time_max"]) |
105 |
| - else: |
106 |
| - t_min = float(row["time"]) |
107 |
| - t_max = float(row["time"]) + 0.1 |
108 |
| - if MJD_min > 0 and t_min < MJD_min: |
109 |
| - continue |
110 |
| - if MJD_max > 0 and t_max > MJD_max: |
111 |
| - continue |
112 |
| - |
113 |
| - f["time_min"].append(t_min) |
114 |
| - f["time_max"].append(t_max) |
115 |
| - |
116 |
| - f["flux"].append(float(row["flux"])) |
117 |
| - if "flux_err" in row: |
118 |
| - f["flux_err"].append(float(row["flux_err"])) |
119 |
| - elif "flux_up" in row: |
120 |
| - f["flux_err"].append(0.5 * abs(float(row["flux_up"]) - float(row["flux_down"]))) |
121 |
| - fp.close() |
122 |
| - |
123 |
| - f["MJD"] = [0.5 * (a + b) for a, b in zip(f["time_min"], f["time_max"])] |
124 |
| - f["MJD_err"] = [0.5 * (b - a) for a, b in zip(f["time_min"], f["time_max"])] |
125 |
| - |
126 |
| - return f |
127 |
| - |
128 | 75 | def _add_orbital_parameters(self, data):
|
129 | 76 | """
|
130 | 77 | Add orbital phase to light-curve data data.
|
@@ -165,3 +112,53 @@ def convert_photon_to_energy_flux(C_v, C_e, E_0, gamma):
|
165 | 112 | # conversion to erg
|
166 | 113 | f = f * (E_0.to(u.erg)).value
|
167 | 114 | return [v * f for v in C_v], [e * f for e in C_e]
|
| 115 | + |
| 116 | + def _read_fluxes_from_ecsv_file(self, data_config, TimeMinMax=True, MJD_min=-1.0, MJD_max=-1.0): |
| 117 | + """ |
| 118 | + Read gamma-ray fluxes from ecsv file (open gamma-ray format) |
| 119 | +
|
| 120 | + Parameters: |
| 121 | + ----------- |
| 122 | + data_config: dict |
| 123 | + configuration dictionary |
| 124 | + TimeMinMax: bool |
| 125 | + flag to read time_min and time_max |
| 126 | + MJD_min: float |
| 127 | + MJD min value for MJD cut |
| 128 | + MJD_max: float |
| 129 | + MJD max value for MJD cut |
| 130 | +
|
| 131 | + """ |
| 132 | + table = Table.read(data_config["file_name"]) |
| 133 | + f = {} |
| 134 | + if not TimeMinMax: |
| 135 | + table["time_min"] = table["time"].data |
| 136 | + table["time_max"] = table["time"].data + 0.1 |
| 137 | + |
| 138 | + # MJD filter |
| 139 | + condition = np.ones(len(table), dtype=bool) |
| 140 | + if MJD_min > -1: |
| 141 | + condition &= table["time_min"] > MJD_min |
| 142 | + if MJD_max > -1: |
| 143 | + condition &= table["time_max"] < MJD_max |
| 144 | + table = table[condition] |
| 145 | + |
| 146 | + f = {} |
| 147 | + f["time_min"] = table["time_min"].data.tolist() |
| 148 | + f["time_max"] = table["time_max"].data.tolist() |
| 149 | + f["flux"] = table["flux"].data.flatten().tolist() |
| 150 | + if "flux_err" in table.colnames: |
| 151 | + f["flux_err"] = table["flux_err"].data.flatten().tolist() |
| 152 | + else: |
| 153 | + up = table["flux_up"].data.flatten().tolist() |
| 154 | + down = table["flux_down"].data.flatten().tolist() |
| 155 | + f["flux_err"] = [0.5 * abs(u - d) for u, d in zip(up, down)] |
| 156 | + f["MJD"] = [0.5 * (a + b) for a, b in zip(f["time_min"], f["time_max"])] |
| 157 | + f["MJD_err"] = [0.5 * (b - a) for a, b in zip(f["time_min"], f["time_max"])] |
| 158 | + if "flux_ul" in table.colnames: |
| 159 | + flux_ul = table["flux_ul"].data.flatten().tolist() |
| 160 | + is_ul = table["is_ul"].data.flatten().tolist() |
| 161 | + f["flux_ul"] = [flux if is_ul else -1.0 for flux, is_ul in zip(flux_ul, is_ul)] |
| 162 | + else: |
| 163 | + f["flux_ul"] = [-1.0 for _ in f["flux"]] |
| 164 | + return f |
0 commit comments