<commit_before>"""
===========================
Analytical energy demand model algorithm
===========================
File history and credits:
<NAME>  script development          24.08.15
<NAME>   formatting, refactoring, debugging and cleaning
<NAME>   integration in toolbox
<NAME>  refactoring to new properties file   22.03.16
"""
from __future__ import division
import pandas as pd
import numpy as np
import functions as f
import maker as m
import globalvar
from geopandas import GeoDataFrame as gpdf
import inputlocator

reload(f)
reload(globalvar)


def demand_calculation(locator, gv):
    """
    Algorithm to calculate the hourly demand of energy services in buildings
    using the integrated model of Fonseca et al. 2015. Applied energy.

    Parameters
    ----------
    :param locator: An InputLocator to locate input files
    :type locator: inputlocator.InputLocator

    path_radiation:
        path to solar radiation file in vertical surfaces
        RadiationYearFinal.csv
    path_schedules: string
        path to folder containing occupancy profile schedules
    path_weather : string
        path to weather data file weather_design_hour.csv
    path_results : string
        path to demand results folder demand
    path_properties: string
        path to folder with .shp files created with the properties script there should be a total of 6

    Returns
    -------
    single_demand: .csv
        csv file for every building with hourly demand data
    total_demand: .csv
        csv file of yearly demand data per buidling.
    """
    # local variables
    print "reading input files"
    weather_data = pd.read_csv(locator.get_weather_hourly(), usecols=['te', 'RH'])
    solar = pd.read_csv(locator.get_radiation())
    surfaces = pd.read_csv(locator.get_surfaces())
    prop_geometry = gpdf.from_file(locator.get_building_geometry())
    prop_geometry['footprint'] = prop_geometry.area
    prop_geometry['perimeter'] = prop_geometry.length
    prop_geometry = prop_geometry.drop('geometry', axis=1).set_index('Name')
    prop_HVAC = gpdf.from_file(locator.get_building_hvac()).drop('geometry', axis=1)
    prop_thermal = gpdf.from_file(locator.get_building_thermal()).drop('geometry', axis=1).set_index('Name')
    prop_occupancy_df = gpdf.from_file(locator.get_building_occupancy()).drop('geometry', axis=1).set_index('Name')
    prop_occupancy = prop_occupancy_df.loc[:, (prop_occupancy_df != 0).any(axis=0)] # trick to erase occupancies that are not being used (it speeds up the code)
    prop_architecture = gpdf.from_file(locator.get_building_architecture()).drop('geometry', axis=1).set_index('Name')
    prop_age = gpdf.from_file(locator.get_building_age()).drop('geometry', axis=1).set_index('Name')
    prop_comfort = gpdf.from_file(locator.get_building_comfort()).drop('geometry', axis=1).set_index('Name')
    prop_internal_loads = gpdf.from_file(locator.get_building_internal()).drop('geometry', axis=1).set_index('Name')
    # get temperatures of operation
    prop_HVAC_result= get_temperatures(locator, prop_HVAC).set_index('Name')
    # weather conditions
    T_ext = np.array(weather_data.te)
    RH_ext = np.array(weather_data.RH)
    #get list of uses
    list_uses = list(prop_occupancy.drop('PFloor', axis=1).columns)
    #get date
    date = pd.date_range(gv.date_start, periods=8760, freq='H')
    print "done"

    print "reading occupancy schedules"
    # get schedules
    schedules = m.schedule_maker(date, locator, list_uses)
    print "done"

    print "calculating thermal properties"
    # get thermal properties for RC model
    prop_RC_model = f.get_prop_RC_model(prop_occupancy, prop_architecture, prop_thermal, prop_geometry,
                                        prop_HVAC_result, surfaces, gv)
    print "done"

    # get timeseries of demand
    num_buildings = len(prop_RC_model.index)
    counter = 0
    for building in prop_RC_model.index[:2]:
        gv.models['calc-thermal-loads'](building, prop_occupancy.ix[building], prop_architecture.ix[building],
                           prop_geometry.ix[building], prop_HVAC_result.ix[building], prop_RC_model.ix[building],
                           prop_comfort.ix[building],prop_internal_loads.ix[building],
                           prop_age.ix[building], solar.ix[building], locator.get_demand_results_folder(),
                           schedules, T_ext, RH_ext, locator.get_temporary_folder(), gv, date, list_uses)
        gv.log('Building No. %(bno)i completed out of %(btot)i', bno=counter + 1, btot=num_buildings)
        counter += 1

    # get total file

    counter = 0
    for name in prop_RC_model.index:
        temporary_file = locator.get_temporary_file('%(name)sT.csv' % locals())
        # TODO: check this logic
        if counter == 0:
            df = pd.read_csv(temporary_file)
            counter += 1
        else:
            df2 = pd.read_csv(temporary_file)
            df = df.append(df2, ignore_index=True)
    df.to_csv(locator.get_total_demand(), index=False, float_format='%.2f')

    gv.log('finished')

