-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathfilter_version_PIXC_ex.py
More file actions
81 lines (61 loc) · 2.68 KB
/
Copy pathfilter_version_PIXC_ex.py
File metadata and controls
81 lines (61 loc) · 2.68 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
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
"""
Takes a list of directories of SWOT PIXC data, filters
them to find the best version of each granule, and writes
out a JSON containing the best files for each directory.
Currently built for PGC0 and PIC0 versions
Usage:
python3 filterVersionRiverSP.py
Authors: Fiona Bennitt and Elisa Friedmann
Date: 2024-10-31
"""
import json
import os
import pandas as pd
def filterVersionPIXC(directories, outpath):
"""
Reads in all filenames, sorts them, and retrieves the best version/
counter for each file (e.g. PGC0 over PIC0, PIC0_03 over PIC0_02).
Writes out a json with the filenames for filtering upon read in
to the outpath directory.
Parameters:
directories (list): List of directories to search for best files.
outpath (string): Where to save the output JSON.
Returns:
None
"""
# Get all file names from directories
for directory in directories:
# List to store all file paths
files = []
for file in os.listdir(directory):
files.append(file)
print(f"There are {str(len(files))} original files in directory.")
# Make DataFrame of filenames
granules = pd.DataFrame({'files': files})
granules['cycle'] = granules['files'].str.slice(16, 19)
granules['pass'] = granules['files'].str.slice(20, 23)
granules['tile'] = granules['files'].str.slice(24, 28)
granules['version'] = granules['files'].str.slice(-10, -6)
granules['counter'] = granules['files'].str.slice(-5, -3)
# Sort the files
granules = granules.sort_values(by=['cycle', 'pass', 'tile', 'version', 'counter'],
ascending=[True, True, True, True, False])
# Keep only the best version of each granule
granules = granules.drop_duplicates(subset=['cycle', 'pass', 'tile'],
keep='first')
# Extract the file names of files passing the test
best_files = list(granules['files'])
print(f"There are {str(len(best_files))} best files in directory.")
# Split filepath for naming json
pieces = directory.split('/')
# Write out best files as json
with open(os.path.join(outpath, pieces[-2] + '_filtered.json'), 'w', encoding='utf-8') as f:
json.dump(best_files, f)
print(f"Wrote out the unique and most recently processed {str(len(best_files))} files to {outpath}{pieces[5]}_filtered.json\n" )
if __name__ == "__main__":
# Directories to filter
dirs = ['/yourPath/file1',
'/yourPath/file2']
# Outpath for json
out = '/yourPath/'
filterVersionPIXC(directories=dirs, outpath=out)