Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,8 +116,13 @@ regions = {
|
|
| 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
|
| 121 |
years = range(1950, 2024)
|
| 122 |
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
| 123 |
data = {'Year': list(years)}
|
|
@@ -129,17 +134,17 @@ def generate_sample_oni_data():
|
|
| 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
|
| 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': [],
|
|
@@ -197,73 +202,8 @@ def generate_sample_typhoon_data():
|
|
| 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 |
# -----------------------------
|
|
@@ -308,18 +248,16 @@ def update_oni_data():
|
|
| 308 |
os.remove(temp_file)
|
| 309 |
|
| 310 |
def load_data(oni_path, typhoon_path):
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
logging.error(f"Error loading data: {e}")
|
| 322 |
-
return pd.DataFrame(), pd.DataFrame()
|
| 323 |
|
| 324 |
def process_oni_data(oni_data):
|
| 325 |
oni_long = oni_data.melt(id_vars=['Year'], var_name='Month', value_name='ONI')
|
|
|
|
| 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 |
+
# After your imports section but before any other code, add:
|
| 120 |
+
|
| 121 |
+
# -----------------------------
|
| 122 |
+
# Sample Data Generation
|
| 123 |
+
# -----------------------------
|
| 124 |
def generate_sample_oni_data():
|
| 125 |
+
"""Generate sample ONI data"""
|
| 126 |
years = range(1950, 2024)
|
| 127 |
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
| 128 |
data = {'Year': list(years)}
|
|
|
|
| 134 |
|
| 135 |
df = pd.DataFrame(data)
|
| 136 |
df.to_csv(ONI_DATA_PATH, index=False)
|
| 137 |
+
logging.info(f"Generated sample ONI data and saved to {ONI_DATA_PATH}")
|
| 138 |
return df
|
| 139 |
|
| 140 |
def generate_sample_typhoon_data():
|
| 141 |
+
"""Generate sample typhoon data"""
|
| 142 |
# Create sample data with realistic values
|
| 143 |
np.random.seed(42)
|
| 144 |
|
| 145 |
# Generate 100 sample typhoons
|
| 146 |
n_typhoons = 100
|
| 147 |
n_points_per_typhoon = 20
|
|
|
|
| 148 |
|
| 149 |
data = {
|
| 150 |
'SID': [],
|
|
|
|
| 202 |
|
| 203 |
df = pd.DataFrame(data)
|
| 204 |
df.to_csv(TYPHOON_DATA_PATH, index=False)
|
| 205 |
+
logging.info(f"Generated sample typhoon data and saved to {TYPHOON_DATA_PATH}")
|
| 206 |
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
# -----------------------------
|
| 208 |
# ONI and Typhoon Data Functions
|
| 209 |
# -----------------------------
|
|
|
|
| 248 |
os.remove(temp_file)
|
| 249 |
|
| 250 |
def load_data(oni_path, typhoon_path):
|
| 251 |
+
# Always generate sample data for Huggingface Spaces
|
| 252 |
+
logging.info("Generating sample data for Huggingface Spaces")
|
| 253 |
+
oni_data = generate_sample_oni_data()
|
| 254 |
+
typhoon_data = generate_sample_typhoon_data()
|
| 255 |
+
|
| 256 |
+
# Convert ISO_TIME to datetime
|
| 257 |
+
typhoon_data['ISO_TIME'] = pd.to_datetime(typhoon_data['ISO_TIME'], errors='coerce')
|
| 258 |
+
typhoon_data = typhoon_data.dropna(subset=['ISO_TIME'])
|
| 259 |
+
|
| 260 |
+
return oni_data, typhoon_data
|
|
|
|
|
|
|
| 261 |
|
| 262 |
def process_oni_data(oni_data):
|
| 263 |
oni_long = oni_data.melt(id_vars=['Year'], var_name='Month', value_name='ONI')
|