def get_temperatures(locator, prop_HVAC):<commit_msg>Demand script also good to go<commit_after>"""
===========================
Analytical energy demand model algorithm
===========================
File history and credits:
<NAME>  script development          24.08.15
<NAME>   formatting, refactoring, debugging and cleaning
<NAME>   integration in toolbox
<NAME>  refactoring to new properties file   22.03.16
"""
from __future__ import division
import pandas as pd
import numpy as np
import functions as f
import maker as m
import globalvar
from geopandas import GeoDataFrame as gpdf
import inputlocator

reload(f)
reload(globalvar)


def demand_calculation(locator, gv):
    """
    Algorithm to calculate the hourly demand of energy services in buildings
    using the integrated model of Fonseca et al. 2015. Applied energy.

    Parameters
    ----------
    :param locator: An InputLocator to locate input files
    :type locator: inputlocator.InputLocator

    path_radiation:
        path to solar radiation file in vertical surfaces
        RadiationYearFinal.csv
    path_schedules: string
        path to folder containing occupancy profile schedules
    path_weather : string
        path to weather data file weather_design_hour.csv
    path_results : string
        path to demand results folder demand
    path_properties: string
        path to folder with .shp files created with the properties script there should be a total of 6

    Returns
    -------
    single_demand: .csv
        csv file for every building with hourly demand data
    total_demand: .csv
        csv file of yearly demand data per buidling.
    """
    # local variables
    print "reading input files"
    weather_data = pd.read_csv(locator.get_weather_hourly(), usecols=['te', 'RH'])
    solar = pd.read_csv(locator.get_radiation()).set_index('Name')
    surfaces = pd.read_csv(locator.get_surfaces())
    prop_geometry = gpdf.from_file(locator.get_building_geometry())
    prop_geometry['footprint'] = prop_geometry.area
    prop_geometry['perimeter'] = prop_geometry.length
    prop_geometry = prop_geometry.drop('geometry', axis=1).set_index('Name')
    prop_HVAC = gpdf.from_file(locator.get_building_hvac()).drop('geometry', axis=1)
    prop_thermal = gpdf.from_file(locator.get_building_thermal()).drop('geometry', axis=1).set_index('Name')
    prop_occupancy_df = gpdf.from_file(locator.get_building_occupancy()).drop('geometry', axis=1).set_index('Name')
    prop_occupancy = prop_occupancy_df.loc[:, (prop_occupancy_df != 0).any(axis=0)] # trick to erase occupancies that are not being used (it speeds up the code)
    prop_architecture = gpdf.from_file(locator.get_building_architecture()).drop('geometry', axis=1).set_index('Name')
    prop_age = gpdf.from_file(locator.get_building_age()).drop('geometry', axis=1).set_index('Name')
    prop_comfort = gpdf.from_file(locator.get_building_comfort()).drop('geometry', axis=1).set_index('Name')
    prop_internal_loads = gpdf.from_file(locator.get_building_internal()).drop('geometry', axis=1).set_index('Name')
    # get temperatures of operation
    prop_HVAC_result= get_temperatures(locator, prop_HVAC).set_index('Name')
    # weather conditions
    T_ext = np.array(weather_data.te)
    RH_ext = np.array(weather_data.RH)
    #get list of uses
    list_uses = list(prop_occupancy.drop('PFloor', axis=1).columns)
    #get date
    date = pd.date_range(gv.date_start, periods=8760, freq='H')
    print "done"

    print "reading occupancy schedules"
    # get schedules
    schedules = m.schedule_maker(date, locator, list_uses)
    print "done"

    print "calculating thermal properties"
    # get thermal properties for RC model
    prop_RC_model = f.get_prop_RC_model(prop_occupancy, prop_architecture, prop_thermal, prop_geometry,
                                        prop_HVAC_result, surfaces, gv)
    print "done"

    # get timeseries of demand
    num_buildings = len(prop_RC_model.index)
    counter = 0
    for building in prop_RC_model.index:
        gv.models['calc-thermal-loads'](building, prop_occupancy.ix[building], prop_architecture.ix[building],
                           prop_geometry.ix[building], prop_HVAC_result.ix[building], prop_RC_model.ix[building],
                           prop_comfort.ix[building],prop_internal_loads.ix[building],
                           prop_age.ix[building], solar.ix[building], locator.get_demand_results_folder(),
                           schedules, T_ext, RH_ext, locator.get_temporary_folder(), gv, date, list_uses)
        gv.log('Building No. %(bno)i completed out of %(btot)i', bno=counter + 1, btot=num_buildings)
        counter += 1

    # get total file

    counter = 0
    for name in prop_RC_model.index:
        temporary_file = locator.get_temporary_file('%(name)sT.csv' % locals())
        # TODO: check this logic
        if counter == 0:
            df = pd.read_csv(temporary_file)
            counter += 1
        else:
            df2 = pd.read_csv(temporary_file)
            df = df.append(df2, ignore_index=True)
    df.to_csv(locator.get_total_demand(), index=False, float_format='%.2f')

    gv.log('finished')

def get_temperatures(locator, prop_HVAC):
{'fraction_non_alphanumeric': 0.11009353078721745, 'fraction_numerical': 0.00808651597817615, 'mean_word_length': 3.5360141405214316, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '173076', 'n_tokens_mistral': 3213, 'n_tokens_neox': 3062, 'n_words': 756}