Skip to content

Commit

Permalink
refactor download data function
Browse files Browse the repository at this point in the history
  • Loading branch information
mathleur committed Oct 10, 2023
1 parent 067abea commit 8f38b53
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 113 deletions.
21 changes: 21 additions & 0 deletions tests/helper_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os

import requests


def download_test_data(nexus_url, filename):
local_directory = "./tests/data"

if not os.path.exists(local_directory):
os.makedirs(local_directory)

# Construct the full path for the local file
local_file_path = os.path.join(local_directory, filename)

if not os.path.exists(local_file_path):
session = requests.Session()
response = session.get(nexus_url)
if response.status_code == 200:
# Save the downloaded data to the local file
with open(local_file_path, "wb") as f:
f.write(response.content)
22 changes: 3 additions & 19 deletions tests/test_datacube_axes_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os

import pytest
import requests
from earthkit import data

from polytope.datacube.backends.xarray import XArrayDatacube
Expand All @@ -10,26 +7,13 @@
from polytope.polytope import Polytope, Request
from polytope.shapes import Box, Select

from .helper_functions import download_test_data


class TestInitDatacubeAxes:
def setup_method(self, method):
nexus_url = "https://get.ecmwf.int/test-data/polytope/test-data/foo.grib"

local_directory = "./tests/data"

if not os.path.exists(local_directory):
os.makedirs(local_directory)

# Construct the full path for the local file
local_file_path = os.path.join(local_directory, "foo.grib")

if not os.path.exists(local_file_path):
session = requests.Session()
response = session.get(nexus_url)
if response.status_code == 200:
# Save the downloaded data to the local file
with open(local_file_path, "wb") as f:
f.write(response.content)
download_test_data(nexus_url, "foo.grib")

ds = data.from_source("file", "./tests/data/foo.grib")
latlon_array = ds.to_xarray().isel(step=0).isel(number=0).isel(surface=0).isel(time=0)
Expand Down
22 changes: 3 additions & 19 deletions tests/test_healpix_mapper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os

import pytest
import requests
from earthkit import data
from eccodes import codes_grib_find_nearest, codes_grib_new_from_file

Expand All @@ -10,26 +7,13 @@
from polytope.polytope import Polytope, Request
from polytope.shapes import Box, Select

from .helper_functions import download_test_data


class TestOctahedralGrid:
def setup_method(self, method):
nexus_url = "https://get.ecmwf.int/test-data/polytope/test-data/healpix.grib"

local_directory = "./tests/data"

if not os.path.exists(local_directory):
os.makedirs(local_directory)

# Construct the full path for the local file
local_file_path = os.path.join(local_directory, "healpix.grib")

if not os.path.exists(local_file_path):
session = requests.Session()
response = session.get(nexus_url)
if response.status_code == 200:
# Save the downloaded data to the local file
with open(local_file_path, "wb") as f:
f.write(response.content)
download_test_data(nexus_url, "healpix.grib")

ds = data.from_source("file", "./tests/data/healpix.grib")
self.latlon_array = ds.to_xarray().isel(step=0).isel(time=0).isel(isobaricInhPa=0).z
Expand Down
22 changes: 3 additions & 19 deletions tests/test_merge_octahedral_one_axis.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
import os

import pytest
import requests
from earthkit import data

from polytope.datacube.backends.xarray import XArrayDatacube
from polytope.engine.hullslicer import HullSlicer
from polytope.polytope import Polytope, Request
from polytope.shapes import Box, Select

from .helper_functions import download_test_data


class TestSlicingMultipleTransformationsOneAxis:
def setup_method(self, method):
nexus_url = "https://get.ecmwf.int/test-data/polytope/test-data/foo.grib"

local_directory = "./tests/data"

if not os.path.exists(local_directory):
os.makedirs(local_directory)

# Construct the full path for the local file
local_file_path = os.path.join(local_directory, "foo.grib")

if not os.path.exists(local_file_path):
session = requests.Session()
response = session.get(nexus_url)
if response.status_code == 200:
# Save the downloaded data to the local file
with open(local_file_path, "wb") as f:
f.write(response.content)
download_test_data(nexus_url, "foo.grib")

