1
1
#!/usr/bin/env python3
2
2
# David Bekaert
3
+
4
+
3
5
import os
4
6
import glob
5
7
import argparse
6
- from uncompressFile import uncompressfile
7
8
import shutil
9
+ import tarfile
10
+ import zipfile
11
+ from uncompressFile import uncompressfile
12
+
8
13
9
14
def createParser ():
10
15
'''
11
16
Create command line parser.
12
17
'''
13
18
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 ,
16
22
help = 'directory with the raw data' )
17
23
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 ,
20
27
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;' )
23
30
return parser
24
31
25
32
@@ -29,7 +36,15 @@ def cmdLineParse(iargs=None):
29
36
'''
30
37
31
38
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
+
33
48
34
49
def get_Date (ALOSfolder ):
35
50
@@ -56,46 +71,60 @@ def get_Date(ALOSfolder):
56
71
acquisitionDate = 'FAIL'
57
72
return successflag , acquisitionDate
58
73
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
+
59
97
def main (iargs = None ):
60
98
'''
61
99
The main driver.
62
100
'''
63
101
64
102
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
73
103
74
104
# filename of the runfile
75
105
run_unPack = 'run_unPackALOS'
76
106
77
107
# 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' ))
79
111
for ALOS_extension in ALOS_extensions :
112
+ # loop over zip/tar files
80
113
ALOS_filesfolders = glob .glob (ALOS_extension )
81
114
for ALOS_infilefolder in ALOS_filesfolders :
82
115
## the path to the folder/zip
83
116
workdir = os .path .dirname (ALOS_infilefolder )
84
-
117
+
85
118
## 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 )
91
120
# 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
+
94
123
# loop over two cases (either file or folder):
95
124
### this is a file, try to unzip/untar it
96
125
if os .path .isfile (ALOS_infilefolder ):
97
126
# unzip the file in the outfolder
98
- successflag_unzip = uncompressfile (ALOS_infilefolder ,ALOS_outfolder )
127
+ successflag_unzip = uncompressfile (ALOS_infilefolder , ALOS_outfolder )
99
128
100
129
# put failed files in a seperate directory
101
130
if not successflag_unzip :
@@ -104,7 +133,7 @@ def main(iargs=None):
104
133
os .rename (ALOS_infilefolder ,os .path .join (workdir ,'FAILED_FILES' ,'.' ))
105
134
else :
106
135
# check if file needs to be removed or put in archive folder
107
- if rmfile :
136
+ if inps . rmfile :
108
137
os .remove (ALOS_infilefolder )
109
138
print ('Deleting: ' + ALOS_infilefolder )
110
139
else :
@@ -116,9 +145,10 @@ def main(iargs=None):
116
145
117
146
# loop over the different ALOS folders and make sure the folder names are consistent.
118
147
# 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*' ))
120
149
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
122
152
temp = os .path .basename (ALOS_folder )
123
153
parts = temp .split ("." )
124
154
parts = parts [0 ].split ('-' )
@@ -134,7 +164,7 @@ def main(iargs=None):
134
164
135
165
136
166
# 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*' ))
138
168
for ALOS_folder in ALOS_folders :
139
169
# get the date
140
170
successflag , imgDate = get_Date (ALOS_folder )
@@ -160,14 +190,14 @@ def main(iargs=None):
160
190
161
191
162
192
# 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 :
165
195
f = open (run_unPack ,'w' )
166
196
for dataDir in dateDirs :
167
197
AlosFiles = glob .glob (os .path .join (dataDir , 'ALP*' ))
168
198
if len (AlosFiles )> 0 :
169
199
acquisitionDate = os .path .basename (dataDir )
170
- slcDir = os .path .join (outputDir , acquisitionDate )
200
+ slcDir = os .path .join (inps . outputDir , acquisitionDate )
171
201
if not os .path .exists (slcDir ):
172
202
os .makedirs (slcDir )
173
203
cmd = 'unpackFrame_ALOS_raw.py -i ' + os .path .abspath (dataDir ) + ' -o ' + slcDir
@@ -179,9 +209,9 @@ def main(iargs=None):
179
209
print (cmd )
180
210
f .write (inps .text_cmd + cmd + '\n ' )
181
211
f .close ()
212
+ return
213
+
182
214
183
215
if __name__ == '__main__' :
184
216
185
217
main ()
186
-
187
-
0 commit comments