Skip to content

Commit 8673550

Browse files
committed
support PIXEL/ALOS1 .gz files
uncompressFile.py: support multiple sub-layers of folders uncompressing prepRawALOS.py: 1. add get_ALOS_ALP_name() to support the compressed ALOS file that is not named with "ALP*", such as the ones from PIXEL 2. remove duplicated inputDir, outputDir and rmfile, as they are in the Namespace inps object already. 3. move the inputDir and outputDir abspath from main() to cmdLineParse() 4. basic formating adjustment
1 parent 5e1ca08 commit 8673550

File tree

3 files changed

+82
-44
lines changed

3 files changed

+82
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.pyc
22
*~
33
*.swp
4+
*.DS_Store
45
__pycache__
56
.sconf_temp
67
.sconsign.dblite

contrib/stack/stripmapStack/prepRawALOS.py

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
#!/usr/bin/env python3
22
# David Bekaert
3+
4+
35
import os
46
import glob
57
import argparse
6-
from uncompressFile import uncompressfile
78
import shutil
9+
import tarfile
10+
import zipfile
11+
from uncompressFile import uncompressfile
12+
813

914
def createParser():
1015
'''
1116
Create command line parser.
1217
'''
1318

14-
parser = argparse.ArgumentParser(description='Prepare ALOS raw processing (unzip/untar files, organize in date folders, generate script to unpack into isce formats).')
15-
parser.add_argument('-i', '--input', dest='input', type=str, required=True,
19+
parser = argparse.ArgumentParser(description='Prepare ALOS raw processing (unzip/untar files, '
20+
'organize in date folders, generate script to unpack into isce formats).')
21+
parser.add_argument('-i', '--input', dest='inputDir', type=str, required=True,
1622
help='directory with the raw data')
1723
parser.add_argument('-rmfile', '--rmfile', dest='rmfile',action='store_true', default=False,
18-
help='Optional: remove zip/tar/compressed files after unpacking into date structure (default is to keep in archive fo lder)')
19-
parser.add_argument('-o', '--output', dest='output', type=str, required=False,
24+
help='Optional: remove zip/tar/compressed files after unpacking into date structure '
25+
'(default is to keep in archive fo lder)')
26+
parser.add_argument('-o', '--output', dest='outputDir', type=str, required=False,
2027
help='output directory where data needs to be unpacked into isce format (for script generation).')
21-
parser.add_argument('-t', '--text_cmd', dest='text_cmd', type=str, default='source ~/.bash_profile;'
22-
, help='text command to be added to the beginning of each line of the run files. Default: source ~/.bash_profile;')
28+
parser.add_argument('-t', '--text_cmd', dest='text_cmd', type=str, default='source ~/.bash_profile;',
29+
help='text command to be added to the beginning of each line of the run files. Default: source ~/.bash_profile;')
2330
return parser
2431

2532

@@ -29,7 +36,15 @@ def cmdLineParse(iargs=None):
2936
'''
3037

3138
parser = createParser()
32-
return parser.parse_args(args = iargs)
39+
inps = parser.parse_args(args = iargs)
40+
41+
# parsing required inputs
42+
inps.inputDir = os.path.abspath(inps.inputDir)
43+
# parsing optional inputs
44+
if inps.outputDir:
45+
inps.outputDir = os.path.abspath(inps.outputDir)
46+
return inps
47+
3348

3449
def get_Date(ALOSfolder):
3550

@@ -56,46 +71,60 @@ def get_Date(ALOSfolder):
5671
acquisitionDate = 'FAIL'
5772
return successflag, acquisitionDate
5873

74+
75+
def get_ALOS_ALP_name(infile):
76+
"""Get the ALPSRP075780620 name from compress file in various format."""
77+
outname = None
78+
fbase = os.path.basename(infile)
79+
if fbase.startswith("ALP"):
80+
outname = fbase.split("-")[0]
81+
else:
82+
fext = os.path.splitext(infile)[1]
83+
if fext in ['.tar', '.gz']:
84+
with tarfile.open(infile, 'r') as tar:
85+
file_list = tar.getnames()
86+
elif fext in ['.zip']:
87+
with zipfile.ZipFile(infile, 'r') as z:
88+
file_list = z.namelist()
89+
else:
90+
raise ValueError('unrecognized file extension: {}'.format(fext))
91+
led_file = [i for i in file_list if 'LED' in i][0]
92+
led_file = os.path.basename(led_file)
93+
outname = [i for i in led_file.split("-") if 'ALP' in i][0]
94+
return outname
95+
96+
5997
def main(iargs=None):
6098
'''
6199
The main driver.
62100
'''
63101

64102
inps = cmdLineParse(iargs)
65-
# parsing required inputs
66-
inputDir = os.path.abspath(inps.input)
67-
# parsing optional inputs
68-
if inps.output:
69-
outputDir = os.path.abspath(inps.output)
70-
else:
71-
outputDir = None
72-
rmfile = inps.rmfile
73103

74104
# filename of the runfile
75105
run_unPack = 'run_unPackALOS'
76106

77107
# loop over the different folder, ALOS zip/tar files and unzip them, make the names consistent
78-
ALOS_extensions = (os.path.join(inputDir, 'ALP*.zip'),os.path.join(inputDir, 'ALP*.tar'),os.path.join(inputDir, 'ALP*.gz'))
108+
ALOS_extensions = (os.path.join(inps.inputDir, '*.zip'),
109+
os.path.join(inps.inputDir, '*.tar'),
110+
os.path.join(inps.inputDir, '*.gz'))
79111
for ALOS_extension in ALOS_extensions:
112+
# loop over zip/tar files
80113
ALOS_filesfolders = glob.glob(ALOS_extension)
81114
for ALOS_infilefolder in ALOS_filesfolders:
82115
## the path to the folder/zip
83116
workdir = os.path.dirname(ALOS_infilefolder)
84-
117+
85118
## get the output name folder without any extensions
86-
temp = os.path.basename(ALOS_infilefolder)
87-
# trim the extensions and keep only very first part
88-
parts = temp.split(".")
89-
parts = parts[0].split('-')
90-
ALOS_outfolder = parts[0]
119+
ALOS_outfolder = get_ALOS_ALP_name(ALOS_infilefolder)
91120
# add the path back in
92-
ALOS_outfolder = os.path.join(workdir,ALOS_outfolder)
93-
121+
ALOS_outfolder = os.path.join(workdir, ALOS_outfolder)
122+
94123
# loop over two cases (either file or folder):
95124
### this is a file, try to unzip/untar it
96125
if os.path.isfile(ALOS_infilefolder):
97126
# unzip the file in the outfolder
98-
successflag_unzip = uncompressfile(ALOS_infilefolder,ALOS_outfolder)
127+
successflag_unzip = uncompressfile(ALOS_infilefolder, ALOS_outfolder)
99128

100129
# put failed files in a seperate directory
101130
if not successflag_unzip:
@@ -104,7 +133,7 @@ def main(iargs=None):
104133
os.rename(ALOS_infilefolder,os.path.join(workdir,'FAILED_FILES','.'))
105134
else:
106135
# check if file needs to be removed or put in archive folder
107-
if rmfile:
136+
if inps.rmfile:
108137
os.remove(ALOS_infilefolder)
109138
print('Deleting: ' + ALOS_infilefolder)
110139
else:
@@ -116,9 +145,10 @@ def main(iargs=None):
116145

117146
# loop over the different ALOS folders and make sure the folder names are consistent.
118147
# this step is not needed unless the user has manually unzipped data before.
119-
ALOS_folders = glob.glob(os.path.join(inputDir, 'ALP*'))
148+
ALOS_folders = glob.glob(os.path.join(inps.inputDir, 'ALP*'))
120149
for ALOS_folder in ALOS_folders:
121-
# in case the user has already unzipped some files, make sure they are unzipped similar like the uncompressfile code
150+
# in case the user has already unzipped some files
151+
# make sure they are unzipped similar like the uncompressfile code
122152
temp = os.path.basename(ALOS_folder)
123153
parts = temp.split(".")
124154
parts = parts[0].split('-')
@@ -134,7 +164,7 @@ def main(iargs=None):
134164

135165

136166
# loop over the different ALOS folders and organize in date folders
137-
ALOS_folders = glob.glob(os.path.join(inputDir, 'ALP*'))
167+
ALOS_folders = glob.glob(os.path.join(inps.inputDir, 'ALP*'))
138168
for ALOS_folder in ALOS_folders:
139169
# get the date
140170
successflag, imgDate = get_Date(ALOS_folder)
@@ -160,14 +190,14 @@ def main(iargs=None):
160190

161191

162192
# now generate the unpacking script for all the date dirs
163-
dateDirs = glob.glob(os.path.join(inputDir,'2*'))
164-
if outputDir is not None:
193+
dateDirs = glob.glob(os.path.join(inps.inputDir,'2*'))
194+
if inps.outputDir is not None:
165195
f = open(run_unPack,'w')
166196
for dataDir in dateDirs:
167197
AlosFiles = glob.glob(os.path.join(dataDir, 'ALP*'))
168198
if len(AlosFiles)>0:
169199
acquisitionDate = os.path.basename(dataDir)
170-
slcDir = os.path.join(outputDir, acquisitionDate)
200+
slcDir = os.path.join(inps.outputDir, acquisitionDate)
171201
if not os.path.exists(slcDir):
172202
os.makedirs(slcDir)
173203
cmd = 'unpackFrame_ALOS_raw.py -i ' + os.path.abspath(dataDir) + ' -o ' + slcDir
@@ -179,9 +209,9 @@ def main(iargs=None):
179209
print (cmd)
180210
f.write(inps.text_cmd + cmd+'\n')
181211
f.close()
212+
return
213+
182214

183215
if __name__ == '__main__':
184216

185217
main()
186-
187-

contrib/stack/stripmapStack/uncompressFile.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
# David Bekaert
44

5-
import zipfile
5+
66
import os
77
import glob
88
import argparse
9-
import tarfile
109
import shutil
10+
import tarfile
11+
import zipfile
12+
1113

1214
def createParser():
1315
'''
@@ -50,6 +52,8 @@ def main(iargs=None):
5052
print('Done')
5153
elif completeFlag == False:
5254
print('Failed')
55+
return
56+
5357

5458
def uncompressfile(inputFile,outputDir):
5559

@@ -89,7 +93,7 @@ def uncompressfile(inputFile,outputDir):
8993
temp, extension = os.path.splitext(inputFile)
9094

9195
# File update
92-
print('File: ', inputFile, ' to ', outputDir)
96+
print('File: ', inputFile, ' to ', outputDir)
9397
if extension == '.zip':
9498
ZIP = zipfile.ZipFile(inputFile)
9599

@@ -105,7 +109,7 @@ def uncompressfile(inputFile,outputDir):
105109

106110
# Check if the data is unpacked in its own folder
107111
folderfiles = glob.glob(os.path.join(outputDir,'*'))
108-
if len(folderfiles)==1:
112+
while len(folderfiles)==1:
109113
# get the sub-folder name only
110114
tempdir = os.path.basename(folderfiles[0])
111115
if os.path.isdir(folderfiles[0]):
@@ -114,27 +118,30 @@ def uncompressfile(inputFile,outputDir):
114118
os.rename(folderfiles[0],tempdir2)
115119
os.rmdir(outputDir)
116120
os.rename(tempdir2,outputDir)
121+
folderfiles = glob.glob(os.path.join(outputDir,'*'))
117122
return completeFlag
123+
118124
elif extension == '.tar' or extension == '.gz':
119125
TAR = tarfile.open(inputFile)
120-
126+
121127
# first test the tar is in good condition
122128
try:
123129
TAR.extractall(outputDir)
124130
TAR.close()
125131
completeFlag = True
126132

127-
# Check if the data is unpacked in its own folder
133+
# Check if the data is unpacked in its own folder or its sub-folders
128134
folderfiles = glob.glob(os.path.join(outputDir,'*'))
129-
if len(folderfiles)==1:
130-
# get the sub-folder name only
135+
while len(folderfiles) == 1:
136+
# get the sub-folder name only
131137
tempdir = os.path.basename(folderfiles[0])
132138
if os.path.isdir(folderfiles[0]):
133139
# it seems there is a subfolder, will copy the content in the parent
134-
tempdir2=os.path.join(workdir,tempdir + '.temp')
140+
tempdir2 = os.path.join(workdir, tempdir + '.temp')
135141
os.rename(folderfiles[0],tempdir2)
136142
os.rmdir(outputDir)
137143
os.rename(tempdir2,outputDir)
144+
folderfiles = glob.glob(os.path.join(outputDir,'*'))
138145
return completeFlag
139146
except:
140147
print('Tar file seems to be corrupted, abord...')

0 commit comments

Comments
 (0)