Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ classifiers=[
"Programming Language :: Python :: 3.11",
]
dependencies = [
"netCDF4>=1.6,<1.7",
"NREL-gaps>=0.8.0,<0.9",
"sphinx-tabs>=3.4.5,<3.4.6",
"sphinx>=8.0.2,<8.1.0",
"NREL-gaps>=0.6.14,<0.8.0",
"NREL-cloud_fs>=0.0.8",
"NREL-farms>=1.0.6",
"scikit-learn>=1.0",
"NREL-rest2>=1.0.2",
"NREL-mlclouds>=0.0.5",
"netCDF4>=1.5.8,<1.7",
"scikit-learn>=1.0",
"matplotlib>=3.1"
]

[project.optional-dependencies]
dev = [
"build>=0.5",
"pre-commit",
"ruff>=0.5.0",
"matplotlib>=3.10.0"
]
doc = [
"sphinx>=7.4.7",
"sphinx>=8.0.2",
"sphinx_rtd_theme>=3.0.2",
"sphinx-click>=5.2.1",
]
Expand Down Expand Up @@ -172,4 +174,4 @@ include-package-data = true
packages = ["nsrdb"]

[tool.setuptools_scm]
version_file = "nsrdb/_version.py"
version_file = "nsrdb/_version.py"
145 changes: 82 additions & 63 deletions tests/gap_fill/test_gap_fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,107 @@

@author: gbuster
"""

from copy import deepcopy

import numpy as np

from nsrdb.gap_fill.cloud_fill import CloudGapFill
from nsrdb.utilities.pytest import execute_pytest

CLOUD_TYPE = np.array([[0, 0, -128, 0, 0, 7],
[1, 1, -15, 0, 0, 7],
[7, 3, -15, 0, 0, 0],
[7, -15, -15, 0, 1, 0],
[3, 8, -15, 5, 1, 7],
[3, 8, -15, 5, 1, 7],
[7, 3, -15, -15, 4, -15],
[7, 3, -15, 5, 4, 7],
])

CLD_OPD_DCOMP = np.array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[71, 43, 0, 0, 0, 0],
[73, 45, 0, 0, 0, 41],
[17, 29, 0, 0, 0, 0],
[14, 21, 0, 0, 0, 0],
], dtype=np.int32)

SZA = np.array([[0, 0, 0, 0, 0, 180],
[0, 0, 0, 0, 0, 180],
[0, 0, 0, 0, 180, 180],
[0, 0, 0, 0, 180, 180],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 180, 0],
[180, 0, 0, 180, 180, 0],
])

OUT_CTYPE = np.array([[0, 0, 0, 0, 0, 7],
[1, 1, 0, 0, 0, 7],
[7, 3, 0, 0, 0, 0],
[7, 3, 0, 0, 1, 0],
[3, 8, 0, 5, 1, 7],
[3, 8, 0, 5, 1, 7],
[7, 3, 0, 5, 4, 7],
[7, 3, 0, 5, 4, 7]], dtype=np.int8)

OUT_PROP = np.array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[17, 29, 0, 0, 0, 0],
[17, 29, 0, 0, 0, 0],
[71, 43, 0, 10, 0, 41],
[73, 45, 0, 10, 0, 41],
[17, 29, 0, 10, 0, 41],
[0, 21, 0, 0, 0, 41]], dtype=np.int32)

OUT_FILL_FLAG = np.array([[0, 0, 2, 0, 0, 0],
[0, 0, 2, 0, 0, 0],
[3, 3, 2, 0, 0, 0],
[3, 1, 2, 0, 0, 0],
[0, 0, 2, 4, 0, 3],
[0, 0, 2, 4, 0, 0],
[0, 0, 2, 1, 0, 1],
[0, 0, 2, 0, 0, 3]], dtype=np.int8)
CLOUD_TYPE = np.array([
[0, 0, -128, 0, 0, 7],
[1, 1, -15, 0, 0, 7],
[7, 3, -15, 0, 0, 0],
[7, -15, -15, 0, 1, 0],
[3, 8, -15, 5, 1, 7],
[3, 8, -15, 5, 1, 7],
[7, 3, -15, -15, 4, -15],
[7, 3, -15, 5, 4, 7],
])

CLD_OPD_DCOMP = np.array(
[
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[71, 43, 0, 0, 0, 0],
[73, 45, 0, 0, 0, 41],
[17, 29, 0, 0, 0, 0],
[14, 21, 0, 0, 0, 0],
],
dtype=np.int32,
)

SZA = np.array([
[0, 0, 0, 0, 0, 180],
[0, 0, 0, 0, 0, 180],
[0, 0, 0, 0, 180, 180],
[0, 0, 0, 0, 180, 180],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 180, 0],
[180, 0, 0, 180, 180, 0],
])

OUT_CTYPE = np.array(
[
[0, 0, 0, 0, 0, 7],
[1, 1, 0, 0, 0, 7],
[7, 3, 0, 0, 0, 0],
[7, 3, 0, 0, 1, 0],
[3, 8, 0, 5, 1, 7],
[3, 8, 0, 5, 1, 7],
[7, 3, 0, 5, 4, 7],
[7, 3, 0, 5, 4, 7],
],
dtype=np.int8,
)

OUT_PROP = np.array(
[
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[17, 29, 0, 0, 0, 0],
[17, 29, 0, 0, 0, 0],
[71, 43, 0, 10, 0, 41],
[73, 45, 0, 10, 0, 41],
[17, 29, 0, 10, 0, 41],
[0, 21, 0, 0, 0, 41],
],
dtype=np.int32,
)

OUT_FILL_FLAG = np.array(
[
[0, 0, 2, 0, 0, 0],
[0, 0, 2, 0, 0, 0],
[3, 3, 2, 0, 0, 0],
[3, 1, 2, 0, 0, 0],
[0, 0, 2, 4, 0, 3],
[0, 0, 2, 4, 0, 0],
[0, 0, 2, 1, 0, 1],
[0, 0, 2, 0, 0, 3],
],
dtype=np.int8,
)


def test_type():
"""Test the cloud property gap fill algorithm."""
cloud_type, fill_flag = CloudGapFill.fill_cloud_type(deepcopy(CLOUD_TYPE))
cloud_type, _ = CloudGapFill.fill_cloud_type(deepcopy(CLOUD_TYPE))
assert np.array_equal(cloud_type, OUT_CTYPE)
return cloud_type, fill_flag


def test_opd():
"""Test the cloud property gap fill algorithm."""
_, fill_flag = CloudGapFill.fill_cloud_type(deepcopy(CLOUD_TYPE))
cloud_prop, fill_flag = CloudGapFill.fill_cloud_prop('cld_opd_dcomp',
CLD_OPD_DCOMP,
CLOUD_TYPE, SZA,
fill_flag=fill_flag)
cloud_prop, fill_flag = CloudGapFill.fill_cloud_prop(
'cld_opd_dcomp', CLD_OPD_DCOMP, CLOUD_TYPE, SZA, fill_flag=fill_flag
)
assert np.array_equal(cloud_prop, OUT_PROP)
assert np.array_equal(fill_flag, OUT_FILL_FLAG)
return cloud_prop, fill_flag


if __name__ == '__main__':
Expand Down
61 changes: 23 additions & 38 deletions tests/test_nearest_neighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
@author: gbuster
"""


