import { WSQuery } from '../types' // Returns the current UTC time in the format // YYYY-MM-DD HH:MM:SS const getCurrentTimeString = () => { const now = new Date() const formatter = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false, timeZone: 'UTC', }) return formatter.format(now).replace(',', '') } // Convert miles per hour to kilometers per hour const mphToKph = (mph: string) => (parseFloat(mph) * 1.60934).toFixed(1) // Convert Fahreneheit to Celsius const fToC = (f: string) => (((parseFloat(f) - 32) * 5) / 9).toFixed(1) // Convert inches Hg to hectopascal const inHgTohPa = (inHg: string) => (parseFloat(inHg) * 33.8639).toFixed(1) // Convert inches to mm const inchToCm = (inch: string) => (parseFloat(inch) * 25.4).toFixed(1) // Calculate the wet bulb temperature // const wetbulbtemperature = (tempf: string, humidity: string) => { const T = ((parseFloat(tempf) - 32) * 5) / 9 const RH = parseFloat(humidity) return ( T * Math.atan(0.151977 * Math.sqrt(RH + 8.313659)) + Math.atan(T + RH) - Math.atan(RH - 1.676331) + 0.00391838 * Math.pow(RH, 1.5) * Math.atan(0.023101 * RH) - 4.686035 ).toFixed(2) } const transformData = ( params: Omit ) => ({ ...params, dateutc: params.dateutc === 'now' ? getCurrentTimeString() : params.dateutc, windspeedkph: mphToKph(params.windspeedmph), windgustkph: mphToKph(params.windgustmph), tempc: fToC(params.tempf), dewptc: fToC(params.dewptf), baromhpa: inHgTohPa(params.baromin), rainmm: inchToCm(params.rainin), dailyrainmm: inchToCm(params.dailyrainin), indoortempc: fToC(params.indoortempf), wetbulbtemperature: wetbulbtemperature(params.tempf, params.humidity), }) export default transformData