Selaa lähdekoodia

r.topidx: Rewrite Perl scripts in Python 3 (#766)

Huidae Cho 4 vuotta sitten
vanhempi
commit
c402ca5de8

+ 0 - 45
raster/r.topidx/arc.to.gridatb.pl

@@ -1,45 +0,0 @@
-#!/usr/bin/env perl
-if($#ARGV != 1 || $ARGV[0] =~ m/^-*help$/){
-	print "Usage: arc.to.gridatb arc_file gridatb_file\n";
-	exit;
-}
-open IN, $ARGV[0] or die;
-$head = 0;
-while(<IN>){
-	if(m/^ncols[ \t]+([0-9.]+)[ \t]*$/i){
-		$ncols = $1;
-		$head |= 0x1;
-	}elsif(m/^nrows[ \t]+([0-9.]+)[ \t]*$/i){
-		$nrows = $1;
-		$head |= 0x2;
-	}elsif(m/^xllcorner[ \t]+([0-9.]+)[ \t]*$/i){
-		$xllcorner = $1;
-		$head |= 0x4;
-	}elsif(m/^yllcorner[ \t]+([0-9.]+)[ \t]*$/i){
-		$yllcorner = $1;
-		$head |= 0x8;
-	}elsif(m/^cellsize[ \t]+([0-9.]+)[ \t]*$/i){
-		$cellsize = $1;
-		$head |= 0x10;
-	}elsif(m/^nodata_value[ \t]+([0-9.]+)[ \t]*$/i){
-		$nodata_value = $1;
-		$head |= 0x20;
-	}else{
-		die;
-	}
-	if($head == 0x3f){
-		last;
-	}
-}
-open OUT, ">$ARGV[1]" or die;
-print OUT <<EOT
-arc.to.gridatb $ARGV[0] $ARGV[1]
-$ncols $nrows $cellsize
-EOT
-;
-while(<IN>){
-	s/(?=^|[ \t]*)$nodata_value(\.0*)?(?=([ \t]*|$))/9999.00/g;
-	print OUT;
-}
-close IN;
-close OUT;

+ 80 - 0
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()

+ 0 - 29
raster/r.topidx/gridatb.to.arc.pl

@@ -1,29 +0,0 @@
-#!/usr/bin/env perl
-if($#ARGV < 1 || $#ARGV == 2 || $#ARGV > 3 || $ARGV[0] =~ m/^-*help$/){
-	print "Usage: gridatb.to.arc gridatb_file arc_file [xllcorner yllcorner]\n";
-	exit;
-}
-$xllcorner = 0;
-$yllcorner = 0;
-if($#ARGV == 3){
-	$xllcorner = $ARGV[2];
-	$yllcorner = $ARGV[3];
-}
-open IN, $ARGV[0] or die;
-$title = <IN>;
-($ncols, $nrows, $cellsize) = (<IN> =~ m/^[ \t]*([0-9.]+)[ \t]+([0-9.]+)[ \t]+([0-9.]+)[ \t]*$/);
-open OUT, ">$ARGV[1]" or die;
-print OUT <<EOT
-ncols         $ncols
-nrows         $nrows
-xllcorner     $xllcorner
-yllcorner     $yllcorner
-cellsize      $cellsize
-NODATA_value  9999
-EOT
-;
-while(<IN>){
-	print OUT;
-}
-close IN;
-close OUT;

+ 61 - 0
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()