1 | from viz.utils import peek |
---|
2 | from viz.utils import cardinal_2_month |
---|
3 | import pylab as p |
---|
4 | import matplotlib.numerix.ma as ma |
---|
5 | import numpy as n |
---|
6 | from string import zfill |
---|
7 | from matplotlib.toolkits.basemap import Basemap |
---|
8 | |
---|
9 | a_small_number = 1e-8 |
---|
10 | |
---|
11 | def set_default_basemap(lon, lat): |
---|
12 | map = Basemap( |
---|
13 | llcrnrlon=lon.min() - 0.5, |
---|
14 | llcrnrlat=lat.min() - 0.5, |
---|
15 | urcrnrlon=lon.max() + 0.5, |
---|
16 | urcrnrlat=lat.max() + 0.5, |
---|
17 | resolution='l', |
---|
18 | projection='cyl', |
---|
19 | lon_0=lon.min() + (lon.max()-lon.min())/2., |
---|
20 | lat_0=lat.min() + (lat.max()-lat.min())/2. |
---|
21 | ) |
---|
22 | return map |
---|
23 | |
---|
24 | def plot_temp(file_name, cntr_lvl=None, save_frames=False): |
---|
25 | file, vars = peek(file_name, show_vars=False) |
---|
26 | lon = vars['lon'].get_value() |
---|
27 | lat = vars['lat'].get_value() |
---|
28 | air_temp_var = vars['air_temp'] |
---|
29 | air_temp = air_temp_var.get_value()[0] |
---|
30 | lvl = vars['lvl'].get_value() |
---|
31 | valid_date = str(vars['valid_date'].get_value()[0]) |
---|
32 | valid_time = zfill(str(vars['valid_time'].get_value()[0]), 4) |
---|
33 | valid_when = valid_date[6:] + ' ' \ |
---|
34 | + cardinal_2_month(int(valid_date[4:6])) + ' ' \ |
---|
35 | + valid_date[0:4] \ |
---|
36 | + ' ' + valid_time[:2] + ':' \ |
---|
37 | + valid_time[2:] + ' UTC' |
---|
38 | if save_frames: |
---|
39 | frame_number = 0 |
---|
40 | m = set_default_basemap(lon,lat) |
---|
41 | # must plot using 2d lon and lat |
---|
42 | LON, LAT = p.meshgrid(lon,lat) |
---|
43 | for lvl_idx in range(5): |
---|
44 | #for lvl_idx in range(len(lvl)): |
---|
45 | p.figure() |
---|
46 | if cntr_lvl is not None: |
---|
47 | m.contourf(LON,LAT,air_temp[lvl_idx], cntr_lvl) |
---|
48 | else: |
---|
49 | m.contourf(LON,LAT,air_temp[lvl_idx]) |
---|
50 | m.drawcoastlines() |
---|
51 | m.drawmeridians(n.array(n.arange(lon.min(), lon.max() + a_small_number, 15.)), labels=[1,0,0,1]) |
---|
52 | m.drawparallels(n.array(n.arange(lat.min(), lat.max() + a_small_number, 15.)), labels=[1,0,0,1]) |
---|
53 | p.colorbar(orientation='horizontal', shrink=0.7, fraction=0.02, pad=0.07, aspect=50) |
---|
54 | title_string = 'Air temperature (K) valid at ' \ |
---|
55 | + '\n' + valid_when + ' ' \ |
---|
56 | + str(round(lvl[lvl_idx],4)) + ' sigma' \ |
---|
57 | + ' from LAPS' |
---|
58 | p.title(title_string) |
---|
59 | if save_frames: |
---|
60 | p.savefig('frames/frame_' + zfill(str(frame_number),3) +'_air_temp_' + str(lvl[lvl_idx]) + '_sigma.png') |
---|
61 | frame_number += 1 |
---|
62 | return |
---|
63 | |
---|
64 | |
---|