Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
toargridding
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
esde
toar-public
toargridding
Commits
ae84d05a
Commit
ae84d05a
authored
11 months ago
by
Simon Grasse
Browse files
Options
Downloads
Patches
Plain Diff
wip status of unit testing, various notebooks
parent
623f9362
No related branches found
No related tags found
1 merge request
!11
Creation of first beta release version
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
tests/__init__.py
+4
-0
4 additions, 0 deletions
tests/__init__.py
tests/conftest.py
+26
-4
26 additions, 4 deletions
tests/conftest.py
tests/get_sample_data.ipynb
+59
-42
59 additions, 42 deletions
tests/get_sample_data.ipynb
tests/test_toar_rest_client.py
+12
-0
12 additions, 0 deletions
tests/test_toar_rest_client.py
with
101 additions
and
46 deletions
tests/__init__.py
+
4
−
0
View file @
ae84d05a
from
toargridding.grids
import
GridDefinition
,
GridType
from
toargridding.toar_rest_client
import
AnalysisService
from
toargridding.metadata
import
TimeSample
,
Metadata
from
toargridding.gridding
import
get_gridded_toar_data
This diff is collapsed.
Click to expand it.
tests/conftest.py
+
26
−
4
View file @
ae84d05a
from
itertools
import
product
from
datetime
import
datetime
from
pathlib
import
Path
import
pytest
from
toargridding.grids
import
RegularGrid
from
toargridding.metadata
import
TimeSample
from
toargridding.metadata
import
TimeSample
,
Metadata
,
TOARVariable
from
toargridding.toar_rest_client
import
AnalysisService
test_data
=
list
((
Path
(
__file__
).
parent
/
"
data
"
).
iterdir
())
TEST_SAMPLINGS
=
[
"
daily
"
]
TEST_STATS
=
[
"
mean
"
]
@pytest.fixture
def
regular_grid
():
...
...
@@ -16,15 +20,33 @@ def regular_grid():
# TODO investigate -1 day discrepancy in time index
@pytest.fixture
def
time
():
@pytest.fixture
(
params
=
TEST_SAMPLINGS
)
def
time
(
request
):
start
=
datetime
(
2009
,
12
,
31
)
end
=
datetime
(
2011
,
1
,
1
)
sampling
=
"
daily
"
sampling
=
request
.
param
return
TimeSample
(
start
,
end
,
sampling
)
@pytest.fixture
def
toar_variable_ozon
():
return
TOARVariable
(
"
o3
"
,
"
ozone
"
,
"
Ozone
"
,
"
mole_fraction_of_ozone_in_air
"
,
"
nmol mol-1
"
,
"
O3
"
,
5
,
)
@pytest.fixture
def
metadata_ozone_mean
(
toar_variable_ozon
,
time
):
return
Metadata
(
toar_variable_ozon
,
"
mean
"
,
time
)
def
load_local_data
(
*
args
,
**
kwargs
):
with
open
(
test_data
[
2
],
"
r+b
"
)
as
f
:
return
f
.
read
()
...
...
This diff is collapsed.
Click to expand it.
tests/get_sample_data.ipynb
+
59
−
42
View file @
ae84d05a
...
...
@@ -6,14 +6,22 @@
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"from datetime import datetime
, timedelta
\n",
"\n",
"sampling = \"monthly\"\n",
"start = datetime(2010,1,1)\n",
"end = datetime(2011,1,1)\n",
"from toargridding.metadata import TimeSample, Metadata\n",
"\n",
"print(start.date(), end.date())\n",
"print(start.isoformat(), end.isoformat())"
"sampling = \"daily\" # FIXME check monthly !!!\n",
"start = datetime(2016, 3, 1)\n",
"end = datetime(2016, 3, 3)\n",
"\n",
"statistics_endpoint = \"https://toar-data.fz-juelich.de/api/v2/analysis/statistics/\"\n",
"statistic = \"mean\"\n",
"\n",
"time = TimeSample(start, end, sampling=sampling)\n",
"metadata = Metadata.construct(\"mole_fraction_of_ozone_in_air\", statistic, time)\n",
"\n",
"start_time = datetime.now()\n",
"print(start_time)"
]
},
{
...
...
@@ -22,21 +30,20 @@
"metadata": {},
"outputs": [],
"source": [
"
import requests
\n",
"
from pathlib import Path
\n",
"\n",
"response = requests.get(\n",
" \"https://toar-data.fz-juelich.de/api/v2/analysis/statistics/\",\n",
" params={\n",
" \"daterange\": f\"{start.isoformat()},{end.isoformat()}\", # 1-year\n",
" \"variable_id\": 5,\n",
" \"statistics\": \"mean\",\n",
" \"sampling\": sampling, # daily sampling\n",
" \"min_data_capture\": 0,\n",
" \"limit\": \"None\", # get all timeseries\n",
" \"format\": \"by_statistic\",\n",
" \"metadata_scheme\": \"basic\"\n",
" }\n",
")"
"from toargridding.toar_rest_client import AnalysisServiceDownload\n",
"\n",
"toargridding_base_path = Path(\"/home/simon/Projects/toar/toargridding/\")\n",
"cache_dir = toargridding_base_path / \"tests\" / \"results\"\n",
"download_dir = toargridding_base_path / \"tests\" / \"data\"\n",
"\n",
"analysis_service = AnalysisServiceDownload(statistics_endpoint, cache_dir, download_dir)\n",
"\n",
"analysis_service.get_data(metadata)\n",
"\n",
"end_time = datetime.now()\n",
"print(end_time-start_time)"
]
},
{
...
...
@@ -45,9 +52,28 @@
"metadata": {},
"outputs": [],
"source": [
"print(response.status_code)\n",
"status_endpoint = response.json()[\"status\"]\n",
"print(status_endpoint)"
"# %%script false --no-error\n",
"import requests\n",
"\n",
"end_with_padding = end + timedelta(1)\n",
"\n",
"response = requests.get(\n",
" statistics_endpoint,\n",
" params={\n",
" \"daterange\": f\"{start.isoformat()},{end_with_padding.isoformat()}\", # 1-year\n",
" \"variable_id\": 5,\n",
" \"statistics\": statistic,\n",
" \"sampling\": sampling,\n",
" \"min_data_capture\": 0,\n",
" \"limit\": \"None\", # get all timeseries\n",
" \"format\": \"by_statistic\",\n",
" \"metadata_scheme\": \"basic\",\n",
" },\n",
")\n",
"\n",
"print(response)\n",
"print(response.headers)\n",
"print(response.json())"
]
},
{
...
...
@@ -56,19 +82,7 @@
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"waiting_for_data = True\n",
"\n",
"start_time = time.time()\n",
"while waiting_for_data:\n",
" time.sleep(30)\n",
" response = requests.get(status_endpoint)\n",
"\n",
" waiting_for_data = (response.headers[\"Content-Type\"] == \"application/json\")\n",
"\n",
"response_time = time.time() - start_time\n",
"print(response_time)"
"response.headers[\"Content-Type\"] == \"application/json\"\n"
]
},
{
...
...
@@ -77,7 +91,13 @@
"metadata": {},
"outputs": [],
"source": [
"response.headers[\"Content-Type\"], response.headers[\"Content-Length\"]"
"import requests\n",
"\n",
"status_endpoint = \"https://toar-data.fz-juelich.de/api/v2/analysis/status/5ec3a54c-322c-4bce-a3f5-fdf485a56514\"\n",
"\n",
"response = requests.get(status_endpoint)\n",
"print(response.headers)\n",
"print(response.json())"
]
},
{
...
...
@@ -85,10 +105,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(f\"data/{sampling}_{start.date()}_{end.date()}.zip\", \"w+b\") as sample_file:\n",
" sample_file.write(response.content)"
]
"source": []
}
],
"metadata": {
...
...
@@ -107,7 +124,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.
6
"
"version": "3.11.
8
"
}
},
"nbformat": 4,
...
...
%% Cell type:code id: tags:
```
python
from
datetime
import
datetime
from
datetime
import
datetime
,
timedelta
sampling
=
"
monthly
"
start
=
datetime
(
2010
,
1
,
1
)
end
=
datetime
(
2011
,
1
,
1
)
from
toargridding.metadata
import
TimeSample
,
Metadata
print
(
start
.
date
(),
end
.
date
())
print
(
start
.
isoformat
(),
end
.
isoformat
())
sampling
=
"
daily
"
# FIXME check monthly !!!
start
=
datetime
(
2016
,
3
,
1
)
end
=
datetime
(
2016
,
3
,
3
)
statistics_endpoint
=
"
https://toar-data.fz-juelich.de/api/v2/analysis/statistics/
"
statistic
=
"
mean
"
time
=
TimeSample
(
start
,
end
,
sampling
=
sampling
)
metadata
=
Metadata
.
construct
(
"
mole_fraction_of_ozone_in_air
"
,
statistic
,
time
)
start_time
=
datetime
.
now
()
print
(
start_time
)
```
%% Cell type:code id: tags:
```
python
from
pathlib
import
Path
from
toargridding.toar_rest_client
import
AnalysisServiceDownload
toargridding_base_path
=
Path
(
"
/home/simon/Projects/toar/toargridding/
"
)
cache_dir
=
toargridding_base_path
/
"
tests
"
/
"
results
"
download_dir
=
toargridding_base_path
/
"
tests
"
/
"
data
"
analysis_service
=
AnalysisServiceDownload
(
statistics_endpoint
,
cache_dir
,
download_dir
)
analysis_service
.
get_data
(
metadata
)
end_time
=
datetime
.
now
()
print
(
end_time
-
start_time
)
```
%% Cell type:code id: tags:
```
python
# %%script false --no-error
import
requests
end_with_padding
=
end
+
timedelta
(
1
)
response
=
requests
.
get
(
"
https://toar-data.fz-juelich.de/api/v2/analysis/statistics/
"
,
statistics_endpoint
,
params
=
{
"
daterange
"
:
f
"
{
start
.
isoformat
()
}
,
{
end
.
isoformat
()
}
"
,
# 1-year
"
daterange
"
:
f
"
{
start
.
isoformat
()
}
,
{
end
_with_padding
.
isoformat
()
}
"
,
# 1-year
"
variable_id
"
:
5
,
"
statistics
"
:
"
mean
"
,
"
sampling
"
:
sampling
,
# daily sampling
"
statistics
"
:
statistic
,
"
sampling
"
:
sampling
,
"
min_data_capture
"
:
0
,
"
limit
"
:
"
None
"
,
# get all timeseries
"
limit
"
:
"
None
"
,
# get all timeseries
"
format
"
:
"
by_statistic
"
,
"
metadata_scheme
"
:
"
basic
"
}
"
metadata_scheme
"
:
"
basic
"
,
}
,
)
print
(
response
)
print
(
response
.
headers
)
print
(
response
.
json
())
```
%% Cell type:code id: tags:
```
python
print
(
response
.
status_code
)
status_endpoint
=
response
.
json
()[
"
status
"
]
print
(
status_endpoint
)
response
.
headers
[
"
Content-Type
"
]
==
"
application/json
"
```
%% Cell type:code id: tags:
```
python
import
time
waiting_for_data
=
True
start_time
=
time
.
time
()
while
waiting_for_data
:
time
.
sleep
(
30
)
response
=
requests
.
get
(
status_endpoint
)
waiting_for_data
=
(
response
.
headers
[
"
Content-Type
"
]
==
"
application/json
"
)
import
requests
response_time
=
time
.
time
()
-
start_time
print
(
response_time
)
```
status_endpoint
=
"
https://toar-data.fz-juelich.de/api/v2/analysis/status/5ec3a54c-322c-4bce-a3f5-fdf485a56514
"
%% Cell type:code id: tags:
```
python
response
.
headers
[
"
Content-Type
"
],
response
.
headers
[
"
Content-Length
"
]
response
=
requests
.
get
(
status_endpoint
)
print
(
response
.
headers
)
print
(
response
.
json
())
```
%% Cell type:code id: tags:
```
python
with
open
(
f
"
data/
{
sampling
}
_
{
start
.
date
()
}
_
{
end
.
date
()
}
.zip
"
,
"
w+b
"
)
as
sample_file
:
sample_file
.
write
(
response
.
content
)
```
...
...
This diff is collapsed.
Click to expand it.
tests/test_toar_rest_client.py
0 → 100644
+
12
−
0
View file @
ae84d05a
from
unittest
import
mock
from
pytest
import
fixture
from
toargridding.toar_rest_client
import
AnalysisService
,
QueryOptions
def
test_query_options_cache_key
(
metadata_ozone_mean
):
q1
=
QueryOptions
.
from_metadata
(
metadata_ozone_mean
)
q2
=
QueryOptions
.
from_metadata
(
metadata_ozone_mean
)
assert
q1
.
cache_key
==
q2
.
cache_key
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