Skip to content
Open
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
22 changes: 22 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

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

12 changes: 12 additions & 0 deletions .idea/ios-shell.iml

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

7 changes: 7 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

82 changes: 82 additions & 0 deletions .idea/workspace.xml

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

55 changes: 54 additions & 1 deletion ios_shell/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def get_history(contents: List[str]) -> Tuple[sections.History, List[str]]:
"""Parse the \\*HISTORY section"""
history_dict, rest = get_section(contents, "history")
programs = (
[sections.Program(*elem) for elem in history_dict[PROGRAMS]]
[sections.Program(*elem.values()) for elem in history_dict[PROGRAMS]]
if PROGRAMS in history_dict
else []
)
Expand Down Expand Up @@ -413,13 +413,66 @@ def _postprocess_line(line: List[Any]) -> List[Any]:
return [_process_item(item) for item in line]



def handle_DMH_in_Data(lines: str, format: str):
fmt = format.strip("()")
fmt_list = [f.strip() for f in fmt.split(",")]

# Use DMH position from the format list to remove character before and after each line
dmh_indices = [i for i, f in enumerate(fmt_list) if f == "DMH"]
first_dmh = dmh_indices[0]
last_dmh = dmh_indices[-1]

width_before = sum(int(re.search(r"\d+", f).group()) if re.search(r"\d+", f) else 0
for f in fmt_list[:first_dmh])
width_after = sum(int(re.search(r"\d+", f).group()) if re.search(r"\d+", f) else 0
for f in fmt_list[last_dmh + 1:])

new_lines = []
for line in lines:
dmh_block = line[width_before: len(line) - width_after]
pattern = r"(\d+)\s+([\d.]+)\s*([NSEW])"
matches = re.findall(pattern, dmh_block)

if len(matches) != 2:
new_lines.append(line) # keep line unchanged if DMH parsing fails
continue

lat = lon = None
for deg, minute, hemi in matches:
value = float(deg) + float(minute) / 60.0
if hemi in ("S", "W"):
value = -value

if hemi in ("N", "S"):
lat = value
else:
lon = value

new_line = (
line[:width_before]
+ f"{lat:11.4f}{lon:11.4f}"
+ line[len(line) - width_after:])

new_lines.append(new_line)

format = format.replace("DMH", "F11.4")

return new_lines, format



def get_data(contents: str, format: str, records: int) -> Tuple[List[List[Any]], str]:
"""Process the data in the file"""
lines = contents.splitlines()
while "" in lines:
lines.remove("")
if len(lines) < records:
raise ValueError(f"Insufficient data for requested number of records")

if "DMH" in format:
lines, format = handle_DMH_in_Data(lines, format)

reader = ff.FortranRecordReader(format)
data = [_postprocess_line(reader.read(line)) for line in lines[:records]]
rest = "\n".join(lines[records:]) # pragma: no mutate
Expand Down
8 changes: 6 additions & 2 deletions ios_shell/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ def __init__(
minimum = "0"
if maximum.strip().upper() == "O":
maximum = "0"
self.minimum = float(minimum) if minimum.strip() not in EMPTY else NAN
self.maximum = float(maximum) if maximum.strip() not in EMPTY else NAN

#self.minimum = float(minimum) if minimum.strip() not in EMPTY else NAN
#self.maximum = float(maximum) if maximum.strip() not in EMPTY else NAN

self.minimum = (NAN if minimum.strip() in EMPTY or "/" in minimum.strip() or ":" in minimum.strip() else float(minimum))
self.maximum = (NAN if maximum.strip() in EMPTY or "/" in maximum.strip() or ":" in maximum.strip() else float(maximum))


class ChannelDetail:
Expand Down
11 changes: 9 additions & 2 deletions ios_shell/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ class ShellFile:
@classmethod
def fromfile(cls, filename, process_data=True): # pragma: no mutate
"""Construct a ShellFile object from the contents of a file."""
with open(filename, "r", encoding="ASCII", errors="ignore") as f:
contents = f.read()
# with open(filename, "r", encoding="ASCII", errors="ignore") as f:
# contents = f.read()
try:
with open(filename, "r", encoding="utf-8") as f:
contents = f.read()
except UnicodeDecodeError:
with open(filename, "r", encoding="ANSI") as f:
contents = f.read()

try:
return ShellFile.fromcontents(contents, process_data, filename=filename)
except ValueError as e:
Expand Down
6 changes: 6 additions & 0 deletions ios_shell/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def format_string(format: str, kind: str, width: int, decimals: int) -> str:
elif fortrantype in ["I"]:
return f"I{width}"
elif fortrantype.upper() in [
"DD/MM/YYYY",
"YYYY/MM/DD",
"YYYY-MM-DD",
"HH:MM",
Expand All @@ -50,6 +51,11 @@ def format_string(format: str, kind: str, width: int, decimals: int) -> str:
return f"A{len(fortrantype)+1}"
elif fortrantype in ["' '", "NQ"]:
return f"A{width}"
elif fortrantype in ["D"]:
if datatype in ["I"]:
return f"I{width}"
else:
return f"D{width}.{decimals}"
else:
return fortrantype

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ include = [
python = ">=3.8,<3.11"
fortranformat = "^1.0.1"
pandas = {version = "^1.4.2", optional = true}
numpy = "<2.0"



[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
Expand Down
Loading