mirror of
https://github.com/LeRoid-hub/humiditycalc.git
synced 2025-01-31 03:34:56 +00:00
converts to absolut Humidity
This commit is contained in:
parent
d28b11acaa
commit
434b9860c3
26
internal/calc.go
Normal file
26
internal/calc.go
Normal file
@ -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
|
||||||
|
}
|
9
main.go
Normal file
9
main.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/LeRoid-hub/humiditycalc/server"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
server.Run()
|
||||||
|
}
|
69
server/server.go
Normal file
69
server/server.go
Normal file
@ -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 := `<html>
|
||||||
|
<head>
|
||||||
|
<title>Humidity Calculator</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Humidity Calculator</h1>
|
||||||
|
<p>Calculate absolute humidity in g/m³ given temperature in Celsius and relative humidity percentage.</p>
|
||||||
|
<form action="/absolute-humidity">
|
||||||
|
<label for="temp">Temperature (°C):</label>
|
||||||
|
<input type="text" id="temp" name="temp" required>
|
||||||
|
<br>
|
||||||
|
<label for="rh">Relative Humidity (%):</label>
|
||||||
|
<input type="text" id="rh" name="rh" required>
|
||||||
|
<br>
|
||||||
|
<button type="submit">Calculate</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>`
|
||||||
|
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)))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user