ds = data.from_source("file", "./tests/data/foo.grib")
self.latlon_array = ds.to_xarray().isel(step=0).isel(number=0).isel(surface=0).isel(time=0)
Expand Down
22 changes: 3 additions & 19 deletions tests/test_octahedral_grid.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os

import pytest
import requests
from earthkit import data
from eccodes import codes_grib_find_nearest, codes_grib_new_from_file

Expand All @@ -10,26 +7,13 @@
from polytope.polytope import Polytope, Request
from polytope.shapes import Box, Select

from .helper_functions import download_test_data


class TestOctahedralGrid:
def setup_method(self, method):
nexus_url = "https://get.ecmwf.int/test-data/polytope/test-data/foo.grib"

local_directory = "./tests/data"

if not os.path.exists(local_directory):
os.makedirs(local_directory)

# Construct the full path for the local file
local_file_path = os.path.join(local_directory, "foo.grib")

if not os.path.exists(local_file_path):
session = requests.Session()
response = session.get(nexus_url)
if response.status_code == 200:
# Save the downloaded data to the local file
with open(local_file_path, "wb") as f:
f.write(response.content)
download_test_data(nexus_url, "foo.grib")

ds = data.from_source("file", "./tests/data/foo.grib")
self.latlon_array = ds.to_xarray().isel(step=0).isel(number=0).isel(surface=0).isel(time=0)
Expand Down
22 changes: 3 additions & 19 deletions tests/test_slicer_era5.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
import os

import numpy as np
import pytest
import requests
from earthkit import data

from polytope.datacube.backends.xarray import XArrayDatacube
from polytope.engine.hullslicer import HullSlicer
from polytope.polytope import Polytope, Request
from polytope.shapes import Box, Select

from .helper_functions import download_test_data


class TestSlicingEra5Data:
def setup_method(self, method):
nexus_url = "https://get.ecmwf.int/test-data/polytope/test-data/era5-levels-members.grib"

local_directory = "./tests/data"

if not os.path.exists(local_directory):
os.makedirs(local_directory)

# Construct the full path for the local file
local_file_path = os.path.join(local_directory, "era5-levels-members.grib")

if not os.path.exists(local_file_path):
session = requests.Session()
response = session.get(nexus_url)
if response.status_code == 200:
# Save the downloaded data to the local file
with open(local_file_path, "wb") as f:
f.write(response.content)
download_test_data(nexus_url, "era5-levels-members.grib")

ds = data.from_source("file", "./tests/data/era5-levels-members.grib")
array = ds.to_xarray().isel(step=0).t
Expand Down
21 changes: 3 additions & 18 deletions tests/test_snapping_real_data.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
# import geopandas as gpd
# import matplotlib.pyplot as plt
import os

import numpy as np
import pytest
import requests
from earthkit import data

from polytope.datacube.backends.xarray import XArrayDatacube
from polytope.engine.hullslicer import HullSlicer
from polytope.polytope import Polytope, Request
from polytope.shapes import Box, Select

from .helper_functions import download_test_data


class TestSlicingEra5Data:
def setup_method(self, method):
nexus_url = "https://get.ecmwf.int/test-data/polytope/test-data/era5-levels-members.grib"

local_directory = "./tests/data"

if not os.path.exists(local_directory):
os.makedirs(local_directory)

# Construct the full path for the local file
local_file_path = os.path.join(local_directory, "era5-levels-members.grib")

if not os.path.exists(local_file_path):
session = requests.Session()
response = session.get(nexus_url)
if response.status_code == 200:
# Save the downloaded data to the local file
with open(local_file_path, "wb") as f:
f.write(response.content)
download_test_data(nexus_url, "era5-levels-members.grib")

ds = data.from_source("file", "./tests/data/era5-levels-members.grib")
array = ds.to_xarray().isel(step=0).t
Expand Down

0 comments on commit 8f38b53

Please sign in to comment.