import numpy as np
import pandas as pd

Expand All @@ -21,38 +20,27 @@
lat_mesh = lat_mesh.flatten()
source_meta = pd.DataFrame({'latitude': lat_mesh, 'longitude': lon_mesh})

nsrdb_meta = pd.DataFrame(
{'latitude': [1.1, 12.8, 23.3, 43.4, 44.2],
'longitude': [-120.4, -112.3, -101.1, -91.3, -88.4]})

baseline4 = np.array([[[1, -120],
[1, -121],
[2, -120],
[2, -121]],
[[13, -112],
[13, -113],
[12, -112],
[12, -113]],
[[23, -101],
[24, -101],
[23, -102],
[23, -100]],
[[43, -91],
[44, -91],
[43, -92],
[44, -92]],
[[44, -88],
[44, -89],
[45, -88],
[45, -89]]])
nsrdb_meta = pd.DataFrame({
'latitude': [1.1, 12.8, 23.3, 43.4, 44.2],
'longitude': [-120.4, -112.3, -101.1, -91.3, -88.4],
})

baseline4 = np.array([
[[1, -120], [1, -121], [2, -120], [2, -121]],
[[13, -112], [13, -113], [12, -112], [12, -113]],
[[23, -101], [24, -101], [23, -102], [23, -100]],
[[43, -91], [44, -91], [43, -92], [44, -92]],
[[44, -88], [44, -89], [45, -88], [45, -89]],
])

# calculated using Vincenty: https://www.cqsrg.org/tools/GCDistance/
baseline_dist = np.array([45.873,
39.365,
34.766,
50.688,
38.983,
])
baseline_dist = np.array([
45.873,
39.365,
34.766,
50.688,
38.983,
])


def test_geo_haversine_nn():
Expand All @@ -63,20 +51,19 @@ def test_geo_haversine_nn():
result = np.allclose(baseline4, coords_closest)
msg = 'Haversine NN failed!'
assert result, msg
return coords_closest


def test_geo_haversine_dist():
"""Test the geographic haversine nearest neighbor."""

dist, ind = geo_nn(source_meta, nsrdb_meta, k=1)
dist, _ = geo_nn(source_meta, nsrdb_meta, k=1)
dist = dist.flatten()
result = np.allclose(baseline_dist, dist, rtol=0.01, atol=0.0)
diff = np.abs(baseline_dist - dist)
msg = ('Haversine distance failed! '
'\nDist: \n{}\nDiff:\n{}'.format(dist, diff))
msg = 'Haversine distance failed! ' '\nDist: \n{}\nDiff:\n{}'.format(
dist, diff
)
assert result, msg
return dist


def test_knn():
Expand All @@ -87,7 +74,6 @@ def test_knn():
result = np.allclose(baseline4, coords_closest)
msg = 'KNN failed!'
assert result, msg
return coords_closest


def test_regular_grid_nn():
Expand All @@ -98,7 +84,6 @@ def test_regular_grid_nn():
coords_closest_knn = source_meta.values[ind]
msg = 'Regular grid NN failed!'
assert np.allclose(coords_closest_knn, coords_closest_reg), msg
return coords_closest_reg


if __name__ == '__main__':
Expand Down