Files
NEXRAD_Public/temp.py
2024-05-02 00:09:41 -05:00

62 lines
2.7 KiB
Python

from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS
import requests
from datetime import datetime, timedelta
import pytz
# Fetch data from NOAA Radar API
response = requests.get('https://api.weather.gov/radar/stations/KBIS')
data = response.json()
# Extract relevant information
radar_data = data['properties']
# Convert Zulu time to Central Time
zulu_time = datetime.fromisoformat(radar_data["latency"]["levelTwoLastReceivedTime"].replace("Z", "+00:00"))
zulu_time = zulu_time.replace(tzinfo=pytz.utc)
central_timezone = pytz.timezone('America/Chicago')
central_time = zulu_time.astimezone(central_timezone)
# Calculate data age
current_time = datetime.now(tz=pytz.utc)
current_time = current_time.astimezone(central_timezone)
data_age = (current_time - central_time).total_seconds() / 60 # Convert seconds to minutes
# Prepare data for InfluxDB
measurement = 'radar_station'
time = current_time.isoformat()
# Create InfluxDB client
client = InfluxDBClient(url="http://10.10.3.212:8086", token="TOKEN GOES HERE", org="ORG NAME HERE")
# Create write API
write_api = client.write_api(write_options=SYNCHRONOUS)
# Define tags and fields
tags = {"id": radar_data["id"]}
fields = {
"name": str(radar_data["name"]),
"station_type": str(radar_data["stationType"]),
"elevation": float(radar_data["elevation"]["value"]),
"latency_current": float(radar_data["latency"]["current"]["value"]),
"latency_average": float(radar_data["latency"]["average"]["value"]),
"latency_max": float(radar_data["latency"]["max"]["value"]),
"resolution_version": str(radar_data["rda"]["properties"]["resolutionVersion"]),
"control_status": str(radar_data["rda"]["properties"]["controlStatus"]),
"shelter_temperature": float(radar_data["performance"]["properties"]["shelterTemperature"]["value"]),
"xmtr_temperature": float(radar_data["performance"]["properties"]["transmitterLeavingAirTemperature"]["value"]),
"output_power": float(radar_data["performance"]["properties"]["transmitterPeakPower"]["value"]),
"data_age_minutes": data_age,
# Check if volume_coverage_pattern starts with 'R' or 'L', remove prefix if exists and convert to float
"vcp": float(str(radar_data["rda"]["properties"]["volumeCoveragePattern"])[1:]) if str(radar_data["rda"]["properties"]["volumeCoveragePattern"]).startswith('R') or str(radar_data["rda"]["properties"]["volumeCoveragePattern"]).startswith('L') else float(radar_data["rda"]["properties"]["volumeCoveragePattern"]),
# Add more fields as needed
}
# Write data to InfluxDB
point = Point(measurement).time(time).tag("id", radar_data["id"])
for field, value in fields.items():
point.field(field, value)
write_api.write("BUCKET NAME HERE", "ORG NAME HERE", point)