Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r.topidx: Rewrite Perl scripts in Python 3 (#766)
- Loading branch information
Showing
4 changed files
with
141 additions
and
74 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |