Repository: Unidata/MetPy

Issue:
GEMPAK reader assigns grid mapping information incorrectly
As referenced in https://github.com/Unidata/MetPy/issues/641, the GEMPAK reader (particularly https://github.com/Unidata/MetPy/blob/e263d08e71ac55d2d9f12fa6c11594f4c3bea74d/src/metpy/io/gempak.py#L1266-L1280) incorrectly inserts the CF grid mapping attributes as DataArray attributes (where they are not useful without manual handling), rather than in a `metpy_crs` coordinate (where they can be leveraged by the rest of MetPy). Suggested change would be

```python
xrda = xr.DataArray(
    data=data[np.newaxis, np.newaxis, ...],
    coords={
        'time': [valid],
        gvcord: [col_head.GLV1],
        'x': self.x,
        'y': self.y,
        'metpy_crs': CFProjection(self.crs.to_cf())
    },
    dims=['time', gvcord, 'y', 'x'],
    name=var,
    attrs={
        'gempak_grid_type': ftype,
    }
)
```

(Though, looking at this now, this is also apparently missing unit information? If so, may need another issue for that as well)

Discussion:
jthielen: Test failure looks to be an inconsistency in `pyproj.CRS.to_cf()` output given same input with old pyproj. What would the best way to work around this be?
- skip/xfail on old pyrpoj?
- bump minimum to oldest consistent version?
- write version-dependent test?
dopplershift: Well, we can easily bump to 2.4, could consider 2.5 or 2.6--if any of that makes this easier? Otherwise, just skip on old pyproj.

Patch:
diff --git a/setup.cfg b/setup.cfg
index 60c803d1bf..a229f9604b 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -46,7 +46,7 @@ install_requires =
     pandas>=0.24.0
     pint>=0.10.1
     pooch>=0.1
-    pyproj>=2.3.0
+    pyproj>=2.5.0
     scipy>=1.2.0
     traitlets>=4.3.0
     xarray>=0.14.1
diff --git a/src/metpy/io/gempak.py b/src/metpy/io/gempak.py
index dcd31aed10..8d4937ead2 100644
--- a/src/metpy/io/gempak.py
+++ b/src/metpy/io/gempak.py
@@ -25,6 +25,7 @@ from ..calc import (mixing_ratio_from_specific_humidity, scale_height,
                     specific_humidity_from_dewpoint, thickness_hydrostatic,
                     virtual_temperature)
 from ..package_tools import Exporter
+from ..plots.mapping import CFProjection
 from ..units import units
 
 exporter = Exporter(globals())
@@ -1270,12 +1271,12 @@ class GempakGrid(GempakFile):
                             gvcord: [col_head.GLV1],
                             'x': self.x,
                             'y': self.y,
+                            'metpy_crs': CFProjection(self.crs.to_cf())
                         },
                         dims=['time', gvcord, 'y', 'x'],
                         name=var,
                         attrs={
-                            **self.crs.to_cf(),
-                            'grid_type': ftype,
+                            'gempak_grid_type': ftype,
                         }
                     )
                     grids.append(xrda)


Test patch:
diff --git a/tests/io/test_gempak.py b/tests/io/test_gempak.py
index 5dd2b6af56..58498868f3 100644
--- a/tests/io/test_gempak.py
+++ b/tests/io/test_gempak.py
@@ -225,3 +225,29 @@ def test_coordinates_creation(proj_type):
 
     assert_allclose(decode_lat, true_lat, rtol=1e-6, atol=1e-2)
     assert_allclose(decode_lon, true_lon, rtol=1e-6, atol=1e-2)
+
+
+@pytest.mark.parametrize('proj_type, proj_attrs', [
+    ('conical', {
+        'grid_mapping_name': 'lambert_conformal_conic',
+        'standard_parallel': (25.0, 25.0),
+        'latitude_of_projection_origin': 0.0,
+        'longitude_of_central_meridian': -95.0,
+        'semi_major_axis': 6371200.,
+        'semi_minor_axis': 6371200.,
+    }),
+    ('azimuthal', {
+        'grid_mapping_name': 'polar_stereographic',
+        'latitude_of_projection_origin': 90.0,
+        'straight_vertical_longitude_from_pole': -105.0,
+        'scale_factor_at_projection_origin': 1.0,
+        'semi_major_axis': 6371200.,
+        'semi_minor_axis': 6371200.,
+    })
+])
+def test_metpy_crs_creation(proj_type, proj_attrs):
+    """Test grid mapping metadata."""
+    grid = GempakGrid(get_test_data(f'gem_{proj_type}.grd'))
+    metpy_crs = grid.gdxarray()[0].metpy.crs
+    for k, v in proj_attrs.items():
+        assert metpy_crs[k] == v