Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
visualizer-embed
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Owen Graham
visualizer-embed
Commits
86f7d9c2
Verified
Commit
86f7d9c2
authored
3 years ago
by
Owen Graham
Browse files
Options
Downloads
Patches
Plain Diff
Refactor to allow use of station IDs
parent
f65c1d7f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
visualizer.py
+38
-25
38 additions, 25 deletions
visualizer.py
with
38 additions
and
25 deletions
visualizer.py
+
38
−
25
View file @
86f7d9c2
...
@@ -115,20 +115,18 @@ def get_records():
...
@@ -115,20 +115,18 @@ def get_records():
@app.route
(
'
/record/years
'
)
@app.route
(
'
/record/years
'
)
def
get_record_years
():
def
get_record_years
():
"""
Return a JSON list of years for the selected station.
"""
"""
Return a JSON list of years for the selected station.
"""
name
=
get_query
(
'
name
'
)
station
=
get_station_record
(
get_query
(
'
name
'
))
for
station
in
get_records
():
years
=
[
i
[
'
year
'
]
for
i
in
station
[
'
records
'
]]
if
station
[
'
name
'
]
==
name
:
return
jsonify
(
years
)
years
=
[
i
[
'
year
'
]
for
i
in
station
[
'
records
'
]]
return
jsonify
(
years
)
abort
(
404
)
@app.route
(
'
/plot/boxplot
'
)
@app.route
(
'
/plot/boxplot
'
)
def
plot_boxplot
():
def
plot_boxplot
():
name
=
get_query
(
'
name
'
)
station_id
=
get_query
(
'
name
'
)
year1
=
get_query
(
'
year1
'
,
to
=
year_type
)
year1
=
get_query
(
'
year1
'
,
to
=
year_type
)
year2
=
get_query
(
'
year2
'
,
to
=
year_type
)
year2
=
get_query
(
'
year2
'
,
to
=
year_type
)
meas
=
get_query
(
'
measurement
'
,
to
=
meas_type
)
meas
=
get_query
(
'
measurement
'
,
to
=
meas_type
)
station
=
get_station_record
(
station_id
)
plot_data
=
[]
plot_data
=
[]
...
@@ -142,7 +140,7 @@ def plot_boxplot():
...
@@ -142,7 +140,7 @@ def plot_boxplot():
return
row
[
meas
.
field
]
return
row
[
meas
.
field
]
for
year
in
years
:
for
year
in
years
:
data
=
read_data
(
name
,
year
)
data
=
read_data
(
station
,
year
)
selected_data
=
data
[:,
meas
.
field
]
selected_data
=
data
[:,
meas
.
field
]
plot_data
.
append
(
selected_data
)
plot_data
.
append
(
selected_data
)
...
@@ -165,6 +163,7 @@ def plot_boxplot():
...
@@ -165,6 +163,7 @@ def plot_boxplot():
f
'
Min
{
meas
.
title
}
:
{
minimum
[
meas
.
field
]
}
, Date: (
{
minimum
[
0
]
}
).
'
),
f
'
Min
{
meas
.
title
}
:
{
minimum
[
meas
.
field
]
}
, Date: (
{
minimum
[
0
]
}
).
'
),
fontsize
=
'
small
'
,
fontsize
=
'
small
'
,
)
)
name
=
station
[
'
name
'
]
plt
.
suptitle
(
f
'
{
meas
.
title
}
measurements,
{
name
}
Station,
'
plt
.
suptitle
(
f
'
{
meas
.
title
}
measurements,
{
name
}
Station,
'
f
'
{
start_year
}
-
{
end_year
}
.
'
)
f
'
{
start_year
}
-
{
end_year
}
.
'
)
return
savefig_response
(
fig
)
return
savefig_response
(
fig
)
...
@@ -172,10 +171,11 @@ def plot_boxplot():
...
@@ -172,10 +171,11 @@ def plot_boxplot():
@app.route
(
'
/plot
'
)
@app.route
(
'
/plot
'
)
def
plot
():
def
plot
():
name
=
get_query
(
'
name
'
)
station_id
=
get_query
(
'
name
'
)
year
=
get_query
(
'
year
'
,
to
=
year_type
)
year
=
get_query
(
'
year
'
,
to
=
year_type
)
meas
=
get_query
(
'
measurement
'
,
to
=
meas_type
)
meas
=
get_query
(
'
measurement
'
,
to
=
meas_type
)
data
=
read_data
(
name
,
year
)
station
=
get_station_record
(
station_id
)
data
=
read_data
(
station
,
year
)
fig
,
axes
=
plt
.
subplots
()
fig
,
axes
=
plt
.
subplots
()
fig
.
set_figheight
(
6
)
fig
.
set_figheight
(
6
)
fig
.
set_figwidth
(
12
)
fig
.
set_figwidth
(
12
)
...
@@ -195,6 +195,7 @@ def plot():
...
@@ -195,6 +195,7 @@ def plot():
f
'
Min
{
meas
.
title
}
:
{
minimum
[
meas
.
field
]
}
, Date: (
{
minimum
[
0
]
}
).
'
),
f
'
Min
{
meas
.
title
}
:
{
minimum
[
meas
.
field
]
}
, Date: (
{
minimum
[
0
]
}
).
'
),
fontsize
=
'
small
'
,
fontsize
=
'
small
'
,
)
)
name
=
station
[
'
name
'
]
plt
.
suptitle
(
f
'
{
meas
.
title
}
measurements,
{
name
}
Station,
'
plt
.
suptitle
(
f
'
{
meas
.
title
}
measurements,
{
name
}
Station,
'
f
'
{
data
[
0
][
0
].
year
}
'
)
f
'
{
data
[
0
][
0
].
year
}
'
)
return
savefig_response
(
fig
)
return
savefig_response
(
fig
)
...
@@ -204,16 +205,19 @@ def plot():
...
@@ -204,16 +205,19 @@ def plot():
def
plot_overlay
():
def
plot_overlay
():
num_datasets
=
2
num_datasets
=
2
datasets
=
tuple
(
SimpleNamespace
()
for
_
in
range
(
num_datasets
))
datasets
=
tuple
(
SimpleNamespace
()
for
_
in
range
(
num_datasets
))
stations
=
get_records
()
for
n
,
dset
in
enumerate
(
datasets
,
start
=
1
):
for
n
,
dset
in
enumerate
(
datasets
,
start
=
1
):
dset
.
name
=
get_query
(
f
'
name
{
n
}
'
)
dset
.
station_id
=
get_query
(
f
'
name
{
n
}
'
)
dset
.
year
=
get_query
(
f
'
year
{
n
}
'
,
to
=
year_type
)
dset
.
year
=
get_query
(
f
'
year
{
n
}
'
,
to
=
year_type
)
dset
.
station
=
get_station_record
(
dset
.
station_id
,
stations
=
stations
)
dset
.
name
=
dset
.
station
[
'
name
'
]
meas
=
get_query
(
'
measurement
'
,
to
=
meas_type
)
meas
=
get_query
(
'
measurement
'
,
to
=
meas_type
)
def
ignore_feb_29
(
rows
):
def
ignore_feb_29
(
rows
):
return
[
row
for
row
in
rows
if
(
row
[
0
].
month
,
row
[
0
].
day
)
!=
(
2
,
29
)]
return
[
row
for
row
in
rows
if
(
row
[
0
].
month
,
row
[
0
].
day
)
!=
(
2
,
29
)]
for
dset
in
datasets
:
for
dset
in
datasets
:
raw_data
=
read_data
(
dset
.
name
,
dset
.
year
)
raw_data
=
read_data
(
dset
.
station
,
dset
.
year
)
dset
.
data
=
np
.
array
(
ignore_feb_29
(
raw_data
))
dset
.
data
=
np
.
array
(
ignore_feb_29
(
raw_data
))
fig
,
axes
=
plt
.
subplots
()
fig
,
axes
=
plt
.
subplots
()
...
@@ -292,25 +296,34 @@ def year_type(s):
...
@@ -292,25 +296,34 @@ def year_type(s):
raise
ValueError
(
f
'
bad year arg:
{
s
!r}
'
)
raise
ValueError
(
f
'
bad year arg:
{
s
!r}
'
)
def
get_link
(
name
,
year
):
def
get_link
(
station
,
year
):
"""
Get the link to a dataset.
"""
Get the link to a dataset.
Calls `abort(404)` if there is no link/record for the given station
Calls `abort(404)` if there is no link/record for the given year.
and year.
"""
"""
for
station
in
get_records
():
for
record
in
station
[
'
records
'
]:
if
station
[
'
name
'
]
==
name
:
if
record
[
'
year
'
]
==
year
:
for
record
in
station
[
'
records
'
]:
return
record
[
'
url
'
]
if
record
[
'
year
'
]
==
year
:
return
record
[
'
url
'
]
abort
(
404
)
abort
(
404
)
@app.route
(
'
/record/link
'
)
@app.route
(
'
/record/link
'
)
def
get_record_link
():
def
get_record_link
():
name
=
get_query
(
'
name
'
)
station
=
get_station_record
(
get_query
(
'
name
'
)
)
year
=
get_query
(
'
year
'
,
to
=
year_type
)
year
=
get_query
(
'
year
'
,
to
=
year_type
)
return
jsonify
(
get_link
(
name
,
year
))
return
jsonify
(
get_link
(
station
,
year
))
def
get_station_record
(
station_id
,
stations
=
None
):
"""
Get the station record corresponding to the station ID.
Calls `abort(404)` if there is none.
"""
if
stations
is
None
:
stations
=
get_records
()
for
station
in
stations
:
if
station
[
'
name
'
]
==
station_id
:
return
station
abort
(
404
)
def
get_resources
(
link
):
def
get_resources
(
link
):
...
@@ -331,13 +344,13 @@ def get_resources(link):
...
@@ -331,13 +344,13 @@ def get_resources(link):
yield
url
yield
url
def
read_data
(
name
,
year
):
def
read_data
(
station
,
year
):
daily_data
=
[]
daily_data
=
[]
resource_list
=
get_resources
(
get_link
(
name
,
year
))
resource_list
=
get_resources
(
get_link
(
station
,
year
))
for
url
in
resource_list
:
for
url
in
resource_list
:
with
urlopen
(
url
)
as
data
:
with
urlopen
(
url
)
as
data
:
lines
=
map
(
bytes
.
decode
,
data
)
lines
=
map
(
bytes
.
decode
,
data
)
spec
=
(
SOUTH_POLE_DATA
if
name
==
'
South Pole Station
'
spec
=
(
SOUTH_POLE_DATA
if
station
[
'
name
'
]
==
'
South Pole Station
'
else
ONE_HOUR_DATA
)
else
ONE_HOUR_DATA
)
for
row
in
asccol
.
parse_data
(
lines
,
spec
):
for
row
in
asccol
.
parse_data
(
lines
,
spec
):
date
=
datetime
.
datetime
(
row
.
year
,
row
.
month
,
row
.
day
,
date
=
datetime
.
datetime
(
row
.
year
,
row
.
month
,
row
.
day
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment