[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[python #RRM-635285]: Help with plotting temperatures on a map



Hi,

Apologies for the delay in responding, we've been kinda busy of late with 
workshops and travel. But we're here now!

Your first problem is caused by MapQuest changing their interface and breaking 
CartoPy (see here for more information: 
https://github.com/SciTools/cartopy/issues/781)

What you're looking for out of the original example are a couple of the loops 
from the original code. I've assembled all of the code together below, as well 
as updated it to use the built-in states feature in CartoPy:

from MesoPy import Meso
import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
import pprint
pp = pprint.PrettyPrinter(indent=2)
m = Meso(token = '1954af0b2a264daf94995118595a3856')
latest = m.latest(stid=('kbvy','E9578', 'AN278', 'AR572', 'AV154', 'AR100', 
'F1086', 'D4800', 'C0169', 'D5181', 'D4800', 'D2257', 'E7580', 'MA010', 
'AT034', 'AT375', 'AV086', 'F3197', 'D6723', 'C6151', 'E5193', 'MA001', 
'C3868', 'D2806', 'D5391', 'KLWM', 'D7873', 'E5913', 'D1334', 'C9683', 'C3660', 
'MA003', 'C0210', 'C6612', 'D7355', 'E4331', 'E4960', 'E6480', 'E9012', 
'E9991', 'BHBM3', 'AN287', 'KBOS', 'A1261', 'E2727', 'F0642', 'D3706'), 
within='10', units='ENGLISH')
#pp.pprint (latest)
from MesoPy import Meso
import pprint

stations = ['kbvy','E9578', 'AN278', 'AR572', 'AV154', 'AR100', 'F1086', 
'D4800', 'C0169', 'D5181', 'D4800', 'D2257', 'E7580', 'MA010', 'AT034', 
'AT375', 'AV086', 'F3197', 'D6723', 'C6151', 'E5193', 'MA001', 'C3868', 
'D2806', 'D5391', 'KLWM', 'D7873', 'E5913', 'D1334', 'C9683', 'C3660', 'MA003', 
'C0210', 'C6612', 'D7355', 'E4331', 'E4960', 'E6480', 'E9012', 'E9991', 
'BHBM3', 'AN287', 'KBOS', 'A1261', 'E2727', 'F0642', 'D3706']
latest = m.latest(stid=stations, within='90', vars='air_temp', units='temp|F')

data = []
[data.append((float(ob['LATITUDE']), float(ob['LONGITUDE']), 
float(ob['OBSERVATIONS']['air_temp_value_1']['value']),
ob['STID'])) for ob in latest['STATION']];


import matplotlib.pyplot as plt
%matplotlib inline
import cartopy.crs as ccrs
import cartopy.feature as cfeat

proj = ccrs.Stereographic(central_longitude=-65, central_latitude=35)
fig = plt.figure(figsize=(15,15))
ax=fig.add_subplot(1,1,1,projection=proj)
ax.add_feature(cfeat.STATES.with_scale('50m'))
ax.add_feature(cfeat.LAND.with_scale('50m'))
ax.add_feature(cfeat.OCEAN.with_scale('50m'))
ax.add_feature(cfeat.COASTLINE.with_scale('50m'))
ax.add_feature(cfeat.BORDERS.with_scale('50m'))
ax.add_feature(cfeat.RIVERS.with_scale('50m'))
ax.gridlines()

# Plot lat/long pts with below params
for lat, lon, temp, stid in data:
    plt.plot(lon, lat, marker='o', color='y', markersize=1,
             alpha=0.7, transform=ccrs.Geodetic())
    
# Transforms for the text func we're about to call
geodetic_transform = ccrs.Geodetic()._as_mpl_transform(ax)
text_transform = offset_copy(geodetic_transform, units='dots', x=0, y=0)

# Plot temp and station id for each of the markers
for lat, lon, temp, stid in data:
    plt.text(lon, lat, stid + '\n' + str(round(temp, 1)) + u' \N{DEGREE SIGN}' 
+ 'F',
             verticalalignment='center', horizontalalignment='center',
             transform=text_transform, fontsize=14,
             bbox=dict(facecolor='wheat', alpha=0.5, boxstyle='round'))
plt.title('Current Weather Around Boston', fontsize=20)

ax.set_extent([-70.2, -71.3, 42.3, 43])



You may also be interested in MetPy's station model plots for such data: 

https://unidata.github.io/MetPy/latest/examples/plots/Station_Plot.html#sphx-glr-examples-plots-station-plot-py

Hope this helps,

Ryan

> Hello,
> 
> I am writing to see if I could possibly have some assistance with a couple of 
> issues that I am having with learning/practicing Python, Metpy, Mesopy, etc.
> 
> 
> 1.   I am practicing the code to make a current temperature plot using 
> Mesopy, cartopy, and matplotlib.  The example which is provided on the 
> following link:
> 
> https://github.com/mesowx/MesoPy/blob/master/examples/Map_Plot.ipynb
> 
> When I run the code that is there...just to make sure that everything 
> works...I get this error.  I haven't altered anything, just pure copy and 
> pasting.  This is being done while being in the unidata workshop environment. 
>  Since this example is not working for me, I am wondering if I do not have 
> things properly installed...or if there is some other simple mistake that I 
> am making.
> 
> 
> Text(0.5, 1.0, 'Current Weather Around Colorado')

<snip>

> ~\Miniconda3\lib\site-packages\cartopy\io\img_tiles.py in 
> image_for_domain(self, target_domain, target_z)
> 98             tiles.append([img, x, y, origin])
> 99
> --> 100         img, extent, origin = _merge_tiles(tiles)
> 101         return img, extent, origin
> 102
> 
> ~\Miniconda3\lib\site-packages\cartopy\io\img_tiles.py in _merge_tiles(tiles)
> 407     """Return a single image, merging the given images."""
> 408     if not tiles:
> --> 409         raise ValueError('A non-empty list of tiles should '
> 410                          'be provided to merge.')
> 411     xset = [set(x) for i, x, y, _ in tiles]
> 
> ValueError: A non-empty list of tiles should be provided to merge.
> 
> <Figure size 1080x864 with 1 Axes>
> 
> Issue 2:  I have used the example video for MetPY on creating a base map.  I 
> created the base map for the region of Boston northward to the New Hampshire 
> border.  I also extracted data from a bunch of stations in this area.  So I 
> have the base map and I have the data (lat, lon, air temp, station id) but I 
> am struggling to get them to plot.  In the example link that I provided 
> above...a MapQuest map is used...but I can not seem to get that to work.  So 
> I made the basemap by replicating the steps in the video on making a base map 
> with Cartopy.  The problem is that I can't seem to plot the temps on either 
> map.
> 
> Here is the code that I'm working on for that:
> 
> from MesoPy import Meso
> import matplotlib.pyplot as plt
> from matplotlib.transforms import offset_copy
> import cartopy.crs as ccrs
> import cartopy.io.img_tiles as cimgt
> import pprint
> pp = pprint.PrettyPrinter(indent=2)
> m = Meso(token = '1954af0b2a264daf94995118595a3856')
> latest = m.latest(stid=('kbvy','E9578', 'AN278', 'AR572', 'AV154', 'AR100', 
> 'F1086', 'D4800', 'C0169', 'D5181', 'D4800', 'D2257', 'E7580', 'MA010', 
> 'AT034', 'AT375', 'AV086', 'F3197', 'D6723', 'C6151', 'E5193', 'MA001', 
> 'C3868', 'D2806', 'D5391', 'KLWM', 'D7873', 'E5913', 'D1334', 'C9683', 
> 'C3660', 'MA003', 'C0210', 'C6612', 'D7355', 'E4331', 'E4960', 'E6480', 
> 'E9012', 'E9991', 'BHBM3', 'AN287', 'KBOS', 'A1261', 'E2727', 'F0642', 
> 'D3706'), within='10', units='ENGLISH')
> pp.pprint (latest)
> from MesoPy import Meso
> import pprint
> 
> stations = ['kbvy','E9578', 'AN278', 'AR572', 'AV154', 'AR100', 'F1086', 
> 'D4800', 'C0169', 'D5181', 'D4800', 'D2257', 'E7580', 'MA010', 'AT034', 
> 'AT375', 'AV086', 'F3197', 'D6723', 'C6151', 'E5193', 'MA001', 'C3868', 
> 'D2806', 'D5391', 'KLWM', 'D7873', 'E5913', 'D1334', 'C9683', 'C3660', 
> 'MA003', 'C0210', 'C6612', 'D7355', 'E4331', 'E4960', 'E6480', 'E9012', 
> 'E9991', 'BHBM3', 'AN287', 'KBOS', 'A1261', 'E2727', 'F0642', 'D3706']
> latest = m.latest(stid=stations, within='90', vars='air_temp', units='temp|F')
> 
> data = []
> [data.append((float(ob['LATITUDE']), float(ob['LONGITUDE']), 
> float(ob['OBSERVATIONS']['air_temp_value_1']['value']),
> ob['STID'])) for ob in latest['STATION']]
> data
> 
> import matplotlib.pyplot as plt
> %matplotlib inline
> import cartopy.crs as ccrs
> import cartopy.feature as cfeat
> 
> fig = plt.figure(figsize=(12,12))
> proj = ccrs.Stereographic(central_longitude=-65, central_latitude=35)
> 
> state_borders = cfeat.NaturalEarthFeature(category='cultural', 
> name='admin_1_states_provinces_lakes', scale='10m', facecolor='none')
> ax.add_feature(state_borders, linestyle='dotted', edgecolor='black')
> 
> fig = plt.figure(figsize=(15,15))
> ax=fig.add_subplot(1,1,1,projection=proj)
> ax.add_feature(state_borders, edgecolor='black')
> ax.add_feature(cfeat.LAND)
> ax.add_feature(cfeat.OCEAN)
> ax.add_feature(cfeat.COASTLINE)
> ax.add_feature(cfeat.BORDERS)
> ax.add_feature(cfeat.RIVERS)
> ax.gridlines()
> ax.set_extent([-70.2, -71.3, 42.3, 43])
> 
> 
> I get this this point and then I'm not sure how to plot it since I'm not 
> using the MapQuest map that was included in the example.  Any help on this 
> would be appreciated as well.
> 


Ticket Details
===================
Ticket ID: RRM-635285
Department: Support Python
Priority: Low
Status: Closed
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.