-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
279 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ | |
build | ||
dist | ||
node_modules | ||
docs/static | ||
docs/timeseries.* | ||
docs/weather.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { modules } from "https://unpkg.com/wq"; | ||
import { components } from "https://unpkg.com/@wq/markdown"; | ||
import { Analyst } from "https://unpkg.com/@wq/analyst"; | ||
|
||
const React = modules.react; | ||
const Code = components.code; | ||
|
||
export default function CodeDetect(props) { | ||
const { children: value } = props; | ||
if (value.includes("// @wq/analyst")) { | ||
const config = parseConfig(value); | ||
if (config) { | ||
return React.createElement(Analyst, config); | ||
} else { | ||
return React.createElement(Code, { | ||
children: "// Error parsing @wq/analyst config\n\n" + value, | ||
}); | ||
} | ||
} else { | ||
return React.createElement(Code, props); | ||
} | ||
} | ||
|
||
function parseConfig(value) { | ||
value = value.replace("// @wq/analyst", "").trim(); | ||
try { | ||
return JSON.parse(value); | ||
} catch { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import unittest | ||
from rest_framework.test import APITestCase | ||
from tests.testapp.models import TimeSeries | ||
from tests.weather.models import Station | ||
from django.core.management import call_command | ||
import pathlib | ||
|
||
|
||
DOCS = pathlib.Path("docs") | ||
|
||
STATIONS = { | ||
"MSP": "USW00014922", | ||
"ATL": "USW00013874", | ||
"LAX": "USW00023174", | ||
} | ||
|
||
class DocsTestCase(APITestCase): | ||
def setUp(self): | ||
data = ( | ||
("2014-01-01", 0.5), | ||
("2014-01-02", 0.4), | ||
("2014-01-03", 0.6), | ||
("2014-01-04", 0.2), | ||
("2014-01-05", 0.1), | ||
) | ||
for date, value in data: | ||
TimeSeries.objects.create(date=date, value=value) | ||
|
||
for name, code in STATIONS.items(): | ||
station = Station.objects.create(name=name, code=code) | ||
station.load_weather() | ||
|
||
def test_docs(self): | ||
call_command('collectstatic', interactive=False) | ||
for url in ( | ||
"timeseries.html", | ||
"timeseries.csv", | ||
"timeseries.json", | ||
"timeseries.xlsx", | ||
"timeseries.png", | ||
"timeseries.svg", | ||
"weather.html", | ||
"weather.csv", | ||
"weather.json", | ||
"weather.xlsx", | ||
"weather.png", | ||
"weather.svg", | ||
): | ||
response = self.client.get(f"/{url}") | ||
path = DOCS / url | ||
path.parent.mkdir(parents=True, exist_ok=True) | ||
path.write_bytes(response.content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Generated by Django 5.0.3 on 2024-04-02 02:59 | ||
|
||
import django.db.models.deletion | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Station", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("name", models.CharField(max_length=50, unique=True)), | ||
("code", models.CharField(max_length=20, unique=True)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name="Weather", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("date", models.DateField(verbose_name="Date")), | ||
( | ||
"tavg", | ||
models.IntegerField(null=True, verbose_name="Average Temp (°F)"), | ||
), | ||
("tmax", models.IntegerField(verbose_name="Max Temp (°F)")), | ||
("tmin", models.IntegerField(verbose_name="Min Temp (°F)")), | ||
("prcp", models.FloatField(verbose_name="Precipitation (in)")), | ||
("snow", models.FloatField(null=True, verbose_name="Snow (in)")), | ||
("snwd", models.FloatField(null=True, verbose_name="Snow Depth (in)")), | ||
( | ||
"station", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.PROTECT, | ||
to="weather.station", | ||
), | ||
), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from django.db import models | ||
import requests | ||
|
||
|
||
DATA_URL = "https://www.ncei.noaa.gov/access/past-weather/{code}/data.csv" | ||
|
||
|
||
class Station(models.Model): | ||
name = models.CharField(max_length=50, unique=True) | ||
code = models.CharField(max_length=20, unique=True) | ||
|
||
def load_weather(self): | ||
response = requests.get(DATA_URL.format(code=self.code)) | ||
|
||
for i, row in enumerate(response.iter_lines(decode_unicode=True)): | ||
if i < 2: | ||
continue | ||
assert row.count(",") == 6 | ||
date, tavg, tmax, tmin, prcp, snow, snwd = row.split(",") | ||
if date < '2020-01-01': | ||
continue | ||
self.weather_set.create( | ||
date=date, | ||
tavg=tavg or None, | ||
tmax=tmax or tavg, | ||
tmin=tmin, | ||
prcp=prcp or None, | ||
snow=snow or None, | ||
snwd=snwd or None, | ||
) | ||
|
||
|
||
class Weather(models.Model): | ||
station = models.ForeignKey(Station, on_delete=models.PROTECT) | ||
date = models.DateField(verbose_name="Date") | ||
tavg = models.IntegerField(verbose_name="Average Temp (°F)", null=True) | ||
tmax = models.IntegerField(verbose_name="Max Temp (°F)") | ||
tmin = models.IntegerField(verbose_name="Min Temp (°F)") | ||
prcp = models.FloatField(verbose_name="Precipitation (in)") | ||
snow = models.FloatField(verbose_name="Snow (in)", null=True) | ||
snwd = models.FloatField(verbose_name="Snow Depth (in)", null=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from rest_framework import serializers | ||
from .models import Weather | ||
|
||
|
||
class WeatherSerializer(serializers.ModelSerializer): | ||
station = serializers.ReadOnlyField(source="station.name", label="Station") | ||
|
||
class Meta: | ||
model = Weather | ||
exclude = ["id"] | ||
pandas_index = ["date"] # Date | ||
pandas_unstacked_header = ["Station"] |
Oops, something went wrong.