Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.9 kB
2
Indexable
Never

import pandas as pd
# import matplotlib.pyplot as plt
import io
import sharepy
import base64
import numpy as np

# --------- GET FILE FROM SHAREPOINT ---------------------

# Set Log in for Sharepoint
sharepoint_user = 'ee20kg@leeds.ac.uk'  # email adress
encoded_password = 'TTNpbmVTY2gwMGwmIQ=='  # password, but encoded for privacy
url = 'https://leeds365.sharepoint.com'  # base sharepoint url

# Set Files name for Sharepoint
file_url = '/sites/TEAM-Shazaib-ProjectSpace/Shared%20Documents/General/spen_farm/'
sensors = ['SL048', 'SL049', 'SL051', 'SL052', 'SL057', 'SL058', 'SL059', 'SL062', 'SL063', 'SL065', 'SL068']
days = ['20230222', '20230223', '20230224', '20230225', '20230226', '20230227', '20230228', '20230301', '20230302', '20230303', '20230304', '20230305', '20230306', '20230307']
datas = []

# Set Files name for Sharepoint
file_url2 = '/sites/TEAM-SEEAQProjects-UKRI-GLASGOW/Shared%20Documents/UKRI%20-%20GLASGOW/sensor_data/'
sensors2 = ['SL043', 'SL048', 'SL049', 'SL051', 'SL057', 'SL058','SL059', 'SL061', 'SL062', 'SL063','SL065', 'SL068']
days2 = ['02/2023-02-22','02/2023-02-23','02/2023-02-24','02/2023-02-25','02/2023-02-26','02/2023-02-27','02/2023-02-28','03/2023-03-01','03/2023-03-02','03/2023-03-03','03/2023-03-04', '03/2023-03-05', '03/2023-03-06', '03/2023-03-07']
datas2 = []

s = sharepy.connect(url, username=sharepoint_user, password=base64.b64decode(encoded_password).decode('ascii'))  # connect to url with login details

# Access each of the files and turn to csvs
for sensor in sensors:
    for day in days:
        r = s.get(url+file_url+sensor+"/"+day+".csv")
        f = io.BytesIO(r.content)
        df1 = (pd.read_csv(f))
        for column in df1:
            if column == "UTCDateTime":
                df1 = df1.rename(columns={'UTCDateTime':'date'}) # rename columns
                df1['date'] = pd.to_datetime(df1['date'], errors='coerce')
                for item in df1['pm2_5_atm']:
                    if item > 50:
                        df1['pm2_5_atm'] = pd.DataFrame(np.zeros((1, 1)))
        datas.append(df1)    

# Access each of the files and turn to csvs
for sensor2 in sensors2:
    for day2 in days2:
        r2 = s.get(url+file_url2+sensor2+"/2023/"+day2+".csv")
        f2 = io.BytesIO(r2.content)
        df2 = (pd.read_csv(f2))
        df2 = df2.rename(columns={'PM2.5 A (CF=ATM) (ug/m3)': 'pm2_5_atm'})
        df2['date'] = pd.to_datetime(df2['date'],errors='coerce')
        for item in df2['pm2_5_atm']:
            if item > 50:
                df2.at[item, 'pm2_5_atm'] = 0
        datas2.append(df2)    


# --------- PLOT ---------------------

sensor48data = datas[0:13]
sensor49data = datas[14:27]
sensor51data = datas[28:41]
sensor52data = datas[42:55]
sensor57data = datas[56:69]
sensor58data = datas[70:83]
sensor59data = datas[84:97]
sensor62data = datas[98:111]
sensor63data = datas[112:125]
sensor65data = datas[126:139]
sensor68data = datas[139:152]

S48 = pd.concat(sensor48data)
S49 = pd.concat(sensor49data)
S51 = pd.concat(sensor51data)
S52 = pd.concat(sensor52data)
S57 = pd.concat(sensor57data)
S58 = pd.concat(sensor58data)
S59 = pd.concat(sensor59data)
S62 = pd.concat(sensor62data)
S63 = pd.concat(sensor63data)
S65 = pd.concat(sensor65data)
S68 = pd.concat(sensor68data)


