How to plot contours from a polar stereographic grib2 file in Python

Campbell

I am trying to plot a CMC grib2 pressure forecast file using matplotlib to plot the pressure contours. The description of the grib2 grid can be found here: https://weather.gc.ca/grib/grib2_reg_10km_e.html. The grib2 file is found in this directory: http://dd.weather.gc.ca/model_gem_regional/10km/grib2/00/000/ and starts with CMC_reg_PRMSL_MSL_0_ps10km followed by the date. It is a grib file containing pressure at mean sea level.

My problem is that I end up having some straight line contours that follow the lines of latitude on top of the actual pressure contours. I thought it might be because I am plotting in PlateCarree as opposed to Geodetic but the contour plot will not allow using Geodetic. The result of my plot is: enter image description here

Code is as follows:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
import cartopy
import cartopy.crs as ccrs
import Nio

gr = Nio.open_file('./data/CMC_reg_PRMSL_MSL_0_ps10km_2018111800_P000.grib2', 'r')
print(gr)
names = gr.variables.keys()
print("Variable Names:", names)
dims = gr.dimensions
print("Dimensions: ", dims)
attr = gr.attributes.keys()
print("Attributes: ", attr)

obs = gr.variables['PRMSL_P0_L101_GST0'][:]
lats = gr.variables["gridlat_0"][:]
lons = gr.variables["gridlon_0"][:]

fig = plt.figure(figsize=(15, 2))
intervals = range(95000, 105000, 400)
ax=plt.axes([0.,0.,1.,1.],projection=ccrs.PlateCarree())
obsobj = plt.contour(lons, lats, obs, intervals, cmap='jet',transform=ccrs.PlateCarree())
states_provinces = cartopy.feature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lines',
        scale='50m',
        facecolor='none')
ax.add_feature(cartopy.feature.BORDERS)
ax.coastlines(resolution='10m')  
ax.add_feature(states_provinces,edgecolor='gray')
obsobj.clabel()
colbar =plt.colorbar(obsobj)

Any suggestions would be appreciated.

UPDATE

For anyone without PyNIO the following can be used to reproduce using the dump files in the comments section.

Just remove all the references to NIO and replace the lats, lons, obs assignment with the following.

lats = np.load('lats.dump')
lons = np.load('lons.dump')
obs = np.load('obs.dump')
ImportanceOfBeingErnest

The problem

The problem is that the grid winds around the earth. Hence there will be points on the grid at -180° whose nearst neighbor sits at +180°, i.e. the grid wraps around the antimeridian. The following plots the grid index along both directions. One can see that the first grid row (black) appears on both sides of the plot.

enter image description here

Hence a contour line following the pacific westwards needs to then cross straight through the plot to continue towards japan on the other side of the plot. This will lead to the undesired lines

enter image description here

A solution

A solution is to mask the outer points of the PlateCarree out. Those occur in the middle of the grid. Cutting the grid at coordinates of longitude larger than 179° or smaller than -179°, as well as leaving the north pole out would look like

enter image description here

where the blue denotes the cut out points.

Applying this to the contour plot gives:

import matplotlib.pyplot as plt
import numpy as np
import cartopy
import cartopy.crs as ccrs

lats = np.load('data/lats.dump')
lons = np.load('data/lons.dump')
obs = np.load('data/obs.dump')

intervals = range(95000, 105000, 400)

fig, ax = plt.subplots(figsize=(15,4), subplot_kw=dict(projection=ccrs.PlateCarree()))
fig.subplots_adjust(left=0.03, right=0.97, top=0.8, bottom=0.2)

mask = (lons > 179) | (lons < -179) | (lats > 89)
maskedobs = np.ma.array(obs, mask=mask)

pc = ax.contour(lons, lats, maskedobs, intervals, cmap='jet', transform=ccrs.PlateCarree())

ax.add_feature(cartopy.feature.BORDERS)
ax.coastlines(resolution='10m')  

