Skip to content

Commit

Permalink
use lsst.resource's as_local where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ehneilsen committed Jan 12, 2024
1 parent 7fbef0a commit 02f787d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 35 deletions.
14 changes: 7 additions & 7 deletions schedview/app/prenight/prenight.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import sys
import urllib.parse
from pathlib import Path

import astropy.utils.iers
Expand Down Expand Up @@ -545,7 +544,6 @@ def _update_rewards(self):
else:
self.logger.warning("Could not update obs_rewards.")

# @param.depends("rewards_fname", watch=True)
def _update_reward_df(self):
if self.rewards_fname is None or len(self.rewards_fname) < 1:
return None
Expand All @@ -558,8 +556,7 @@ def _update_reward_df(self):
raise FileNotFoundError(f"Resource not found: {self.rewards_fname}")

with reward_resource.as_local() as local_resource:
local_fname = Path(urllib.parse.urlparse(str(local_resource)).path)
reward_df = pd.read_hdf(local_fname, "reward_df")
reward_df = pd.read_hdf(local_resource.ospath, "reward_df")
self.logger.info("Finished updating reward dataframe.")
except Exception as e:
self.logger.error(e)
Expand Down Expand Up @@ -624,8 +621,7 @@ def _update_obs_rewards(self):
raise FileNotFoundError(f"Resource not found: {self.rewards_fname}")

with reward_resource.as_local() as local_resource:
local_fname = Path(urllib.parse.urlparse(str(local_resource)).path)
obs_rewards = pd.read_hdf(local_fname, "obs_rewards")
obs_rewards = pd.read_hdf(local_resource.ospath, "obs_rewards")

self._obs_rewards = obs_rewards
self.logger.info("Finished updating obs_rewards.")
Expand Down Expand Up @@ -946,12 +942,16 @@ def prenight_app(*args, **kwargs):
"""Create the pre-night briefing app."""

try:
# Let the user write URL for specific data files directely into
# the dashboard.
data_from_urls = kwargs["data_from_urls"]
del kwargs["data_from_urls"]
except KeyError:
data_from_urls = False

try:
# Provide a URI for an anchive that contains the data from
# which a user may choose.
data_from_archive = kwargs["data_from_archive"]
del kwargs["data_from_archive"]
except KeyError:
Expand Down Expand Up @@ -1023,7 +1023,7 @@ def parse_prenight_args():
"-d",
type=str,
default=DEFAULT_RESOURCE_URI,
help="The base URI for data files.",
help="The base URI for the archive containing the data.",
)

parser.add_argument(
Expand Down
15 changes: 2 additions & 13 deletions schedview/collect/opsim.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import sqlite3
from pathlib import Path
from tempfile import TemporaryDirectory

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -42,17 +40,8 @@ def read_opsim(opsim_uri, start_time="2000-01-01", end_time="2100-01-01"):
# otherwise, assume we were given the path to the observations file.
obs_path = original_resource_path

# ResourcePath.as_local runs into threading problems when used with
# bokeh/panel, so write the file to a temporary directory and read it
# "by hand" here.
opsimdb_bytes = obs_path.read()

with TemporaryDirectory() as temp_dir:
temp_file = Path(temp_dir).joinpath("opsim.db")
with open(temp_file, "wb") as opsimdb_io:
opsimdb_io.write(opsimdb_bytes)

with sqlite3.connect(temp_file) as sim_connection:
with obs_path.as_local() as local_obs_path:
with sqlite3.connect(local_obs_path.ospath) as sim_connection:
visits = pd.read_sql_query(
f"SELECT * FROM observations WHERE observationStartMJD BETWEEN {start_mjd} AND {end_mjd}",
sim_connection,
Expand Down
18 changes: 3 additions & 15 deletions schedview/collect/rewards.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from pathlib import Path
from tempfile import TemporaryDirectory

import numpy as np
import pandas as pd
import yaml
Expand Down Expand Up @@ -51,24 +48,15 @@ def read_rewards(rewards_uri, start_time="2000-01-01", end_time="2100-01-01"):
# otherwise, assume we were given the path to the observations file.
rewards_path = original_resource_path

# ResourcePath.as_local runs into threading problems when used with
# bokeh/panel, so write the file to a temporary directory and read it
# "by hand" here.
rewards_bytes = rewards_path.read()

with TemporaryDirectory() as temp_dir:
temp_file = Path(temp_dir).joinpath("rewards.h5")
with open(temp_file, "wb") as rewards_io:
rewards_io.write(rewards_bytes)

with rewards_path.as_local() as local_rewards_path:
try:
rewards_df = pd.read_hdf(temp_file, key="reward_df")
rewards_df = pd.read_hdf(local_rewards_path.ospath, key="reward_df")
rewards_df.query(f"{start_mjd} <= queue_start_mjd <= {end_mjd}", inplace=True)
except KeyError:
rewards_df = None

try:
obs_rewards = pd.read_hdf(temp_file, key="obs_rewards")
obs_rewards = pd.read_hdf(local_rewards_path.ospath, key="obs_rewards")
obs_rewards = obs_rewards.loc[start_mjd:end_mjd]
except KeyError:
obs_rewards = None
Expand Down

0 comments on commit 02f787d

Please sign in to comment.