Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,8 +33,16 @@ DATA_PATH = args.data_path
|
|
| 33 |
|
| 34 |
ONI_DATA_PATH = os.path.join(DATA_PATH, 'oni_data.csv')
|
| 35 |
TYPHOON_DATA_PATH = os.path.join(DATA_PATH, 'processed_typhoon_data.csv')
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
CACHE_FILE = 'ibtracs_cache.pkl'
|
| 39 |
CACHE_EXPIRY_DAYS = 0 # Force refresh for testing
|
| 40 |
|
|
@@ -130,21 +138,54 @@ def load_ibtracs_data():
|
|
| 130 |
with open(CACHE_FILE, 'rb') as f:
|
| 131 |
return pickle.load(f)
|
| 132 |
|
| 133 |
-
all_basins_path = os.path.join(DATA_PATH, 'ibtracs.ALL.list.v04r01.csv')
|
| 134 |
-
|
| 135 |
try:
|
| 136 |
-
if
|
| 137 |
-
|
| 138 |
-
|
|
|
|
| 139 |
else:
|
| 140 |
-
print("Downloading
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
with open(CACHE_FILE, 'wb') as f:
|
| 150 |
pickle.dump(ibtracs, f)
|
|
@@ -249,7 +290,7 @@ def classify_enso_phases(oni_value):
|
|
| 249 |
# Load data globally
|
| 250 |
update_oni_data()
|
| 251 |
ibtracs = load_ibtracs_data()
|
| 252 |
-
convert_typhoondata(
|
| 253 |
oni_data, typhoon_data = load_data(ONI_DATA_PATH, TYPHOON_DATA_PATH)
|
| 254 |
oni_long = process_oni_data(oni_data)
|
| 255 |
typhoon_max = process_typhoon_data(typhoon_data)
|
|
|
|
| 33 |
|
| 34 |
ONI_DATA_PATH = os.path.join(DATA_PATH, 'oni_data.csv')
|
| 35 |
TYPHOON_DATA_PATH = os.path.join(DATA_PATH, 'processed_typhoon_data.csv')
|
| 36 |
+
|
| 37 |
+
# Basin-specific files instead of the global file
|
| 38 |
+
BASIN_FILES = {
|
| 39 |
+
'EP': 'ibtracs.EP.list.v04r01.csv',
|
| 40 |
+
'NA': 'ibtracs.NA.list.v04r01.csv',
|
| 41 |
+
'WP': 'ibtracs.WP.list.v04r01.csv'
|
| 42 |
+
}
|
| 43 |
+
IBTRACS_BASE_URL = 'https://www.ncei.noaa.gov/data/international-best-track-archive-for-climate-stewardship-ibtracs/v04r01/access/csv/'
|
| 44 |
+
LOCAL_MERGED_PATH = os.path.join(DATA_PATH, 'ibtracs.merged.v04r01.csv')
|
| 45 |
+
|
| 46 |
CACHE_FILE = 'ibtracs_cache.pkl'
|
| 47 |
CACHE_EXPIRY_DAYS = 0 # Force refresh for testing
|
| 48 |
|
|
|
|
| 138 |
with open(CACHE_FILE, 'rb') as f:
|
| 139 |
return pickle.load(f)
|
| 140 |
|
|
|
|
|
|
|
| 141 |
try:
|
| 142 |
+
# Check if merged file already exists
|
| 143 |
+
if os.path.exists(LOCAL_MERGED_PATH):
|
| 144 |
+
print("Loading merged basins file...")
|
| 145 |
+
ibtracs = tracks.TrackDataset(source='ibtracs', ibtracs_url=LOCAL_MERGED_PATH)
|
| 146 |
else:
|
| 147 |
+
print("Downloading and merging basin files...")
|
| 148 |
+
|
| 149 |
+
# Create temporary file for merged data
|
| 150 |
+
header = None
|
| 151 |
+
with open(LOCAL_MERGED_PATH, 'w', newline='') as merged_file:
|
| 152 |
+
writer = None
|
| 153 |
+
|
| 154 |
+
# Download and process each basin file
|
| 155 |
+
for basin, filename in BASIN_FILES.items():
|
| 156 |
+
basin_url = IBTRACS_BASE_URL + filename
|
| 157 |
+
local_path = os.path.join(DATA_PATH, filename)
|
| 158 |
+
|
| 159 |
+
# Download the basin file if it doesn't exist
|
| 160 |
+
if not os.path.exists(local_path):
|
| 161 |
+
print(f"Downloading {basin} basin file...")
|
| 162 |
+
response = requests.get(basin_url)
|
| 163 |
+
response.raise_for_status()
|
| 164 |
+
with open(local_path, 'wb') as f:
|
| 165 |
+
f.write(response.content)
|
| 166 |
+
print(f"Downloaded {basin} basin file.")
|
| 167 |
+
|
| 168 |
+
# Process and merge the basin file
|
| 169 |
+
with open(local_path, 'r', newline='') as basin_file:
|
| 170 |
+
reader = csv.reader(basin_file)
|
| 171 |
+
|
| 172 |
+
# Save header from the first file
|
| 173 |
+
if header is None:
|
| 174 |
+
header = next(reader)
|
| 175 |
+
writer = csv.writer(merged_file)
|
| 176 |
+
writer.writerow(header)
|
| 177 |
+
# Skip the second header line
|
| 178 |
+
next(reader)
|
| 179 |
+
else:
|
| 180 |
+
# Skip header lines in subsequent files
|
| 181 |
+
next(reader)
|
| 182 |
+
next(reader)
|
| 183 |
+
|
| 184 |
+
# Write all data rows
|
| 185 |
+
writer.writerows(reader)
|
| 186 |
+
|
| 187 |
+
print(f"Created merged basin file at {LOCAL_MERGED_PATH}")
|
| 188 |
+
ibtracs = tracks.TrackDataset(source='ibtracs', ibtracs_url=LOCAL_MERGED_PATH)
|
| 189 |
|
| 190 |
with open(CACHE_FILE, 'wb') as f:
|
| 191 |
pickle.dump(ibtracs, f)
|
|
|
|
| 290 |
# Load data globally
|
| 291 |
update_oni_data()
|
| 292 |
ibtracs = load_ibtracs_data()
|
| 293 |
+
convert_typhoondata(LOCAL_MERGED_PATH, TYPHOON_DATA_PATH)
|
| 294 |
oni_data, typhoon_data = load_data(ONI_DATA_PATH, TYPHOON_DATA_PATH)
|
| 295 |
oni_long = process_oni_data(oni_data)
|
| 296 |
typhoon_max = process_typhoon_data(typhoon_data)
|