NWPS_SWAN / read_arctic_data.py
nakas's picture
PYGRIB SETUP: System dependencies and test suite
a0faaf6
Raw
History Blame Contribute Delete
4.99 kB
#!/usr/bin/env python3
"""
Read and analyze already extracted Arctic GRIB coordinate data
"""
import numpy as np
import os
def read_extracted_arctic_data():
"""Read the Arctic coordinate data that was already extracted."""
base_path = "/Users/nakas/Documents/pythonGribExtraction"
# File paths for the extracted data
data_file = f"{base_path}/wave_data_Significant_height_of_combined_wind_waves_and_swell_data.npy"
lats_file = f"{base_path}/wave_data_Significant_height_of_combined_wind_waves_and_swell_lats.npy"
lons_file = f"{base_path}/wave_data_Significant_height_of_combined_wind_waves_and_swell_lons.npy"
print("🧪 Reading extracted Arctic GRIB coordinate data")
print("=" * 60)
# Check if files exist
for filepath in [data_file, lats_file, lons_file]:
if not os.path.exists(filepath):
print(f"❌ File not found: {filepath}")
return
print(f"✅ Found: {os.path.basename(filepath)}")
try:
# Load the numpy arrays
print("\n📂 Loading extracted arrays...")
wave_data = np.load(data_file)
lats = np.load(lats_file)
lons = np.load(lons_file)
print(f"✅ Arrays loaded successfully")
print(f"📏 Data shape: {wave_data.shape}")
print(f"📏 Coordinates shape: lats={lats.shape}, lons={lons.shape}")
# Validate data ranges
print(f"\n📊 Data validation:")
print(f"Wave height range: {np.nanmin(wave_data):.3f} to {np.nanmax(wave_data):.3f} m")
print(f"Latitude range: {np.nanmin(lats):.2f} to {np.nanmax(lats):.2f}°")
print(f"Longitude range: {np.nanmin(lons):.2f} to {np.nanmax(lons):.2f}°")
# Check for valid data
valid_mask = ~np.isnan(wave_data) & ~np.isnan(lats) & ~np.isnan(lons)
valid_count = np.sum(valid_mask)
total_count = wave_data.size
print(f"\n🔍 Data quality:")
print(f"Total grid points: {total_count:,}")
print(f"Valid data points: {valid_count:,}")
print(f"Valid percentage: {(valid_count/total_count)*100:.1f}%")
# Arctic region filter (lat >= 50°N)
arctic_mask = (lats >= 50.0) & (lats <= 85.0) & valid_mask
arctic_count = np.sum(arctic_mask)
print(f"\n🧊 Arctic region (50°-85°N):")
print(f"Arctic data points: {arctic_count:,}")
if arctic_count > 0:
arctic_lats = lats[arctic_mask]
arctic_lons = lons[arctic_mask]
arctic_waves = wave_data[arctic_mask]
print(f"Arctic lat range: {arctic_lats.min():.2f} to {arctic_lats.max():.2f}°")
print(f"Arctic lon range: {arctic_lons.min():.2f} to {arctic_lons.max():.2f}°")
print(f"Arctic wave range: {arctic_waves.min():.3f} to {arctic_waves.max():.3f} m")
# Sample some Arctic points
print(f"\n🎯 Sample Arctic coordinate points:")
sample_indices = np.random.choice(len(arctic_lats), min(10, len(arctic_lats)), replace=False)
for i, idx in enumerate(sample_indices):
print(f" {i+1:2d}. Lat: {arctic_lats[idx]:7.2f}°, Lon: {arctic_lons[idx]:8.2f}°, Wave: {arctic_waves[idx]:.3f}m")
# Create a sample dataset for your app
print(f"\n💾 Creating sample dataset...")
sample_size = min(1000, arctic_count) # Sample 1000 points
sample_indices = np.random.choice(arctic_count, sample_size, replace=False)
sample_data = []
for idx in sample_indices:
sample_data.append({
'latitude': float(arctic_lats[idx]),
'longitude': float(arctic_lons[idx]),
'value': float(arctic_waves[idx]),
'parameter': 'Significant height of combined wind waves and swell',
'parameter_type': 'wave_height'
})
# Save sample as simple text format
output_file = "/tmp/arctic_sample_coordinates.txt"
with open(output_file, 'w') as f:
f.write("latitude,longitude,wave_height\n")
for point in sample_data:
f.write(f"{point['latitude']:.6f},{point['longitude']:.6f},{point['value']:.6f}\n")
print(f"Sample data saved to: {output_file}")
print(f"\n✅ Arctic coordinate data analysis COMPLETE!")
print(f"You have {arctic_count:,} real Arctic coordinate points ready to use")
return sample_data
else:
print("❌ No Arctic data points found")
except Exception as e:
print(f"❌ Failed to read extracted data: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
read_extracted_arctic_data()