def to_event_time(self, jtimes): j_start = wr.j2000(date=self.t_start) j_stop = wr.j2000(date=self.t_stop) assert j_start < j_stop ev_time_start = pd.Timestamp(self.t_start).value ev_time_stop = pd.Timestamp(self.t_stop).value assert ev_time_start < ev_time_stop jfrac = (jtimes - j_start) / (j_stop - j_start) return jfrac * (ev_time_stop - ev_time_start) + ev_time_start
def __init__(self, *args, wimp_kwargs=None, **kwargs): # Compute the energy spectrum in a given time range # Times used by wimprates are J2000 timestamps assert self.n_time_bins >= 1, "Need >= 1 time bin" if hasattr(self, 'n_in'): raise RuntimeError( "n_in is gone! Use n_time_bins to control accuracy, or set " "pretend_wimps_dont_modulate to use a time-averaged spectrum.") times = np.linspace(wr.j2000(self.t_start.value), wr.j2000(self.t_stop.value), self.n_time_bins + 1) time_centers = self.bin_centers(times) if wimp_kwargs is None: # No arguments given at all; # use default mass, xsec and energy range wimp_kwargs = dict(mw=self.mw, sigma_nucleon=self.sigma_nucleon, es=self.es) else: assert 'mw' in wimp_kwargs and 'sigma_nucleon' in wimp_kwargs, \ "Pass at least 'mw' and 'sigma_nucleon' in wimp_kwargs" if 'es' not in wimp_kwargs: # Energies not given, use default energy bin edges wimp_kwargs['es'] = self.es es = wimp_kwargs['es'] es_centers = self.bin_centers(es) del wimp_kwargs['es'] # To avoid confusion centers / edges # Transform wimp_kwargs to arguments that can be passed to wimprates # which means transforming es from edges to centers spectra = np.array([ wr.rate_wimp_std(t=t, es=es_centers, **wimp_kwargs) * np.diff(es) for t in time_centers ]) assert spectra.shape == (len(time_centers), len(es_centers)) self.energy_hist = Histdd.from_histogram(spectra, bin_edges=(times, es)) if self.pretend_wimps_dont_modulate: self.energy_hist.histogram = ( np.ones_like(self.energy_hist.histogram) * self.energy_hist.sum(axis=0).histogram.reshape(1, -1) / self.n_time_bins) # Initialize the rest of the source, needs to be after energy_hist is # computed because of _populate_tensor_cache super().__init__(*args, **kwargs)
def add_extra_columns(self, d): super().add_extra_columns(d) # Add J2000 timestamps to data for use with wimprates if 't' not in d: d['t'] = [ wr.j2000(date=t) for t in pd.to_datetime(d['event_time']) ]
def test_j2000_conversion(): j2000_times = pd.np.linspace(0., 10000., 100) # convert j2000 -> event_time -> j2000 test_times = wr.j2000(fd.j2000_to_event_time(j2000_times)) pd.np.testing.assert_array_almost_equal(j2000_times, test_times, decimal=6)
def __init__(self, *args, wimp_kwargs=None, **kwargs): # Compute the energy spectrum in a given time range # Times used by wimprates are J2000 timestamps assert self.n_in > 1, \ f"Number of time bin edges needs to be at least 2" times = np.linspace(wr.j2000(date=self.t_start), wr.j2000(date=self.t_stop), self.n_in) time_centers = self.bin_centers(times) if wimp_kwargs is None: # No arguments given at all; # use default mass, xsec and energy range wimp_kwargs = dict(mw=self.mw, sigma_nucleon=self.sigma_nucleon, es=self.es) else: assert 'mw' in wimp_kwargs and 'sigma_nucleon' in wimp_kwargs, \ "Pass at least 'mw' and 'sigma_nucleon' in wimp_kwargs" if 'es' not in wimp_kwargs: # Energies not given, use default energy bin edges wimp_kwargs['es'] = self.es es = wimp_kwargs['es'] es_centers = self.bin_centers(es) del wimp_kwargs['es'] # To avoid confusion centers / edges # Transform wimp_kwargs to arguments that can be passed to wimprates # which means transforming es from edges to centers spectra = np.array([ wr.rate_wimp_std(t=t, es=es_centers, **wimp_kwargs) * np.diff(es) for t in time_centers ]) assert spectra.shape == (len(time_centers), len(es_centers)) self.energy_hist = Histdd.from_histogram(spectra, bin_edges=(times, es)) # Initialize the rest of the source, needs to be after energy_hist is # computed because of _populate_tensor_cache super().__init__(*args, **kwargs)
def test_j2000_ns_array(): # Generate some test cases and compare the two implementations years = np.arange(2008, 2020) months = np.arange(1, 12 + 1) days = np.arange(1, 12 + 1) hours = np.arange(1, 12 + 1) dates = np.zeros(12) j2000_ymd = np.zeros(12) for i in range(len(j2000_ymd)): print(years[i], months[i], days[i], hours[i]) date = pd.datetime(year=years[i], month=months[i], day=days[i], hour=hours[i]) dates[i] = pd.to_datetime(date).value # Compute according to j2000_from_ymd j2000_ymd[i] = j2000_from_ymd(years[i], months[i], days[i] + hours[i] / 24) np.testing.assert_array_almost_equal(j2000(dates), j2000_ymd, decimal=6)
def test_j2000_datetime(): date = pd.datetime(year=2009, month=1, day=31, hour=18) assert j2000(date=date) == 3318.25
def test_j2000(): assert j2000(2009, 1, 31.75) == 3318.25
def add_extra_columns(self, d): super().add_extra_columns(d) # Add J2000 timestamps to data for use with wimprates d['t_j2000'] = wr.j2000(d['event_time'])
def test_j2000_ns_int(): date = pd.datetime(year=2009, month=1, day=31, hour=18) value = pd.to_datetime(date).value assert isinstance(value, int) assert j2000(value) == 3318.25