Create generate_metadata.py
Browse files- generate_metadata.py +76 -0
generate_metadata.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import xarray as xr
|
3 |
+
from glob import glob
|
4 |
+
from typing import Optional, List
|
5 |
+
|
6 |
+
def extract_region_id(filepath: str) -> str:
|
7 |
+
"""Extract region ID from netCDF file attributes."""
|
8 |
+
ds = xr.open_dataset(filepath)
|
9 |
+
original_id = ds.attrs.get('original_id', '')
|
10 |
+
ice_service = ds.attrs.get('ice_service', '')
|
11 |
+
ds.close()
|
12 |
+
parts = original_id.split('_')
|
13 |
+
if ice_service == "dmi":
|
14 |
+
return parts[-2] + "_" + parts[-1].split('.')[0]
|
15 |
+
return parts[-4]
|
16 |
+
|
17 |
+
def load_split_data(splits: List[str]) -> pd.DataFrame:
|
18 |
+
"""Load and preprocess data from split directories."""
|
19 |
+
dfs = []
|
20 |
+
for split in splits:
|
21 |
+
paths = glob(f"{split}/*.nc")
|
22 |
+
split_df = pd.DataFrame(paths, columns=["path"])
|
23 |
+
split_df["split"] = split
|
24 |
+
dfs.append(split_df)
|
25 |
+
|
26 |
+
df = pd.concat(dfs, ignore_index=True)
|
27 |
+
df['date'] = pd.to_datetime(df['path'].str.extract(r'(\d{8}T\d{6})')[0], format='%Y%m%dT%H%M%S')
|
28 |
+
df['ice_service'] = df['path'].str.extract(r'_(dmi|cis)_')[0]
|
29 |
+
df['is_reference'] = df['path'].str.contains('reference')
|
30 |
+
return df
|
31 |
+
|
32 |
+
def process_test_data(test_data: pd.DataFrame) -> pd.DataFrame:
|
33 |
+
"""Process test split data to pair inputs with references."""
|
34 |
+
test_pairs = []
|
35 |
+
for (date, ice_service), group in test_data.groupby(['date', 'ice_service']):
|
36 |
+
input_file = group[~group['is_reference']]['path'].iloc[0]
|
37 |
+
ref_file = group[group['is_reference']]['path'].iloc[0]
|
38 |
+
test_pairs.append({
|
39 |
+
'input_path': input_file,
|
40 |
+
'reference_path': ref_file,
|
41 |
+
'date': date,
|
42 |
+
'ice_service': ice_service,
|
43 |
+
'split': 'test'
|
44 |
+
})
|
45 |
+
return pd.DataFrame(test_pairs)
|
46 |
+
|
47 |
+
def create_summary_df() -> pd.DataFrame:
|
48 |
+
"""Create summary DataFrame with all samples."""
|
49 |
+
splits = ["train", "test"]
|
50 |
+
df = load_split_data(splits)
|
51 |
+
|
52 |
+
# Process train data
|
53 |
+
train_data = df[df['split'] == 'train'].copy()
|
54 |
+
train_data['input_path'] = train_data['path']
|
55 |
+
train_data['reference_path'] = None
|
56 |
+
|
57 |
+
# Process test data
|
58 |
+
test_data = process_test_data(df[df['split'] == 'test'])
|
59 |
+
|
60 |
+
# Combine and add region IDs
|
61 |
+
summary_df = pd.concat([
|
62 |
+
train_data[['input_path', 'reference_path', 'date', 'ice_service', 'split']],
|
63 |
+
test_data
|
64 |
+
])
|
65 |
+
summary_df['region_id'] = summary_df['input_path'].apply(extract_region_id)
|
66 |
+
|
67 |
+
return summary_df
|
68 |
+
|
69 |
+
def main():
|
70 |
+
"""Main function to generate metadata summary."""
|
71 |
+
summary_df = create_summary_df()
|
72 |
+
print("\nFinal Summary:")
|
73 |
+
print(summary_df)
|
74 |
+
|
75 |
+
if __name__ == '__main__':
|
76 |
+
main()
|