Skip to content

Commit

Permalink
r.topidx: Rewrite Perl scripts in Python 3 (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuidaeCho committed Feb 18, 2021
1 parent 847219c commit c402ca5
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 74 deletions.
45 changes: 0 additions & 45 deletions raster/r.topidx/arc.to.gridatb.pl

This file was deleted.

80 changes: 80 additions & 0 deletions raster/r.topidx/arc_to_gridatb.py
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
import sys
import re
import os


def match(pattern, string):
m = re.match(pattern, string, re.IGNORECASE)
if m:
match.value = m.group(1)
return True
else:
match.value = None
return False


if len(sys.argv) != 3 or re.match("^-*help", sys.argv[1]):
print("Usage: arc.to.gridatb.py arc_file gridatb_file")
exit()

infname = sys.argv[1]
outfname = sys.argv[2]

if not os.path.isfile(infname):
print(f"{infname}: File not found")
exit()

if os.path.isfile(outfname):
print(f"{outfname}: File already exists")
exit()

inf = open(infname)

head = 0
for inline in inf:
if match("^ncols[ \t]+([0-9.]+)[ \t]*$", inline):
ncols = match.value
head |= 0x1
elif match("^nrows[ \t]+([0-9.]+)[ \t]*$", inline):
nrows = match.value
head |= 0x2
elif match("^xllcorner[ \t]+([0-9.]+)[ \t]*$", inline):
xllcorner = match.value
head |= 0x4
elif match("^yllcorner[ \t]+([0-9.]+)[ \t]*$", inline):
yllcorner = match.value
head |= 0x8
elif match("^cellsize[ \t]+([0-9.]+)[ \t]*$", inline):
cellsize = match.value
head |= 0x10
elif match("^nodata_value[ \t]+([0-9.]+)[ \t]*$", inline):
nodata_value = match.value
head |= 0x20
else:
print(f"{infname}: Invalid input file format")
inf.close()
exit()
if head == 0x3F:
break

if head != 0x3F:
print(f"{infname}: Invalid input file format")
inf.close()
exit()

outf = open(outfname, "w")
outf.write(
f"""\
arc.to.gridatb.py {infname} {outfname}
{ncols} {nrows} {cellsize}
"""
)
for inline in inf:
outline = re.sub(
f"(?=^|[ \t]*){nodata_value}(\\.0*)?(?=([ \t]*|$))", "9999.00", inline
)
outf.write(outline)

inf.close()
outf.close()
29 changes: 0 additions & 29 deletions raster/r.topidx/gridatb.to.arc.pl

This file was deleted.

61 changes: 61 additions & 0 deletions raster/r.topidx/gridatb_to_arc.py
@@ -0,0 +1,61 @@
#!/usr/bin/env python3
import sys
import re
import os

if (
len(sys.argv) == 1
or len(sys.argv) == 4
or len(sys.argv) > 5
or re.match("^-*help", sys.argv[1])
):
print("Usage: gridatb.to.arc.py gridatb_file arc_file [xllcorner yllcorner]")
exit()

xllcorner = 0
yllcorner = 0
if len(sys.argv) == 5:
xllcorner = sys.argv[3]
yllcorner = sys.argv[4]

infname = sys.argv[1]
outfname = sys.argv[2]

if not os.path.isfile(infname):
print(f"{infname}: File not found")
exit()

if os.path.isfile(outfname):
print(f"{outfname}: File already exists")
exit()

inf = open(infname)

title = inf.readline()
inline = inf.readline()
m = re.match("^[ \t]*([0-9.]+)[ \t]+([0-9.]+)[ \t]+([0-9.]+)[ \t]*$", inline)
if not m:
print(f"{infname}: Invalid input file format")
inf.close()
exit()

ncols = m.group(1)
nrows = m.group(2)
cellsize = m.group(3)

outf = open(outfname, "w")
outf.write(
f"""\
ncols {ncols}
nrows {nrows}
xllcorner {xllcorner}
yllcorner {yllcorner}
cellsize {cellsize}
NODATA_value 9999
"""
)
for inline in inf:
outf.write(inline)

inf.close()
outf.close()

0 comments on commit c402ca5

Please sign in to comment.