# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.7.11 ('py_btms_controller')
#     language: python
#     name: python3
# ---

# ## visualize Input Data
# this is expected to be run with the csv-file generated in importTestData.ipynb

import pandas as pd
import matplotlib.pyplot as plt
import math
import numpy as np

path = "beam1/beam1-0.csv"
dtype = {
       'time': 'int64', 
       'type': 'category', 
       'vehicle': 'int64', 
       'parkingTaz': 'category', #
       'chargingPointType': 'category', 
       'primaryFuelLevel': 'float64', #
       'mode': 'category', 
       'currentTourMode': 'category', 
       'vehicleType': 'category', 
       'arrivalTime': 'float64', #
       'departureTime': 'float64', # 
       'linkTravelTime': 'string', 
       'primaryFuelType': 'category', 
       'parkingZoneId': 'category',
       'duration': 'float64' #
}
df_sim_new = pd.read_csv(path, dtype=dtype, index_col="time")
df_sim_new.head(3)

df = df_sim_new.sort_index()
df.head(20)

df.tail(10)

i = 18406
slice = df["vehicle"].iloc[1000]
slice

idx = df["type"] == "RefuelSessionEvent"
idx.iloc[1]

plugin = 1.490243e+08
delta = 92700000
out = 2.417243e+08
capacity = 302234052
print (out - plugin == delta)
print (out/capacity)

# print taz
a = df.parkingTaz.cat.categories.to_numpy().astype("int64")
a = np.sort(a)
print(a)

# +
# perform data cleaning. make sure that every Charging Event consits of
# ChargingPlugInEvent, RefuelSessionEvent, ChargingPlugOutEvent.

# did not finished coding, did not test.

PlugIn = np.array([], dtype=int)
RefuelSession = np.array([], dtype=int)
PlugOut = np.array([],dtype = int)
VehicleId = np.array([], dtype = int)
completeSequence = np.array([], dtype=bool)
for i in range(0, len(df)):
    row = df.iloc[i,:]
    if np.isin( row["vehicle"], VehicleId):
        if row["type"] == "ChargingPlugInEvent":
            PlugIn.append(i)
        if row["type"] == "RefuelSessionEvent":
            RefuelSession.append(i)
        if row["type"] == "ChargingPlugOutEvent":
            PlugOut.append(i)
    else:
        if row["type"] == "ChargingPlugInEvent" or row["type"] == "RefuelSessionEvent":
            VehicleId.append(row["vehicle"])
            if row["type"] == "ChargingPlugInEvent":
                PlugIn.append(i)
            else:
                PlugIn.append(0)
            if row["type"] == "RefuelSessionEvent":
                RefuelSession.append(i)
            else:
                RefuelSession.append(0)
            PlugOut.append(0)
    

    

# +
# read out charging demand
#set this, if you want to do this for a special TAZ

setTAZ = False
taz = 103

time = []
startTime = []
demand = []
duration = []
power = []
for i in range(0, len(df)):
    if df["type"].iloc[i] == "RefuelSessionEvent":
        if not setTAZ or df["parkingTaz"].iloc[i] == str(taz):
            time.append(df.index[i])
            startTime.append(df.index[i] - df["duration"].iloc[i])
            demand.append(df["fuel"].iloc[i])
            duration.append(df["duration"].iloc[i])
            #calculate power
            x = df["fuel"].iloc[i] / df["duration"].iloc[i]
            if math.isnan(x):
                x = 0
            power.append(x)

#discretize / resample
t_start = 18000
t_end   = max(time)
step    = 60

power_new  = []
time_new = []
no_active =[]
t_act = t_start
time = np.array(time)
startTime = np.array(startTime)
power = np.array(power)
while t_act < t_end + step:
    idx1 = (time >= t_act)
    idx2 = (startTime < t_act)
    idx = idx1 & idx2
    power_new.append( power[idx].sum())
    no_active.append(idx.sum())
    time_new.append( t_act )
    t_act += step    

#print power demand
fig, ax = plt.subplots(2,1)
ax[0].plot(time_new, np.array(power_new)/1e3)
ax[0].set_ylabel("power in kW")
ax[1].step(time_new, no_active)
ax[1].set_ylabel("number active charges")
ax[1].set_xlabel("time in sec.")

StarcoderdataJupyterScriptsDedupFiltered