Skip to content

Commit

Permalink
updated API and docs; fix duration
Browse files Browse the repository at this point in the history
  • Loading branch information
dlakaplan committed Aug 12, 2022
1 parent b18d94b commit 9157011
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ Use cases:

Requires an API key for posting, either through environment variable `$PULSAR_API_KEY` or `key=...` in `post()` methods

Querying (through RESTful API or python):
* `pulsar=<name>`: query a pulsar by exact name (but this will also query by aliases)
* `pulsar_contains=<name>`: query a pulsar by partial match (will not also query aliases)
* `project=<name>`: query by one or more (logical OR)
* `min_time=<time>`: start time (`astropy` `Time`, float (MJD), or string that `astropy` can parse)
* `max_time=<time>`: end time (`astropy` `Time`, float (MJD), or string that `astropy` can parse)
* `min_frequency=<frequency>`: (`astropy` `Quantity` or float (MHz))
* `max_frequency=<frequency>`: (`astropy` `Quantity` or float (MHz))
* `backend=<name>`: query by one or more (logical OR)
* `receiver=<name>`: query by one or more (logical OR)
* `telescope=<name>`: query a telescope by exact name (but this will also query by aliases)
* `telescopes=<name>`: query a telescope by one or more exact names (logical OR, will not also query aliases)
* `sort=<key>`: can be `frequency`, `pulsar`, `telescope`, `project`, `submitter`, `datetime_range__startswith`, `datetime_range__endswith`. Start key with `-` to do descending

Examples:

Post an observation:
Expand Down
39 changes: 23 additions & 16 deletions pulsardb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def get(self, pulsar=None, format="json"):
if format.lower() == "json":
return response.json()
elif format.lower() == "pandas":
return pd.json_normalize(response.json()["results"])
return pd.json_normalize(response.json())
elif format.lower() == "table":
return Table.from_pandas(pd.json_normalize(response.json()["results"]))
return Table.from_pandas(pd.json_normalize(response.json()))
else:
logger.warning(
f"Attempt to retrieve Pulsars from database did not succeed (code={response.status_code})"
Expand Down Expand Up @@ -182,9 +182,9 @@ def get(self, format="json"):
if format.lower() == "json":
return response.json()
elif format.lower() == "pandas":
return pd.json_normalize(response.json()["results"])
return pd.json_normalize(response.json())
elif format.lower() == "table":
return Table.from_pandas(pd.json_normalize(response.json()["results"]))
return Table.from_pandas(pd.json_normalize(response.json()))
else:
logger.warning(
f"Attempt to retrieve Telescopes from database did not succeed (code={response.status_code})"
Expand All @@ -204,6 +204,7 @@ class Observations:
def get(
self,
pulsar=None,
pulsar_contains=None,
telescope=None,
receiver=None,
backend=None,
Expand All @@ -219,15 +220,19 @@ def get(
Parameters
----------
pulsar : str, optional
Pulsar name to match
pulsar : str or list, optional
Pulsar name to match (exact, aliases allowed)
pulsar_contains : str, optional
Pulsar name to match (case insensitive, partial match)
telescope : str, optional
Telescope name to match
receiver : str, optional
Telescope name to match (exact, aliases allowed)
telescopes : str or list, optional
Telescope name to match (exact, multiple values possible)
receiver : str or list, optional
Receiver name to match
backend : str, optional
backend : str or list, optional
Backend name to match
project : str, optional
project : str or list, optional
Project name to match
min_time : `astropy.time.Time`, float, str, optional
Minimum start time of observation (`float` is assumed to be MJD)
Expand All @@ -251,6 +256,8 @@ def get(
data = {}
if pulsar is not None:
data["pulsar"] = pulsar
if pulsar_contains is not None:
data["pulsar__icontains"] = pulsar_contains
if telescope is not None:
data["telescope"] = telescope
if receiver is not None:
Expand All @@ -271,23 +278,23 @@ def get(
data["max_MJD"] = max_time
if min_frequency is not None:
if isinstance(min_frequency, u.quantity.Quantity):
data["min_frequency"] = min_frequency.to_value(u.MHz)
data["frequency__after"] = min_frequency.to_value(u.MHz)
else:
data["min_frequency"] = min_frequency
data["frequency__after"] = min_frequency
if max_frequency is not None:
if isinstance(max_frequency, u.quantity.Quantity):
data["max_frequency"] = max_frequency.to_value(u.MHz)
data["frequency__before"] = max_frequency.to_value(u.MHz)
else:
data["max_frequency"] = max_frequency
data["frequency__before"] = max_frequency

response = requests.get(_url + self.endpoint, params=data, headers=_json_header)
if response.status_code == 200:
if format.lower() == "json":
return response.json()
elif format.lower() == "pandas":
return pd.json_normalize(response.json()["results"])
return pd.json_normalize(response.json())
elif format.lower() == "table":
return Table.from_pandas(pd.json_normalize(response.json()["results"]))
return Table.from_pandas(pd.json_normalize(response.json()))
else:
logger.warning(
f"Attempt to retrieve Observations from database did not succeed (code={response.status_code})"
Expand Down
3 changes: 2 additions & 1 deletion pulsardb/fromtim.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def fromtim(
)[0]
)
* u.s
).max()
).sum()
# this may not be exact
stop = start + duration
# assume they all have the same values of these (check?)
receiver = toas[select].get_flag_value(receiverflag, fill_value=receiver)[0][0]
Expand Down

0 comments on commit 9157011

Please sign in to comment.