import pandas as pd
import numpy as np

# 1. Load the data, skipping the first 20 lines of NASA header text
file_name = "POWER_Point_Daily_20160701_20260701_012d00N_008d52E_UTC.csv"
df = pd.read_csv(file_name, skiprows=20)

# 2. Combine Year, Month, and Day into a single proper Date column
df['Date'] = pd.to_datetime(df[['YEAR', 'MO', 'DY']].rename(columns={'YEAR': 'year', 'MO': 'month', 'DY': 'day'}))

# 3. Replace NASA's missing data code (-999) with actual blank spaces (NaN)
df.replace(-999, np.nan, inplace=True)

# 4. Fill any missing satellite precipitation data using the corrected ground precipitation column
df['IMERG_PRECTOT'] = df['IMERG_PRECTOT'].fillna(df['PRECTOTCORR'])

# 5. Drop the old split year/month/day columns and reorganize
df.drop(columns=['YEAR', 'MO', 'DY'], inplace=True)
cols = ['Date'] + [col for col in df.columns if col != 'Date']
df = df[cols]

# 6. Save the perfectly clean data to a new file
output_name = "cleaned_kano_weather.csv"
df.to_csv(output_name, index=False)

print(f"Success! Cleaned data saved as '{output_name}'")
print(f"Total rows processed: {len(df)}")