Skip to content

Commit 90e7df1

Browse files
cdelinekandersolar
andauthored
Issue #120 Revise NaN pixel handling in clearsky_temperature.py (#274)
* Revise NaN pixel handling in clearsky_temperature.py. Raise specific warning. Fixes #120 * Changelog update * rewrite cs tamb test in pytest style * add test for not-on-land Co-authored-by: Kevin Anderson <[email protected]>
1 parent 7c39f0c commit 90e7df1

File tree

3 files changed

+30
-34
lines changed

3 files changed

+30
-34
lines changed

docs/sphinx/source/changelog/pending.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
************************
22
Pending
3+
34
************************
45

56
API Changes
@@ -32,14 +33,13 @@ Bug fixes
3233
* Unexpected recoveries when using ``method=random_clean`` in the soiling module
3334
have been fixed. (:pull:`199`, :issue:`234`)
3435

36+
* Improved NaN pixel handling in
37+
:py:func:`~rdtools.clearsky_temperature.get_clearsky_tamb` (:pull:`274`).
3538

3639
Testing
3740
-------
3841

3942

40-
Documentation
41-
-------------
42-
4343

4444
Documentation
4545
-------------

rdtools/clearsky_temperature.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ def get_clearsky_tamb(times, latitude, longitude, window_size=40,
8585
day = _get_pixel_value(a, lon_index, lat_index, k, radius)
8686
night = _get_pixel_value(b, lon_index, lat_index, k, radius)
8787

88-
if pd.isnull(day):
89-
day = a[:, lat_index, k]
90-
if pd.isnull(night):
91-
night = a[:, lat_index, k]
92-
9388
ave_day.append(day)
9489
ave_night.append(night)
9590

@@ -118,6 +113,9 @@ def solar_noon_offset(utc_offset):
118113
df['night'].values,
119114
df['day'].values,
120115
df['solar_noon_offset'].values)
116+
if df['Clear Sky Temperature (C)'].isna().any():
117+
warnings.warn("Clear Sky Temperature includes NaNs, "
118+
"possibly invalid Lat/Lon coordinates.", UserWarning)
121119
return df['Clear Sky Temperature (C)']
122120

123121

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
import unittest
1+
import pytest
22
import pandas as pd
33

44
from rdtools.clearsky_temperature import get_clearsky_tamb
55

66

7-
class ClearSkyTemperatureTestCase(unittest.TestCase):
8-
'''Unit tests for clearsky_temperature module'''
7+
@pytest.fixture
8+
def china_cs_tamb():
9+
dt = pd.date_range('2015-01-01', '2015-02-01', freq='15min', tz='Asia/Shanghai')
10+
china_west = get_clearsky_tamb(dt, 37.951721, 80.609843)
11+
china_east = get_clearsky_tamb(dt, 36.693692, 117.699686)
12+
return china_west, china_east
913

10-
def setUp(self):
11-
12-
dt = pd.date_range('2015-01-01', '2015-02-01', freq='15min', tz='Asia/Shanghai')
13-
14-
self.china_west = get_clearsky_tamb(dt, 37.951721, 80.609843)
15-
self.china_east = get_clearsky_tamb(dt, 36.693692, 117.699686)
16-
17-
# self.china_west.to_csv("west.csv")
18-
# self.china_east.to_csv("east.csv")
1914

15+
def test_hour_offset(china_cs_tamb):
2016
# Test for shifting temperature peak with longitude for same timezone
21-
def test_hour_offset(self):
22-
23-
df = pd.DataFrame(index=self.china_west.index)
24-
df['west'] = self.china_west
25-
df['east'] = self.china_east
26-
df['hour'] = df.index.hour
17+
china_west, china_east = china_cs_tamb
2718

28-
west_hottest_hour = df.sort_values(by='west', ascending=False)['hour'].iloc[0]
29-
east_hottest_hour = df.sort_values(by='east', ascending=False)['hour'].iloc[0]
19+
df = pd.DataFrame(index=china_west.index)
20+
df['west'] = china_west
21+
df['east'] = china_east
22+
df['hour'] = df.index.hour
3023

31-
# print west_hottest_hour , east_hottest_hour
24+
west_hottest_hour = df.sort_values(by='west', ascending=False)['hour'].iloc[0]
25+
east_hottest_hour = df.sort_values(by='east', ascending=False)['hour'].iloc[0]
3226

33-
self.assertTrue(west_hottest_hour > 12)
34-
self.assertTrue(east_hottest_hour > 12)
35-
self.assertTrue(west_hottest_hour > east_hottest_hour)
27+
assert west_hottest_hour > 12
28+
assert east_hottest_hour > 12
29+
assert west_hottest_hour > east_hottest_hour
3630

3731

38-
if __name__ == '__main__':
39-
unittest.main()
32+
def test_not_on_land():
33+
# test that specifying a point in the ocean returns NaN and warns
34+
dt = pd.date_range('2015-01-01', freq='15min', periods=1, tz='UTC')
35+
with pytest.warns(UserWarning, match='possibly invalid Lat/Lon coordinates'):
36+
ocean_cs_tamb = get_clearsky_tamb(dt, 40, -60)
37+
assert ocean_cs_tamb.isnull().all()

0 commit comments

Comments
 (0)