Page MenuHomec4science

fill_shading_CH.py
No OneTemporary

File Metadata

Created
Fri, Apr 25, 15:00

fill_shading_CH.py

import pandas as pd
import numpy as np
import xarray as xr
from rooftop_handling import get_tilt_category, get_orientation_category
import os
import time
## ===== PATH DEFINITIONS ===== ##
path = '/work/hyenergy/output/solar_potential/geographic_potential/available_area/shading_stats'
data = 'CH_LV03/CH_shading_all.csv'
cats = 'CH_shading_categories.csv'
roofs = '/work/hyenergy/output/solar_potential/geographic_potential/available_area/ROOFS_CH_replaced.csv'
scratch = '/scratch/walch/scratch/'
## ===== LOAD DATA ===== ##
shading_cats = pd.read_csv( os.path.join( path, cats ))
roof_cols = pd.read_csv( roofs, usecols = ['DF_UID', 'NEIGUNG', 'AUSRICHTUNG'] ) # only necessary columns (memory)
shade_head = pd.read_csv( os.path.join( path, data ), nrows = 5) # load only column names - later one col loaded at a time
## ===== FILL ROWS ===== ##
# Step 1: find rows that need to be replaced (using fully_shaded_ratio)
# load fully shaded column and merge with roof data
tt = time.time()
print('Loading data ... ')
shading = pd.read_csv( os.path.join( path, data ), usecols = ['DF_UID', 'fully_shaded_ratio'])
all_roofs = roof_cols.merge( shading, on = 'DF_UID', how = 'left' ).drop_duplicates()
print('Finished in %.3f seconds\n' %(time.time() - tt))
# get index of rows without shading data
shade_null_idx = all_roofs[ 'fully_shaded_ratio' ].isnull()
# assign tilt and orientation categories to missing roofs
tt = time.time()
print('Computing tilt and orientation categories ... ')
replacemnt_shade = all_roofs.loc[shade_null_idx, ['DF_UID', 'NEIGUNG', 'AUSRICHTUNG']]
replacemnt_shade['tilt_cat'] = replacemnt_shade.apply(get_tilt_category, axis = 1)
replacemnt_shade['orientation_cat'] = replacemnt_shade.apply(get_orientation_category, axis = 1)
print('Finished in %.3f seconds\n' %(time.time() - tt))
# get replacement table with all values to be replaced
REPLACEMENT = replacemnt_shade.merge(shading_cats, how = 'left', on = ['orientation_cat', 'tilt_cat'])
print('Merged categories with missing indices\n')
# FOR-LOOP: replace each column seperately and save intermediate results (to avoid loading entire table)
replaced_shade = []
for col_name in shade_head.columns:
tmp = col_name.split('_')
if tmp[0] == 'mean':
month = tmp[1]
hour = tmp[2]
else: continue
# Load current column
tt = time.time()
print('Loading column %s ...' %col_name)
curr_shade = pd.read_csv( os.path.join( path, data ), usecols = ['DF_UID', col_name])
curr_roofs = roof_cols.merge( curr_shade, on = 'DF_UID', how = 'left' ).drop_duplicates()
print('Finished in %.3f seconds\n' %(time.time() - tt))
# replace all rows without partly shaded factor
tt = time.time()
print('Replacing missing values ...')
curr_roofs.loc[ shade_null_idx, col_name ] = REPLACEMENT[ col_name ].values
curr_roofs.rename( { col_name : 'partly_shading'}, axis = 1, inplace = True )
print('Finished in %.3f seconds\n' %(time.time() - tt))
# create timestep, make xarray and save data
tt = time.time()
print('Converting to xarray ...')
timestamp = pd.to_datetime('2001-%s-15 %s' %(month, hour))
roof_xr = curr_roofs.set_index('DF_UID').drop( columns = ['NEIGUNG', 'AUSRICHTUNG']).to_xarray()
roof_xr.coords['timestamp'] = timestamp
print('Finished in %.3f seconds\n' %(time.time() - tt))
roof_xr.to_netcdf( os.path.join( scratch, 'partly_shading_%s_%s.nc' %(month, hour)) )
replaced_shade.append( roof_xr )
all_replaced = xr.concat( replaced_shade , dim = 'timestamp')
all_replaced.to_netcdf( os.path.join( path, 'CH_partly_shading_replaced.nc') )
print('DONE - concatenated all dataframes and saved in %s' %(os.path.join( path, 'CH_partly_shading_replaced.nc')))

Event Timeline