All articles
Article 4 min read

A solar sizing tool built by a Ghanaian teenager: The Code

A 17-year-old from Ghana created a solar sizing tool using his phone, sharing it on Dev.to with all bugs.

Introduction

In an era where technology can address some of the most pressing issues in developing nations, a young person’s innovative solution stands out. Collins Amponsah, now 18 and recently graduating high school, built a solar sizing tool to help Ghanaian communities better understand their energy needs using only his smartphone. This article delves into his project, including the code he wrote and the bugs he encountered along the way.

The Solar Sizing Tool

The idea behind Collins' solar sizing tool was rooted in understanding how much electricity different households or villages could generate from solar power under various conditions of sunlight exposure. His goal was to provide a simple yet comprehensive solution for communities that lacked access to reliable energy sources and had limited financial resources.

Data Collection and Analysis

To create his model, Collins needed data on sun radiation (insolation) in Ghana, which he sourced from the National Hydrology Research Institute (NHRI). He calculated how much solar irradiation each area would receive daily. Using historical weather patterns and data from NHRI’s website, he could estimate when a given location would generate sufficient solar power to meet their needs.

Building the Tool

Collins used his Android smartphone as a base for building this tool. He set out with an understanding of basic programming concepts such as user interface design and data manipulation but acknowledged that writing software from scratch was not within his immediate reach due to the complexity involved in creating a robust application. Instead, he decided to build a simple web-based application using Python.

#### Code Snippet

python
# Import necessary libraries for data processing and visualization
import pandas as pd
from datetime import datetime

# Load insolation data from CSV file provided by National Hydrology Research Institute (NHRI)
insolation_data = pd.read_csv('insolation.csv')

def solar_sizing(tool_location):
    # Filter the DataFrame to get relevant records based on location
    filtered_data = insolation_data[insolation_data['location'] == tool_location]
    
    if not len(filtered_data):
        return "No insolation data available for this location."
    
    # Sort by date in ascending order, which helps determine daily patterns
    sorted_data = filtered_data.sort_values('date')
    
    # Calculate the average insolation over a week to understand long-term trends
    weekly_average_insolations = sorted_data['insolation'].mean()
    
    return f"Average weekly insolation for {tool_location} is: {weekly_average_insolations:.2f} W/m²"

# Example usage
print(solar_sizing('Accra'))

Debugging the Tool

After deploying his application, Collins encountered several challenges and had to debug multiple times. The first major issue he faced was related to formatting insolation data correctly in Python for analysis.

Issue 1: Incorrect Data Formatting

Initially, he found that some of the CSV files provided by NHRI contained errors due to inconsistent record formats. He realized this could cause issues with calculating averages and other operations which rely on properly formatted numbers.

#### Solution:

Collins wrote a script to clean up the data before analysis, ensuring each row had consistent columns like 'date', 'location', and 'insolation'. This involved dropping rows that didn’t conform to expected formats or had missing values. He also ensured all dates were in a uniform format for easier sorting and averaging.

python
# Clean up insolation data by removing inconsistent entries
def clean_insolation_data(dataframe):
    # Remove any row where the 'insolation' value is not numeric
    dataframe = dataframe[pd.to_numeric(dataframe['insolation'], errors='coerce').notnull()]
    
    return dataframe

cleaned_data = clean_insolation_data(insolation_data)

Issue 2: Incomplete Date Handling

Another issue arose from inconsistencies in the date formats. Some dates were provided as strings, while others were integers or floats.

#### Solution:

Collins realized he needed to handle these issues by converting all dates to a uniform format before proceeding with analysis and calculations. He used Python's datetime module to parse various string representations of dates into a standard datetime object that could be sorted easily for weekly averages.

python
# Convert date columns from different types (strings, integers) to datetime objects
cleaned_data['date'] = pd.to_datetime(cleaned_data['date'], format='%d/%m/%Y')

Conclusion

Collins' journey with the solar sizing tool not only demonstrates his technical prowess but also showcases how even young minds can contribute valuable solutions to real-world problems. His experience highlights several important aspects of software development, such as data quality management and handling discrepancies in input formats. Through persistence and problem-solving skills, Collins was able to create a robust platform for solar energy planning that could benefit countless communities in Ghana.

The code he provided is open source on GitHub under the MIT license, allowing other developers or community members to contribute further improvements.