# Path: scripts/constants.py
# D0 = 10.       # specific humidity deficit threshold for Lasslop et al (2010) NEE expression
# R = 8.314      # universal gas constant, J/mol.K
# T0   = -46.02  # zero temp[erature in the Lloyd-Taylor respiration equation, degC
# C2K = 273.15   # convert degrees celsius to kelvin
# 
# Path: scripts/meteorologicalfunctions.py
# def absolutehumidityfromrelativehumidity(Ta,RH):
# def co2_ppmfrommgCO2pm3(c_mgpm3,T,p):
# def co2_mgCO2pm3fromppm(c_ppm,T,p):
# def co2_umolpm3fromppm(c_ppm, T, p):
# def densitydryair(Ta,ps,vp):
# def densitymoistair(Ta,ps,vp):
# def densitywatervapour(Ta,vp):
# def VPsat(T):
# def ET_kgpm2fromkgpm2ps(ET_kgpm2ps, ts):
# def Fco2_gCpm2fromumolpm2ps(Fc_umolpm2ps, ts):
# def Fco2_gCpm2psfromumolpm2ps(Fc_umolpm2ps):
# def Fco2_mgCO2pm2psfromumolpm2ps(Fc_umolpm2ps):
# def Fco2_umolpm2psfromgCpm2(Fc_gCpm2, ts=None):
# def Fco2_umolpm2psfrommgCO2pm2ps(Fc_mgpm2ps):
# def h2o_gpm3frommmolpmol(h_mmpm,T,p):
# def h2o_mmolpmolfromgpm3(h_gpm3,T,p):
# def h2o_mmolpm3fromgpm3(h_gpm3):
# def Lv(Ta):
# def mixingratio(ps,vp):
# def molen(T,AH,p,ustar,heatflux,fluxtype='sensible'):
# def specifichumidityfromrelativehumidity(RH, T, p):
# def SHsat(vpsat, ps):
# def relativehumidityfromabsolutehumidity(AH, Ta):
# def relativehumidityfromdewpoint(Td,Ta):
# def relativehumidityfromspecifichumidity(SH,Ta,ps):
# def densitytimesspecificheat(rhow,Cpw,rhoa,Cpa):
# def specificheatcapacitydryair(Tv):
# def specificheatcapacitywatervapour(Ta, AH):
# def specificheatmoistair(SH):
# def specifichumidity(mr):
# def specifichumidityfromRH(RH, T, p):
# def tafromtv(Tv,SH):
# def tvfromta(Ta, mr):
# def theta(T,p):
# def vapourpressure(AH,Ta):
# def virtualtheta(theta_in, mr):
#     T = T + 273.15             # temperature in K
#     T = T + 273.15             # temperature in K
#     T = T + 273.15             # temperature in K
#         L = -Tvp*densitydryair(T, p, vp)*c.Cp*(ustar**3)/(c.g*c.k*heatflux)
#         L = -Tvp*(ustar**3)/(c.g*c.k*heatflux)
#     SH = (c.Mv / c.Md) * (0.01 * RH * VPsat(T) / p)
#     VP = vapourpressure(AH, Ta)
#     RH = float(100)*VP/VPsat(Ta)
#     if WasND: RH, _ = pfp_utils.MAtoSeries(RH)
#     RH = 100*10**(7.591386*(Td/(Td+240.7263)-Ta/(Ta+240.7263)))
#     if WasND: RH, _ = pfp_utils.MAtoSeries(RH)
#     RH = float(100)*SH*(c.Md/c.Mv)*ps/VPsat(Ta)
#     if WasND: RH, _ = pfp_utils.MAtoSeries(RH)
#     RH = relativehumidityfromabsolutehumidity(AH,Ta)
#     SH = mr/(1+mr)
#     SH = (c.Mv / c.Md) * (0.01 * RH * VPsat(T) / p)


import copy
import datetime
import logging
import numbers
import os
import sys
import time
import dateutil
import numpy
import pytz
import xlrd
    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.backends
from scripts import constants as c
from scripts import meteorologicalfunctions as pfp_mf
        lwr = pfp_mf.h2o_mmolpmolfromgpm3(lwr, Ta, ps)
        attr_out = str(numpy.ma.min(lwr))
    elif len(limits) == 12:
        lwr = numpy.zeros(nrecs)
        attr_list = []
        # set lwr to monthly limits
        for m, l in enumerate(limits):
            idx = numpy.where(month == m+1)[0]
            lwr[idx] = float(l)
            # convert lwr to new units
            lwr[idx] = pfp_mf.h2o_mmolpmolfromgpm3(lwr[idx], Ta[idx], ps[idx])
            attr_list.append(numpy.ma.min(lwr[idx]))
        attr_out = ','.join(str(x) for x in attr_list)
    return attr_out

def convert_units_h2o_upper_mmolpmol(attr_in, Ta, ps, month):
    nrecs = len(Ta)
    limits = parse_rangecheck_limits(attr_in)
    if len(limits) == 1:
        upr = numpy.ma.array(numpy.full(nrecs, float(limits[0])))
        upr = pfp_mf.h2o_mmolpmolfromgpm3(upr, Ta, ps)
        attr_out = str(numpy.ma.max(upr))
    elif len(limits) == 12:
        upr = numpy.zeros(nrecs)
        attr_list = []
        # set upr to monthly limits
        for m, l in enumerate(limits):
            idx = numpy.where(month == m+1)[0]
            upr[idx] = float(l)
            # convert upr to new units

