-
Notifications
You must be signed in to change notification settings - Fork 40
/
comfort.py
204 lines (163 loc) · 5.48 KB
/
comfort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from io import TextIOWrapper
import os
from flask import (
Flask,
request,
render_template,
send_from_directory,
abort,
jsonify,
make_response,
)
from pythermalcomfort.models import pmv_ppd, set_tmp, cooling_effect
from pythermalcomfort.utilities import v_relative, clo_dynamic
import pandas as pd
ALLOWED_EXTENSIONS = {"csv"}
STATIC_URL_PATH = "/static"
app = Flask(__name__, static_url_path=STATIC_URL_PATH)
# define the extension files that can be uploaded by users
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1] in ALLOWED_EXTENSIONS
@app.route("/download/<path:filename>")
def download_file(filename):
if ".." in filename:
abort(404)
return send_from_directory(
"./media/", filename, mimetype="application/octet-stream"
)
@app.route("/api/v1/comfort/pmv", methods=["GET"])
def api_id():
# Check if an ID was provided as part of the URL.
# If ID is provided, assign it to a variable.
# If no ID is provided, display an error in the browser.
try:
clo = float(request.args["clo"])
ta = float(request.args["ta"])
tr = float(request.args["tr"])
v = float(request.args["v"])
met = float(request.args["met"])
rh = float(request.args["rh"])
except KeyError:
return "Error: You did not provided all the input parameters"
# Create an empty list for our results
results = dict()
value = pmv_ppd(ta, tr, v, rh, met, clo, wme=0, standard="ASHRAE")
results["data"] = []
results["data"].append(value)
results["message"] = "success"
# Use the jsonify function from Flask to convert our list of
# Python dictionaries to the JSON format.
return jsonify(results)
# Upload page - The user can upload a csv with input environmental parameters and
# returns a csv with indexes calculated
@app.route(
"/upload"
) # tutorial https://stackoverflow.com/questions/27628053/uploading-and
# -downloading-files-with-flask
def upload():
return render_template("upload.html")
# this function process the uploaded file and automatically downloads the file with the
# results
@app.route("/transform", methods=["POST"])
def transform_view():
# get the uploaded file
request_file = request.files["data_file"]
# check file
if not request_file:
return "No file selected"
if not allowed_file(request_file.filename):
return "The file format is not allowed. Please upload a csv"
# read file
csv_file = TextIOWrapper(request_file, encoding="utf-8")
df = pd.read_csv(csv_file)
fields = {
"Air temperature": "ta",
"MRT": "tr",
"Air speed": "vel",
"Relative humidity": "rh",
"Metabolic rate": "met",
"Clothing level": "clo",
}
si_unit = any(
[
True if ("Air temperature" in x) and (x.split(" [")[1] == "C]") else False
for x in df.columns
]
)
df.columns = [fields[x.split(" [")[0]] if " [" in x else x for x in df.columns]
df["clo_dynamic"] = df.apply(
lambda row: clo_dynamic(clo=row["clo"], met=row["met"]), axis=1
)
results = []
ta = df["ta"].values
tr = df["tr"].values
vel = df["vel"].values
rh = df["rh"].values
met = df["met"].values
clo = df["clo"].values
clo_d = df["clo_dynamic"].values
for ix in range(df.shape[0]):
if si_unit:
units = "SI"
_vr = v_relative(vel[ix], met[ix])
else:
units = "IP"
_vr = v_relative(vel[ix] / 60 * 0.3048, met[ix]) * 3.28084
try:
_set = set_tmp(ta[ix], tr[ix], _vr, rh[ix], met[ix], clo[ix], units=units)
except:
_set = 9999
try:
_ce = cooling_effect(
ta[ix], tr[ix], _vr, rh[ix], met[ix], clo_d[ix], units=units
)
except:
_ce = 9999
try:
_pmv_ppd = pmv_ppd(
ta[ix],
tr[ix],
_vr,
rh[ix],
met[ix],
clo_d[ix],
standard="ashrae",
units=units,
)
_pmv = _pmv_ppd["pmv"]
_ppd = _pmv_ppd["ppd"]
except:
_pmv, _ppd = [9999, 9999]
results.append({"pmv": _pmv, "ppd": _ppd, "ce": _ce, "vr": _vr, "set": _set})
# split the pmv column in two since currently contains both pmv and ppd values
df_ = pd.DataFrame(results)
df = pd.concat([df, df_], axis=1, sort=False)
df["LEED compliance"] = [True if x < 10 else False for x in df.ppd]
df.loc[df.ppd == 9999, ["pmv", "ppd", "ce", "LEED compliance"]] = None
resp = make_response(df.to_csv(index=False))
resp.headers["Content-Disposition"] = "attachment; filename=export.csv"
resp.headers["Content-Type"] = "text/csv"
return resp
@app.route("/compare")
def compare():
return render_template("compare.html")
@app.route("/ranges")
def ranges():
return render_template("ranges.html")
@app.route("/EN")
def en():
return render_template("en.html")
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
@app.route("/")
def index():
return render_template("ashrae.html")
@app.route("/fan_heatwaves")
def fan_heatwaves():
return render_template("use_fans_heatwaves.html")
@app.route("/phs")
def phs():
return render_template("phs.html")
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))