import pandas as pd
import pyproj

file_path = 'danish_windturbines.xlsx'

header_line = pd.read_excel(file_path, skiprows=6, nrows=0)
column_names = header_line.columns.tolist()
data = pd.read_excel(file_path, skiprows=10, names=column_names)

start_date = pd.to_datetime('2023-01-01')
end_date = pd.to_datetime('2024-12-31')

# Get dates for turbines for filtering
connection_date = data['Date of original connection to grid']
decommissioning_date = data['Date of decommissioning']
# If 'Date of decommissioning' is empty, we assume it is still active (set to future date)
decommisioning_date_fixed = decommissioning_date.fillna(pd.to_datetime('2099-12-31'))

# Hub heights for filter
hub_heights_m = data['Hub height (m)']
rotor_diameter_m = data['Rotor diameter (m)']

# Filtering on date and height
active_within_interval_mask = (connection_date <= end_date) & (decommisioning_date_fixed >= start_date)
active_data = data[active_within_interval_mask].copy()

# Get geographical coordinates for active turbines
transformer = pyproj.Transformer.from_crs("EPSG:25832", "EPSG:4326", always_xy=True)
lons, lats = transformer.transform(
    active_data['X (east) coordinate\nUTM 32 Euref89'].values, 
    active_data['Y (north) coordinate\nUTM 32 Euref89'].values
)

active_data['lon'] = lons
active_data['lat'] = lats

# Drop turbines with no location
active_clean = active_data.dropna(subset=['lat', 'lon'])

print(f"# Total number of turbines in file: {len(data)}")
print(f"# Number of active turbines between {start_date} and {end_date}: {len(active_data)}")
print("# Lon Lat HubHeight RotorDiameter")
for index, row in active_clean.iterrows():
    print(f"{row['lon']:7.4f} {row['lat']:7.4f} {row['Hub height (m)']:5.1f} {row['Rotor diameter (m)']:5.1f}")
