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

List Selected Stations with Longitude / Latitude #36

Merged
merged 6 commits into from
Dec 13, 2021

Conversation

yasuit21
Copy link
Contributor

Background

  • A function to return stations with Lon/Lat is helpful to plot them in a map.
  • Client.get_seleced_stations() only returns the number of selected stations, not the station info.
  • Here, add Client.list_selected_stations() to show selected stations with Lon/Lat data!

Feature

  • Client.list_selected_stations() returns a dict containing stations as keys.
  • For each value, a dict with 'longitude' and 'latitude' as keys.
  • len() returns the number of selected stations, the same feature to Client.get_seleced_stations().

Example

>>> client = Client("username", "password")
>>> stations = client.list_selected_stations(code='0101', parser='lxml')
>>> stations
{'N.TRGH': {'longitude': 136.0303, 'latitude': 35.639},
 'N.MIKH': {'longitude': 135.8694, 'latitude': 35.5587},
 ...,
 'N.NAKH': {'longitude': 132.9524, 'latitude': 33.0754}}

>>> len(stations)
23
>>> [*stations.keys()]
['N.TRGH',
 'N.MIKH',
  ...,
 'N.NAKH']

>>> print(*stations['N.MIKH'].values())
135.8694 35.5587

Note

  • Need to install beautifulsoup4 to run this function (I did not touch setup.py and requirements.txt)
  • Can run faster if the parser lxml is installed, as the default parser. If not, html.parser is used in BeautifulSoup.
  • This func can be renamed to get_seleced_stations()?

add list_selected_stations() to get the list of selected stations
list_selected_stations() returns stations with Lon/Lat.
try/except for `parser`.
@seisman
Copy link
Owner

seisman commented Aug 18, 2020

@yasuit21 Thanks for your contribution! It looks like a useful feature.

I think the feature should be merged into get_selected_stations function, but I'm a little hesitate to add one more dependency (beautifulsoup4), that's why I use regrex in the codes.

Used the standard lib `html.parser` instead of beautifulsoup4.
@yasuit21
Copy link
Contributor Author

Thanks. I have changed as below:

  • Used the standard lib html.parser instead of beautifulsoup4.
  • New class _GrepTableData() in this function, which is inherited from html.parser.HTMLParser
  • _GrepTableData() searchs all tags for station names
  • Removed the parser argument.

No dependency is added. Would you check?

@seisman
Copy link
Owner

seisman commented Aug 19, 2020

@yasuit21 Your changes work well. The CI jobs fail because your fork can't access some secret settings in this repository. Don't worry. I'll run the tests locally.

I have two more suggestions:

  1. As you already suggested, it's a good idea to rename your function to get_selected_stations(). The old function returns the number of the selected stations, and your new function returns a list/dict of the selected stations. It's a great improvement, although it's backward incompatible.
  2. Your function returns a list/dict of selected stations. Actually, HinetPy has a class for Station:

    HinetPy/HinetPy/client.py

    Lines 1341 to 1357 in 6cbf4cc

    class Station(object):
    """
    Class for Stations.
    """
    def __init__(self, code, name, latitude, longitude, elevation):
    self.code = code
    self.name = name
    self.latitude = float(latitude)
    self.longitude = float(longitude)
    self.elevation = float(elevation)
    def __str__(self):
    string = "{} {} {} {} {}".format(
    self.code, self.name, self.latitude, self.longitude, self.elevation
    )
    return string

    Perhaps your function can return a list of Station?

Rename and change of `get_selected_stations()`

1. Rename `get_selected_stations()` to `_get_selected_stations()` and `list_selected_stations()` to `get_selected_stations()`.

2. `get_selected_stations()` returns a list of `Station` and `print()` shows the number of selected stations.
@yasuit21
Copy link
Contributor Author

@seisman Thank you for suggestions.

  1. Renamed the new function to get_selected_stations() and old one to _get_selected_stations(). Also renamed that used in _get_allowed_span().

  2. Returns a list of Station instances instead of a dict. I have added examples to call. It may be more useful if print() returns station info, like an Inventory class of ObsPy.

  3. Put _GrepTableData outside the Client class (line 1466-). I will return it to the original position if any problems.

@seisman seisman merged commit 3a8905f into seisman:master Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants