예제 #1
0
def test_t_globe():
    assert (t_mrt(tg=53.2, tdb=30, v=0.3, d=0.1, emissivity=0.95)) == 74.8
    assert (t_mrt(tg=55, tdb=30, v=0.3, d=0.1, emissivity=0.95)) == 77.8
def comfortAnalysis(count, tdb, tg, rh, tout, occupancy):
    #START###########################################
    #sensor data here to be removed once the data is read in
    #tdb = 80# dry-bulb air temperature, [$^{\circ}$C]
    #tg = 78#globe temperature
    #rh = 50 # relative humidity, [%]
    #tout = 90 #outdoor temperature
    #occupancy = 1 #occupancy
    print(count, tdb, tg, rh, tout, occupancy)
    #formula to convert to Fahrenheit from Celsius
    #F = (Cx1.8) + 32

    #reset all error flags, needed all error variables in case more than one is true
    error_1 = ""
    error_2 = ""
    error_3 = ""
    error_4 = ""
    error_5 = ""

    if tdb <= 0 or tdb >= 110:  #test logic behind the measurements
        error_1 = "Malfunction indoor temp"

    if tdb <= 55 or tdb >= 90:
        error_2 = "Potential unsafe conditions"

    if rh < 0 or rh > 100:
        error_3 = "Malfunction rh"

    if tout < -50 or tout > 110:
        error_4 = "Malfunction outdoor temp"

    if tg <= 0 or tg >= 110:
        error_5 = "Malfunction gt"

    #clothing level prediction
    if tout >= 75:  #summer
        icl = 0.5  # clo_typical_ensembles('Typical summer indoor clothing') # calculate total clothing insulation
    if tout < 75 and tout >= 60:  #spring
        icl = 0.61  #clo_typical_ensembles('Trousers, long-sleeve sweatshirt')
    if tout < 60 and tout >= 45:  #fall
        icl = 0.74  #clo_typical_ensembles('Sweat pants, long-sleeve sweatshirt')
    if tout < 45:  #winter
        icl = 1.0  #clo_typical_ensembles('Typical winter indoor clothing')

    #imput from user - recieved from GUI
    activity = "moderately active"  #  "resting", "moderately active", "active"
    if activity == "resting":
        met = met_typical_tasks['Seated, quiet']  #1.0 met range
    if activity == "moderately active":
        met = 1.5  #walking around
    if activity == "active":
        met = 2  #light exercise
        if tout < 50:
            clo = .5
        else:
            clo = 0.36  #workout clothes

        print(met)

    #Held Constant for residencies
    v = 0.1  # average air velocity, [m/s]

    #MRT Calculation
    tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)
    #operative temp calulation
    op = 0.5 * (tdb + tr)
    #print(op)

    # calculate the relative air velocity
    vr = v_relative(v=v, met=met)
    # calculate PMV in accordance with the ASHRAE 55 2017
    results = pmv_ppd(tdb=tdb,
                      tr=tr,
                      vr=vr,
                      rh=rh,
                      met=met,
                      clo=icl,
                      standard="ASHRAE",
                      units="IP")

    def get_first_key(dictionary):
        for key in dictionary:
            return key
        raise IndexError

    key1 = get_first_key(results)
    pmv = results[key1]

    ##################################################################
    #finding setpoints with iterative loop

    setpoint_options = np.array([
        60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 78,
        79, 80
    ])
    pmv_options = np.zeros(len(setpoint_options))
    global setpoint_temp_prev

    if occupancy:  #someone is home Occupancy = True
        if pmv >= -0.5 and pmv <= 0.5:
            pmv_bool = 1  #comfort conditions met
            print("Comfort Conditions met")
            print(pmv)
            setpoint_temp = setpoint_temp_prev  #maintain the same temperature

        else:
            pmv_bool = 0  #comfrot conditons are not met
            print("Comfort Conditions NOT met")
            print(pmv)

            #for loop to collect the mathematicaly PMV that would result from each setpoint option
            for x in range(len(setpoint_options)):
                results = pmv_ppd(tdb=setpoint_options[x],
                                  tr=tr,
                                  vr=vr,
                                  rh=rh,
                                  met=met,
                                  clo=icl,
                                  standard="ASHRAE",
                                  units="IP")
                key1 = get_first_key(results)
                pmv_options[x] = results[key1]

            pmv_minimum = 5  #nonsensical value to be replaced by the loop
            pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
            for x in range(len(pmv_options)):
                if pmv_options[x] <= 0.5 and pmv_options[x] >= -0.5:
                    if pmv > 0:
                        if 0.5 - pmv_options[
                                x] < pmv_minimum:  #want to have to change the pmv the least amount
                            pmv_minimum = 0.5 - pmv_options[x]
                            pmv_minimum_index = x
                    if pmv < 0:
                        if -0.5 + pmv_options[x] < pmv_minimum:
                            pmv_minimum = -0.5 + pmv_options[x]
                            pmv_minimum_index = x

            setpoint_temp = setpoint_options[pmv_minimum_index]
            print("PMV selected")
            print(pmv_options[pmv_minimum_index])
            print("setpoint selected:")
            print(setpoint_temp)

    else:  #someone is not home Occupancy = False
        print("No one home")
        if pmv >= -1.0 and pmv <= 1.0:
            pmv_bool = 1  #comfort conditions met
            print("Comfort Conditions met")
            print(pmv)
            setpoint_temp = setpoint_temp_prev  #maintain the same temperature
            print(setpoint_temp_prev)

            if pmv <= 0.75 and pmv >= -0.75:
                print('but want to relax comfort conditons to save energy')

                for x in range(len(setpoint_options)):
                    results = pmv_ppd(tdb=setpoint_options[x],
                                      tr=tr,
                                      vr=vr,
                                      rh=rh,
                                      met=met,
                                      clo=icl,
                                      standard="ASHRAE",
                                      units="IP")
                    key1 = get_first_key(results)
                    pmv_options[x] = results[key1]

                pmv_minimum = 5  #nonsensical value to be replaced by the loop
                pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
                for x in range(len(pmv_options)):
                    if pmv_options[x] <= 1.0 and pmv_options[x] >= -1.0:

                        if pmv > 0:
                            if 1 - pmv_options[x] < pmv_minimum:
                                pmv_minimum = 1 - pmv_options[x]
                                pmv_minimum_index = x

                        if pmv < 0:
                            if 1 + pmv_options[x] < pmv_minimum:
                                pmv_minimum = 1 + pmv_options[x]
                                pmv_minimum_index = x

                setpoint_temp = setpoint_options[pmv_minimum_index]
                print("PMV selected")
                print(pmv_options[pmv_minimum_index])
                print("setpoint selected:")
                print(setpoint_temp)

        else:
            pmv_bool = 0  #comfrot conditons are not met
            print("Comfort Conditions NOT met")

            #for loop to collect the mathematicaly PMV that would result from each setpoint option
            for x in range(len(setpoint_options)):
                results = pmv_ppd(tdb=setpoint_options[x],
                                  tr=tr,
                                  vr=vr,
                                  rh=rh,
                                  met=met,
                                  clo=icl,
                                  standard="ASHRAE",
                                  units="IP")
                key1 = get_first_key(results)
                pmv_options[x] = results[key1]

            pmv_minimum = 5  #nonsensical value to be replaced by the loop
            pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
            for x in range(len(pmv_options)):
                if pmv_options[x] <= 0.5 and pmv_options[x] >= -0.5:
                    if 0.5 - abs(pmv_options[x]) < pmv_minimum:
                        pmv_minimum = pmv_options[x]
                        pmv_minimum_index = x

            setpoint_temp = setpoint_options[pmv_minimum_index]
            print("PMV minimum")
            print(pmv_minimum)
            print("setpoint selected:")
            print(setpoint_temp)

        setpoint_temp_prev = setpoint_temp  #saves the previous setpoint

    #END###########################################
    return pmv, setpoint_temp
