{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **Import all functions and libraries**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.Functions_Energy_Model import *\n",
    "from src.Functions_General import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## **Inputs**\n",
    "\n",
    "\n",
    "Before to run the simulation we need to set some important parameter such as:\n",
    "\n",
    "- **location_ita**: the name of the location in Italian;\n",
    "\n",
    "- **tilt_angle**: the tilt angle of the module surface (inclination);\n",
    "\n",
    "- **azimuth**: the azimuth angle of the module surface (south : 180; north: 0; east: 90; west: 270);\n",
    "\n",
    "- **derating_factor**: derating factor for the yearly productivity of the photovoltaic generator in percentage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "location_ita = 'Milano' # location in Italian"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "capacity = 100 # kWp\n",
    "tilt_angle = 40 # degrees\n",
    "azimuth = 180 # degrees"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_years = 1 # number of years to simulate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "derating_factor_percent = 1 # derating factor that reduce the efficiency of the modules in percentage [%]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = yaml.safe_load(open(\"config.yml\", 'r'))\n",
    "path_export = str(config['filename_output_csv_gen_pv']) # path to save the results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **1. Initialize parameters for simulation**\n",
    "\n",
    "Using the input parametes specified in the previous section we initialize the productivity simulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "location_eng = location_italian_to_english(location_ita)\n",
    "\n",
    "path = \"config.yml\"\n",
    "filename = \"\"\n",
    "key  = \"project_lifetime_yrs\"\n",
    "value = num_years\n",
    "add_to_file_yml(path, filename, key, value)\n",
    "\n",
    "suppress_printing(generate_calendar) # generate the calendar for the location\n",
    "\n",
    "coordinates_dataset = suppress_printing(create_coordinates_dataset, [location_eng]) # create a dataset with the coordinates of all the locations\n",
    "\n",
    "derating_factor = derating_factor_percent / 100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **2. Calculate productivity for 1 kWp**\n",
    "\n",
    "For the selected location, a dictionary with the photovoltaic productivity of a generator with a capacity of 1 kWp is simulated.\n",
    "\n",
    "\n",
    "The dictionary has a unique key equal to the location name in english and a dataframe as element. \n",
    "\n",
    "The dataframe has the following structure:\n",
    "\n",
    "| datetime  (type DatetimeIndex)| 0|     \n",
    "| :----:| :----:|\n",
    "| 2025-01-01  00:00:00+01:00| 0| \n",
    "| 2025-01-01  00:15:00+01:00| 0| \n",
    "| ...| ...| ...| ...| ...| "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_ac_energies_resampled = suppress_printing(simulate_1_kWp_generators, coordinates_dataset, tilt_angle, azimuth)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **3. Scaling productivity to capacity of the generator**\n",
    "\n",
    "The photovoltaic productivity is scaled respect the capacity of the generator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_ac_energies_gens = {} # initialization of the output dictionary\n",
    "result_ac_energies_gens['gen_pv_' + str(capacity) + '_kWp'] = result_ac_energies_resampled[location_eng] * capacity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **4. Derating productivity over years of the simulation**\n",
    "\n",
    "The photovoltaic productivity is derated with a yearly derating factor.\n",
    "\n",
    "The output is a dictionary with two level: the key for the first level is *gen_pv_{capacity}_kWp*, while in the second level the keys are the years of the simulation (e.g. 2025, 2026, ...). \n",
    "\n",
    "The element for each year is a dataframe with the following structure:\n",
    "\n",
    "| datetime  (type DatetimeIndex)| 0|     \n",
    "| :----:| :----:|\n",
    "| 2025-01-01  00:00:00+01:00| 0| \n",
    "| 2025-01-01  00:15:00+01:00| 0| \n",
    "| ...| ...| ...| ...| ...| "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_ac_energies_gens_derated = suppress_printing(simulate_gens_derated_productivity, derating_factor, result_ac_energies_gens)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **5. Create a dataframe with results**\n",
    "\n",
    "The dictionary is transformed in a dataframe with the following structure:\n",
    "\n",
    "| datetime  (type DatetimeIndex)| gen_pv_{capacity}_kWp|     \n",
    "| :----:| :----:|\n",
    "| 2025-01-01  00:00:00| 0| \n",
    "| 2025-01-01  00:15:00| 0| \n",
    "| ...| ...| ...| ...| ...| "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_ac_energies_to_csv_df = suppress_printing(simulate_unstacked_productivity, result_ac_energies_gens_derated) # create two unstacked dataframe (the other functions work with dictionaries)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **6. Export results to csv file**\n",
    "\n",
    "The dataframe is exported in a csv file.\n",
    "\n",
    "The default folder for the exportation is:\n",
    "\n",
    "        📂  *files/gen_pv*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_ac_energies_to_csv_df.to_csv(path_export, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### **7. Open csv file**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.startfile(path_export)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
