Page MenuHomec4science

merge_commune_all.py
No OneTemporary

File Metadata

Created
Thu, May 1, 09:25

merge_commune_all.py

import numpy as np
import pandas as pd
import xarray as xr
import os
import time
import sys
COMM_FP = '/work/hyenergy/output/solar_potential/Sonnendach_Gemeinden_Dachflaechen.csv'
OUTFILE = sys.argv[1]
INFILES = lambda i: (os.path.splitext( OUTFILE )[0] + '_%d.nc' %i)
N_BATCHES = int(sys.argv[2])
OUT_PV = os.path.splitext( OUTFILE )[0] + '_pv.csv'
OUT_GT = os.path.splitext( OUTFILE )[0] + '_gt.csv'
# Load data
comm_map = pd.read_csv(COMM_FP, encoding = 'cp1250', usecols = ['DF_UID', 'BFS_NUMMER', 'NAME'] )
# For-loop
init = True
gt_weight = 0
BFS_IDs = np.sort(comm_map.BFS_NUMMER.unique())
for BATCH in range(N_BATCHES):
tt = time.time()
try:
new_data = xr.open_dataset( INFILES(BATCH) )
new_df = new_data.to_dataframe().dropna().reset_index()
pv_new_df = new_df.pivot(values='pv_potential', index='time', columns='BFS_NUMMER')
gt_new_df = new_df.pivot(values='gt_weighted_by_APV', index='time', columns='BFS_NUMMER')
if init:
init = False
pv_df = pd.DataFrame(index = pv_new_df.index, columns = BFS_IDs).fillna(0)
gt_df = pd.DataFrame(index = pv_new_df.index, columns = BFS_IDs).fillna(0)
A_df = pd.Series(np.zeros(len(BFS_IDs)), index = BFS_IDs)
pv_df[pv_new_df.columns] += pv_new_df
gt_df[gt_new_df.columns] += gt_new_df
A_df[new_data.BFS_NUMMER.values] += new_data.APV_weight_sum.to_pandas()
except:
print('Error loading %s' %INFILES(BATCH))
print('Executed iteration %i in %.2fs' %(BATCH, time.time()-tt))
gt_df.loc[:,A_df > 0] /= A_df[A_df > 0]
print(pv_df.head())
print(gt_df.head())
# Save output
tt = time.time()
pv_df.to_csv( OUT_PV )
gt_df.to_csv( OUT_GT )
print('\nSaved outputs %s and %s in %.2fs' %(OUT_PV, OUT_GT, time.time()-tt))

Event Timeline