euler314 commited on
Commit
063bd99
·
verified ·
1 Parent(s): 35c3de6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -496
app.py CHANGED
@@ -4145,500 +4145,5 @@ initialize_data()
4145
  demo = create_interface()
4146
 
4147
  if __name__ == "__main__":
4148
- demo.launch(share=True) # Enable sharing with public link': current_lat,
4149
- 'lon': current_lon,
4150
- 'intensity_kt': current_intensity,
4151
- 'category': categorize_typhoon_enhanced(current_intensity),
4152
- 'confidence': confidence,
4153
- 'development_stage': stage,
4154
- 'forward_speed_kmh': base_speed * 111, # Convert to km/h
4155
- 'pressure_hpa': max(900, 1013 - (current_intensity - 25) * 0.9),
4156
- 'environmental_limit': environmental_limit,
4157
- 'sst_celsius': current_sst,
4158
- 'slp_hpa': current_slp,
4159
- 'intensity_tendency': intensity_tendency
4160
- })
4161
-
4162
- results['route_forecast'] = route_points
4163
-
4164
- # Enhanced confidence scores with environmental factors
4165
- base_confidence = 0.90 if use_real_data else 0.75
4166
-
4167
- results['confidence_scores'] = {
4168
- 'genesis': base_confidence,
4169
- 'early_development': base_confidence - 0.05,
4170
- 'position_24h': base_confidence - 0.08,
4171
- 'position_48h': base_confidence - 0.15,
4172
- 'position_72h': base_confidence - 0.25,
4173
- 'intensity_24h': (base_confidence - 0.10) if use_real_data else 0.65,
4174
- 'intensity_48h': (base_confidence - 0.20) if use_real_data else 0.55,
4175
- 'intensity_72h': (base_confidence - 0.30) if use_real_data else 0.45,
4176
- 'environmental_coupling': 0.85 if use_real_data else 0.60
4177
- }
4178
-
4179
- # Enhanced model information
4180
- data_sources = []
4181
- if sst_data and sst_data['success']:
4182
- data_sources.append("NOAA OISST v2")
4183
- if slp_data and slp_data['success']:
4184
- data_sources.append("NCEP/NCAR Reanalysis")
4185
-
4186
- if data_sources:
4187
- results['model_info'] = f"Enhanced Oceanic Model using {', '.join(data_sources)}"
4188
- else:
4189
- results['model_info'] = "Enhanced Climatological Model"
4190
-
4191
- logging.info(f"Enhanced prediction complete: {len(route_points)} forecast points")
4192
- return results
4193
-
4194
- except Exception as e:
4195
- logging.error(f"Error in enhanced oceanic prediction: {e}")
4196
- import traceback
4197
- traceback.print_exc()
4198
-
4199
- # Fallback to basic prediction
4200
- return predict_storm_route_and_intensity_realistic(
4201
- genesis_region, month, oni_value, models, forecast_hours, True
4202
- )
4203
-
4204
- def calculate_environmental_steering_speed(lat, lon, month, oni_value, slp_data):
4205
- """Calculate storm forward speed based on environmental steering"""
4206
- base_speed = 0.15 # Default speed in degrees/hour
4207
-
4208
- # Latitude effects
4209
- if lat < 20:
4210
- speed_factor = 0.8 # Slower in tropics
4211
- elif lat < 30:
4212
- speed_factor = 1.2 # Faster in subtropics
4213
- else:
4214
- speed_factor = 1.5 # Fast in mid-latitudes
4215
-
4216
- # Pressure gradient effects (if SLP data available)
4217
- if slp_data and slp_data['success']:
4218
- try:
4219
- # Calculate approximate pressure gradient (simplified)
4220
- slp_value = oceanic_manager.interpolate_data_to_point(slp_data, lat, lon, 'slp')
4221
- if not np.isnan(slp_value):
4222
- slp_hpa = slp_value if slp_value > 500 else slp_value / 100
4223
- if slp_hpa < 1008: # Low pressure - faster motion
4224
- speed_factor *= 1.2
4225
- elif slp_hpa > 1015: # High pressure - slower motion
4226
- speed_factor *= 0.8
4227
- except:
4228
- pass
4229
-
4230
- return base_speed * speed_factor
4231
-
4232
- def calculate_motion_tendency(lat, lon, month, oni_value, hour, slp_data):
4233
- """Calculate motion tendency with environmental steering"""
4234
- # Base climatological motion
4235
- ridge_position = 32 + 4 * np.sin(2 * np.pi * (month - 6) / 4)
4236
-
4237
- if lat < ridge_position - 10:
4238
- base_lat_tendency = 0.05 # Poleward
4239
- base_lon_tendency = -0.12 # Westward
4240
- elif lat > ridge_position - 3:
4241
- base_lat_tendency = 0.15 # Strong poleward (recurvature)
4242
- base_lon_tendency = 0.08 # Eastward
4243
- else:
4244
- base_lat_tendency = 0.08 # Moderate poleward
4245
- base_lon_tendency = -0.06 # Moderate westward
4246
-
4247
- # ENSO steering effects
4248
- if oni_value > 0.5: # El Niño
4249
- base_lon_tendency += 0.03 # More eastward
4250
- base_lat_tendency += 0.01 # Slightly more poleward
4251
- elif oni_value < -0.5: # La Niña
4252
- base_lon_tendency -= 0.04 # More westward
4253
-
4254
- # Add realistic motion uncertainty
4255
- motion_uncertainty = 0.02 + (hour / 120) * 0.03
4256
- lat_noise = np.random.normal(0, motion_uncertainty)
4257
- lon_noise = np.random.normal(0, motion_uncertainty)
4258
-
4259
- return base_lat_tendency + lat_noise, base_lon_tendency + lon_noise
4260
-
4261
- def calculate_environmental_intensity_change(
4262
- current_intensity, environmental_limit, hour, lat, lon, month, oni_value, sst_data
4263
- ):
4264
- """Calculate intensity change based on environmental conditions"""
4265
-
4266
- # Base intensity tendency based on development stage
4267
- if hour <= 48: # Development phase
4268
- if current_intensity < environmental_limit * 0.6:
4269
- base_tendency = 3.5 # Rapid development possible
4270
- elif current_intensity < environmental_limit * 0.8:
4271
- base_tendency = 2.0 # Moderate development
4272
- else:
4273
- base_tendency = 0.5 # Near limit
4274
- elif hour <= 120: # Mature phase
4275
- if current_intensity < environmental_limit:
4276
- base_tendency = 1.0 # Slow intensification
4277
- else:
4278
- base_tendency = -0.5 # Slight weakening
4279
- else: # Extended phase
4280
- base_tendency = -2.0 # General weakening trend
4281
-
4282
- # Environmental limit constraint
4283
- if current_intensity >= environmental_limit:
4284
- base_tendency = min(base_tendency, -1.0) # Force weakening if over limit
4285
-
4286
- # SST effects on development rate
4287
- if sst_data and sst_data['success']:
4288
- try:
4289
- sst_value = oceanic_manager.interpolate_data_to_point(sst_data, lat, lon, 'sst')
4290
- if not np.isnan(sst_value):
4291
- sst_celsius = sst_value if sst_value < 50 else sst_value - 273.15
4292
- if sst_celsius >= 29.5: # Very warm - enhanced development
4293
- base_tendency += 1.5
4294
- elif sst_celsius >= 28.0: # Warm - normal development
4295
- base_tendency += 0.5
4296
- elif sst_celsius < 26.5: # Cool - inhibited development
4297
- base_tendency -= 2.0
4298
- except:
4299
- pass
4300
-
4301
- # Land interaction
4302
- if lon < 110 or (120 < lon < 125 and lat > 20): # Near land masses
4303
- base_tendency -= 8.0
4304
-
4305
- # High latitude weakening
4306
- if lat > 35:
4307
- base_tendency -= 10.0
4308
- elif lat > 30:
4309
- base_tendency -= 4.0
4310
-
4311
- # Add realistic intensity uncertainty
4312
- intensity_noise = np.random.normal(0, 1.0)
4313
-
4314
- return base_tendency + intensity_noise
4315
-
4316
- def calculate_dynamic_confidence(hour, lat, lon, use_real_data, sst_success, slp_success):
4317
- """Calculate dynamic confidence based on data availability and conditions"""
4318
- base_confidence = 0.92
4319
-
4320
- # Time penalty
4321
- time_penalty = (hour / 120) * 0.35
4322
-
4323
- # Data quality bonus
4324
- data_bonus = 0.0
4325
- if use_real_data:
4326
- if sst_success:
4327
- data_bonus += 0.08
4328
- if slp_success:
4329
- data_bonus += 0.05
4330
-
4331
- # Environmental uncertainty
4332
- environment_penalty = 0.0
4333
- if lat > 30 or lon < 115: # Challenging forecast regions
4334
- environment_penalty = 0.12
4335
- elif lat > 25:
4336
- environment_penalty = 0.06
4337
-
4338
- final_confidence = base_confidence + data_bonus - time_penalty - environment_penalty
4339
- return max(0.25, min(0.95, final_confidence))
4340
-
4341
- def get_environmental_development_stage(hour, intensity, environmental_limit):
4342
- """Determine development stage based on time and environmental context"""
4343
- intensity_fraction = intensity / max(environmental_limit, 50)
4344
-
4345
- if hour <= 24:
4346
- return 'Genesis'
4347
- elif hour <= 72:
4348
- if intensity_fraction < 0.3:
4349
- return 'Early Development'
4350
- elif intensity_fraction < 0.6:
4351
- return 'Active Development'
4352
- else:
4353
- return 'Rapid Development'
4354
- elif hour <= 120:
4355
- if intensity_fraction > 0.8:
4356
- return 'Peak Intensity'
4357
- else:
4358
- return 'Mature Stage'
4359
- else:
4360
- return 'Extended Forecast'
4361
-
4362
- def predict_storm_route_and_intensity_realistic(genesis_region, month, oni_value, models=None, forecast_hours=72, use_advanced_physics=True):
4363
- """Realistic prediction with proper typhoon speeds and development"""
4364
- try:
4365
- genesis_locations = get_realistic_genesis_locations()
4366
-
4367
- if genesis_region not in genesis_locations:
4368
- genesis_region = "Western Pacific Main Development Region" # Default
4369
-
4370
- genesis_info = genesis_locations[genesis_region]
4371
- lat = genesis_info["lat"]
4372
- lon = genesis_info["lon"]
4373
-
4374
- results = {
4375
- 'current_prediction': {},
4376
- 'route_forecast': [],
4377
- 'confidence_scores': {},
4378
- 'model_info': 'Realistic Genesis Model',
4379
- 'genesis_info': genesis_info
4380
- }
4381
-
4382
- # REALISTIC starting intensity - Tropical Depression level
4383
- base_intensity = 30 # Start at TD level (25-35 kt)
4384
-
4385
- # Environmental factors for genesis
4386
- if oni_value > 1.0: # Strong El Niño - suppressed development
4387
- intensity_modifier = -6
4388
- elif oni_value > 0.5: # Moderate El Niño
4389
- intensity_modifier = -3
4390
- elif oni_value < -1.0: # Strong La Niña - enhanced development
4391
- intensity_modifier = +8
4392
- elif oni_value < -0.5: # Moderate La Niña
4393
- intensity_modifier = +5
4394
- else: # Neutral
4395
- intensity_modifier = oni_value * 2
4396
-
4397
- # Seasonal genesis effects
4398
- seasonal_factors = {
4399
- 1: -8, 2: -6, 3: -4, 4: -2, 5: 2, 6: 6,
4400
- 7: 10, 8: 12, 9: 15, 10: 10, 11: 4, 12: -5
4401
- }
4402
- seasonal_modifier = seasonal_factors.get(month, 0)
4403
-
4404
- # Genesis region favorability
4405
- region_factors = {
4406
- "Western Pacific Main Development Region": 8,
4407
- "South China Sea": 4,
4408
- "Philippine Sea": 5,
4409
- "Marshall Islands": 7,
4410
- "Monsoon Trough": 6,
4411
- "ITCZ Region": 3,
4412
- "Subtropical Region": 2,
4413
- "Bay of Bengal": 4,
4414
- "Eastern Pacific": 6,
4415
- "Atlantic MDR": 5
4416
- }
4417
- region_modifier = region_factors.get(genesis_region, 0)
4418
-
4419
- # Calculate realistic starting intensity (TD level)
4420
- predicted_intensity = base_intensity + intensity_modifier + seasonal_modifier + region_modifier
4421
- predicted_intensity = max(25, min(40, predicted_intensity)) # Keep in TD-weak TS range
4422
-
4423
- # Add realistic uncertainty for genesis
4424
- intensity_uncertainty = np.random.normal(0, 2)
4425
- predicted_intensity += intensity_uncertainty
4426
- predicted_intensity = max(25, min(38, predicted_intensity)) # TD range
4427
 
