UNPKG

@sailboat-computer/data-storage

Version:

Shared data storage library for sailboat computer v3

167 lines (154 loc) 4.88 kB
/** * Weather data fixtures for testing */ import { StoredData } from '../../src/types'; /** * Generate a single weather data point * * @param latitude - Latitude * @param longitude - Longitude * @param timestamp - Timestamp * @param options - Additional options * @returns Weather data point */ export function createWeatherDataPoint( latitude: number, longitude: number, timestamp: string, options: { temperature?: number; pressure?: number; pressureChangeRate?: number; humidity?: number; windSpeed?: number; windDirection?: number; cloudCover?: number; precipitation?: number; visibility?: number; tags?: Record<string, string>; } = {} ): StoredData { return { data: { latitude, longitude, temperature: options.temperature ?? 18.5, pressure: options.pressure ?? 1013.2, pressureChangeRate: options.pressureChangeRate ?? 0.5, humidity: options.humidity ?? 72, windSpeed: options.windSpeed ?? 15, windDirection: options.windDirection ?? 225, cloudCover: options.cloudCover ?? 30, precipitation: options.precipitation ?? 0, visibility: options.visibility ?? 10000 }, metadata: { id: `weather-${Date.now()}-${Math.floor(Math.random() * 1000)}`, category: 'weather.data', timestamp, tags: options.tags ?? { source: 'noaa', region: 'san-francisco-bay' } } }; } /** * Generate a storm weather data point * * @param latitude - Latitude * @param longitude - Longitude * @param timestamp - Timestamp * @returns Storm weather data point */ export function createStormWeatherDataPoint( latitude: number, longitude: number, timestamp: string ): StoredData { return createWeatherDataPoint(latitude, longitude, timestamp, { pressure: 990.5, pressureChangeRate: 2.5, windSpeed: 35, windDirection: 200, cloudCover: 90, precipitation: 15, visibility: 2000, tags: { source: 'noaa', region: 'san-francisco-bay', condition: 'storm' } }); } /** * Generate a grid of weather data points * * @param centerLat - Center latitude * @param centerLon - Center longitude * @param gridSize - Grid size in degrees * @param gridPoints - Number of points per side * @param baseTime - Base timestamp * @returns Grid of weather data points */ export function createWeatherDataGrid( centerLat: number, centerLon: number, gridSize: number, gridPoints: number, baseTime: Date ): StoredData[] { const result: StoredData[] = []; const halfGrid = gridSize / 2; const step = gridSize / (gridPoints - 1); for (let i = 0; i < gridPoints; i++) { for (let j = 0; j < gridPoints; j++) { const lat = centerLat - halfGrid + i * step; const lon = centerLon - halfGrid + j * step; const time = new Date(baseTime.getTime() - (i * gridPoints + j) * 3600000); // 1 hour intervals // Create normal weather data result.push(createWeatherDataPoint(lat, lon, time.toISOString(), { temperature: 15 + Math.sin((i + j) / 10 * Math.PI) * 5, pressure: 1013 + Math.cos((i - j) / 8 * Math.PI) * 5, windSpeed: 10 + Math.sin((i * j) / 20 * Math.PI) * 10, tags: { source: 'noaa', region: 'san-francisco-bay', grid: `${Math.floor(lat * 10)},${Math.floor(lon * 10)}` } })); } } // Add a storm in the middle const stormTime = new Date(baseTime.getTime() - Math.floor(gridPoints * gridPoints / 2) * 3600000); result.push(createStormWeatherDataPoint(centerLat, centerLon, stormTime.toISOString())); return result; } /** * Generate a time series of weather data at a single location * * @param latitude - Latitude * @param longitude - Longitude * @param count - Number of data points * @param interval - Interval in milliseconds * @param baseTime - Base timestamp * @returns Time series of weather data */ export function createWeatherTimeSeries( latitude: number, longitude: number, count: number, interval: number, baseTime: Date ): StoredData[] { const result: StoredData[] = []; for (let i = 0; i < count; i++) { const time = new Date(baseTime.getTime() - i * interval); // Create weather data with daily cycles result.push(createWeatherDataPoint(latitude, longitude, time.toISOString(), { temperature: 15 + Math.sin(i / 24 * Math.PI * 2) * 5 + Math.random() * 2, // Daily cycle pressure: 1013 + Math.sin(i / 48 * Math.PI * 2) * 5 + Math.random(), // 2-day cycle pressureChangeRate: Math.sin(i / 12 * Math.PI * 2) * 0.5, // 12-hour cycle windSpeed: 5 + Math.random() * 15 + (i % 24 < 12 ? 5 : 0), // Stronger during day tags: { source: 'noaa', region: 'san-francisco-bay', timestamp: time.toISOString() } })); } return result; }