From b794c931e133679e6cd476ca85899b1b08fa8ced Mon Sep 17 00:00:00 2001
From: kgao <kenny.gao@ssec.wisc.edu>
Date: Mon, 15 Aug 2016 20:51:15 +0000
Subject: [PATCH] Added qc variables

---
 aosstower/level_b1/daily/nc.py | 47 ++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)

diff --git a/aosstower/level_b1/daily/nc.py b/aosstower/level_b1/daily/nc.py
index c1b676f..63e6266 100644
--- a/aosstower/level_b1/daily/nc.py
+++ b/aosstower/level_b1/daily/nc.py
@@ -12,6 +12,24 @@ from datetime import timedelta as delta
 
 LOG = logging.getLogger(__name__)
 
+def filterArray(array, valid_min, valid_max):
+    qcControl = []
+
+    for value in array:
+        if value == float(-999):
+            qcControl.append(np.byte(0b1))
+
+        elif value < valid_min:
+            qcControl.append(np.byte(0b10))
+ 
+        elif value > valid_min:
+            qcControl.append(np.byte(0b100))
+ 
+        else:
+            qcControl.append(np.byte(0b0))
+
+    return np.array(qcControl)
+
 # The purpose of this function is to write the dimensions
 # for the nc file
 # no parameters
@@ -109,13 +127,31 @@ def createVariables(ncFile, firstStamp, chunksizes, zlib):
             variable.valid_min = float(varTup[5])
             variable.valid_max = float(varTup[6])
 
+        qcVariable = ncFile.createVariable('qc_' + entry, 'b',
+        dimensions=('time'), fill_value=0b0,  zlib=zlib, chunksizes=chunksizes)
+
+        qcVariable.long_name = 'data quality'
+        qcVariable.valid_range = np.byte(0b1), np.byte(0b1111)
+        qcVariable.flag_masks = np.byte(0b1), np.byte(0b10), np.byte(0b100), np.byte(0b1000)
+
+        flagMeaning = ['value is equal to missing_value', 
+                       'value is less than the valid min', 
+                       'value is greater than the valid max',
+                       'difference between current and previous values exceeds valid_delta.']
+
+        qcVariable.flag_meaning = ', '.join(flagMeaning)
+
     #create global attributes
     ncFile.source = 'surface observation'
     ncFile.conventions = 'ARM-1.2 CF-1.6'
     ncFile.institution = 'UW SSEC'
     ncFile.featureType = 'timeSeries'
-    ncFile.data_level = 'a0'
-    ncFile.datastream = 'aoss.tower.nc.la0.v00'
+    ncFile.data_level = 'b1'
+
+    #monthly files end with .month.nc
+    #these end with .day.nc
+
+    ncFile.datastream = 'aoss.tower.y_nc.b1.v00'
     ncFile.software_version = '00'
 
     #generate history
@@ -255,6 +291,13 @@ def writeVars(ncFile, frame):
 
         dataArray = np.asarray(dataList)
         fileVar[varName][:] = dataArray
+
+        if parser.database[varName][5] != '':
+
+            valid_min = float(parser.database[varName][5])
+            valid_max = float(parser.database[varName][6])
+
+            fileVar['qc_' + varName][:] = filterArray(dataArray, valid_min, valid_max)
     
     return ncFile
 
-- 
GitLab