Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -115,7 +115,155 @@ regions = {
|
|
| 115 |
"Hong Kong": {"lat_min": 21.5, "lat_max": 23, "lon_min": 113, "lon_max": 115},
|
| 116 |
"Philippines": {"lat_min": 5, "lat_max": 21, "lon_min": 115, "lon_max": 130}
|
| 117 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# -----------------------------
|
| 120 |
# ONI and Typhoon Data Functions
|
| 121 |
# -----------------------------
|
|
|
|
| 115 |
"Hong Kong": {"lat_min": 21.5, "lat_max": 23, "lon_min": 113, "lon_max": 115},
|
| 116 |
"Philippines": {"lat_min": 5, "lat_max": 21, "lon_min": 115, "lon_max": 130}
|
| 117 |
}
|
| 118 |
+
# Add these functions near the top of the file after imports
|
| 119 |
+
def generate_sample_oni_data():
|
| 120 |
+
"""Generate sample ONI data when the real data can't be loaded"""
|
| 121 |
+
years = range(1950, 2024)
|
| 122 |
+
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
| 123 |
+
data = {'Year': list(years)}
|
| 124 |
+
|
| 125 |
+
# Generate random ONI values between -2.5 and 2.5
|
| 126 |
+
np.random.seed(42) # For reproducibility
|
| 127 |
+
for month in months:
|
| 128 |
+
data[month] = np.round(np.random.uniform(-2.5, 2.5, len(years)), 1)
|
| 129 |
+
|
| 130 |
+
df = pd.DataFrame(data)
|
| 131 |
+
df.to_csv(ONI_DATA_PATH, index=False)
|
| 132 |
+
return df
|
| 133 |
+
|
| 134 |
+
def generate_sample_typhoon_data():
|
| 135 |
+
"""Generate sample typhoon data when the real data can't be loaded"""
|
| 136 |
+
# Create sample data with realistic values
|
| 137 |
+
np.random.seed(42)
|
| 138 |
+
|
| 139 |
+
# Generate 100 sample typhoons
|
| 140 |
+
n_typhoons = 100
|
| 141 |
+
n_points_per_typhoon = 20
|
| 142 |
+
total_points = n_typhoons * n_points_per_typhoon
|
| 143 |
+
|
| 144 |
+
data = {
|
| 145 |
+
'SID': [],
|
| 146 |
+
'ISO_TIME': [],
|
| 147 |
+
'NAME': [],
|
| 148 |
+
'SEASON': [],
|
| 149 |
+
'LAT': [],
|
| 150 |
+
'LON': [],
|
| 151 |
+
'USA_WIND': [],
|
| 152 |
+
'USA_PRES': []
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
# Basin prefixes and sample names
|
| 156 |
+
basin_prefixes = ['WP', 'EP', 'NA']
|
| 157 |
+
typhoon_names = ['HAIYAN', 'YOLANDA', 'MANGKHUT', 'YUTU', 'HAGIBIS', 'MERANTI',
|
| 158 |
+
'MEGI', 'HAGUPIT', 'MAYSAK', 'HATO', 'NEPARTAK', 'SOUDELOR']
|
| 159 |
+
|
| 160 |
+
# Generate data
|
| 161 |
+
for i in range(n_typhoons):
|
| 162 |
+
basin = np.random.choice(basin_prefixes)
|
| 163 |
+
year = np.random.randint(1980, 2024)
|
| 164 |
+
name = np.random.choice(typhoon_names)
|
| 165 |
+
sid = f"{basin}{year}{i:02d}"
|
| 166 |
+
|
| 167 |
+
# Starting position
|
| 168 |
+
start_lon = np.random.uniform(120, 170)
|
| 169 |
+
start_lat = np.random.uniform(5, 30)
|
| 170 |
+
|
| 171 |
+
# Generate track points
|
| 172 |
+
for j in range(n_points_per_typhoon):
|
| 173 |
+
# Time progression
|
| 174 |
+
date = datetime(year, np.random.randint(6, 11), np.random.randint(1, 28),
|
| 175 |
+
np.random.randint(0, 24))
|
| 176 |
+
date += timedelta(hours=j*6) # 6-hour intervals
|
| 177 |
+
|
| 178 |
+
# Position progression (typically moves northwest in WP)
|
| 179 |
+
lon = start_lon - j * np.random.uniform(0.3, 0.8)
|
| 180 |
+
lat = start_lat + j * np.random.uniform(0.2, 0.5)
|
| 181 |
+
|
| 182 |
+
# Intensity progression (typically intensifies then weakens)
|
| 183 |
+
intensity_factor = min(1.0, j/(n_points_per_typhoon/2)) if j < n_points_per_typhoon/2 else \
|
| 184 |
+
1.0 - min(1.0, (j-n_points_per_typhoon/2)/(n_points_per_typhoon/2))
|
| 185 |
+
wind = np.random.randint(30, 150) * intensity_factor
|
| 186 |
+
pressure = 1010 - (wind * 0.75) # Approximate relationship
|
| 187 |
+
|
| 188 |
+
# Add to data dict
|
| 189 |
+
data['SID'].append(sid)
|
| 190 |
+
data['ISO_TIME'].append(date)
|
| 191 |
+
data['NAME'].append(name)
|
| 192 |
+
data['SEASON'].append(year)
|
| 193 |
+
data['LAT'].append(lat)
|
| 194 |
+
data['LON'].append(lon)
|
| 195 |
+
data['USA_WIND'].append(wind)
|
| 196 |
+
data['USA_PRES'].append(pressure)
|
| 197 |
+
|
| 198 |
+
df = pd.DataFrame(data)
|
| 199 |
+
df.to_csv(TYPHOON_DATA_PATH, index=False)
|
| 200 |
+
return df
|
| 201 |
+
|
| 202 |
+
# Modify load_data function to handle missing data
|
| 203 |
+
def load_data(oni_path, typhoon_path):
|
| 204 |
+
oni_data = pd.DataFrame()
|
| 205 |
+
typhoon_data = pd.DataFrame()
|
| 206 |
+
|
| 207 |
+
# Try to load ONI data, generate sample if not found
|
| 208 |
+
if not os.path.exists(oni_path):
|
| 209 |
+
logging.warning(f"ONI data file not found: {oni_path}")
|
| 210 |
+
logging.info("Generating sample ONI data")
|
| 211 |
+
oni_data = generate_sample_oni_data()
|
| 212 |
+
else:
|
| 213 |
+
try:
|
| 214 |
+
oni_data = pd.read_csv(oni_path)
|
| 215 |
+
except Exception as e:
|
| 216 |
+
logging.error(f"Error loading ONI data: {e}")
|
| 217 |
+
logging.info("Generating sample ONI data")
|
| 218 |
+
oni_data = generate_sample_oni_data()
|
| 219 |
+
|
| 220 |
+
# Try to load Typhoon data, generate sample if not found
|
| 221 |
+
if not os.path.exists(typhoon_path):
|
| 222 |
+
logging.warning(f"Typhoon data file not found: {typhoon_path}")
|
| 223 |
+
logging.info("Generating sample typhoon data")
|
| 224 |
+
typhoon_data = generate_sample_typhoon_data()
|
| 225 |
+
else:
|
| 226 |
+
try:
|
| 227 |
+
typhoon_data = pd.read_csv(typhoon_path, low_memory=False)
|
| 228 |
+
typhoon_data['ISO_TIME'] = pd.to_datetime(typhoon_data['ISO_TIME'], errors='coerce')
|
| 229 |
+
typhoon_data = typhoon_data.dropna(subset=['ISO_TIME'])
|
| 230 |
+
except Exception as e:
|
| 231 |
+
logging.error(f"Error loading typhoon data: {e}")
|
| 232 |
+
logging.info("Generating sample typhoon data")
|
| 233 |
+
typhoon_data = generate_sample_typhoon_data()
|
| 234 |
+
|
| 235 |
+
return oni_data, typhoon_data
|
| 236 |
|
| 237 |
+
# Also update the load_ibtracs_data function to be more robust
|
| 238 |
+
def load_ibtracs_data():
|
| 239 |
+
ibtracs_data = {}
|
| 240 |
+
for basin, filename in BASIN_FILES.items():
|
| 241 |
+
local_path = os.path.join(DATA_PATH, filename)
|
| 242 |
+
try:
|
| 243 |
+
if not os.path.exists(local_path):
|
| 244 |
+
logging.info(f"Downloading {basin} basin file...")
|
| 245 |
+
try:
|
| 246 |
+
response = requests.get(IBTRACS_BASE_URL+filename)
|
| 247 |
+
response.raise_for_status()
|
| 248 |
+
with open(local_path, 'wb') as f:
|
| 249 |
+
f.write(response.content)
|
| 250 |
+
logging.info(f"Downloaded {basin} basin file.")
|
| 251 |
+
except Exception as e:
|
| 252 |
+
logging.error(f"Failed to download {basin} basin file: {e}")
|
| 253 |
+
continue
|
| 254 |
+
|
| 255 |
+
logging.info(f"--> Starting to read in IBTrACS data for basin {basin}")
|
| 256 |
+
try:
|
| 257 |
+
ds = tracks.TrackDataset(source='ibtracs', ibtracs_url=local_path)
|
| 258 |
+
logging.info(f"--> Completed reading in IBTrACS data for basin {basin}")
|
| 259 |
+
ibtracs_data[basin] = ds
|
| 260 |
+
except Exception as e:
|
| 261 |
+
logging.warning(f"Skipping basin {basin} due to error: {e}")
|
| 262 |
+
ibtracs_data[basin] = None
|
| 263 |
+
except Exception as e:
|
| 264 |
+
logging.error(f"Error processing basin {basin}: {e}")
|
| 265 |
+
ibtracs_data[basin] = None
|
| 266 |
+
return ibtracs_data
|
| 267 |
# -----------------------------
|
| 268 |
# ONI and Typhoon Data Functions
|
| 269 |
# -----------------------------
|