4428
- results['current_prediction'] = {
4429
- 'intensity_kt': predicted_intensity,
4430
- 'pressure_hpa': 1008 - (predicted_intensity - 25) * 0.6, # Realistic TD pressure
4431
- 'category': categorize_typhoon_enhanced(predicted_intensity),
4432
- 'genesis_region': genesis_region
4433
- }
4434
-
4435
- # REALISTIC route prediction with proper typhoon speeds
4436
- current_lat = lat
4437
- current_lon = lon
4438
- current_intensity = predicted_intensity
4439
-
4440
- route_points = []
4441
-
4442
- # Track storm development over time with REALISTIC SPEEDS
4443
- for hour in range(0, forecast_hours + 6, 6):
4444
-
4445
- # REALISTIC typhoon motion - much faster speeds
4446
- # Typical typhoon forward speed: 15-25 km/h (0.14-0.23°/hour)
4447
-
4448
- # Base forward speed depends on latitude and storm intensity
4449
- if current_lat < 20: # Low latitude - slower
4450
- base_speed = 0.12 # ~13 km/h
4451
- elif current_lat < 30: # Mid latitude - moderate
4452
- base_speed = 0.18 # ~20 km/h
4453
- else: # High latitude - faster
4454
- base_speed = 0.25 # ~28 km/h
4455
-
4456
- # Intensity affects speed (stronger storms can move faster)
4457
- intensity_speed_factor = 1.0 + (current_intensity - 50) / 200
4458
- base_speed *= max(0.8, min(1.4, intensity_speed_factor))
4459
-
4460
- # Beta drift (Coriolis effect) - realistic values
4461
- beta_drift_lat = 0.02 * np.sin(np.radians(current_lat))
4462
- beta_drift_lon = -0.05 * np.cos(np.radians(current_lat))
4463
-
4464
- # Seasonal steering patterns with realistic speeds
4465
- if month in [6, 7, 8, 9]: # Peak season
4466
- ridge_strength = 1.2
4467
- ridge_position = 32 + 4 * np.sin(2 * np.pi * (month - 6) / 4)
4468
- else: # Off season
4469
- ridge_strength = 0.9
4470
- ridge_position = 28
4471
-
4472
- # REALISTIC motion based on position relative to subtropical ridge
4473
- if current_lat < ridge_position - 10: # Well south of ridge - westward movement
4474
- lat_tendency = base_speed * 0.3 + beta_drift_lat # Slight poleward
4475
- lon_tendency = -base_speed * 0.9 + beta_drift_lon # Strong westward
4476
- elif current_lat > ridge_position - 3: # Near ridge - recurvature
4477
- lat_tendency = base_speed * 0.8 + beta_drift_lat # Strong poleward
4478
- lon_tendency = base_speed * 0.4 + beta_drift_lon # Eastward
4479
- else: # In between - normal WNW motion
4480
- lat_tendency = base_speed * 0.4 + beta_drift_lat # Moderate poleward
4481
- lon_tendency = -base_speed * 0.7 + beta_drift_lon # Moderate westward
4482
-
4483
- # ENSO steering modulation (realistic effects)
4484
- if oni_value > 0.5: # El Niño - more eastward/poleward motion
4485
- lon_tendency += 0.05
4486
- lat_tendency += 0.02
4487
- elif oni_value < -0.5: # La Niña - more westward motion
4488
- lon_tendency -= 0.08
4489
- lat_tendency -= 0.01
4490
-
4491
- # Add motion uncertainty that grows with time (realistic error growth)
4492
- motion_uncertainty = 0.02 + (hour / 120) * 0.04
4493
- lat_noise = np.random.normal(0, motion_uncertainty)
4494
- lon_noise = np.random.normal(0, motion_uncertainty)
4495
-
4496
- # Update position with realistic speeds
4497
- current_lat += lat_tendency + lat_noise
4498
- current_lon += lon_tendency + lon_noise
4499
-
4500
- # REALISTIC intensity evolution with proper development cycles
4501
-
4502
- # Development phase (first 48-72 hours) - realistic intensification
4503
- if hour <= 48:
4504
- if current_intensity < 50: # Still weak - rapid development possible
4505
- if 10 <= current_lat <= 25 and 115 <= current_lon <= 165: # Favorable environment
4506
- intensity_tendency = 4.5 if current_intensity < 35 else 3.0
4507
- elif 120 <= current_lon <= 155 and 15 <= current_lat <= 20: # Best environment
4508
- intensity_tendency = 6.0 if current_intensity < 40 else 4.0
4509
- else:
4510
- intensity_tendency = 2.0
4511
- elif current_intensity < 80: # Moderate intensity
4512
- intensity_tendency = 2.5 if (120 <= current_lon <= 155 and 10 <= current_lat <= 25) else 1.0
4513
- else: # Already strong
4514
- intensity_tendency = 1.0
4515
-
4516
- # Mature phase (48-120 hours) - peak intensity maintenance
4517
- elif hour <= 120:
4518
- if current_lat < 25 and current_lon > 120: # Still in favorable waters
4519
- if current_intensity < 120:
4520
- intensity_tendency = 1.5
4521
- else:
4522
- intensity_tendency = 0.0 # Maintain intensity
4523
- else:
4524
- intensity_tendency = -1.5
4525
-
4526
- # Extended phase (120+ hours) - gradual weakening
4527
- else:
4528
- if current_lat < 30 and current_lon > 115:
4529
- intensity_tendency = -2.0 # Slow weakening
4530
- else:
4531
- intensity_tendency = -3.5 # Faster weakening
4532
-
4533
- # Environmental modulation (realistic effects)
4534
- if current_lat > 35: # High latitude - rapid weakening
4535
- intensity_tendency -= 12
4536
- elif current_lat > 30: # Moderate latitude
4537
- intensity_tendency -= 5
4538
- elif current_lon < 110: # Land interaction
4539
- intensity_tendency -= 15
4540
- elif 125 <= current_lon <= 155 and 10 <= current_lat <= 25: # Warm pool
4541
- intensity_tendency += 2
4542
- elif 160 <= current_lon <= 180 and 15 <= current_lat <= 30: # Still warm
4543
- intensity_tendency += 1
4544
-
4545
- # SST effects (realistic temperature impact)
4546
- if current_lat < 8: # Very warm but weak Coriolis
4547
- intensity_tendency += 0.5
4548
- elif 8 <= current_lat <= 20: # Sweet spot for development
4549
- intensity_tendency += 2.0
4550
- elif 20 < current_lat <= 30: # Marginal
4551
- intensity_tendency -= 1.0
4552
- elif current_lat > 30: # Cool waters
4553
- intensity_tendency -= 4.0
4554
-
4555
- # Shear effects (simplified but realistic)
4556
- if month in [12, 1, 2, 3]: # High shear season
4557
- intensity_tendency -= 2.0
4558
- elif month in [7, 8, 9]: # Low shear season
4559
- intensity_tendency += 1.0
4560
-
4561
- # Update intensity with realistic bounds and variability
4562
- intensity_noise = np.random.normal(0, 1.5) # Small random fluctuations
4563
- current_intensity += intensity_tendency + intensity_noise
4564
- current_intensity = max(20, min(185, current_intensity)) # Realistic range
4565
-
4566
- # Calculate confidence based on forecast time and environment
4567
- base_confidence = 0.92
4568
- time_penalty = (hour / 120) * 0.45
4569
- environment_penalty = 0.15 if current_lat > 30 or current_lon < 115 else 0
4570
- confidence = max(0.25, base_confidence - time_penalty - environment_penalty)
4571
-
4572
- # Determine development stage
4573
- if hour <= 24:
4574
- stage = 'Genesis'
4575
- elif hour <= 72:
4576
- stage = 'Development'
4577
- elif hour <= 120:
4578
- stage = 'Mature'
4579
- elif hour <= 240:
4580
- stage = 'Extended'
4581
- else:
4582
- stage = 'Long-term'
4583
-
4584
- route_points.append({
4585
- 'hour': hour,
4586
- 'lat': current_lat,
4587
- 'lon': current_lon,
4588
- 'intensity_kt': current_intensity,
4589
- 'category': categorize_typhoon_enhanced(current_intensity),
4590
- 'confidence': confidence,
4591
- 'development_stage': stage,
4592
- 'forward_speed_kmh': base_speed * 111, # Convert to km/h
4593
- 'pressure_hpa': max(900, 1013 - (current_intensity - 25) * 0.9)
4594
- })
4595
-
4596
- results['route_forecast'] = route_points
4597
-
4598
- # Realistic confidence scores
4599
- results['confidence_scores'] = {
4600
- 'genesis': 0.88,
4601
- 'early_development': 0.82,
4602
- 'position_24h': 0.85,
4603
- 'position_48h': 0.78,
4604
- 'position_72h': 0.68,
4605
- 'intensity_24h': 0.75,
4606
- 'intensity_48h': 0.65,
4607
- 'intensity_72h': 0.55,
4608
- 'long_term': max(0.3, 0.8 - (forecast_hours / 240) * 0.5)
4609
- }
4610
-
4611
- # Model information
4612
- results['model_info'] = f"Enhanced Realistic Model - {genesis_region}"
4613
-
4614
- return results
4615
-
4616
- except Exception as e:
4617
- logging.error(f"Realistic prediction error: {str(e)}")
4618
- return {
4619
- 'error': f"Prediction error: {str(e)}",
4620
- 'current_prediction': {'intensity_kt': 30, 'category': 'Tropical Depression'},
4621
- 'route_forecast': [],
4622
- 'confidence_scores': {},
4623
- 'model_info': 'Error in prediction'
4624
- }
4625
-
4626
- # Update the existing predict_storm_route_and_intensity_realistic function to use oceanic data
4627
- def predict_storm_route_and_intensity_realistic_enhanced(
4628
- genesis_region, month, oni_value, models=None,
4629
- forecast_hours=72, use_advanced_physics=True
4630
- ):
4631
- """Enhanced wrapper that uses oceanic data when available"""
4632
- return predict_storm_route_and_intensity_with_oceanic_data(
4633
- genesis_region, month, oni_value, forecast_hours,
4634
- use_real_data=True, models=models, enable_animation=True
4635
- )
4636
-
4637
- # Initialize data
4638
- initialize_data()
4639
-
4640
- # Create and launch the interface
4641
- demo = create_interface()
4642
-
4643
- if __name__ == "__main__":
4644
- demo.launch(share=True) # Enable sharing with public link
 
4145
  demo = create_interface()
4146
 
4147
  if __name__ == "__main__":
4148
+ demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4149