Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,12 +12,55 @@ def get_raw_data(station_id):
|
|
| 12 |
"""
|
| 13 |
Get raw data from multiple endpoints to find snow depth data
|
| 14 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Try standard observations endpoint
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
headers = {
|
| 23 |
'User-Agent': '(Weather Data Viewer, contact@yourdomain.com)',
|
|
@@ -89,12 +132,37 @@ def parse_raw_data(data):
|
|
| 89 |
for feature in data['features']:
|
| 90 |
props = feature['properties']
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
record = {
|
| 100 |
'timestamp': props['timestamp'],
|
|
@@ -298,4 +366,4 @@ with gr.Blocks() as demo:
|
|
| 298 |
# Launch the app
|
| 299 |
demo.launch()
|
| 300 |
|
| 301 |
-
#
|
|
|
|
| 12 |
"""
|
| 13 |
Get raw data from multiple endpoints to find snow depth data
|
| 14 |
"""
|
| 15 |
+
headers = {
|
| 16 |
+
'User-Agent': '(Weather Data Viewer, contact@yourdomain.com)',
|
| 17 |
+
'Accept': 'application/json'
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
# Calculate correct date range for last 3 days
|
| 21 |
+
end_time = datetime.utcnow()
|
| 22 |
+
start_time = end_time - timedelta(hours=72) # Last 3 days
|
| 23 |
+
|
| 24 |
+
params = {
|
| 25 |
+
'start': start_time.isoformat() + 'Z',
|
| 26 |
+
'end': end_time.isoformat() + 'Z'
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
# Try standard observations endpoint
|
| 30 |
+
url = f"https://api.weather.gov/stations/{station_id}/observations"
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
print("\nFetching observations...")
|
| 34 |
+
print(f"URL: {url}")
|
| 35 |
+
print(f"Time range: {start_time} to {end_time}")
|
| 36 |
+
response = requests.get(url, headers=headers, params=params)
|
| 37 |
+
print(f"Response status: {response.status_code}")
|
| 38 |
+
|
| 39 |
+
if response.status_code != 200:
|
| 40 |
+
print(f"Response content: {response.text}")
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
|
| 43 |
+
data = response.json()
|
| 44 |
+
|
| 45 |
+
if 'features' in data:
|
| 46 |
+
print(f"\nNumber of observations: {len(data['features'])}")
|
| 47 |
+
if len(data['features']) > 0:
|
| 48 |
+
print("\nFirst observation properties:")
|
| 49 |
+
print(json.dumps(data['features'][0]['properties'], indent=2))
|
| 50 |
+
|
| 51 |
+
print("\nAll available property keys:")
|
| 52 |
+
keys = set()
|
| 53 |
+
for feature in data['features']:
|
| 54 |
+
keys.update(feature['properties'].keys())
|
| 55 |
+
print(sorted(list(keys)))
|
| 56 |
+
|
| 57 |
+
return data
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error fetching data: {e}")
|
| 60 |
+
print("Full error details:")
|
| 61 |
+
import traceback
|
| 62 |
+
traceback.print_exc()
|
| 63 |
+
return None
|
| 64 |
|
| 65 |
headers = {
|
| 66 |
'User-Agent': '(Weather Data Viewer, contact@yourdomain.com)',
|
|
|
|
| 132 |
for feature in data['features']:
|
| 133 |
props = feature['properties']
|
| 134 |
|
| 135 |
+
# Extract all properties starting with 'snow'
|
| 136 |
+
snow_fields = {k: v for k, v in props.items() if 'snow' in k.lower()}
|
| 137 |
+
if snow_fields:
|
| 138 |
+
print("\nFound snow-related fields:")
|
| 139 |
+
for k, v in snow_fields.items():
|
| 140 |
+
print(f"{k}: {v}")
|
| 141 |
|
| 142 |
+
record = {
|
| 143 |
+
'timestamp': props['timestamp'],
|
| 144 |
+
'temperature': props.get('temperature', {}).get('value'),
|
| 145 |
+
'wind_speed': props.get('windSpeed', {}).get('value'),
|
| 146 |
+
'wind_direction': props.get('windDirection', {}).get('value')
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
# Add all snow-related fields to the record
|
| 150 |
+
for k, v in snow_fields.items():
|
| 151 |
+
if isinstance(v, dict) and 'value' in v:
|
| 152 |
+
record[k] = v['value']
|
| 153 |
+
else:
|
| 154 |
+
record[k] = v
|
| 155 |
+
|
| 156 |
+
records.append(record)
|
| 157 |
+
|
| 158 |
+
df = pd.DataFrame(records)
|
| 159 |
+
|
| 160 |
+
print("\nDataFrame columns:")
|
| 161 |
+
print(df.columns.tolist())
|
| 162 |
+
print("\nSample of raw data:")
|
| 163 |
+
print(df.head())
|
| 164 |
+
|
| 165 |
+
return df
|
| 166 |
|
| 167 |
record = {
|
| 168 |
'timestamp': props['timestamp'],
|
|
|
|
| 366 |
# Launch the app
|
| 367 |
demo.launch()
|
| 368 |
|
| 369 |
+
#
|