-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfil_org.py
More file actions
56 lines (43 loc) · 1.8 KB
/
fil_org.py
File metadata and controls
56 lines (43 loc) · 1.8 KB
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
'''
Code Purpose: Find filterbanks in a given directory and sort them into a master directory based on date and source name.
'''
import your
import os
import glob
import argparse
def get_args():
parser = argparse.ArgumentParser(description="Find filterbanks in a given directory and sort them into a master directory based on date and source name.")
parser.add_argument('-i', '--input_dir', type=str, required=True, help='Input directory to search for filterbank files.')
parser.add_argument('-o', '--output_dir', type=str, required=True, help='Output master directory to sort filterbank files into.')
return parser.parse_args()
def get_hdr(fil_path):
hdr = your.Your(fil_path).your_header
# print(hdr)
trgt = hdr.source_name
strt_date = hdr.tstart_utc
return trgt, strt_date
def main():
args = get_args()
input_dir = args.input_dir
output_dir = args.output_dir
fil_files = glob.glob(os.path.join(input_dir, '**', '*.fil'), recursive=True)
print('Number of .fil files found:', len(fil_files))
for fil in fil_files:
try:
trgt, strt_date = get_hdr(fil)
except Exception as e:
print(f"Error reading header for {fil}: {e}")
continue
date_str = strt_date.split('T')[0]
target_dir = os.path.join(output_dir, trgt, date_str)
# print(f"Creating directory: {target_dir}")
os.makedirs(target_dir, exist_ok=True)
dest_path = os.path.join(target_dir, os.path.basename(fil))
# check if alreadu in dest_path
if os.path.abspath(fil) == os.path.abspath(dest_path):
# print(f"{fil} is already in the correct location.")
continue
os.rename(fil, dest_path)
# print(f"Moved {fil} to {dest_path}")
if __name__ == "__main__":
main()