Software source reference
"""
Contains utility functions for writing the measured data to a csv file.
Authors:
Min Tai Yap
"""
import os
import csv
import datetime
# Function to format numbers to two decimal places
[docs]
def format_value(value):
"""
Converts floating point value to a string cut-off at the second decimal.
:param value: Floating point number to convert
:returns: If the input is a numerical value, string representation of the input cut-off at the second decimal place,
otherwise returns the input
"""
if isinstance(value, (int, float)):
return f"{value:.2f}"
return value
[docs]
def csv_write(ws_name, flag_csv, flag_csv_df, folder_path, current_date, current_hour, current_time, battery_voltage, rpm, rain_mm, rain_ml,
sht_temp, sht_rh, bmp_temp, bmp_pressure, bmp_altitude, angle_degrees, new_speed, SI1151_visible, SI1151_UV, SI1151_IR):
"""
Helper function to write a row of measurements to a csv file. Each hour, a new csv file is created, in which the measurements
are recorded every 60 seconds. For each single day, a new folder is created. The location of the csv files is thus given by
{folder_path}/{current_date}_data/{ws_name}_{current_data}_{current_hour}.csv.
In practice, we disregard the first measurement after starting the weather station for data consistency. Note that we only
do this for the first directory specified in parameter/user_path.txt
:param ws_name: Prefix of the csv files, specified in parameter/parameters.txt
:param flag_csv: If this flag is set to 0, we additionally write the csv header to the csv file and set the flag to 1
:param flag_csv_df: This flag is set to 0 to ignore the first measurement after booting
:param folder_path: Path of the directory in which the data is collected
:param current_date: Current data
:param current_hour: Current hour
:param current_time: Timestamp to be written in the csv file
:param battery_voltage: Battery voltage in V
:param rpm: Wind speed in rpm measured by the anemometer
:param rain_mm: Rainfall measured in mm
:param rain_ml: Rainfall collected in the rain gauge in ml
:param sht_temp: Temperature measured by the SHT45 sensor in °C
:param sht_rh: Humidity measured by the SHT45 sensor in %
:param bmp_temp: Temperature measured by the BMP388 sensor in °C
:param bmp_pressure: Air pressure measured by the BMP388 sensor in hPa
:param bmp_altitude: Altitude measured by the BMP388 sensor in m
:param angle_degrees: Wind direction in °
:param new_speed: Wind speed measured in m/s
:param SI1151_visible: Visible light illuminance in lux measured by the SI1151 sensor
:param SI1151_UV: UV light illuminance in lux measured by the SI1151 sensor
:param SI1151_IR: IR light illuminance in lux measured by the SI1151 sensor
:returns: Tuple containing the updated csv_flag and the file path of the csv file
"""
# Define the filename with the current date
filename = f"{ws_name}_{current_date}_{current_hour}.csv"
folder_path_new = f"{current_date}_data"
folder_path_new = os.path.join(folder_path, folder_path_new)
# Check if the directory exists, if not, create it
if not os.path.exists(folder_path_new):
os.makedirs(folder_path_new)
# New fille path
file_path = os.path.join(folder_path_new, filename)
# Check if the file exists, if not, create it
mode = 'a' if os.path.exists(file_path) else 'w'
# Open the CSV file in append mode
with open(file_path, mode, newline='') as csvfile:
# Create a CSV writer object
csv_writer = csv.writer(csvfile)
if flag_csv == 0: # if flag_csv == 0 or mode == 'w':
# Write header if needed
csv_writer.writerow(['Date', 'Wind vane RPM', 'Rain (mm)', 'Rain (ml)', 'temperature_sht45', 'humidity_sht45', 'temperature_bmp388','pressure_bmp388',
'altitude_bmp388', 'Wind direction', 'Wind speed', 'Visible light', 'UV', 'IR', 'Battery_voltage'])
print("Writing header for csv")
# Update
flag_csv = 1
if flag_csv_df == 1: # disregard the first data
# Write the variables to the CSV file
csv_writer.writerow([current_time, rpm, rain_mm, rain_ml, sht_temp, sht_rh, bmp_temp, bmp_pressure, bmp_altitude, angle_degrees, new_speed,
SI1151_visible, SI1151_UV, SI1151_IR, battery_voltage])
return flag_csv, file_path