"""
ax = S48.plot(x='date', y='pm2_5_atm', color='red', ylabel="pm2_5_atm", label='Sensor 48')
S49.plot(x='date', y='pm2_5_atm', color='blue', label='Sensor 49', ax=ax)
S51.plot(x='date', y='pm2_5_atm', color='green',  label='Sensor 51', ax=ax)
S52.plot(x='date', y='pm2_5_atm', color='orange',  label='Sensor 52', ax=ax)
S57.plot(x='date', y='pm2_5_atm', color='yellow',  label='Sensor 57', ax=ax)
S58.plot(x='date', y='pm2_5_atm', color='purple', label='Sensor 58', ax=ax)
S59.plot(x='date', y='pm2_5_atm', color='black',  label='Sensor 59', ax=ax)
S62.plot(x='date', y='pm2_5_atm', color='grey',  label='Sensor 62', ax=ax)
S63.plot(x='date', y='pm2_5_atm',  label='Sensor 63', ax=ax)
S65.plot(x='date', y='pm2_5_atm', label='Sensor 65', ax=ax)
S68.plot(x='date', y='pm2_5_atm', color='pink', label='Sensor 68', ax=ax)

"""

# --------- PLOT ---------------------

sensor243data = datas2[0:13]
sensor248data = datas2[14:27]
sensor249data = datas2[28:41]
sensor251data = datas2[42:55]
sensor257data = datas2[56:69]
sensor258data = datas2[70:83]
sensor259data = datas2[84:97]
sensor261data = datas2[98:111]
sensor262data = datas2[112:125]
sensor263data = datas2[126:139]
sensor265data = datas2[139:152]
sensor268data = datas2[139:152]

S243 = pd.concat(sensor243data)
S248 = pd.concat(sensor248data)
S249 = pd.concat(sensor249data)
S251 = pd.concat(sensor251data)
S257 = pd.concat(sensor257data)
S258 = pd.concat(sensor258data)
S259 = pd.concat(sensor259data)
S261 = pd.concat(sensor261data)
S262 = pd.concat(sensor262data)
S263 = pd.concat(sensor263data)
S265 = pd.concat(sensor265data)
S268 = pd.concat(sensor268data)

"""

ax = S243.plot(x='date', y='pm2_5_atm', color='red', ylabel="pm2_5_atm", label='Sensor 43')
S248.plot(x='date', y='pm2_5_atm',  label='Sensor 48', ax=ax)
S249.plot(x='date', y='pm2_5_atm',  label='Sensor 49', ax=ax)
S251.plot(x='date', y='pm2_5_atm',  label='Sensor 51', ax=ax)
S257.plot(x='date', y='pm2_5_atm', label='Sensor 57', ax=ax)
S258.plot(x='date', y='pm2_5_atm',  label='Sensor 58', ax=ax)
S259.plot(x='date', y='pm2_5_atm',  label='Sensor 59', ax=ax)
S261.plot(x='date', y='pm2_5_atm',  label='Sensor 61', ax=ax)
S262.plot(x='date', y='pm2_5_atm',  label='Sensor 62', ax=ax)
S263.plot(x='date', y='pm2_5_atm', label='Sensor 63', ax=ax)
S265.plot(x='date', y='pm2_5_atm', label='Sensor 65', ax=ax)
S268.plot(x='date', y='pm2_5_atm', label='Sensor 68', ax=ax)


"""

mergedS48 = pd.concat([S248, S48])
mergedS49 = pd.concat([S249, S49])
mergedS51 = pd.concat([S251, S51])
mergedS57 = pd.concat([S257, S57])
mergedS58 = pd.concat([S258, S58])
mergedS59 = pd.concat([S259, S59])
mergedS62 = pd.concat([S262, S62])
mergedS63 = pd.concat([S263, S63])
mergedS65 = pd.concat([S265, S65])
mergedS68 = pd.concat([S268, S68])



ax = S243.plot(x='date', y='pm2_5_atm',xlabel='time', ylabel="pm2_5_atm", label='Sensor 43')
mergedS48.plot(x='date', y='pm2_5_atm',  label='Sensor 48', ax=ax)
mergedS49.plot(x='date', y='pm2_5_atm',  label='Sensor 49', ax=ax)
mergedS51.plot(x='date', y='pm2_5_atm',  label='Sensor 51', ax=ax)
S52.plot(x='date', y='pm2_5_atm', label='Sensor 52', ax=ax)
mergedS57.plot(x='date', y='pm2_5_atm', label='Sensor 57', ax=ax)
mergedS58.plot(x='date', y='pm2_5_atm',  label='Sensor 58', ax=ax)
mergedS59.plot(x='date', y='pm2_5_atm',  label='Sensor 59', ax=ax)
S261.plot(x='date', y='pm2_5_atm',  label='Sensor 61', ax=ax)
mergedS62.plot(x='date', y='pm2_5_atm',  label='Sensor 62', ax=ax)
mergedS63.plot(x='date', y='pm2_5_atm', label='Sensor 63', ax=ax)
mergedS65.plot(x='date', y='pm2_5_atm', label='Sensor 65', ax=ax)
mergedS68.plot(x='date', y='pm2_5_atm', label='Sensor 68', ax=ax)