Skip to content

Commit

Permalink
working on weather data
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Nov 16, 2018
1 parent eb292e2 commit e7c4cc0
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 6 deletions.
11 changes: 11 additions & 0 deletions energy_demand/read_write/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,17 @@ def load_temp_data(local_paths, weather_yrs_scenario, save_fig=False):
temp_data : dict
Temperatures
"""

'''
# Get path to weather_yr
# Get all station_ids with coordinates
# Get all min, max weather data
'''


temp_data_short = defaultdict(dict)
weather_stations_with_data = defaultdict(dict)

Expand Down
142 changes: 136 additions & 6 deletions energy_demand/scripts/weather_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,146 @@
http://joehamman.com/2013/10/12/plotting-netCDF-data-with-Python/
"""
import csv
import pandas as pd
import pytemperature
import xarray as xr

tasmax = "C:/_WEATHERDATA/nf-2020/2020/m00m/daily/WAH_m00m_tasmax_daily_g2_2020.nc"
tasmin = "C:/_WEATHERDATA/nf-2020/2020/m00m/daily/WAH_m00m_tasmin_daily_g2_2020.nc"
def get_temp_data_from_nc(path_nc_file, attribute_to_keep):
"""Open c file, convert it into dataframe,
clean it and extract attribute data
"""
# Open as xarray
x_array_data = xr.open_dataset(path_nc_file)

# Convert to dataframe
df = x_array_data.to_dataframe()

# Add index as columns
df = df.reset_index()

# Drop unnecessary columns
df = df.drop(['time_bnds', 'rotated_latitude_longitude', 'rlat', 'rlon', 'bnds', 'height'], axis=1)

# Drop all missing value
df = df.dropna(subset=[attribute_to_keep])

return df

def convert_to_celcius(df, attribute_to_convert):
"""# Convert Kelvin to Celsius (# Kelvin to Celsius)
"""
df[attribute_to_convert] = df[attribute_to_convert].apply(pytemperature.k2c)
return df

def extend_360_day_to_365(df, attribute):
"""Add evenly distributed days across the 360 days
Return
------
out_list : list
Returns list with days with the following attributes
e.g. [[lat, long, yearday365, value]]
"""
# Copy the following position of day in the 360 year
days_to_duplicate = [60, 120, 180, 240, 300]

#columns = df.columns
#df_365 = pd.DataFrame(columns=columns)

out_list = []
# Copy every sithieth day over the year and duplicate it. Do this five times --> get from 360 to 365 days
vals = []
day_cnt, day_cnt_365 = 0, 0

for index, row in df.iterrows():
day_cnt += 1
# Create row into dataframe
#df_row = pd.DataFrame([list(row)], columns=columns)

# Copy extra day
if day_cnt in days_to_duplicate:
#df_row['time'] = day_cnt_365 # Add yearday
#df_365 = df_365.append(df_row)
out_list.append([row['lat'], row['lon'], day_cnt_365, row[attribute]])
day_cnt_365 += 1

# Copy day
day_cnt_365 += 1
#df_row['time'] = day_cnt_365 # Add yearday
#df_365 = df_365.append(df_row)
out_list.append([row['lat'], row['lon'], day_cnt_365, row[attribute]])

# Reset counter if next weather station
if day_cnt == 360:
day_cnt = 0
day_cnt_365 = 0

return out_list

def write_wether_data():
"""Write weather data to array
"""
#station_coordinates =
#station_dat =

nr_of_entries = len(min_vals)
cnt = 0
for i in range(nr_of_entries):
print("o " + str(i))
min_val = max_vals[cnt]
max_val = min_vals[cnt]
lon = longs[cnt]
lat = lats[cnt]

if cnt == 0:
station_name = "station_{}_{}.csv".format(lon, lat)
station = pd.DataFrame(columns=list('tasmax', 'tasmin'))
path_station = "C:/_scrap/{}".format(station_name)

station_row = pd.DataFrame([[lon, lat, min_val, max_val]], columns=list( 'tasmax', 'tasmin'))

station.append(station_row)

if cnt == 364:
# Write station
station.to_csv(station, path_station)
station_id_positions.append([station_name, lon, lat])
cnt = 0

cnt +=1

return station_coordinates, station_data


path_tasmax = "C:/_WEATHERDATA/nf-2020/2020/m00m/daily/WAH_m00m_tasmax_daily_g2_2020.nc"
path_tasmin = "C:/_WEATHERDATA/nf-2020/2020/m00m/daily/WAH_m00m_tasmin_daily_g2_2020.nc"

out_path = "C:/_scrap/test.csv"
out_path_station_names = "C:/_scrap/station_position.csv"
ds_max = xr.open_dataset(tasmax)

# Load data
df_max = get_temp_data_from_nc(path_tasmax, 'tasmax')
df_min = get_temp_data_from_nc(path_tasmin, 'tasmin')

# Convert Kelvin to Celsius (# Kelvin to Celsius)
df_max['tasmax'] = convert_to_celcius(df_max, 'tasmax')
df_min['tasmin'] = convert_to_celcius(df_min, 'tasmin')

# Convert 360 day to 365 days
list_max = extend_360_day_to_365(df_max, 'tasmax')
list_min = extend_360_day_to_365(df_min, 'tasmin')

# Write out single weather stations as numpy array

# Write out coordinates of weather data to csv





raise Exception("ff")
'''ds_max = xr.open_dataset(tasmax)
ds_min = xr.open_dataset(tasmin)
df_max = ds_max.to_dataframe()
Expand All @@ -39,8 +169,8 @@
# Convert Kelvin to Celsius (# Kelvin to Celsius)
df_max['tasmax'] = df_max['tasmax'].apply(pytemperature.k2c)
df_min['tasmin'] = df_min['tasmin'].apply(pytemperature.k2c)

df_min['tasmin'] = df_min['tasmin'].apply(pytemperature.k2c)'''
'''
# Add evenly distributed days
# Copy every sithieth day over the year and duplicate it. Do this five times --> get from 360 to 365 days
min_vals = []
Expand Down Expand Up @@ -86,7 +216,7 @@
#print(max_vals)
print(len(min_vals))
print(len(max_vals))

'''
nr_of_entries = len(min_vals)
cnt = 0
for i in range(nr_of_entries):
Expand Down

0 comments on commit e7c4cc0

Please sign in to comment.