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

Script to parse units and name #28

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ xarray = ">=0.20.2"
gsw = ">=3.4.0"
Sphinx = {version = ">=4.4.0", optional = true}
furo = {version = ">=2022.1.2", optional = true}
beautifulsoup4 = "^4.11.1"

[tool.poetry.dev-dependencies]
pytest = ">=6.2.5"
Expand Down
65 changes: 65 additions & 0 deletions utils/parse_info_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from urllib.request import urlopen
from bs4 import BeautifulSoup, NavigableString, Tag

import gsw
from types import FunctionType

from functools import reduce

url = "https://www.teos-10.org/pubs/gsw/html/gsw_{}.html"

repls = ("deg C", "degC"), ("unitless", "1"), ("degrees of rotation", "arcdeg")


def parse_info(func_name, url=url):
soup = BeautifulSoup(urlopen(url.format(func_name)), features="html.parser")
for header in soup.find_all("h2"):
if header.text != "INPUT:":
continue
nextNode = header
while True:
nextNode = nextNode.nextSibling
if nextNode is None:
break
if isinstance(nextNode, Tag):
if nextNode.name == "h2":
break
txt = nextNode.get_text(strip=True).strip()
args = [i for i in txt.split('\n') if '=' in i]
return [(i.split("=")[0].strip(), reduce(lambda a, kv: a.replace(*kv), repls, i.split("[")[1].split("]")[0].strip())) for i in args]
return []


def print_dict_attrs():
all_gsw_function = [
i
for i in dir(gsw)
if (isinstance(getattr(gsw, i), FunctionType) and not i.startswith("_"))
]
args_all = {}
for func in all_gsw_function[:]:
try:
args = parse_info(func)
except:
args = []
args_all[func] = args
print(args_all)
print('\n\n********************\n\n')
get_units_per_arg(args_all)

def get_units_per_arg(args_all):
units = {}
for f in args_all.keys():
args = args_all[f]
for a in args:
if a[0] == 'h':
print(f, a)
if a[0] in units.keys():
if a[1] not in units[a[0]]:
units[a[0]].append(a[1])
else:
units[a[0]] = [a[1]]
print(units)

if __name__ == '__main__':
print_dict_attrs()
52 changes: 52 additions & 0 deletions utils/parse_info_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from urllib.request import urlopen
from bs4 import BeautifulSoup, NavigableString, Tag

import gsw
from types import FunctionType

from functools import reduce

url = "https://www.teos-10.org/pubs/gsw/html/gsw_{}.html"

repls = ("deg C", "degC"), ("unitless", "1"), ("degrees of rotation", "arcdeg")


def parse_info(func_name, url=url):
soup = BeautifulSoup(urlopen(url.format(func_name)), features="html.parser")
for header in soup.find_all("h2"):
if header.text != "OUTPUT:":
continue
nextNode = header
while True:
nextNode = nextNode.nextSibling
if nextNode is None:
break
if isinstance(nextNode, Tag):
if nextNode.name == "h2":
break
txt = nextNode.get_text(strip=True).strip()
name = txt.split("=")[0].strip().split("_")[0]
unit = txt.split("[")[1].split("]")[0].strip()
return (name, reduce(lambda a, kv: a.replace(*kv), repls, unit))


def print_dict_attrs():
all_gsw_function = [
i
for i in dir(gsw)
if (isinstance(getattr(gsw, i), FunctionType) and not i.startswith("_"))
]
attrs = {}
names = {}
for func in all_gsw_function:
try:
name, unit = parse_info(func)
except:
name, unit = ("", "")
names[func] = name
attrs[func] = {"units": unit}
print(attrs)
print(names)

if __name__ == '__main__':
print_dict_attrs()