Как исправить перекрывающиеся изображения Metpy / Cartopy? - PullRequest
0 голосов
/ 11 октября 2019

Когда я запускаю этот код

import Scientific.IO.NetCDF as S
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import xarray as xr
import metpy
import numpy as N

from metpy.plots import ContourPlot, ImagePlot, MapPanel, PanelContainer
# Any import of metpy will activate the accessors
import metpy.calc as mpcalc
#from metpy.testing import get_test_data
from metpy.units import units
# Open the netCDF file as a xarray Datase


#
datadir='C:/Users/stratus/AppData/Local/lxss/home/stratus/PROJECT/NEWPROJECT/FEB012017/nam_218_20170131_1200_000.nc'
data = xr.open_dataset(datadir,decode_cf=True)
# To parse the full dataset, we can call parse_cf without an argument, and assign the returned
# Dataset.
data = data.metpy.parse_cf()
tempatt=data['TMP_P0_L100_GLC0'].attrs

# If we instead want just a single variable, we can pass that variable name to parse_cf and
# it will return just that data variable as a DataArray.
data_var = data.metpy.parse_cf('TMP_P0_L100_GLC0')

# To rename variables, supply a dictionary between old and new names to the rename method
data.rename({
    'TMP_P0_L100_GLC0': 'temperature',
}, inplace=True)

data['temperature'].metpy.convert_units('degC')

# Get multiple coordinates (for example, in just the x and y direction)
x, y = data['temperature'].metpy.coordinates('x', 'y')

# If we want to get just a single coordinate from the coordinates method, we have to use
# tuple unpacking because the coordinates method returns a generator
vertical, = data['temperature'].metpy.coordinates('vertical')
data_crs = data['temperature'].metpy.cartopy_crs


# Or, we can just get a coordinate from the property
#time = data['temperature'].metpy.time

# To verify, we can inspect all their names
#print([coord.name for coord in (x, y, vertical, time)])

#
#heights = data['height'].metpy.loc[{'time': time[0], 'vertical': 850. * units.hPa}]
#lat, lon = xr.broadcast(y, x)
#f = mpcalc.coriolis_parameter(lat)
#dx, dy = mpcalc.grid_deltas_from_dataarray(heights)
#u_geo, v_geo = mpcalc.geostrophic_wind(heights, f, dx, dy)
#print(u_geo)
#print(v_geo)
fig=plt.figure(1)
# A very simple example example of a plot of 500 hPa heights

data_crs = data['temperature'].metpy.cartopy_crs

ax = plt.axes(projection=ccrs.LambertConformal())
data['temperature'].metpy.loc[{'vertical': 850. * units.hPa}].plot(ax=ax, transform=data_crs)

ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
plt.show()
#ax.set_extent([-120,-80,20,50])
plt.title("850 mb Temperature")
#plt.suptitle("Metpy Test")
plt.show()

Мне пришлось редактировать код в соответствии с некоторыми ответами, но сейчас я получаю в основном пустую карту. 850 T Ошибка карты В основном я пытаюсь, чтобы температура в 850 МБ перекрывала США, чтобы я мог показать это своему другу, чтобы попрактиковаться в проекте, с которым я ему помогаю. Заполнение скобок для данных немного помогло, поэтому я отредактировал его.

1 Ответ

0 голосов
/ 11 октября 2019

Как указано в комментариях, трудно ответить без воспроизводимого примера. Тем не менее, следующее может решить вашу проблему:

data_crs = data['temperature'].metpy.cartopy_crs

ax = plt.axes(projection=ccrs.LambertConformal())
data['temperature'].metpy.loc[{'vertical': 1000. * units.hPa}].plot(ax=ax, transform=data_crs)

ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
plt.show()
...