Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -54,8 +54,60 @@ COLOR_MAP = {
|
|
54 |
class TyphoonAnalyzer:
|
55 |
def __init__(self):
|
56 |
self.last_oni_update = None
|
|
|
57 |
self.load_initial_data()
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def load_initial_data(self):
|
60 |
print("Loading initial data...")
|
61 |
self.update_oni_data()
|
|
|
54 |
class TyphoonAnalyzer:
|
55 |
def __init__(self):
|
56 |
self.last_oni_update = None
|
57 |
+
self.ensure_data_files_exist()
|
58 |
self.load_initial_data()
|
59 |
|
60 |
+
def ensure_data_files_exist(self):
|
61 |
+
"""Ensure all required data files exist before loading"""
|
62 |
+
print("Checking and downloading required data files...")
|
63 |
+
|
64 |
+
# Create data directory if it doesn't exist
|
65 |
+
os.makedirs(DATA_PATH, exist_ok=True)
|
66 |
+
|
67 |
+
# Download ONI data if it doesn't exist
|
68 |
+
if not os.path.exists(ONI_DATA_PATH):
|
69 |
+
print("Downloading ONI data...")
|
70 |
+
url = "https://www.cpc.ncep.noaa.gov/data/indices/oni.ascii.txt"
|
71 |
+
temp_file = os.path.join(DATA_PATH, "temp_oni.ascii.txt")
|
72 |
+
try:
|
73 |
+
response = requests.get(url)
|
74 |
+
response.raise_for_status()
|
75 |
+
with open(temp_file, 'wb') as f:
|
76 |
+
f.write(response.content)
|
77 |
+
self.convert_oni_ascii_to_csv(temp_file, ONI_DATA_PATH)
|
78 |
+
print("ONI data downloaded and converted successfully")
|
79 |
+
except Exception as e:
|
80 |
+
print(f"Error downloading ONI data: {e}")
|
81 |
+
raise
|
82 |
+
finally:
|
83 |
+
if os.path.exists(temp_file):
|
84 |
+
os.remove(temp_file)
|
85 |
+
|
86 |
+
# Download IBTrACS data if it doesn't exist
|
87 |
+
if not os.path.exists(LOCAL_iBtrace_PATH):
|
88 |
+
print("Downloading IBTrACS data...")
|
89 |
+
try:
|
90 |
+
response = requests.get(iBtrace_uri)
|
91 |
+
response.raise_for_status()
|
92 |
+
with open(LOCAL_iBtrace_PATH, 'w') as f:
|
93 |
+
f.write(response.text)
|
94 |
+
print("IBTrACS data downloaded successfully")
|
95 |
+
except Exception as e:
|
96 |
+
print(f"Error downloading IBTrACS data: {e}")
|
97 |
+
raise
|
98 |
+
|
99 |
+
# Create processed typhoon data if it doesn't exist
|
100 |
+
if not os.path.exists(TYPHOON_DATA_PATH):
|
101 |
+
print("Processing typhoon data...")
|
102 |
+
try:
|
103 |
+
self.convert_typhoondata(LOCAL_iBtrace_PATH, TYPHOON_DATA_PATH)
|
104 |
+
print("Typhoon data processed successfully")
|
105 |
+
except Exception as e:
|
106 |
+
print(f"Error processing typhoon data: {e}")
|
107 |
+
raise
|
108 |
+
|
109 |
+
print("All required data files are ready")
|
110 |
+
|
111 |
def load_initial_data(self):
|
112 |
print("Loading initial data...")
|
113 |
self.update_oni_data()
|