Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utils.py 1.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  1. """
  2. Introduction to GRIB2 Files
  3. https://www.ftp.cpc.ncep.noaa.gov/wd51we/wgrib2/intro_grib2.pdf
  4. """
  5. import re
  6. import pandas as pd
  7. import geopandas as gpd
  8. from dataclasses import dataclass, field
  9. pat = re.compile(
  10. (
  11. r"(?P<message_num>\d+)"
  12. r"(\.(?P<submessage_num>\d+))?"
  13. r":(?P<location>\d+):"
  14. r"d=(?P<reference_time>\d+)"
  15. r":(?P<variable_name>\w+)"
  16. r":(?P<z>[^:]+)"
  17. r":(?P<dtime>[^:]+):"
  18. )
  19. )
  20. class InvalidGRIBInventory(Exception):
  21. pass
  22. GRB2_CRS = "EPSG:4326"
  23. """The GFS uses the WGS 84 Coordinate Reference System
  24. """
  25. def parse_grib2_inventory(s):
  26. """
  27. """
  28. match = pat.match(s)
  29. if match is None:
  30. raise InvalidGRIBInventory(f"{s} is not a valid GRIB2 inventory.")
  31. return match.groupdict()
  32. def grb2gdf(df):
  33. """Convert a grb2 DataFrame to a grb2 GeoDataframe based on the GFS grb2 spec.
  34. Specifically, long-lats are converted to proper WGS84 coordinates
  35. and used to create a geometry for each row with geopandas.
  36. Args:
  37. df (pd.Dataframe): Dataframe of interest.
  38. Returns:
  39. gdf (gpd.DataFrame): Geopandas dataframe with lat-lon geometry in WGS84 CRS.
  40. """
  41. # Convert Longitude3 (0, 360) to Longitude (-180, 180)
  42. lon = df["longitude"].where(df["longitude"] < 180, df["longitude"] - 360)
  43. # GRIB2 already has this as (-90, 90)
  44. lat = df["latitude"]
  45. gdf = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(lon, lat), crs=GRB2_CRS)
  46. return gdf
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...