# Path: scripts/constants.py
# D0 = 10.       # specific humidity deficit threshold for Lasslop et al (2010) NEE expression
# R = 8.314      # universal gas constant, J/mol.K
# T0   = -46.02  # zero temp[erature in the Lloyd-Taylor respiration equation, degC
# C2K = 273.15   # convert degrees celsius to kelvin
# 
# Path: scripts/meteorologicalfunctions.py
# def absolutehumidityfromrelativehumidity(Ta,RH):
# def co2_ppmfrommgCO2pm3(c_mgpm3,T,p):
# def co2_mgCO2pm3fromppm(c_ppm,T,p):
# def co2_umolpm3fromppm(c_ppm, T, p):
# def densitydryair(Ta,ps,vp):
# def densitymoistair(Ta,ps,vp):
# def densitywatervapour(Ta,vp):
# def VPsat(T):
# def ET_kgpm2fromkgpm2ps(ET_kgpm2ps, ts):
# def Fco2_gCpm2fromumolpm2ps(Fc_umolpm2ps, ts):
# def Fco2_gCpm2psfromumolpm2ps(Fc_umolpm2ps):
# def Fco2_mgCO2pm2psfromumolpm2ps(Fc_umolpm2ps):
# def Fco2_umolpm2psfromgCpm2(Fc_gCpm2, ts=None):
# def Fco2_umolpm2psfrommgCO2pm2ps(Fc_mgpm2ps):
# def h2o_gpm3frommmolpmol(h_mmpm,T,p):
# def h2o_mmolpmolfromgpm3(h_gpm3,T,p):
# def h2o_mmolpm3fromgpm3(h_gpm3):
# def Lv(Ta):
# def mixingratio(ps,vp):
# def molen(T,AH,p,ustar,heatflux,fluxtype='sensible'):
# def specifichumidityfromrelativehumidity(RH, T, p):
# def SHsat(vpsat, ps):
# def relativehumidityfromabsolutehumidity(AH, Ta):
# def relativehumidityfromdewpoint(Td,Ta):
# def relativehumidityfromspecifichumidity(SH,Ta,ps):
# def densitytimesspecificheat(rhow,Cpw,rhoa,Cpa):
# def specificheatcapacitydryair(Tv):
# def specificheatcapacitywatervapour(Ta, AH):
# def specificheatmoistair(SH):
# def specifichumidity(mr):
# def specifichumidityfromRH(RH, T, p):
# def tafromtv(Tv,SH):
# def tvfromta(Ta, mr):
# def theta(T,p):
# def vapourpressure(AH,Ta):
# def virtualtheta(theta_in, mr):
#     T = T + 273.15             # temperature in K
#     T = T + 273.15             # temperature in K
#     T = T + 273.15             # temperature in K
#         L = -Tvp*densitydryair(T, p, vp)*c.Cp*(ustar**3)/(c.g*c.k*heatflux)
#         L = -Tvp*(ustar**3)/(c.g*c.k*heatflux)
#     SH = (c.Mv / c.Md) * (0.01 * RH * VPsat(T) / p)
#     VP = vapourpressure(AH, Ta)
#     RH = float(100)*VP/VPsat(Ta)
#     if WasND: RH, _ = pfp_utils.MAtoSeries(RH)
#     RH = 100*10**(7.591386*(Td/(Td+240.7263)-Ta/(Ta+240.7263)))
#     if WasND: RH, _ = pfp_utils.MAtoSeries(RH)
#     RH = float(100)*SH*(c.Md/c.Mv)*ps/VPsat(Ta)
#     if WasND: RH, _ = pfp_utils.MAtoSeries(RH)
#     RH = relativehumidityfromabsolutehumidity(AH,Ta)
#     SH = mr/(1+mr)
#     SH = (c.Mv / c.Md) * (0.01 * RH * VPsat(T) / p)


# Path: scripts/pfp_utils.py
import copy
import datetime
import logging
import numbers
import os
import sys
import time
import dateutil
import numpy
import pytz
import xlrd
    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.backends
from scripts import constants as c
from scripts import meteorologicalfunctions as pfp_mf

        lwr = pfp_mf.h2o_mmolpmolfromgpm3(lwr, Ta, ps)
        attr_out = str(numpy.ma.min(lwr))
    elif len(limits) == 12:
        lwr = numpy.zeros(nrecs)
        attr_list = []
        # set lwr to monthly limits
        for m, l in enumerate(limits):
            idx = numpy.where(month == m+1)[0]
            lwr[idx] = float(l)
            # convert lwr to new units
            lwr[idx] = pfp_mf.h2o_mmolpmolfromgpm3(lwr[idx], Ta[idx], ps[idx])
            attr_list.append(numpy.ma.min(lwr[idx]))
        attr_out = ','.join(str(x) for x in attr_list)
    return attr_out

def convert_units_h2o_upper_mmolpmol(attr_in, Ta, ps, month):
    nrecs = len(Ta)
    limits = parse_rangecheck_limits(attr_in)
    if len(limits) == 1:
        upr = numpy.ma.array(numpy.full(nrecs, float(limits[0])))
        upr = pfp_mf.h2o_mmolpmolfromgpm3(upr, Ta, ps)
        attr_out = str(numpy.ma.max(upr))
    elif len(limits) == 12:
        upr = numpy.zeros(nrecs)
        attr_list = []
        # set upr to monthly limits
        for m, l in enumerate(limits):
            idx = numpy.where(month == m+1)[0]
            upr[idx] = float(l)
            # convert upr to new units

            upr[idx] = pfp_mf.h2o_mmolpmolfromgpm3(upr[idx], Ta[idx], ps[idx])