Skip to content

Commit f375dc7

Browse files
committed
Merge branch 'develop'
2 parents 7300b06 + dd33e95 commit f375dc7

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM python:3.7-slim AS compile-image
22
RUN apt-get update \
3-
&& apt-get install -y build-essential libmariadbclient-dev \
3+
&& apt-get install -y build-essential libmariadbclient-dev-compat \
44
&& rm -rf /var/lib/apt/lists/*
55
COPY requirements.txt .
66
RUN pip install --user -r requirements.txt
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.core.management.base import BaseCommand
2+
from astropy.time import Time
3+
4+
from pyobs_weather.weather.models import GoodWeather
5+
6+
7+
class Command(BaseCommand):
8+
help = 'Dump good weather status to command line as CSV'
9+
10+
def handle(self, *args, **options):
11+
# header
12+
print('datetime,good')
13+
14+
# loop good status
15+
for good in GoodWeather.objects.order_by('time').all():
16+
t = Time(good.time)
17+
print(f'{t.isot},{good.good}')
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from astropy.time import Time
2+
from django.core.management.base import BaseCommand
3+
from django_celery_beat.schedulers import CrontabSchedule, IntervalSchedule, PeriodicTask
4+
5+
from pyobs_weather.weather.models import Station, Value, SensorType
6+
7+
8+
class Command(BaseCommand):
9+
help = 'Init pyobs-weather'
10+
11+
def add_arguments(self, parser):
12+
parser.add_argument('-s', '--start', type=str, help='Start date to dump')
13+
parser.add_argument('-e', '--end', type=str, help='End date to dump')
14+
15+
def handle(self, start: str, end: str, *args, **options):
16+
# get average station
17+
station = Station.objects.filter(code='average').first()
18+
19+
# get all sensor types and print header
20+
sensor_types = [st.code for st in SensorType.objects.all()]
21+
print('datetime,' + ','.join(sensor_types))
22+
23+
# get all timestamps for station in range
24+
query = Value.objects.filter(sensor__station=station)
25+
if start is not None:
26+
query = query.filter(time__gte=start)
27+
if end is not None:
28+
query = query.filter(time__lte=end)
29+
timestamps = query.order_by('time').values_list('time', flat=True)
30+
31+
# now loop all times
32+
for t in timestamps:
33+
# get all values
34+
values = dict(Value.objects.filter(sensor__station=station, time=t)
35+
.values_list('sensor__type__code', 'value').all())
36+
37+
# dump it in correct order
38+
line = Time(t).isot
39+
for st in sensor_types:
40+
line += ','
41+
if st in values and values[st] is not None:
42+
line += str(values[st])
43+
print(line)

pyobs_weather/weather/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class Evaluator(models.Model):
1515
"""A sensor evaluator."""
16+
id = models.AutoField(primary_key=True)
1617
name = models.CharField('Name of evaluator', max_length=15, unique=True)
1718
class_name = models.CharField('Python class to call', max_length=50)
1819
kwargs = models.CharField('JSON encoded kwargs to pass to constructor', max_length=50, blank=True, null=True)
@@ -24,6 +25,7 @@ def __str__(self):
2425

2526
class Station(models.Model):
2627
"""A weather station."""
28+
id = models.AutoField(primary_key=True)
2729
code = models.CharField('Code for weather station', max_length=10, unique=True)
2830
name = models.CharField('Name of weather station', max_length=50)
2931
class_name = models.CharField('Name of Python class to handle station', max_length=100)
@@ -80,6 +82,7 @@ def delete(self, *args, **kwargs):
8082

8183
class SensorType(models.Model):
8284
"""A sensor type."""
85+
id = models.AutoField(primary_key=True)
8386
code = models.CharField('Code for sensor type', max_length=10, unique=True)
8487
name = models.CharField('Name of sensor type', max_length=50)
8588
unit = models.CharField('Unit for value', max_length=10)
@@ -91,6 +94,7 @@ def __str__(self):
9194

9295
class Sensor(models.Model):
9396
"""A sensor."""
97+
id = models.AutoField(primary_key=True)
9498
station = models.ForeignKey(Station, on_delete=models.CASCADE, db_index=True)
9599
type = models.ForeignKey(SensorType, on_delete=models.CASCADE)
96100
evaluators = models.ManyToManyField(Evaluator, blank=True)
@@ -111,6 +115,7 @@ class Meta:
111115

112116
class Value(models.Model):
113117
"""A single value from a sensor."""
118+
id = models.AutoField(primary_key=True)
114119
sensor = models.ForeignKey(Sensor, on_delete=models.CASCADE, db_index=True)
115120
time = models.DateTimeField('Date and time when value was measured', db_index=True)
116121
value = models.FloatField('Measured value', null=True, blank=True)
@@ -129,5 +134,6 @@ class Meta:
129134

130135
class GoodWeather(models.Model):
131136
"""Times of changes from good to bad weather and vice versa."""
137+
id = models.AutoField(primary_key=True)
132138
time = models.DateTimeField('Date and time of status change', db_index=True, auto_now_add=True)
133139
good = models.BooleanField('Weather now good?')

0 commit comments

Comments
 (0)