def assign_data_to_year(carbon_trust_data, base_yr): """Fill every base year day with correct data Arguments ---------- carbon_trust_data : data Raw data base_yr : int Base Year """ shape_non_peak_y_dh = np.zeros((365, 24), dtype="float") # Create list with all dates of a whole year list_dates = date_prop.fullyear_dates( start=date(base_yr, 1, 1), end=date(base_yr, 12, 31)) # Assign every date to the place in the array of the year for yearday in list_dates: month_python = yearday.timetuple().tm_mon - 1 # - 1 because in _info: Month 1 = Jan yearday_python = yearday.timetuple().tm_yday - 1 # - 1 because in _info: 1.Jan = 1 daytype = date_prop.get_weekday_type(yearday) # Get day from HES raw data array _data = carbon_trust_data[daytype][month_python] # Add values to yearly _data = np.array(list(_data.items())) shape_non_peak_y_dh[yearday_python] = np.array(_data[:, 1], dtype="float") return shape_non_peak_y_dh
def assign_hes_data_to_year(nr_of_appliances, hes_data, base_yr): """Fill every base year day with correct data Arguments ---------- nr_of_appliances : dict Defines how many appliance types are stored (max 10 provided in original hes file) hes_data : array HES raw data for every month and daytype and appliance base_yr : float Base year to generate shapes Returns ------- year_raw_values : array Energy data for every day in the base year for every appliances """ year_raw_values = np.zeros((365, 24, nr_of_appliances), dtype=float) #yeardays, houry, appliances # Create list with all dates of a whole year list_dates = date_prop.fullyear_dates( start=date(base_yr, 1, 1), end=date(base_yr, 12, 31)) # Assign every date to the place in the array of the year for yearday_date in list_dates: month_python = date_prop.get_month_from_yeraday( yearday_date.timetuple().tm_year, yearday_date.timetuple().tm_yday) yearday_python = date_prop.date_to_yearday( yearday_date.timetuple().tm_year, yearday_date.timetuple().tm_mon, yearday_date.timetuple().tm_mday) daytype_str = date_prop.get_weekday_type(yearday_date) # Get day from HES raw data array year_raw_values[yearday_python] = hes_data[daytype_str][month_python] return year_raw_values
def test_get_weekday_type(): """Testing """ date_to_test = date(2015, 4, 3) expected = 'holiday' # call function out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2017, 9, 7) expected = 'working_day' # call function out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2017, 9, 9) expected = 'holiday' # call function out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected # Test year date_to_test = date(2015, 4, 3) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2014, 4, 21) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2013, 4, 1) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2012, 4, 9) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2011, 1, 2) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2010, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2009, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2008, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2007, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2006, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2005, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2004, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2003, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected date_to_test = date(2002, 12, 25) expected = 'holiday' out_value = date_prop.get_weekday_type(date_to_test) assert out_value == expected
def read_raw_carbon_trust_data(folder_path): """Read in raw carbon trust dataset (used for service sector) Arguments ---------- foder_path : string Path to folder with stored csv files Returns ------- load_shape_y_dh : array Load shape for every day (tot sum 365) ((365, 24)) load_peak_shape_dh : array Peak loadshape for peak day ((24)) shape_non_peak_yd : array Yh load profile ((365)) Note ----- 1. Get gas peak day load shape (the max daily demand can be taken from weather data, the daily shape however is not provided by samson) 2. Iterate individual files which are about a year (even though gaps exist) 3. Select those day with the maximum load 4. Get the hourly shape of this day 5. Calculate total demand of every day 6. Assign percentag of total daily demand to each hour """ def initialise_main_dict(): """Helper function to initialise dict """ out_dict_av = {'working_day': {}, 'holiday': {}} for dtype in out_dict_av: month_dict = {} for month in range(12): month_dict[month] = {k: [] for k in range(24)} out_dict_av[dtype] = month_dict return out_dict_av def initialise_out_dict_av(): """Helper function to initialise dict""" out_dict_av = {'working_day': {}, 'holiday': {}} for dtype in out_dict_av: month_dict = {} for month in range(12): month_dict[month] = {k: 0 for k in range(24)} out_dict_av[dtype] = month_dict return out_dict_av # Get all files in folder all_csv_in_folder = os.listdir(folder_path) main_dict = initialise_main_dict() carbon_trust_raw = dict_init_carbon_trust() nr_of_line_entries = 0 dict_max_dh_shape = {} # Itreatu folder with csv files for path_csv_file in all_csv_in_folder: path_csv_file = os.path.join(folder_path, path_csv_file) print(f"Reading {path_csv_file}") # Read csv file with open(path_csv_file, 'r') as csv_file: read_lines = csv.reader(csv_file, delimiter=',') _ = next(read_lines) max_d_demand = 0 # Used for searching maximum max_dh_shape = np.zeros((24), dtype="float") # Count number of lines in CSV file row_data = list(read_lines) count_row = len(row_data) #print("Number of lines in csv file: " + str(count_row)) # Calc yearly demand based on one year data measurements if count_row > 365: # if more than one year is in csv file for day, row in enumerate(row_data): # Ignore and entries beyond the expected 48 half-hourly entries (plus one date) if len(row) != 49: logging.debug(f"Expected 49 cells in row {day} got {len(row)}: {row} in {path_csv_file}") row = row[:49] # Use only data of one year if day > 365: continue load_shape_dh = np.zeros((24), dtype="float") # Convert all values except date into float values try: # Take zero if any value is negative row[1:] = map(lambda c: max(float(c),0), row[1:]) except ValueError: # Handle empty cells - assume zero if empty def convert_empty(cell): if cell == '': return 0 else: return max(float(cell), 0) row[1:] = map(convert_empty, row[1:]) daily_sum = sum(row[1:]) # Total daily sum nr_of_line_entries += 1 # Nr of lines added day = int(row[0].split("/")[0]) month = int(row[0].split("/")[1]) year = int(row[0].split("/")[2]) # Redefine yearday to another year and skip 28. of Feb. if date_prop.is_leap_year(int(year)) is True: year = year + 1 # Shift whole dataset to another year if month == 2 and day == 29: continue #skip leap day date_row = date(year, month, day) daytype = date_prop.get_weekday_type(date_row) yearday_python = date_row.timetuple().tm_yday - 1 # - 1 because in _info: 1.Jan = 1 month_python = month - 1 # Month Python h_day, cnt, control_sum = 0, 0, 0 # ----------------------------------------------- # Iterate half hour data and summarise to hourly # ----------------------------------------------- for data_h in row[1:]: # Skip first date row in csv file cnt += 1 if cnt == 2: demand_h = first_data_h + data_h control_sum += demand_h # Add demand carbon_trust_raw[yearday_python][h_day].append(demand_h) # Store demand according to daytype (aggregated by doing so) main_dict[daytype][month_python][h_day].append(demand_h) if daily_sum == 0: # Skip row if no demand of the day load_shape_dh[h_day] = 0 continue else: load_shape_dh[h_day] = demand_h / daily_sum cnt = 0 h_day += 1 # Value lagging behind one iteration first_data_h = data_h # Test if this is the day with maximum demand of this CSV file if daily_sum >= max_d_demand: max_d_demand = daily_sum max_dh_shape = load_shape_dh # Check if 100 % np.testing.assert_almost_equal(control_sum, daily_sum, decimal=7, err_msg=f"Row sum failed {day}: {row} in {path_csv_file}") # Add load shape of maximum day in csv file dict_max_dh_shape[path_csv_file] = max_dh_shape # --------------- # Data processing # --------------- # --Average average maxium peak dh of every csv file load_peak_average_dh = np.zeros((24), dtype="float") for peak_shape_dh in dict_max_dh_shape.values(): load_peak_average_dh += peak_shape_dh load_peak_shape_dh = load_peak_average_dh / len(dict_max_dh_shape) # ----------------------------------------------- # Calculate average load shapes for every month # ----------------------------------------------- out_dict_av = initialise_out_dict_av() for daytype in main_dict: for month in main_dict[daytype]: for hour in main_dict[daytype][month]: nr_of_entries = len(main_dict[daytype][month][hour]) if nr_of_entries != 0: out_dict_av[daytype][month][hour] = sum(main_dict[daytype][month][hour]) / nr_of_entries # ---------------------------------------------------------- # Distribute raw data into base year depending on daytype # ---------------------------------------------------------- year_data = assign_data_to_year(out_dict_av, 2015) # Calculate yearly sum yearly_demand = np.sum(year_data) # Create load_shape_dh load_shape_y_dh = np.zeros((365, 24), dtype="float") for day, dh_values in enumerate(year_data): load_shape_y_dh[day] = load_profile.abs_to_rel(dh_values) # daily shape np.testing.assert_almost_equal(np.sum(load_shape_y_dh), 365, decimal=2, err_msg="") # Calculate shape_non_peak_yd shape_non_peak_yd = np.zeros((365), dtype="float") for yearday, carbon_trust_d in enumerate(year_data): shape_non_peak_yd[yearday] = np.sum(carbon_trust_d) shape_non_peak_yd = shape_non_peak_yd / yearly_demand np.testing.assert_almost_equal(np.sum(shape_non_peak_yd), 1, decimal=2, err_msg="") return load_shape_y_dh, load_peak_shape_dh, shape_non_peak_yd