pyastroweatherio 0.75.0.dev1__tar.gz → 0.75.0.dev2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (16) hide show
  1. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/PKG-INFO +1 -1
  2. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio/helper_functions.py +260 -61
  3. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio.egg-info/PKG-INFO +1 -1
  4. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/setup.py +1 -1
  5. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/LICENSE +0 -0
  6. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/README.md +0 -0
  7. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio/__init__.py +0 -0
  8. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio/client.py +0 -0
  9. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio/const.py +0 -0
  10. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio/dataclasses.py +0 -0
  11. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio/errors.py +0 -0
  12. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio.egg-info/SOURCES.txt +0 -0
  13. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio.egg-info/dependency_links.txt +0 -0
  14. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio.egg-info/requires.txt +0 -0
  15. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/pyastroweatherio.egg-info/top_level.txt +0 -0
  16. {pyastroweatherio-0.75.0.dev1 → pyastroweatherio-0.75.0.dev2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyastroweatherio
3
- Version: 0.75.0.dev1
3
+ Version: 0.75.0.dev2
4
4
  Summary: Python Wrapper for OpenMeteo and Met.no REST API
5
5
  Home-page: https://github.com/mawinkler/pyastroweatherio
6
6
  Author: Markus Winkler
@@ -59,6 +59,34 @@ class AtmosphericRoutines:
59
59
  # Air pressure at sea level in hPa
60
60
  # aerosol_density in kg/m^3
61
61
 
62
+ # ---------- small internal helpers ----------
63
+ @staticmethod
64
+ def _is_missing(values: dict) -> list[str]:
65
+ return [
66
+ name
67
+ for name, val in values.items()
68
+ if val is None or (isinstance(val, float) and math.isnan(val))
69
+ ]
70
+
71
+ @staticmethod
72
+ def _svp_hpa_magnus(Tc: float) -> float:
73
+ """
74
+ Saturation vapor pressure over water (hPa), Magnus-Tetens.
75
+ Good for typical near-surface temps.
76
+ """
77
+ return 6.112 * math.exp((17.62 * Tc) / (243.12 + Tc))
78
+
79
+ @staticmethod
80
+ def _mixing_ratio_gpkg(e_hpa: float, p_hpa: float) -> float:
81
+ """Mixing ratio (g/kg) from vapor pressure e (hPa) and pressure p (hPa)."""
82
+ eps = 621.97 # g/kg * (hPa/hPa)
83
+ denom = max(1e-6, (p_hpa - e_hpa))
84
+ return eps * (e_hpa / denom)
85
+
86
+ @staticmethod
87
+ def _sigmoid(x: float) -> float:
88
+ return 1.0 / (1.0 + math.exp(-x))
89
+
62
90
  # #####################################################
63
91
  # Calculate lifted index
64
92
  # #####################################################
@@ -143,32 +171,81 @@ class AtmosphericRoutines:
143
171
  async def calculate_lifted_index_11(
144
172
  self, temperature, altitude, dew_point_temperature, air_pressure_at_sea_level
145
173
  ) -> None | float:
146
- if any(
147
- x is None or (isinstance(x, float) and math.isnan(x))
148
- for x in (temperature, altitude, dew_point_temperature, air_pressure_at_sea_level)
149
- ):
150
- return None
151
-
152
- T = temperature + 273.15
153
- Td = dew_point_temperature + 273.15
154
- p_sfc = air_pressure_at_sea_level # hPa
155
-
156
- # LCL (Bolton or Romps)
157
- T_lcl = self._tlcl_bolton(T, Td)
158
- # p_lcl from Poisson eqn dry-adiabatic: θ = T (1000/p)^(R/cp) constant
159
- Rd_cp = 0.2854
160
- theta = T * (1000.0 / p_sfc) ** Rd_cp
161
- p_lcl = 1000.0 / ((theta / T_lcl) ** (1.0 / Rd_cp))
174
+ """
175
+ Returns a rough Lifted Index (LI) proxy in °C.
162
176
 
163
- # Lift moist-adiabatically to 500 hPa
164
- T_parcel_500 = self._moist_adiabat_to(T_lcl, p_lcl, 500.0)
177
+ Real LI requires an environmental temperature profile (a sounding).
178
+ With only surface data, we:
179
+ - Estimate station pressure from sea-level pressure + altitude.
180
+ - Estimate LCL temperature using a common approximation.
181
+ - Estimate parcel temperature at 500 hPa via:
182
+ dry-adiabatic to LCL, then a simplified moist-adiabatic adjustment.
183
+ - Estimate environmental temperature at 500 hPa using a standard lapse proxy.
165
184
 
166
- # Get environmental T at 500 hPa (best: supply it!)
167
- # Fallback: standard atmosphere lapse from 850→500
168
- T_env_500 = -20.0 + 273.15 # placeholder: expose as parameter for real data
185
+ Output is clamped to [-7, +7] like your original.
186
+ """
187
+ values = {
188
+ "temperature": temperature,
189
+ "altitude": altitude,
190
+ "dew_point_temperature": dew_point_temperature,
191
+ "air_pressure_at_sea_level": air_pressure_at_sea_level,
192
+ }
193
+ missing = self._is_missing(values)
194
+ if missing:
195
+ _LOGGER.warning(f"calculate_lifted_index: None/NaN inputs: {', '.join(missing)}")
196
+ return None
169
197
 
170
- LI = T_env_500 - T_parcel_500 # K
171
- return float(max(-7.0, min(7.0, LI)))
198
+ T = float(temperature) # °C
199
+ Td = float(dew_point_temperature)
200
+ alt = float(altitude)
201
+ p0 = float(air_pressure_at_sea_level) # hPa
202
+
203
+ # Estimate station pressure at altitude (use your existing helper)
204
+ # (This uses a standard-atmosphere-like relationship and is good enough here.)
205
+ p_sfc = float(self._calculate_adjusted_pressure(p0, alt))
206
+
207
+ # --- LCL temperature approximation (Bolton-style quick form) ---
208
+ # Use Kelvin
209
+ Tk = T + 273.15
210
+ Tdk = Td + 273.15
211
+ # Prevent nonsense
212
+ Tdk = min(Tk, max(180.0, Tdk))
213
+
214
+ # Approx LCL temperature (K)
215
+ # This is a widely used approximation; stable and simple.
216
+ Tlcl = 1.0 / (1.0 / (Tdk - 56.0) + math.log(Tk / Tdk) / 800.0) + 56.0
217
+
218
+ # Approx LCL pressure using Poisson (dry adiabatic) relation
219
+ # p_lcl = p_sfc * (Tlcl/Tk)^(cp/Rd) with cp/Rd ≈ 3.5
220
+ p_lcl = p_sfc * (Tlcl / Tk) ** 3.5
221
+
222
+ # Parcel temperature at 500 hPa:
223
+ p_target = 500.0
224
+ if p_lcl <= p_target:
225
+ # LCL above 500 hPa: parcel stays unsaturated to 500
226
+ T_parcel_500 = Tk * (p_target / p_sfc) ** (2.0 / 7.0) # κ ≈ 0.286
227
+ else:
228
+ # Dry-adiabatic to LCL, then “moist-ish” to 500 hPa.
229
+ # Very simplified moist-adiabatic cooling: use a reduced exponent.
230
+ # κ_moist ~ 0.12–0.18 depending on moisture; use RH proxy via Td depression.
231
+ dT = max(0.0, min(20.0, T - Td))
232
+ k_moist = 0.12 + 0.06 * self._sigmoid((5.0 - dT) / 1.5) # wetter air => smaller cooling exponent
233
+ T_parcel_500 = Tlcl * (p_target / p_lcl) ** k_moist
234
+
235
+ # Environmental temperature at 500 hPa:
236
+ # Without a sounding, use a lapse-rate estimate from surface temperature.
237
+ # 500 hPa height is typically ~5.5 km; use (5.5km - altitude) thickness.
238
+ z500 = 5500.0
239
+ dz = max(0.0, z500 - alt) # meters
240
+ env_lapse = 6.5 / 1000.0 # K/m (standard-ish)
241
+ T_env_500 = (T + 273.15) - env_lapse * dz
242
+
243
+ # LI = T_env(500) - T_parcel(500), in °C
244
+ LI = (T_env_500 - T_parcel_500)
245
+
246
+ # Clamp like your current implementation
247
+ LI = max(-7.0, min(7.0, float(LI)))
248
+ return LI
172
249
 
173
250
  # #####################################################
174
251
  # Calculate magniture degradation based on transparency
@@ -285,22 +362,75 @@ class AtmosphericRoutines:
285
362
  async def magnitude_degradation_11(
286
363
  self, temperature, humidity, cloud_cover, wind_speed, altitude, dew_point_temperature, air_pressure_at_sea_level
287
364
  ) -> None | float:
288
- # Estimate visibility from RH (section 1), cap by cloud cover (simple screen)
289
- vis_km = self._visibility_koschmieder(humidity)
290
- if cloud_cover is not None:
291
- # Reduce effective vis as clouds rise; linear knockdown
292
- vis_km /= 1.0 + 3.0 * (cloud_cover / 100.0)
365
+ """
366
+ Returns a "magnitude degradation" (mag) proxy.
293
367
 
294
- k_R, k_a, k_O3, k_H2O = self._extinction_components(air_pressure_at_sea_level, humidity, vis_km)
295
- k_total = k_R + k_a + k_O3 + k_H2O
368
+ Improvements vs old version:
369
+ - Transparency -> mag uses a physically meaningful transform:
370
+ mag_loss ≈ -2.5 * log10(transparency)
371
+ - Uses improved LI + seeing as modifiers.
372
+ - Keeps output bounded by MAG_DEGRATION_MAX.
373
+ """
374
+ values = {
375
+ "temperature": temperature,
376
+ "humidity": humidity,
377
+ "cloud_cover": cloud_cover,
378
+ "wind_speed": wind_speed,
379
+ "altitude": altitude,
380
+ "dew_point_temperature": dew_point_temperature,
381
+ "air_pressure_at_sea_level": air_pressure_at_sea_level,
382
+ }
383
+ missing = self._is_missing(values)
384
+ if missing:
385
+ _LOGGER.warning(f"magnitude_degradation: None/NaN inputs: {', '.join(missing)}")
386
+ return None
296
387
 
297
- # If you know target altitude, compute zenith angle; otherwise assume X~1.2 median
298
- X = 1.2
299
- delta_m = k_total * X
388
+ # Compute ingredients
389
+ lifted_index = await self.calculate_lifted_index(
390
+ temperature, altitude, dew_point_temperature, air_pressure_at_sea_level
391
+ )
392
+ seeing = await self.calculate_seeing(
393
+ temperature,
394
+ humidity,
395
+ dew_point_temperature,
396
+ wind_speed,
397
+ cloud_cover,
398
+ altitude,
399
+ air_pressure_at_sea_level,
400
+ )
300
401
 
301
- # Optionally clamp
302
- return float(min(MAG_DEGRATION_MAX, max(0.0, delta_m)))
402
+ if lifted_index is None or seeing is None:
403
+ return None
303
404
 
405
+ rh = max(0.0, min(100.0, float(humidity)))
406
+ cc = max(0.0, min(100.0, float(cloud_cover)))
407
+ ws = max(0.0, float(wind_speed))
408
+ alt = max(0.0, float(altitude))
409
+
410
+ # Base transparency from simple, bounded components
411
+ # (weights are chosen for stable behavior; tune freely)
412
+ hum_pen = self._sigmoid((rh - 75.0) / 10.0) # humidity haze risk
413
+ cloud_pen = (cc / 100.0) ** 1.2 # clouds dominate quickly
414
+ wind_pen = self._sigmoid((ws - 6.0) / 2.5) * 0.4 # blowing aerosols
415
+ li_pen = self._sigmoid((-lifted_index - 1.0) / 2.0) * 0.35 # unstable air -> more haze/cloud risk
416
+ seeing_pen = self._sigmoid((seeing - 2.0) / 0.8) * 0.25 # poor seeing correlates w/ bad transparency sometimes
417
+
418
+ # altitude improves transparency a bit (less column)
419
+ alt_gain = 1.0 - 0.12 * (1.0 - math.exp(-alt / 1800.0))
420
+
421
+ # Combine into transparency 0..1
422
+ penalty = 0.18*hum_pen + 0.50*cloud_pen + 0.12*wind_pen + 0.10*li_pen + 0.10*seeing_pen
423
+ transparency = max(0.02, min(1.0, (1.0 - penalty) * alt_gain))
424
+
425
+ # Convert transparency to mag loss (extinction-like)
426
+ mag_loss = -2.5 * math.log10(transparency)
427
+
428
+ # Clamp to your global max
429
+ mag_loss = min(float(MAG_DEGRATION_MAX), float(mag_loss))
430
+ if mag_loss < 0.0:
431
+ mag_loss = 0.0
432
+ return float(mag_loss)
433
+
304
434
  # #####################################################
305
435
  # Calculate atmospheric seeing
306
436
  #
@@ -418,18 +548,64 @@ class AtmosphericRoutines:
418
548
  async def calculate_seeing_11(
419
549
  self, temperature, humidity, dew_point_temperature, wind_speed, cloud_cover, altitude, air_pressure_at_sea_level
420
550
  ) -> None | float:
421
- # Map your measured 10 m wind to a free-atmosphere proxy v (cap 10–35 m/s)
422
- v_free = max(10.0, min(35.0, 2.0 * wind_speed + 10.0))
423
-
424
- # Strengthen ground layer at low altitudes / high RH:
425
- A = (
426
- 1.7e-14
427
- * (1.0 + 0.5 * max(0.0, (humidity - 70.0) / 30.0))
428
- * (1.0 + 0.3 * max(0.0, (500.0 - altitude) / 500.0))
429
- )
430
- seeing = self._seeing_from_hv57(v=v_free, A=A)
551
+ """
552
+ Returns a rough seeing estimate in arcseconds.
553
+
554
+ Improvements vs old version:
555
+ - Avoids unit-mismatch water-vapor-pressure math.
556
+ - Uses stable, bounded mapping driven by:
557
+ * wind (mechanical turbulence),
558
+ * dewpoint depression & RH (near-surface stability / saturation),
559
+ * cloud cover (proxy for active layers),
560
+ * altitude (usually helps boundary-layer seeing).
561
+ """
562
+ values = {
563
+ "temperature": temperature,
564
+ "humidity": humidity,
565
+ "dew_point_temperature": dew_point_temperature,
566
+ "wind_speed": wind_speed,
567
+ "cloud_cover": cloud_cover,
568
+ "altitude": altitude,
569
+ "air_pressure_at_sea_level": air_pressure_at_sea_level,
570
+ }
571
+ missing = self._is_missing(values)
572
+ if missing:
573
+ _LOGGER.warning(f"calculate_seeing: None/NaN inputs: {', '.join(missing)}")
574
+ return None
431
575
 
432
- return min(SEEING_MAX, seeing)
576
+ T = float(temperature)
577
+ Td = float(dew_point_temperature)
578
+ rh = max(0.0, min(100.0, float(humidity)))
579
+ ws = max(0.0, float(wind_speed))
580
+ cc = max(0.0, min(100.0, float(cloud_cover)))
581
+ alt = max(0.0, float(altitude))
582
+
583
+ # Boundary-layer proxies
584
+ dT = max(-5.0, min(25.0, T - Td)) # dewpoint depression
585
+ wind_term = math.log1p(ws) # grows slowly, stable
586
+ cloud_term = (cc / 100.0) ** 0.7 # more sensitive at low-mid cloud
587
+ humid_term = self._sigmoid((rh - 80.0) / 8.0) # ramps up beyond ~80%
588
+
589
+ # Altitude often helps (thinner BL, less heat shimmer).
590
+ # Don’t overdo it; this is a mild improvement curve.
591
+ altitude_gain = 0.85 + 0.15 * math.exp(-alt / 1800.0) # ~1.0 at sea lvl -> ~0.85 at high alt
592
+
593
+ # Base seeing floor and contributions (tunable constants)
594
+ seeing = (
595
+ 0.75
596
+ + 0.55 * wind_term
597
+ + 0.65 * cloud_term
598
+ + 0.45 * humid_term
599
+ + 0.35 * self._sigmoid((dT - 4.0) / 1.8) # very dry air often correlates with better seeing aloft, but BL can still shimmer; this keeps it moderate
600
+ ) * altitude_gain
601
+
602
+ # Clamp
603
+ if seeing > SEEING_MAX:
604
+ seeing = SEEING_MAX
605
+ if seeing < 0.4:
606
+ seeing = 0.4
607
+
608
+ return float(seeing)
433
609
 
434
610
  # #####################################################
435
611
  # Calculate fog density at 2m
@@ -506,22 +682,45 @@ class AtmosphericRoutines:
506
682
 
507
683
  @typechecked
508
684
  async def calculate_fog_density_11(self, temp2m, rh2m, dewpoint2m, wind_speed) -> float:
509
- # Favor calm, near-saturated, small (T-Td)
510
- vis_km = self._visibility_koschmieder(rh2m)
511
-
512
- # Penalize higher winds (disperse fog): halve “density” per ~5 m/s
513
- wind_factor = math.exp(-wind_speed / 5.0)
514
-
515
- # Map visibility to density: 0..1 over 0–5 km range
516
- dens_from_vis = max(0.0, min(1.0, (5.0 - min(vis_km, 5.0)) / 5.0))
685
+ """
686
+ Returns a 0..1 "fog likelihood/density" proxy.
517
687
 
518
- # Add a (T-Td) pinch
519
- td_spread = max(0.0, min(1.0, 1.0 - abs(temp2m - dewpoint2m) / 3.0))
688
+ Improvements vs old version:
689
+ - Uses dewpoint depression (T - Td) + RH with a logistic curve (stable).
690
+ - Stronger penalty for wind mixing (fog dispersal).
691
+ - Handles edge cases (negative RH, etc.) safely.
692
+ """
693
+ # Validate
694
+ values = {"temp2m": temp2m, "rh2m": rh2m, "dewpoint2m": dewpoint2m, "wind_speed": wind_speed}
695
+ missing = self._is_missing(values)
696
+ if missing:
697
+ _LOGGER.warning(f"calculate_fog_density: None/NaN inputs: {', '.join(missing)}")
698
+ return 0.0
699
+
700
+ rh = max(0.0, min(100.0, float(rh2m)))
701
+ T = float(temp2m)
702
+ Td = float(dewpoint2m)
703
+ ws = max(0.0, float(wind_speed))
704
+
705
+ # Dewpoint depression in °C
706
+ dT = max(-5.0, min(30.0, T - Td))
707
+
708
+ # Core saturation term:
709
+ # - Fog becomes likely when RH is high AND dewpoint depression is small.
710
+ # - Map into 0..1 smoothly.
711
+ sat_score = (
712
+ 2.2 * (rh - 92.0) / 8.0 # RH ~92% begins to matter strongly
713
+ - 2.6 * (dT - 1.2) / 1.8 # dT <~ 1–2°C drives fog likelihood
714
+ )
715
+ sat = self._sigmoid(sat_score)
520
716
 
521
- density = dens_from_vis * 0.7 + td_spread * 0.3
522
- density *= wind_factor
717
+ # Wind dispersal: calm helps fog; moderate wind destroys it.
718
+ # This is a smooth decay; tune the 2.5–4.0 range if you want.
719
+ wind_decay = math.exp(-ws / 3.0)
523
720
 
524
- return float(max(0.0, min(1.0, density)))
721
+ # Final, clipped
722
+ fog = sat * wind_decay
723
+ return float(max(0.0, min(1.0, fog)))
525
724
 
526
725
  # #####################################################
527
726
  # Calculate dew point at 2m
@@ -561,7 +760,7 @@ class AtmosphericRoutines:
561
760
  Calculate the adjusted pressure at a given altitude above sea level.
562
761
  """
563
762
 
564
- lapse_rate = -0.0065 # Temperature lapse rate in K/m
763
+ lapse_rate = 0.0065 # Temperature lapse rate in K/m
565
764
  temperature_sea_level = 288.15 # Temperature at sea level in K
566
765
  gravity = 9.80665 # Acceleration due to gravity in m/s^2
567
766
  molar_mass_air = 0.02896 # Molar mass of Earth's air in kg/mol
@@ -613,7 +812,7 @@ class AtmosphericRoutines:
613
812
  # B = 265.5
614
813
 
615
814
  # Calculate vapor pressure using Magnus-Tetens formula
616
- e = 0.61078 * math.exp((A * temperature) / (temperature + B))
815
+ e = 6.1078 * math.exp((A * temperature) / (temperature + B))
617
816
 
618
817
  return e
619
818
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyastroweatherio
3
- Version: 0.75.0.dev1
3
+ Version: 0.75.0.dev2
4
4
  Summary: Python Wrapper for OpenMeteo and Met.no REST API
5
5
  Home-page: https://github.com/mawinkler/pyastroweatherio
6
6
  Author: Markus Winkler
@@ -11,7 +11,7 @@ if os.path.isfile(requirement_path):
11
11
  setup(
12
12
  name="pyastroweatherio",
13
13
  packages=["pyastroweatherio"],
14
- version="0.75.0.dev1",
14
+ version="0.75.0.dev2",
15
15
  license="MIT",
16
16
  description="Python Wrapper for OpenMeteo and Met.no REST API",
17
17
  long_description=" ".join(