# 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
            limits = [(l/float(1000)) for l in limits]
            variable["Attr"]["rangecheck_upper"] = ','.join(str(l) for l in limits)
    elif variable["Attr"]["units"] == "hPa" and new_units == "kPa":
        variable["Data"] = variable["Data"]/float(10)
        variable["Attr"]["units"] = "kPa"
        if "rangecheck_lower" in variable["Attr"]:
            attr_in = variable["Attr"]["rangecheck_lower"]
            limits = parse_rangecheck_limits(attr_in)
            limits = [(l/float(10)) for l in limits]
            variable["Attr"]["rangecheck_lower"] = ','.join(str(l) for l in limits)
        if "rangecheck_upper" in variable["Attr"]:
            attr_in = variable["Attr"]["rangecheck_upper"]
            limits = parse_rangecheck_limits(attr_in)
            limits = [(l/float(10)) for l in limits]
            variable["Attr"]["rangecheck_upper"] = ','.join(str(l) for l in limits)
    else:
        msg = " Unrecognised conversion from " + variable["Attr"]["units"] + " to " + new_units
        logger.error(msg)
    return variable

def convert_anglestring(anglestring):
    """
    Purpose:
     Attempt to convert an angle string to a float.
    Usage:
     a = pfp_utils.convert_anglestring(astr)
     Acceptable input formats:
      astr = '''34 12' 24" S'''
      astr = '''34 12 24S'''
      astr = '''34 12'24.123"S''

# 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

            limits = [(l/float(1000)) for l in limits]
            variable["Attr"]["rangecheck_upper"] = ','.join(str(l) for l in limits)
    elif variable["Attr"]["units"] == "hPa" and new_units == "kPa":
        variable["Data"] = variable["Data"]/float(10)
        variable["Attr"]["units"] = "kPa"
        if "rangecheck_lower" in variable["Attr"]:
            attr_in = variable["Attr"]["rangecheck_lower"]
            limits = parse_rangecheck_limits(attr_in)
            limits = [(l/float(10)) for l in limits]
            variable["Attr"]["rangecheck_lower"] = ','.join(str(l) for l in limits)
        if "rangecheck_upper" in variable["Attr"]:
            attr_in = variable["Attr"]["rangecheck_upper"]
            limits = parse_rangecheck_limits(attr_in)
            limits = [(l/float(10)) for l in limits]
            variable["Attr"]["rangecheck_upper"] = ','.join(str(l) for l in limits)
    else:
        msg = " Unrecognised conversion from " + variable["Attr"]["units"] + " to " + new_units
        logger.error(msg)
    return variable

def convert_anglestring(anglestring):
    """
    Purpose:
     Attempt to convert an angle string to a float.
    Usage:
     a = pfp_utils.convert_anglestring(astr)
     Acceptable input formats:
      astr = '''34 12' 24" S'''
      astr = '''34 12 24S'''
      astr = '''34 12'24.123"S''

      astr = '''34.123 S'''