euler314 commited on
Commit
5cfd2b7
·
verified ·
1 Parent(s): 76771b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -30
app.py CHANGED
@@ -162,55 +162,72 @@ def update_oni_data():
162
  os.remove(temp_file)
163
 
164
  def load_data(oni_path, typhoon_path):
165
- # Create default empty DataFrames
166
  oni_data = pd.DataFrame({'Year': [], 'Jan': [], 'Feb': [], 'Mar': [], 'Apr': [],
167
  'May': [], 'Jun': [], 'Jul': [], 'Aug': [], 'Sep': [],
168
  'Oct': [], 'Nov': [], 'Dec': []})
169
- typhoon_data = pd.DataFrame()
170
 
171
- # Try to load ONI data
172
- if os.path.exists(oni_path):
173
- try:
174
- oni_data = pd.read_csv(oni_path)
175
- except Exception as e:
176
- logging.error(f"Error loading ONI data: {e}")
177
- # Update ONI data if loading fails
178
- update_oni_data()
179
- try:
180
- oni_data = pd.read_csv(oni_path)
181
- except Exception as e:
182
- logging.error(f"Still can't load ONI data: {e}")
183
- else:
184
  logging.warning(f"ONI data file not found: {oni_path}")
185
- # Try to download ONI data
 
 
 
 
 
186
  update_oni_data()
187
  try:
188
  oni_data = pd.read_csv(oni_path)
189
  except Exception as e:
190
- logging.error(f"Failed to create ONI data: {e}")
191
 
192
- # Try to load typhoon data
193
  if os.path.exists(typhoon_path):
194
  try:
195
  typhoon_data = pd.read_csv(typhoon_path, low_memory=False)
196
  typhoon_data['ISO_TIME'] = pd.to_datetime(typhoon_data['ISO_TIME'], errors='coerce')
197
  typhoon_data = typhoon_data.dropna(subset=['ISO_TIME'])
 
 
 
198
  except Exception as e:
199
  logging.error(f"Error loading typhoon data: {e}")
 
200
  else:
201
  logging.error(f"Typhoon data file not found: {typhoon_path}")
202
- # Create minimal sample data to prevent crashes
203
- typhoon_data = pd.DataFrame({
204
- 'SID': ['WP012000'],
205
- 'ISO_TIME': [pd.Timestamp('2000-01-01')],
206
- 'NAME': ['SAMPLE'],
207
- 'SEASON': [2000],
208
- 'LAT': [20.0],
209
- 'LON': [140.0],
210
- 'USA_WIND': [50.0],
211
- 'USA_PRES': [990.0]
212
- })
213
- logging.warning("Created minimal sample typhoon data to prevent crashes")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
  return oni_data, typhoon_data
216
 
 
162
  os.remove(temp_file)
163
 
164
  def load_data(oni_path, typhoon_path):
165
+ # Create default empty DataFrames with minimum structure
166
  oni_data = pd.DataFrame({'Year': [], 'Jan': [], 'Feb': [], 'Mar': [], 'Apr': [],
167
  'May': [], 'Jun': [], 'Jul': [], 'Aug': [], 'Sep': [],
168
  'Oct': [], 'Nov': [], 'Dec': []})
 
169
 
170
+ # Try to load ONI data or create it
171
+ if not os.path.exists(oni_path):
 
 
 
 
 
 
 
 
 
 
 
172
  logging.warning(f"ONI data file not found: {oni_path}")
173
+ update_oni_data()
174
+
175
+ try:
176
+ oni_data = pd.read_csv(oni_path)
177
+ except Exception as e:
178
+ logging.error(f"Error loading ONI data: {e}")
179
  update_oni_data()
180
  try:
181
  oni_data = pd.read_csv(oni_path)
182
  except Exception as e:
183
+ logging.error(f"Still can't load ONI data: {e}")
184
 
185
+ # For typhoon data, focus on getting WP data
186
  if os.path.exists(typhoon_path):
187
  try:
188
  typhoon_data = pd.read_csv(typhoon_path, low_memory=False)
189
  typhoon_data['ISO_TIME'] = pd.to_datetime(typhoon_data['ISO_TIME'], errors='coerce')
190
  typhoon_data = typhoon_data.dropna(subset=['ISO_TIME'])
191
+ # Log WP data count
192
+ wp_count = len(typhoon_data[typhoon_data['SID'].str.startswith('WP')])
193
+ logging.info(f"Loaded {wp_count} Western Pacific typhoon records")
194
  except Exception as e:
195
  logging.error(f"Error loading typhoon data: {e}")
196
+ typhoon_data = pd.DataFrame()
197
  else:
198
  logging.error(f"Typhoon data file not found: {typhoon_path}")
199
+ # Download WP typhoon data directly from IBTrACS if available
200
+ try:
201
+ if LOCAL_IBTRACS_PATH and os.path.exists(LOCAL_IBTRACS_PATH):
202
+ logging.info("Loading WP data from local IBTrACS file")
203
+ wp_data = pd.read_csv(LOCAL_IBTRACS_PATH, low_memory=False)
204
+ typhoon_data = wp_data
205
+ logging.info(f"Loaded {len(typhoon_data)} WP records from IBTrACS")
206
+ else:
207
+ # Try to download WP file if not exists
208
+ logging.info("Downloading WP basin file...")
209
+ response = requests.get(IBTRACS_BASE_URL + BASIN_FILES['WP'])
210
+ if response.status_code == 200:
211
+ os.makedirs(os.path.dirname(LOCAL_IBTRACS_PATH), exist_ok=True)
212
+ with open(LOCAL_IBTRACS_PATH, 'wb') as f:
213
+ f.write(response.content)
214
+ wp_data = pd.read_csv(LOCAL_IBTRACS_PATH, low_memory=False)
215
+ typhoon_data = wp_data
216
+ logging.info(f"Downloaded and loaded {len(typhoon_data)} WP records")
217
+ except Exception as e:
218
+ logging.error(f"Failed to load or download WP data: {e}")
219
+ # Create minimal WP sample data to prevent crashes
220
+ typhoon_data = pd.DataFrame({
221
+ 'SID': ['WP012000', 'WP022000', 'WP032000'],
222
+ 'ISO_TIME': [pd.Timestamp('2000-01-01'), pd.Timestamp('2000-02-01'), pd.Timestamp('2000-03-01')],
223
+ 'NAME': ['SAMPLE_WP1', 'SAMPLE_WP2', 'SAMPLE_WP3'],
224
+ 'SEASON': [2000, 2000, 2000],
225
+ 'LAT': [20.0, 21.0, 22.0],
226
+ 'LON': [140.0, 141.0, 142.0],
227
+ 'USA_WIND': [50.0, 60.0, 70.0],
228
+ 'USA_PRES': [990.0, 980.0, 970.0]
229
+ })
230
+ logging.warning("Created minimal Western Pacific sample data to prevent crashes")
231
 
232
  return oni_data, typhoon_data
233