-
Notifications
You must be signed in to change notification settings - Fork 7
/
rename_setsm_add_version.py
executable file
·103 lines (83 loc) · 3.1 KB
/
rename_setsm_add_version.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import argparse
import glob
import logging
import os
import sys
from lib import dem, VERSION, SHORT_VERSION
#### Create Logger
logger = logging.getLogger("logger")
logger.setLevel(logging.DEBUG)
def main():
#### Set Up Arguments
parser = argparse.ArgumentParser(
description="rename setsm strips"
)
parser.add_argument("srcdir", help="source directory")
parser.add_argument("version", help="strip DEM version string (ex: s2s041)")
parser.add_argument("--dryrun", action='store_true', default=False,
help="print actions without executing")
parser.add_argument('--version', action='version', version=f"Current version: {SHORT_VERSION}",
help='print version and exit')
#### Parse Arguments
args = parser.parse_args()
src = args.srcdir
lsh = logging.StreamHandler()
lsh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)s- %(message)s','%m-%d-%Y %H:%M:%S')
lsh.setFormatter(formatter)
logger.addHandler(lsh)
logger.info("Current version: %s", VERSION)
rasters = []
#### ID rasters
logger.info('Identifying DEMs')
if os.path.isfile(src) and src.endswith("_dem.tif") and not os.path.basename(src).startswith("SETSM_"):
logger.debug(src)
try:
raster = dem.SetsmDem(os.path.join(src))
except RuntimeError as e:
logger.error( e )
else:
rasters.append(raster)
else:
for root,dirs,files in os.walk(src):
for f in files:
if f.endswith("_dem.tif") and not f.startswith("SETSM_"):
#logger.info(os.path.join(root, f))
try:
raster = dem.SetsmDem(os.path.join(root, f))
except RuntimeError as e:
logger.error( e )
else:
rasters.append(raster)
i=0
total = len(rasters)
logger.info('Renaming DEMs')
if total > 0:
for raster in rasters:
i+=1
logger.info("[{}/{}] {}".format(i,total,raster.srcfp))
rename(raster, args)
else:
print("No DEMs found requiring processing")
def rename(raster, args):
# get creation date string:
raster.get_metafile_info()
creation_date_str = raster.creation_date.strftime("%Y%m%d")
# glob and rename files
glob1 = glob.glob(os.path.join(raster.srcdir, raster.stripid) + "_*")
tar_path = os.path.join(raster.srcdir, raster.stripid) + ".tar.gz"
if os.path.isfile(tar_path):
glob1.append(tar_path)
for ifp in glob1:
dirp, fn = os.path.split(ifp)
#print(ifp)
ofn = "SETSM_{}_{}".format(args.version, fn)
ofp = os.path.join(dirp, ofn)
if os.path.isfile(ofp):
print("Output file already exists: {}".format(ofp))
else:
logger.debug("{} --> {}".format(ifp, ofp))
if not args.dryrun:
os.rename(ifp, ofp)
if __name__ == '__main__':
main()