import pandas as pd
import numpy as np
import csv

df_sunlight = pd.read_excel('lightintensity.xlsx', header=None, names=['SunlightIntensity'])

start_date = '2023-01-01 00:00:00'  # 根据数据的实际开始日期调整
hours = pd.date_range(start=start_date, periods=len(df_sunlight), freq='h')
df_sunlight['Time'] = hours
df_sunlight.set_index('Time', inplace=True)

df_sunlight_resampled = df_sunlight.resample('15min').interpolate()

df_power = pd.read_excel('factory_power.xlsx', 
                         header=None, 
                         names=['FactoryPower'], 
                         dtype={'FactoryPower': float})
times = pd.date_range(start=start_date, periods=len(df_power), freq='15min')
df_power['Time'] = times
df_power.set_index('Time',inplace=True)
print(df_power.head())

df_combined = df_sunlight_resampled.join(df_power)

df_combined.to_csv('combined_data.csv', index=True, index_label='Time')

price_data = np.random.uniform(0.3, 0.3, len(times))

# 创建DataFrame
price_df = pd.DataFrame(data={'Time': times, 'ElectricityPrice': price_data})

price_df.set_index('Time', inplace=True)

# 保存到CSV文件
price_df.to_csv('electricity_price_data.csv', index=True)
print(price_df.head())
print("Electricity price data generated and saved.")

df_combined2 = df_combined.join(price_df)
print(df_combined2.head())
# 保存结果
with open('combined_data.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['time', 'sunlight', 'demand','price'])
    for index, row in df_combined2.iterrows():
        time_formatted = index.strftime('%H:%M')
        writer.writerow([time_formatted, row['SunlightIntensity'], row['FactoryPower'],row['ElectricityPrice']])
    print('The file is written to combined_data.csv')

# combined_data.to_csv('updated_simulation_with_prices.csv', index=False)

print("Simulation data with electricity prices has been updated and saved.")