Skip to content

Commit

Permalink
Merge pull request #4 from radionets-project/update_layouts
Browse files Browse the repository at this point in the history
Update Layout class
  • Loading branch information
aknierim authored Sep 30, 2024
2 parents 3dbb4ff + b4d3f6d commit 1e18673
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 59 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,5 @@ You can install the necessary packages in a conda/mamba environment of your choi
```
$ pip install -e .
```
Note that it is recommended to install [radionets](https://github.com/radionets-project/radionets)
and [pyvisgen](https://github.com/radionets-project/pyvisgen) first. The latter must be the `gpu` branch version and can be installed
via
```
$ pip install https://github.com/radionets-project/pyvisgen/archive/gpu.zip
```

If you want to use features from the NRAO [CASAtools](https://pypi.org/project/casatools/) package, you must use a python 3.10 version.
If you want to use features from the NRAO [CASAtools](https://pypi.org/project/casatools/) package,
you must use a python 3.10 version.
9 changes: 3 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ dependencies = [
"astropy >=6.1.0",
"matplotlib ~=3.0",
"numpy ~=1.16",
"pandas",
"pyvisgen",
"rich",
"casatools",
"pandas >=2.0",
"rich >=13.0",
"casatools ~=6.6",
]

[project.optional-dependencies]
Expand Down Expand Up @@ -82,7 +81,5 @@ skip-magic-trailing-comma = false

[tool.ruff.lint.isort]
known-first-party = [
"pyvisgen",
"radiotools",
"radionets"
]
10 changes: 0 additions & 10 deletions radiotools/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@

__all__ = ["main"]

_dependencies = sorted(
[
"astropy >=6.1.0",
"matplotlib ~=3.0",
"numpy ~=1.16",
"pyvisgen",
"matplotlib",
]
)


@click.command()
@click.option(
Expand Down
111 changes: 79 additions & 32 deletions radiotools/layouts/layouts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import urllib
import uuid
from pathlib import Path

Expand Down Expand Up @@ -485,11 +486,11 @@ def from_casa(
cls = cls()
cls.cfg_path = cfg_path
cls.rel_to_site = rel_to_site
cls.x = df.iloc[:, 0].to_list()
cls.y = df.iloc[:, 1].to_list()
cls.z = df.iloc[:, 2].to_list()
cls.dish_dia = df.iloc[:, 3].to_list()
cls.names = df.iloc[:, 4].to_list()
cls.x = df["x"]
cls.y = df["y"]
cls.z = df["z"]
cls.dish_dia = df["dish_dia"]
cls.names = df["station_name"]
cls.el_low = np.repeat(el_low, len(cls.x)) if np.isscalar(el_low) else el_low
cls.el_high = (
np.repeat(el_high, len(cls.x)) if np.isscalar(el_high) else el_high
Expand All @@ -499,11 +500,6 @@ def from_casa(
np.repeat(altitude, len(cls.x)) if np.isscalar(altitude) else altitude
)

df.insert(5, "el_low", cls.el_low)
df.insert(6, "el_high", cls.el_high)
df.insert(7, "sefd", cls.sefd)
df.insert(8, "altitude", cls.altitude)

return cls

@classmethod
Expand Down Expand Up @@ -539,19 +535,70 @@ def from_pyvisgen(cls, cfg_path, rel_to_site=None):
"altitude": float,
},
)
df = df.rename(columns={"X": "x", "Y": "y", "Z": "z"})
df.columns = map(str.lower, df.columns)

cls = cls()
cls.names = df.iloc[:, 0].to_list()
cls.names = df["station_name"]
cls.cfg_path = cfg_path
cls.rel_to_site = rel_to_site
cls.x = df.iloc[:, 1].to_list()
cls.y = df.iloc[:, 2].to_list()
cls.z = df.iloc[:, 3].to_list()
cls.dish_dia = df.iloc[:, 4].to_list()
cls.el_low = df.iloc[:, 5].to_list()
cls.el_high = df.iloc[:, 6].to_list()
cls.sefd = df.iloc[:, 7].to_list()
cls.altitude = df.iloc[:, 8].to_list()
cls.x = df["x"]
cls.y = df["y"]
cls.z = df["z"]
cls.dish_dia = df["dish_dia"]
cls.el_low = df["el_low"]
cls.el_high = df["el_high"]
cls.sefd = df["sefd"]
cls.altitude = df["altitude"]

return cls

@classmethod
def from_url(cls, url: str, rel_to_site=None) -> "Layout":
"""Import a layout from a given URL.
Parameters
----------
url : str
URL of the layout file.
rel_to_site : str, optional
The name of the site the coordinates are relative to.
Is ignored is `None` or empty or `fmt`. Has to be an
existing site for `astropy.coordinates.EarthLocation.of_site()`.
Default: None
"""
data = []
for line in urllib.request.urlopen(url):
data.append(line.decode("utf-8").split())

df = pd.DataFrame(data)
df = df.rename(columns=df.iloc[0]).drop(df.index[0])
df.columns = map(str.lower, df.columns)
df = df.astype(
{
"station_name": str,
"x": float,
"y": float,
"z": float,
"dish_dia": float,
"el_low": float,
"el_high": float,
"sefd": float,
"altitude": float,
}
)

cls = cls()
cls.names = df["station_name"]
cls.cfg_path = url
cls.rel_to_site = rel_to_site
cls.x = df["x"]
cls.y = df["y"]
cls.z = df["z"]
cls.dish_dia = df["dish_dia"]
cls.el_low = df["el_low"]
cls.el_high = df["el_high"]
cls.sefd = df["sefd"]
cls.altitude = df["altitude"]

return cls

Expand All @@ -567,22 +614,22 @@ def loc2itrf(cx, cy, cz, locx=0.0, locy=0.0, locz=0.0):
Parameters
----------
locx: array_like or float
The x-coordinate in relative coordinates
The x-coordinate in relative coordinates
locy: array_like or float
The y-coordinate in relative coordinates
The y-coordinate in relative coordinates
locz: array_like or float
The z-coordinate in relative coordinates
The z-coordinate in relative coordinates
cx: float
The center's x-coordinate in WGS84 coordinates
The center's x-coordinate in WGS84 coordinates
cy: float
The center's y-coordinate in WGS84 coordinates
The center's y-coordinate in WGS84 coordinates
cz: float
The center's z-coordinate in WGS84 coordinates
The center's z-coordinate in WGS84 coordinates
"""

Expand Down Expand Up @@ -620,22 +667,22 @@ def itrf2loc(x, y, z, cx, cy, cz):
Parameters
----------
x: array_like or float
The x-coordinate in WGS84 coordinates
The x-coordinate in WGS84 coordinates
y: array_like or float
The y-coordinate in WGS84 coordinates
The y-coordinate in WGS84 coordinates
z: array_like or float
The z-coordinate in WGS84 coordinates
The z-coordinate in WGS84 coordinates
cx: float
The center's x-coordinate in WGS84 coordinates
The center's x-coordinate in WGS84 coordinates
cy: float
The center's y-coordinate in WGS84 coordinates
The center's y-coordinate in WGS84 coordinates
cz: float
The center's z-coordinate in WGS84 coordinates
The center's z-coordinate in WGS84 coordinates
"""

Expand Down
3 changes: 3 additions & 0 deletions radiotools/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .utils import get_array_names

__all__ = ["get_array_names"]
32 changes: 32 additions & 0 deletions radiotools/utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import requests
from bs4 import BeautifulSoup


def get_array_names(url: str) -> list[str]:
"""Fetches array names from a given URL
leading to a directory.
Parameters
----------
url : str
URL leading to a directory containing layout
txt files.
Returns
-------
layouts : list[str]
List of available layouts.
"""
r = requests.get(url)
soup = BeautifulSoup(r.text, features="html.parser")

a_tags = soup.find_all("a")

layouts = []
for i in a_tags:
if ".txt" in str(i.get("href")):
layouts.append(i.get("aria-label").split(".txt")[0])

layouts = list(set(layouts))

return layouts
10 changes: 7 additions & 3 deletions radiotools/visibility/visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from rich.console import Console
from rich.table import Table

from pyvisgen.layouts import layouts
from radiotools.layouts import Layout
from radiotools.utils import get_array_names

COLORS = [
"#fd7f6f",
Expand All @@ -29,6 +30,9 @@
"#b3d4ff",
]

PYVISGEN = "https://raw.githubusercontent.com/radionets-project/pyvisgen/"
PYVISGEN += "refs/heads/main/pyvisgen/layouts/"


class SourceVisibility:
"""Plots the source visibility for a given location
Expand Down Expand Up @@ -79,9 +83,9 @@ def __init__(
"Please either provide a valid target name or a RA/Dec tuple!"
)

if isinstance(location, str) and location in layouts.get_array_names():
if isinstance(location, str) and location in get_array_names(PYVISGEN):
self.name = location
self.array = layouts.get_array_layout(location)
self.array = Layout.from_url(PYVISGEN + location)

self.location = EarthLocation.from_geocentric(
self.array.x * u.m, self.array.y * u.m, self.array.z * u.m
Expand Down

0 comments on commit 1e18673

Please sign in to comment.