Page MenuHomec4science

merge_shading_stats.py
No OneTemporary

File Metadata

Created
Thu, May 1, 03:27

merge_shading_stats.py

import numpy as np
import pandas as pd
import os
import sys
from pathlib import Path
####################### INPUT ####################
stats_directory = sys.argv[1]
##################### CONSTANTS ###################
OVERALL_STATS_NAME = 'summary_stats.csv'
HOURLY_STATS_PATTERN = 'stats_*'
SEPARATOR = '|'
ZONE_NAME = 'zone'
SHADED_CELLS = 'null_cells'
UNSHADED_CELLS = 'non_null_cells'
STATISTIC = 'mean'
IDENTIFIER = 'DF_UID'
OUTFILE_NAME = 'shading.csv'
####################################################
# read summary statisitcs and calculate fully shaded ratio:
overall_stats = os.path.join( stats_directory, OVERALL_STATS_NAME )
overall_stats = pd.read_csv( overall_stats, sep = SEPARATOR )
# create a new dataframe
shading_stats = overall_stats.loc[:,[ ZONE_NAME, UNSHADED_CELLS, SHADED_CELLS ]]
shading_stats.rename({ ZONE_NAME : IDENTIFIER }, axis = 1, inplace = True)
# compute fully shaded ratio
shading_stats['n_cells'] = shading_stats[ UNSHADED_CELLS ] + shading_stats[ SHADED_CELLS ]
shading_stats['fully_shaded_ratio'] = shading_stats[ SHADED_CELLS ]/shading_stats['n_cells']
shading_stats.drop( SHADED_CELLS, axis = 1, inplace = True)
# go through all files in the directory that start with the given pattern and append the selected statistic
pathlist = Path(stats_directory).glob(HOURLY_STATS_PATTERN)
for path in pathlist:
# extract file, month and hour
path_to_file = str(path)
filename = os.path.splitext( os.path.split( path_to_file )[1] )[0]
filename_segments = filename.split('_')
column_name = ('%s_%s_%s' %(STATISTIC, filename_segments[1], filename_segments[2]))
new_file = pd.read_csv(path_to_file, sep=SEPARATOR)
new_file_mean = new_file.loc[:,[ ZONE_NAME, STATISTIC]]
new_file_mean.rename({ZONE_NAME : IDENTIFIER, STATISTIC : column_name}, axis = 1, inplace = True)
shading_stats = shading_stats.merge(new_file_mean, on = IDENTIFIER)
shading_stats.to_csv( os.path.join(stats_directory, OUTFILE_NAME ))

Event Timeline