예제 #3
0
    met = met_typical_tasks['Seated, quiet']
if activity == "moderately active":
    met = met_typical_tasks['Walking about']
if activity == "active":
    met = met_typical_tasks['Calisthenics']
    if tout < 50:
        clo = .5
    else:
        clo = 0.36  #workout clothes
print(met)

#Held Constant for residencies
v = 0.1  # average air velocity, [m/s]

#MRT Calculation
tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)
#operative temp calulation
op = 0.5 * (tdb + tr)
#print(op)

# calculate the relative air velocity
vr = v_relative(v=v, met=met)
# calculate PMV in accordance with the ASHRAE 55 2017
results = pmv_ppd(tdb=tdb,
                  tr=tr,
                  vr=vr,
                  rh=rh,
                  met=met,
                  clo=icl,
                  standard="ASHRAE",
                  units="IP")
예제 #4
0
def comfortAnalysis(tdb, tg, rh, tout, occupancy, desiredTemp, activity):
    start_time = time.time()

    #clothing level prediction
    if tout >= 75:  #summer
        icl = 0.5  # clo_typical_ensembles('Typical summer indoor clothing') # calculate total clothing insulation
    if tout < 75 and tout >= 60:  #spring
        icl = 0.61  #clo_typical_ensembles('Trousers, long-sleeve sweatshirt')
    if tout < 60 and tout >= 45:  #fall
        icl = 0.74  #clo_typical_ensembles('Sweat pants, long-sleeve sweatshirt')
    if tout < 45:  #winter
        icl = 1.0  #clo_typical_ensembles('Typical winter indoor clothing')

    #imput from user - recieved from GUI
    #activity = "moderately active" #  "resting", "moderately active", "active"
    if activity == 0:  # resting
        met = met_typical_tasks['Seated, quiet']  #1.0 met range
    if activity == 1:  # moderately active
        met = 1.5  #walking around
    if activity == 2:  # active
        met = 2  #light exercise
        if tout < 50:
            clo = .5
        else:
            clo = 0.36  #workout clothes

        #print("        comfortAnalysis.py",met)

    #Held Constant for residencies
    v = 0.1  # average air velocity, [m/s]

    #MRT Calculation
    tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)
    #print("tr:")
    #print(tr)
    #operative temp calulation
    op = 0.5 * (tdb + tr)
    #print("        comfortAnalysis.py",op)

    # calculate the relative air velocity
    vr = v_relative(v=v, met=met)

    ############################################################################
    #check data for errors
    #reset all error flags, needed all error variables in case more than one is true
    error_1 = False
    error_2 = False
    error_3 = False
    error_4 = False
    error_5 = False
    error_6 = False
    error_7 = False

    if tdb <= 0 or tdb >= 110:  #test logic behind the measurements
        #error_1 = "Malfunction indoor temp"
        error_1 = True

    if tdb <= 55 or tdb >= 90:
        print("        comfortAnalysis.py",
              "WARNING: Potential unsafe conditions")
        error_2 = True
    if rh < 0 or rh > 100:
        print("        comfortAnalysis.py", "Malfunction RH sensor")
        error_3 = True
    if tout < -50 or tout > 110:
        print("        comfortAnalysis.py", "Malfunction Outdoor Temp sensor")
        error_4 = True

    if tg <= 0 or tg >= 110:
        print("        comfortAnalysis.py", "Malfunction GT Sensor")
        error_5 = True

    if tr <= 50 or tr >= 104:
        print(
            "        comfortAnalysis.py",
            "ERROR: Mean radiant temperature outside ASHRAE operating conditions"
        )
        error_6 = True

    if tdb <= 50 or tdb >= 104:
        print("        comfortAnalysis.py",
              "ERROR: Indoor temperature outside ASHRAE operating conditions")
        error_7 = True

    if error_1 == False and error_2 == False and error_3 == False and error_4 == False and error_5 == False and error_6 == False and error_7 == False:
        #only enter the ashrae calcs if error free
        # calculate PMV in accordance with the ASHRAE 55 2017
        results = pmv_ppd(tdb=tdb,
                          tr=tr,
                          vr=vr,
                          rh=rh,
                          met=met,
                          clo=icl,
                          standard="ASHRAE",
                          units="IP")

        key1 = get_first_key(
            results
        )  #function from above to use python variable type dictionary
        pmv = results[key1]

        ##################################################################
        #finding setpoints with iterative loop

        setpoint_options = np.arange(
            60, 80, 0.1)  # want to test setpoints between 60 and 80
        pmv_options = np.zeros(
            len(setpoint_options)
        )  #will run a hypothetical PMV calc with that temperature to select the best one
        global setpoint_temp_prev  #needs to be accessabile in all parts of the code
        pmv_minimum_index = 0

        if occupancy == False:  #someone is home Occupancy = True
            if pmv >= -0.5 and pmv <= 0.5:
                pmv_bool = 1  #comfort conditions met
                #print("Comfort Conditions are met")
                #print(pmv)
                #print("        comfortAnalysis.py","Comfort Conditions met")
                #print("        comfortAnalysis.py",pmv)
                setpoint_temp = setpoint_temp_prev  #maintain the same temperature
                # print(setpoint_temp)
                pmv_options[pmv_minimum_index] = pmv
            # print( pmv_options[pmv_minimum_index])

            else:
                pmv_bool = 0  #comfort conditons are not met
                print("Comfort conditions NOT met")
                #print(pmv)
                #print("        comfortAnalysis.py","Comfort Conditions NOT met")
                #print("        comfortAnalysis.py",pmv)

                #for loop to iteratively test which PMV that would result from each setpoint option
                for x in range(len(setpoint_options)):
                    results = pmv_ppd(tdb=setpoint_options[x],
                                      tr=tr,
                                      vr=vr,
                                      rh=rh,
                                      met=met,
                                      clo=icl,
                                      standard="ASHRAE",
                                      units="IP")
                    key1 = get_first_key(results)
                    pmv_options[x] = results[key1]
                for x in range(len(pmv_options)):
                    if pmv > 0.5:  #throw out options too cold
                        if pmv_options[x] < 0:
                            pmv_options[x] = -100
                    else:  #throw out options too warm
                        if pmv_options[x] > 0:
                            pmv_options[x] = 100

                pmv_minimum = 5  #nonsensical value to be replaced by the loop
                pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
                for x in range(
                        len(pmv_options)
                ):  #check each potential PMV to find the one that best meets the range -0.5 to 0.5
                    if pmv_options[x] <= 0.5 and pmv_options[x] >= -0.5:
                        if pmv > 0:  # already know heat or cool must come on because it is outside the range so isolate which one and find the closest value to the edge of comfort range
                            if 0 + pmv_options[
                                    x] < pmv_minimum:  #want to have to change the pmv to the closest to 0.5 to save energy and still be comfortable
                                pmv_minimum = 0 - pmv_options[x]
                                pmv_minimum_index = x
                        if pmv < 0:  # already know heat or cool must come on because it is outside the range so isolate which one and find the closest value to the edge of comfort range
                            if 0.5 + pmv_options[x] < pmv_minimum:
                                pmv_minimum = 0.5 + pmv_options[x]
                                pmv_minimum_index = x
                setpoint_temp = round(setpoint_options[pmv_minimum_index], 1)

            # print("Potential PMV selected")
            # print(pmv_options[pmv_minimum_index])
            # print("setpoint selected:")
            # print(setpoint_temp)
            #print("        comfortAnalysis.py","PMV selected")
            #print("        comfortAnalysis.py",pmv_options[pmv_minimum_index])
            #print("        comfortAnalysis.py","setpoint selected:")
            #print("        comfortAnalysis.py",setpoint_temp)

        else:  #someone is not home Occupancy = True
            # print("No one home")
            #print("        comfortAnalysis.py","No one home")
            if pmv >= -1.0 and pmv <= 1.0:
                pmv_bool = 1  #comfort conditions met
                #   print("comfort conditions met")
                #   print(pmv)
                #print("        comfortAnalysis.py","Comfort Conditions met")
                #print("        comfortAnalysis.py",pmv)
                setpoint_temp = setpoint_temp_prev  #maintain the same temperature
                #print("        comfortAnalysis.py",setpoint_temp_prev)

                if pmv <= 0.75 and pmv >= 0:  #pmv is warm but could be relaxed to be warmer
                    #print("        comfortAnalysis.py",'but want to relax comfort conditons to save energy')

                    for x in range(len(setpoint_options)
                                   ):  #get potential PMV for setpoint options
                        if setpoint_options[
                                x] >= tout or tout > 80:  #need to make sure heat does not turn on past conditions outside
                            results = pmv_ppd(tdb=setpoint_options[x],
                                              tr=tr,
                                              vr=vr,
                                              rh=rh,
                                              met=met,
                                              clo=icl,
                                              standard="ASHRAE",
                                              units="IP")
                            key1 = get_first_key(results)
                            pmv_options[x] = results[key1]
                        else:
                            pmv_options[
                                x] = 100  #value will never be picked essentially removing those options

                    pmv_minimum = 5  #nonsensical value to be replaced by the loop
                    pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
                    for x in range(
                            len(pmv_options)
                    ):  #for loop to collect iteratively the PMV that would result from each setpoint option
                        if pmv_options[
                                x] <= 1.0:  #only compare the PMV possibilities within the range we want
                            if 1 - pmv_options[x] < pmv_minimum:
                                pmv_minimum = 1 - pmv_options[x]
                                pmv_minimum_index = x

                    setpoint_temp = round(setpoint_options[pmv_minimum_index],
                                          1)
                # print("PMV selected")
                #  print(pmv_options[pmv_minimum_index])
                # print("setpoint selected:")
                # print(setpoint_temp)
                #print("        comfortAnalysis.py","PMV selected")
                #print("        comfortAnalysis.py",pmv_options[pmv_minimum_index])
                #print("        comfortAnalysis.py","setpoint selected:")
                #print("        comfortAnalysis.py",setpoint_temp)

                if pmv >= -0.75 and pmv <= 0:  ##pmv is cold but could be relaxed to be cooler
                    #print("        comfortAnalysis.py",'but want to relax comfort conditons to save energy')

                    for x in range(len(setpoint_options)
                                   ):  #get potential PMV for setpoint options
                        if setpoint_options[
                                x] >= tout or tout < 60:  #need to make sure cooling does not turn on if the temperature is relaxed
                            results = pmv_ppd(tdb=setpoint_options[x],
                                              tr=tr,
                                              vr=vr,
                                              rh=rh,
                                              met=met,
                                              clo=icl,
                                              standard="ASHRAE",
                                              units="IP")
                            key1 = get_first_key(results)
                            pmv_options[x] = results[key1]
                        else:
                            pmv_options[
                                x] = 100  #value will never be picked essentially removing those options (next if statement will skip all these)

                    pmv_minimum = 5  #nonsensical value to be replaced by the loop
                    pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
                    for x in range(len(pmv_options)):
                        if pmv_options[
                                x] >= -1.0:  # make sure the potential PMV is within the range
                            if 1 + pmv_options[
                                    x] < pmv_minimum:  #pmv will be a negative number so adding to find the minimum distance from edge of range
                                pmv_minimum = 1 + pmv_options[x]
                                pmv_minimum_index = x

                    setpoint_temp = round(setpoint_options[pmv_minimum_index],
                                          1)

                # print("PMV selected")
                # print(pmv_options[pmv_minimum_index])
                # print("setpoint selected:")
                ##print(setpoint_temp)
                #print("        comfortAnalysis.py","PMV selected")
                #print("        comfortAnalysis.py",pmv_options[pmv_minimum_index])
                #print("        comfortAnalysis.py","setpoint selected:")
                #print("        comfortAnalysis.py",setpoint_temp)

            else:
                pmv_bool = 0  #comfort conditons are not met in case no one is home
                #print("        comfortAnalysis.py","Comfort Conditions NOT met")

                if pmv >= 0:  #pmv is too warm
                    #print("        comfortAnalysis.py",'but want to relax comfort conditons to save energy')
                    for x in range(len(setpoint_options)
                                   ):  #get potential PMV for setpoint options
                        if setpoint_options[
                                x] >= tout or tout:  #need to make sure heat does not turn on past conditions outside
                            results = pmv_ppd(tdb=setpoint_options[x],
                                              tr=tr,
                                              vr=vr,
                                              rh=rh,
                                              met=met,
                                              clo=icl,
                                              standard="ASHRAE",
                                              units="IP")
                            key1 = get_first_key(results)
                            pmv_options[x] = results[key1]
                        else:
                            pmv_options[
                                x] = 100  #value will never be picked essentially removing those options

                    pmv_minimum = 5  #nonsensical value to be replaced by the loop
                    pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
                    for x in range(
                            len(pmv_options)
                    ):  #for loop to collect iteratively the PMV that would result from each setpoint option
                        if pmv_options[
                                x] <= 1.0:  #only compare the PMV possibilities within the range we want
                            if 1 - pmv_options[x] < pmv_minimum:
                                pmv_minimum = 1 - pmv_options[x]
                                pmv_minimum_index = x

                    setpoint_temp = round(setpoint_options[pmv_minimum_index],
                                          1)
                    #print("PMV selected")
                # print(pmv_options[pmv_minimum_index])
                #print("setpoint selected:")
                #print(setpoint_temp)
                #print("        comfortAnalysis.py","PMV selected")
                #print("        comfortAnalysis.py",pmv_options[pmv_minimum_index])
                #print("        comfortAnalysis.py","setpoint selected:")
                #print("        comfortAnalysis.py",setpoint_temp)

                if pmv <= 0:  ##pmv is too cold
                    #print("        comfortAnalysis.py",'but want to relax comfort conditons to save energy')

                    for x in range(len(setpoint_options)
                                   ):  #get potential PMV for setpoint options
                        if setpoint_options[
                                x] >= tout or tout > 60:  #need to make sure heat does not turn on if the temperature is relaxed
                            results = pmv_ppd(tdb=setpoint_options[x],
                                              tr=tr,
                                              vr=vr,
                                              rh=rh,
                                              met=met,
                                              clo=icl,
                                              standard="ASHRAE",
                                              units="IP")
                            key1 = get_first_key(results)
                            pmv_options[x] = results[key1]
                        else:
                            pmv_options[
                                x] = 100  #value will never be picked essentially removing those options (next if statement will skip all these)

                    pmv_minimum = 5  #nonsensical value to be replaced by the loop
                    pmv_minimum_index = 0  #returned to isolate the temperaure setpoint that goes with the minimum PMV
                    for x in range(len(pmv_options)):
                        if pmv_options[
                                x] >= -1.0:  # make sure the potential PMV is within the range
                            if 1 + pmv_options[
                                    x] < pmv_minimum:  #pmv will be a negative number so adding to find the minimum distance from edge of range
                                pmv_minimum = 1 + pmv_options[x]
                                pmv_minimum_index = x

                    setpoint_temp = round(setpoint_options[pmv_minimum_index],
                                          1)
                # print("PMV selected")
                # print(pmv_options[pmv_minimum_index])
                # print("setpoint selected:")
                # print(setpoint_temp)
                #print("        comfortAnalysis.py","PMV selected")
                #print("        comfortAnalysis.py",pmv_options[pmv_minimum_index])
                #print("        comfortAnalysis.py","setpoint selected:")
                #print("        comfortAnalysis.py",setpoint_temp)

    else:
        #print("        comfortAnalysis.py",'Error Detected. Operating to entered Desired Temp')
        setpoint_temp = setpoint_temp_prev
        pmv_bool = False
        pmv = 0.00
        pmv_options = [5]
        pmv_minimum_index = 0
        pmv_options[pmv_minimum_index] = 5

    setpoint_temp_prev = setpoint_temp  #saves the previous setpoint
    #print("        comfortAnalysis.py",pmv, setpoint_temp, pmv_options[pmv_minimum_index])
    #print("        comfortAnalysis.py","ComfortAnalysis Code took", time.time() - start_time, "to run")
    #return (pmv,pmv_bool,setpoint_temp, error_1, error_2, error_3, error_4, error_5, error_6, error_7)
    return (pmv, setpoint_temp, pmv_options[pmv_minimum_index])
