From 445bf446fd4aa1f2cd4f0a53a96907590e395356 Mon Sep 17 00:00:00 2001
From: davidh-ssec <david.hoese@ssec.wisc.edu>
Date: Fri, 7 Jul 2017 13:02:35 -0500
Subject: [PATCH] Fix netcdf creation of dewpoint when it isn't in the input
 files

---
 aosstower/level_b1/calc.py | 6 +++++-
 aosstower/level_b1/nc.py   | 5 +++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/aosstower/level_b1/calc.py b/aosstower/level_b1/calc.py
index 1eba391..4db8699 100644
--- a/aosstower/level_b1/calc.py
+++ b/aosstower/level_b1/calc.py
@@ -3,9 +3,11 @@ import math
 import numpy as np
 
 try:
+    import pandas as pd
     from pandas import Series
 except ImportError:
     # expected to use for isinstance
+    pd = None
     Series = np.ndarray
 
 NaN = float('nan')
@@ -33,7 +35,9 @@ def dewpoint(tempC, relhum):
     dp = 1.0 / (1.0 / (273.15 + tempC) - gasconst * np.log((0.0 + relhum) / 100) /
                 (latheat - tempC * 2397.5))
 
-    return min(dp - 273.15, tempC)
+    if pd is not None and isinstance(dp, pd.Series):
+        return pd.concat([dp - 273.15, tempC], axis=1).min(axis=1)
+    return np.min(dp - 273.15, tempC)
 
 
 def relhum(airTempK, dewpointTempK):
diff --git a/aosstower/level_b1/nc.py b/aosstower/level_b1/nc.py
index 7c593ce..0f696a4 100644
--- a/aosstower/level_b1/nc.py
+++ b/aosstower/level_b1/nc.py
@@ -402,6 +402,11 @@ def create_giant_netcdf(input_files, output_fn, zlib, chunk_size,
 
     # Add wind direction components so we can average wind direction properly
     frame['wind_east'], frame['wind_north'], _ = calc.wind_vector_components(frame['wind_speed'], frame['wind_dir'])
+
+    if 'air_temp' in frame and 'rh' in frame and 'dewpoint' in database:
+        LOG.info("'dewpoint' is missing from the input file, will calculate it from air temp and relative humidity")
+        frame['dewpoint'] = calc.dewpoint(frame['air_temp'], frame['rh'])
+
     # round up each 1 minute group so data at time T is the average of data
     # from T - 1 (exclusive) to T (inclusive).
     new_frame = frame.resample('5S', closed='right', loffset='5S').mean()
-- 
GitLab