From 434b9860c38b195ae60e90a2889ece98bbaae08d Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 26 Oct 2024 16:31:51 +0200 Subject: [PATCH] converts to absolut Humidity --- go.mod | 3 +++ internal/calc.go | 26 ++++++++++++++++++ main.go | 9 +++++++ server/server.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 go.mod create mode 100644 internal/calc.go create mode 100644 main.go create mode 100644 server/server.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1ee8d69 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/LeRoid-hub/humiditycalc + +go 1.22.2 diff --git a/internal/calc.go b/internal/calc.go new file mode 100644 index 0000000..c767d99 --- /dev/null +++ b/internal/calc.go @@ -0,0 +1,26 @@ +package internal + +import ( + "math" +) + +// AbsoluteHumidity calculates absolute humidity in g/m³ given temperature in Celsius and relative humidity percentage. +func AbsoluteHumidity(tempCelsius, relativeHumidity float64) float64 { + // Constants + const Mw = 18.016 // Molar mass of water vapor in g/mol + const R = 8.314 // Universal gas constant in J/(mol·K) + const A = 6.112 // Constant for saturation vapor pressure in hPa + const B = 17.67 // Constant for saturation vapor pressure + const C = 243.5 // Constant for saturation vapor pressure in Celsius + + // Convert temperature from Celsius to Kelvin + tempKelvin := tempCelsius + 273.15 + + // Calculate saturation vapor pressure (in hPa) using temperature in Celsius + saturationVaporPressure := A * math.Exp(B*tempCelsius/(tempCelsius+C)) + + // Calculate absolute humidity + absoluteHumidity := (relativeHumidity / 100) * saturationVaporPressure * Mw / (R * tempKelvin) + + return absoluteHumidity +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..653b6fe --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "github.com/LeRoid-hub/humiditycalc/server" +) + +func main() { + server.Run() +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..bebfcb3 --- /dev/null +++ b/server/server.go @@ -0,0 +1,69 @@ +package server + +import ( + "fmt" + "net/http" + "strconv" + + "github.com/LeRoid-hub/humiditycalc/internal" +) + +// StartServer starts the HTTP server. +func Run() { + http.HandleFunc("/", HandleIndex) + + http.HandleFunc("/absolute-humidity", HandleAbsoluteHumidity) + + http.ListenAndServe(":8080", nil) +} + +func HandleIndex(w http.ResponseWriter, r *http.Request) { + html := ` + + Humidity Calculator + + +

Humidity Calculator

+

Calculate absolute humidity in g/m³ given temperature in Celsius and relative humidity percentage.

+
+ + +
+ + +
+ +
+ +` + w.Write([]byte(html)) +} + +func HandleAbsoluteHumidity(w http.ResponseWriter, r *http.Request) { + // Parse query parameters + tempCelsius, relativeHumidity := r.URL.Query().Get("temp"), r.URL.Query().Get("rh") + if tempCelsius == "" || relativeHumidity == "" { + http.Error(w, "missing query parameters", http.StatusBadRequest) + return + } + + // Convert query parameters to float64 + + temp, err := strconv.ParseFloat(tempCelsius, 32) + if err != nil { + http.Error(w, "invalid temperature value", http.StatusBadRequest) + return + } + + rh, err := strconv.ParseFloat(relativeHumidity, 32) + if err != nil { + http.Error(w, "invalid relative humidity value", http.StatusBadRequest) + return + } + + // Calculate absolute humidity + absoluteHumidity := internal.AbsoluteHumidity(temp, rh) + + // Write response + w.Write([]byte(fmt.Sprintf("Absolute humidity: %.2f g/m³\n", absoluteHumidity))) +}