예제 #5
0
def comfortAnalysis(count, tdb, tg, rh, tout, occupancy):
    #START###########################################
    #TODO remove this
    tout_saved = np.random.random_sample(
        (1440 * 30 +
         5 * 1440), ) + 60  #Only for testing that the indexing is done right
    count = 1440 * 30 + 1440
    #tout_saved = np.zeros(1440*365)

    #count = 1440*30 + 1440

    #setpoint_temp = 67

    ############################################################################################
    if count > 365 * 1440:  #keep the number of saved data points from being too large - can adjust if 1 year is too many
        tout_reset = np.zeros(1440 * 365)
        tout_reset[0:1440 * 30 - 1] = tout_saved[365 * 1440 - 30 * 1440:365 *
                                                 1440]
        tout_saved = tout_reset
        count = 1440 * 30 + 1  #starts at zero, count number represents the number of run about to happen adn sensor data just received

    #sensor data here to be removed once the data is read in
    tdb = 75  # dry-bulb air temperature, [$^{\circ}$C]
    tg = 76  #globe temperature
    rh = 50  # relative humidity, [%]
    tout = 40  #outdoor temperature
    occupancy = 1  #occupancy

    #formula to convert to Fahrenheit from Celsius
    #F = (Cx1.8) + 32

    #reset all error flags, needed all error variables in case more than one is true
    error_1 = ""
    error_2 = ""
    error_3 = ""
    error_4 = ""
    error_5 = ""

    if tdb <= 0 | tdb >= 110:  #test logic behind the measurements
        error_1 = "Malfunction indoor temp"

    if tdb <= 55 | tdb >= 90:
        error_2 = "Potential unsafe conditions"

    if rh < 0 | rh > 100:
        error_3 = "Malfunction rh"

    if tout < -50 | tout > 110:
        error_4 = "Malfunction outdoor temp"

    if tg <= 0 | tg >= 110:
        error_5 = "Malfunction gt"

    #clothing level prediction
    if tout >= 75:  #summer
        icl = 0.5  # clo_typical_ensembles('Typical summer indoor clothing') # calculate total clothing insulation
    if tout < 75 & tout >= 60:  #spring
        icl = 0.61  #clo_typical_ensembles('Trousers, long-sleeve sweatshirt')
    if tout < 60 & tout >= 45:  #fall
        icl = 0.74  #clo_typical_ensembles('Sweat pants, long-sleeve sweatshirt')
    if tout < 45:  #winter
        icl = 1.0  #clo_typical_ensembles('Typical winter indoor clothing')

    #imput from user - recieved from GUI
    activity = "moderately active"  #  "resting", "moderately active", "active"
    if activity == "resting":
        met = met_typical_tasks['Seated, quiet']
    if activity == "moderately active":
        met = met_typical_tasks['Walking about']
    if activity == "active":
        met = met_typical_tasks['Calisthenics']
        if tout < 50:
            clo = .5
        else:
            clo = 0.36  #workout clothes
    print(met)

    #Held Constant for residencies
    v = 0.1  # average air velocity, [m/s]

    #MRT Calculation
    tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)
    #operative temp calulation
    op = 0.5 * (tdb + tr)
    #print(op)

    # calculate the relative air velocity
    vr = v_relative(v=v, met=met)
    # calculate PMV in accordance with the ASHRAE 55 2017
    results = pmv_ppd(tdb=tdb,
                      tr=tr,
                      vr=vr,
                      rh=rh,
                      met=met,
                      clo=icl,
                      standard="ASHRAE",
                      units="IP")

    def get_first_key(dictionary):
        for key in dictionary:
            return key
        raise IndexError

    key1 = get_first_key(results)
    pmv = results[key1]

    #print(pmv)

    # print PMV value- this whole group can be eventually deleted
    print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

    if pmv <= float(0.5) and pmv >= float(-0.5):
        print('Acceptable!')

    if pmv >= 0.5:
        print('Too warm!Turn on fan')

    if pmv <= -0.5:
        print('Too cold! Turn on heater! or Grab Blanket')

    ##################################################################
    #finding the setpoints

    num_runs = count  #get this value from pipes
    print("Count:", count)
    if num_runs <= 1440 * 30:  #for when less than a months worth of data is collected
        t_running_mean = 45  #average temperature in March in Lexington, KY
        tout_saved[num_runs] = tout
    else:
        t_running_mean = sum(
            tout_saved[(num_runs - 30 * 1440):num_runs]) / (30 * 1440)
    print("Running mean:", t_running_mean)
    setpoints = adaptive_ashrae(tdb, tr, t_running_mean, v, units="IP")
    #print(setpoints)

    print(
        f"low={setpoints['tmp_cmf_90_low']}, high={setpoints['tmp_cmf_90_up']}%"
    )

    if pmv < -0.5 and pmv > 0.5:  #check if outside the comfortable range
        if occupancy == 1:
            #checking if the change fixed it summer
            PMV_bool = 0
            setpoint_low = setpoints['tmp_cmf_90_low']
            tg = tdb + 2  #can adjust this guess based on how the gt in the fridge changes with the heating or cooling
            tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)

            results_low = pmv_ppd(tdb=tdb,
                                  tr=tr,
                                  vr=vr,
                                  rh=rh,
                                  met=met,
                                  clo=icl,
                                  standard="ASHRAE",
                                  units="IP")
            print('Using low setpoint')
            print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

            setpoint_high = setpoints['tmp_cmf_90_up']
            tg = setpoint_high + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
            tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)

            results_high = pmv_ppd(tdb=tdb,
                                   tr=tr,
                                   vr=vr,
                                   rh=rh,
                                   met=met,
                                   clo=icl,
                                   standard="ASHRAE",
                                   units="IP")
            print('Using high setpoint')
            print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

            tdb = (setpoints['tmp_cmf_90_up'] +
                   setpoints['tmp_cmf_90_low']) / 2
            tg = tdb + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
            tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)
            print('mid setpoint')
            print(tdb)

            results_mid = pmv_ppd(tdb=tdb,
                                  tr=tr,
                                  vr=vr,
                                  rh=rh,
                                  met=met,
                                  clo=icl,
                                  standard="ASHRAE",
                                  units="IP")
            print('Using mid setpoint')
            print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

            #want the set point closest to the middle neutral temperature for optimal comfort
            set_point_PMV = min(
                abs(0 - results_low), abs(0 - results_high),
                abs(0 - results_mid))  #this setpoint will be sent to Joseph

            #select the temperature that corresponds to the PMV setpoint
            if set_point_PMV == results_high:
                set_point_temp = setpoints['tmp_cmf_80_up']
            if set_point_PMV == results_mid:
                set_point_temp = (setpoints['tmp_cmf_80_up'] +
                                  setpoints['tmp_cmf_80_low']) / 2
            if set_point_PMV == results_low:
                set_point_temp = setpoints['tmp_cmf_80_low']

        if occupancy == 0:  #relaxed comfort parameters
            #checking if the change fixed it summer
            if pmv > 1 or pmv < -1:
                PMV_bool = 0
                setpoint_low = setpoints['tmp_cmf_80_low']
                tg = tdb + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
                tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)

                results_low = pmv_ppd(tdb=tdb,
                                      tr=tr,
                                      vr=vr,
                                      rh=rh,
                                      met=met,
                                      clo=icl,
                                      standard="ASHRAE",
                                      units="IP")
                print('Using low setpoint')
                print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

                setpoint_high = setpoints['tmp_cmf_80_up']
                tg = setpoint_high + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
                tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)

                results_high = pmv_ppd(tdb=tdb,
                                       tr=tr,
                                       vr=vr,
                                       rh=rh,
                                       met=met,
                                       clo=icl,
                                       standard="ASHRAE",
                                       units="IP")
                print('Using high setpoint')
                print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

                tdb = (setpoints['tmp_cmf_80_up'] +
                       setpoints['tmp_cmf_80_low']) / 2
                tg = tdb + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
                tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)
                print('mid setpoint')
                print(tdb)

                results_mid = pmv_ppd(tdb=tdb,
                                      tr=tr,
                                      vr=vr,
                                      rh=rh,
                                      met=met,
                                      clo=icl,
                                      standard="ASHRAE",
                                      units="IP")
                print('Using mid setpoint')
                print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

                if results_high < -1 or results_high > 1:
                    results_high = 0  #really low compared to the other options and wont be selected as it is an infeasible option

                if results_low < -1 or results_low > 1:
                    result_low = 0

                if results_mid < -1 or results_mid > 1:
                    results_mid = 0

                    #comparing the setpoints to select the one closest to the edge of the comfortable window to lead to maximum savings in energy costs
                set_point_PMV = max(
                    abs(0 - results_low), abs(0 - results_high),
                    abs(0 -
                        results_mid))  #this setpoint will be sent to Joseph

                #select the temperature that corresponds to the PMV setpoint
                if set_point_PMV == results_high:
                    set_point_temp = setpoints['tmp_cmf_80_up']
                if set_point_PMV == results_mid:
                    set_point_temp = (setpoints['tmp_cmf_80_up'] +
                                      setpoints['tmp_cmf_80_low']) / 2
                if set_point_PMV == results_low:
                    set_point_temp = setpoints['tmp_cmf_80_low']
            else:
                PMV_bool = 1
                setpoint_temp = setpoint_temp

    else:
        PMV_bool = 1
        if occupancy == 1:
            setpoint_temp = setpoint_temp  # keep the same setpoint if it is in the comfortable range no need to cause the hvac to turn on if it is comfortable
        else:  #allow the comfort conditions to relax to with the (-1 to 1 range)
            setpoint_low = setpoints['tmp_cmf_80_low']
            tg = tdb + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
            tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)

            results_low = pmv_ppd(tdb=tdb,
                                  tr=tr,
                                  vr=vr,
                                  rh=rh,
                                  met=met,
                                  clo=icl,
                                  standard="ASHRAE",
                                  units="IP")
            print('Using low setpoint')
            print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

            setpoint_high = setpoints['tmp_cmf_80_up']
            tg = setpoint_high + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
            tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)

            results_high = pmv_ppd(tdb=tdb,
                                   tr=tr,
                                   vr=vr,
                                   rh=rh,
                                   met=met,
                                   clo=icl,
                                   standard="ASHRAE",
                                   units="IP")
            print('Using high setpoint')
            print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

            tdb = (setpoints['tmp_cmf_80_up'] +
                   setpoints['tmp_cmf_80_low']) / 2
            tg = tdb + 2  #can adjust based on how the gt in the fridge changes with the heating or cooling
            tr = t_mrt(tg, tdb, v, d=0.075, emissivity=0.95)
            print('mid setpoint')
            print(tdb)

            results_mid = pmv_ppd(tdb=tdb,
                                  tr=tr,
                                  vr=vr,
                                  rh=rh,
                                  met=met,
                                  clo=icl,
                                  standard="ASHRAE",
                                  units="IP")
            print('Using mid setpoint')
            print(f"pmv={results['pmv']}, ppd={results['ppd']}%")

            if results_high < -1 or results_high > 1:
                results_high = 0  #really low compared to the other options and wont be selected as it is an infeasible option

            if results_low < -1 or results_low > 1:
                result_low = 0

            if results_mid < -1 or results_mid > 1:
                results_mid = 0

            #comparing the setpoints to select the one closest to the edge of the comfortable window to lead to maximum savings in energy costs
            set_point_PMV = max(
                abs(0 - results_low), abs(0 - results_high),
                abs(0 - results_mid))  #this setpoint will be sent to Joseph

            #select the temperature that corresponds to the PMV setpoint
            if set_point_PMV == results_high:
                set_point_temp = setpoints['tmp_cmf_80_up']
            if set_point_PMV == results_mid:
                set_point_temp = (setpoints['tmp_cmf_80_up'] +
                                  setpoints['tmp_cmf_80_low']) / 2
            if set_point_PMV == results_low:
                set_point_temp = setpoints['tmp_cmf_80_low']

    count = count + 1

    #END###########################################
    return pmv, setpoint_temp