Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ch_util): Import objects directly from caput.time #75

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions ch_util/andata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
import h5py
from bitshuffle import h5

tmp = h5 # To appease linters who complain about unused imports.
from caput import memh5, tod
import caput.time as ctime

# If the `caput` package is available, get `memh5` from there. Otherwise, use
# the version of memh5 that ships with `ch_util`, eliminating the dependency.
try:
from caput import memh5, tod
except ImportError:
raise ImportError("Could not import memh5 or tod. Have you installed caput?")
tmp = h5 # To appease linters who complain about unused imports.


ni_msg = "Ask Kiyo to implement this."
Expand Down Expand Up @@ -326,12 +322,7 @@ def timestamp(self):

@staticmethod
def convert_time(time):
try:
from .ephemeris import ensure_unix
except ValueError:
from .ephemeris import ensure_unix

return ensure_unix(time)
return ctime.ensure_unix(time)


class CorrData(BaseData):
Expand Down Expand Up @@ -1223,8 +1214,6 @@ def from_acq_h5(
data : HKPData
"""

from caput import time as ctime

metrics = metrics if metrics is not None else datasets

if "mode" not in kwargs:
Expand Down
2 changes: 1 addition & 1 deletion ch_util/cal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2682,7 +2682,7 @@ def get_reference_times_dataset_id(
logger = logging.getLogger(__name__)

# Dataset IDs before this date are untrustworthy
ds_start = ephemeris.datetime_to_unix(datetime(2020, 11, 1))
ds_start = ctime.datetime_to_unix(datetime(2020, 11, 1))
if (times < ds_start).any():
raise ValueError(
"Dataset IDs before 2020/11/01 are corrupt, so this method won't work. "
Expand Down
15 changes: 9 additions & 6 deletions ch_util/chan_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import numpy as np
import copy

import caput.time as ctime

from chimedb import data_index
from ch_util import ephemeris, finder

Expand All @@ -14,7 +17,7 @@
) # Cylinder rotation matrix
C = 2.9979e8
PHI = ephemeris.CHIMELATITUDE * np.pi / 180.0 # DRAO Latitue
SD = 24.0 * 3600.0 * ephemeris.SIDEREAL_S # Sidereal day
SD = 24.0 * 3600.0 * ctime.SIDEREAL_S # Sidereal day

_DEFAULT_NODE_SPOOF = {"scinet_online": "/scratch/k/krs/jrs65/chime/archive/online/"}
# _DEFAULT_NODE_SPOOF = {'gong': '/mnt/gong/archive'} # For tests on Marimba
Expand Down Expand Up @@ -739,7 +742,7 @@ def fromdate(
bsep2=218,
):
"""Initialize class from date"""
t1 = ephemeris.datetime_to_unix(date)
t1 = ctime.datetime_to_unix(date)
return cls(
t1,
freq_sel=freq_sel,
Expand Down Expand Up @@ -933,7 +936,7 @@ def set_metadata(self, tms, input_map):
from ch_util import tools

# Get CHIME ON channels:
half_time = ephemeris.unix_to_datetime(tms[int(len(tms) // 2)])
half_time = ctime.unix_to_datetime(tms[int(len(tms) // 2)])
corr_inputs = tools.get_correlator_inputs(half_time)
self.corr_inputs = tools.reorder_correlator_inputs(input_map, corr_inputs)
pwds = tools.is_chime_on(self.corr_inputs) # Which inputs are CHIME ON antennas
Expand All @@ -954,7 +957,7 @@ def get_prod_sel(self, data):

input_map = data.input
tms = data.time
half_time = ephemeris.unix_to_datetime(tms[int(len(tms) // 2)])
half_time = ctime.unix_to_datetime(tms[int(len(tms) // 2)])
corr_inputs = tools.get_correlator_inputs(half_time)
corr_inputs = tools.reorder_correlator_inputs(input_map, corr_inputs)
pwds = tools.is_chime_on(corr_inputs) # Which inputs are CHIME ON antennas
Expand Down Expand Up @@ -1099,8 +1102,8 @@ def set_acq_list(self):
self.night_acq_list = f_night.get_results()

# Create a list of acquisitions that flag out sunrise, sun transit, and sunset
mm = ephemeris.unix_to_datetime(self.t1).month
dd = ephemeris.unix_to_datetime(self.t1).day
mm = ctime.unix_to_datetime(self.t1).month
dd = ctime.unix_to_datetime(self.t1).day
mm = mm + float(dd) / 30.0

fct = 3.0
Expand Down
10 changes: 6 additions & 4 deletions ch_util/data_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import numpy as np

import caput.time as ctime

import ch_util.ephemeris as ch_eph
from ch_util import andata
from ch_util import tools
Expand Down Expand Up @@ -364,7 +366,7 @@ def _cut_non_chime(data, visi, chan_array, inputs=None):
input_map = data.input
tmstp = data.index_map["time"]["ctime"] # time stamp
# Datetime halfway through data:
half_time = ch_eph.unix_to_datetime(tmstp[int(len(tmstp) // 2)])
half_time = ctime.unix_to_datetime(tmstp[int(len(tmstp) // 2)])
# Get information on correlator inputs, if not already supplied
if inputs is None:
inputs = tools.get_correlator_inputs(half_time)
Expand Down Expand Up @@ -899,7 +901,7 @@ def _create_plot(

# For title, use start time stamp:
title = "Good channels result for {0}".format(
ch_eph.unix_to_datetime(tmstp1[0]).date()
ctime.unix_to_datetime(tmstp1[0]).date()
)

# I need to know the slot for each channel:
Expand Down Expand Up @@ -985,14 +987,14 @@ def get_slot(channel):
if time_unit == "days":
plt.xlabel(
"Time (days since {0} UTC)".format(
ch_eph.unix_to_datetime(tmstp1[0])
ctime.unix_to_datetime(tmstp1[0])
),
fontsize=10,
)
else:
plt.xlabel(
"Time (hours since {0} UTC)".format(
ch_eph.unix_to_datetime(tmstp1[0])
ctime.unix_to_datetime(tmstp1[0])
),
fontsize=10,
)
Expand Down
31 changes: 14 additions & 17 deletions ch_util/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import peewee as pw
import re

import caput.time as ctime

import chimedb.core as db
import chimedb.data_index as di
from . import layout, ephemeris
Expand Down Expand Up @@ -664,8 +666,8 @@ def set_time_range(self, start_time=None, end_time=None):
start_time = 0
if end_time is None:
end_time = time.time()
start_time = ephemeris.ensure_unix(start_time)
end_time = ephemeris.ensure_unix(end_time)
start_time = ctime.ensure_unix(start_time)
end_time = ctime.ensure_unix(end_time)
old_start_time, old_end_time = self.time_range
start_time = max(start_time, old_start_time)
end_time = min(end_time, old_end_time)
Expand Down Expand Up @@ -733,8 +735,8 @@ def _format_time_interval(self, start_time, end_time):
start_time = 0
if end_time is None:
end_time = time.time()
start_time = ephemeris.ensure_unix(start_time)
end_time = ephemeris.ensure_unix(end_time)
start_time = ctime.ensure_unix(start_time)
end_time = ctime.ensure_unix(end_time)
range_start, range_end = self.time_range
start_time = max(start_time, range_start)
end_time = min(end_time, range_end)
Expand Down Expand Up @@ -915,11 +917,9 @@ def include_RA_interval(self, start_RA, end_RA):
Total 242094.394565 seconds of data.
"""

from . import ephemeris

delta_RA = (end_RA - start_RA) % 360
mid_RA = (start_RA + delta_RA / 2.0) % 360
time_delta = delta_RA * 4 * 60.0 * ephemeris.SIDEREAL_S
time_delta = delta_RA * 4 * 60.0 * ctime.SIDEREAL_S
self.include_transits(mid_RA, time_delta=time_delta)

def exclude_RA_interval(self, start_RA, end_RA):
Expand All @@ -938,11 +938,10 @@ def exclude_RA_interval(self, start_RA, end_RA):
Look under include_RA_interval for very similar example.

"""
from . import ephemeris

delta_RA = (end_RA - start_RA) % 360
mid_RA = (start_RA + delta_RA / 2.0) % 360
time_delta = delta_RA * 4 * 60.0 * ephemeris.SIDEREAL_S
time_delta = delta_RA * 4 * 60.0 * ctime.SIDEREAL_S
self.exclude_transits(mid_RA, time_delta=time_delta)

def include_transits(self, body, time_delta=None):
Expand Down Expand Up @@ -996,7 +995,7 @@ def include_26m_obs(self, source, require_quality=True):

Examples
--------
>>> from ch_util import (finder, ephemeris)
>>> from ch_util import finder
>>> from datetime import datetime
>>> f = finder.Finder()
>>> f.only_corr()
Expand Down Expand Up @@ -1204,8 +1203,8 @@ def get_results_acq(self, acq_ind, file_condition=None):
start, stop = layout.get_global_flag_times(f.id)
if stop is None:
stop = time.time()
start = ephemeris.ensure_unix(start)
stop = ephemeris.ensure_unix(stop)
start = ctime.ensure_unix(start)
stop = ctime.ensure_unix(stop)
flag_times.append((start, stop))
overlap = _check_intervals_overlap(time_intervals, flag_times)
if mode is GF_WARN:
Expand Down Expand Up @@ -1246,8 +1245,8 @@ def get_results_acq(self, acq_ind, file_condition=None):
start, stop = f.start_time, f.finish_time
if stop is None:
stop = time.time()
start = ephemeris.ensure_unix(start)
stop = ephemeris.ensure_unix(stop)
start = ctime.ensure_unix(start)
stop = ctime.ensure_unix(stop)
flag_times.append((start, stop))
overlap = _check_intervals_overlap(time_intervals, flag_times)
if overlap:
Expand Down Expand Up @@ -1326,15 +1325,13 @@ def print_acq_info(self):

"""

from . import ephemeris

print("acquisition | name | start | length (hrs) | N files")
row_proto = "%4d | %-36s | %s | %7.2f | %4d"
for ii, acq in enumerate(self.acqs):
start = acq.start_time
finish = acq.finish_time
length = (finish - start) / 3600.0
start = ephemeris.unix_to_datetime(start)
start = ctime.unix_to_datetime(start)
start = start.strftime("%Y-%m-%d %H:%M")
name = acq.name
n_files = acq.n_timed_files
Expand Down
4 changes: 3 additions & 1 deletion ch_util/fluxcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import time

from caput import misc
import caput.time as ctime

from . import ephemeris
from .tools import ensure_list

Expand Down Expand Up @@ -843,7 +845,7 @@ def skyfield(self):
"""Skyfield star representation :class:`skyfield.starlib.Star`
for the source.
"""
return ephemeris.skyfield_star_from_ra_dec(self.ra, self.dec, self.name)
return ctime.skyfield_star_from_ra_dec(self.ra, self.dec, self.name)

@property
def freq(self):
Expand Down
Loading
Loading