colbar =plt.colorbar(pc)

plt.show()

enter image description here

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I plot GFS grib2 data with Python?

分類Dev

How to plot a polar plot?

分類Dev

How to plot polar coordinates in R?

分類Dev

How to plot horizontal lines with text (i.e. a label) on a polar coordinates matplotlib plot? (Python)

分類Dev

r - how to add label in corner of polar plot

分類Dev

Polar plot in Matplotlib not centered

分類Dev

How to plot simple plot from DataFrame in Python Pandas?

分類Dev

How to import data from file and plot cdf in R

分類Dev

Scaling the axes in a polar plot of ggplot individually?

分類Dev

Pygame Vector2.as_polar()およびVector2.from_polar()メソッド

分類Dev

How to obtain a plot from dataframe

分類Dev

how to plot stdin from terminal?

分類Dev

Wrong ylabels in plot from loaded file

分類Dev

How to run two function from same resource(plot graphic and saving infinite data to txt file)

分類Dev

How to run a python file from another securely?

分類Dev

How to read CDATA from xml file with Python

分類Dev

How to plot spectrum or frequency vs amplitude of entire audio file using python?

分類Dev

plot histogram in python using csv file as input

分類Dev

Creating half a polar plot (rose diagram) with circular package

分類Dev

How can I perform integral on a select area from ''active'' line plot - python

分類Dev

Polar Bar chart using Facetgrid in Python

分類Dev

How to plot a transfer function from a Cauer network

分類Dev

dataframe2delta: how to plot a delta function directly from the dataframe using ggplot2

分類Dev

How to plot individual points without curve in python?

分類Dev

[File IO Error in porting from python2 to python 3]

分類Dev

How to remove special characters except space from a file in python?

分類Dev

How do I call a class method from another file in Python?

分類Dev

How to read list element in Python from a text file?

分類Dev

How to find a string inside a XML like tag from a file in Python?

Related 関連記事

  1. 1

    How do I plot GFS grib2 data with Python?

  2. 2

    How to plot a polar plot?

  3. 3

    How to plot polar coordinates in R?

  4. 4

    How to plot horizontal lines with text (i.e. a label) on a polar coordinates matplotlib plot? (Python)

  5. 5

    r - how to add label in corner of polar plot

  6. 6

    Polar plot in Matplotlib not centered

  7. 7

    How to plot simple plot from DataFrame in Python Pandas?

  8. 8

    How to import data from file and plot cdf in R

  9. 9

    Scaling the axes in a polar plot of ggplot individually?

  10. 10

    Pygame Vector2.as_polar()およびVector2.from_polar()メソッド

  11. 11

    How to obtain a plot from dataframe

  12. 12

    how to plot stdin from terminal?

  13. 13

    Wrong ylabels in plot from loaded file

  14. 14

    How to run two function from same resource(plot graphic and saving infinite data to txt file)

  15. 15

    How to run a python file from another securely?

  16. 16

    How to read CDATA from xml file with Python

  17. 17

    How to plot spectrum or frequency vs amplitude of entire audio file using python?

  18. 18

    plot histogram in python using csv file as input

  19. 19

    Creating half a polar plot (rose diagram) with circular package

  20. 20

    How can I perform integral on a select area from ''active'' line plot - python

  21. 21

    Polar Bar chart using Facetgrid in Python

  22. 22

    How to plot a transfer function from a Cauer network

  23. 23

    dataframe2delta: how to plot a delta function directly from the dataframe using ggplot2

  24. 24

    How to plot individual points without curve in python?

  25. 25

    [File IO Error in porting from python2 to python 3]

  26. 26

    How to remove special characters except space from a file in python?

  27. 27

    How do I call a class method from another file in Python?

  28. 28

    How to read list element in Python from a text file?

  29. 29

    How to find a string inside a XML like tag from a file in Python?

ホットタグ

アーカイブ