1 | #!/usr/bin/python3 |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | """ |
---|
4 | Created on Mar 7, 2016 |
---|
5 | |
---|
6 | @author: petra |
---|
7 | |
---|
8 | Version 2: return also vertical levels, based on U wind |
---|
9 | Version 3: for Python3 |
---|
10 | |
---|
11 | Usage: `./grib_domain.py <gribfile> [map]` |
---|
12 | |
---|
13 | The map will only be shown if the `map` keyward is present. |
---|
14 | |
---|
15 | Dependencies: you need python3-grib and python3-mpltoolkits.basemap |
---|
16 | (Debian packages) or equivalent packages of your OS |
---|
17 | |
---|
18 | """ |
---|
19 | |
---|
20 | import pygrib |
---|
21 | import sys |
---|
22 | |
---|
23 | from mpl_toolkits.basemap import Basemap |
---|
24 | import numpy as np |
---|
25 | import matplotlib.pyplot as plt |
---|
26 | #from matplotlib.backends.backend_pdf import PdfPages |
---|
27 | |
---|
28 | |
---|
29 | if len(sys.argv)==1: |
---|
30 | if sys.argv[1] == '-h' or sys.argv[1] == '--help': |
---|
31 | print('\nUsage: ./grib_domain.py <gribfile> [map]') |
---|
32 | print('The map will only be shown if the `map` keyword is present.\n') |
---|
33 | quit() |
---|
34 | elif len(sys.argv)>=2: |
---|
35 | if sys.argv[1] == '-h' or sys.argv[1] == '--help': |
---|
36 | print('\nUsage: ./grib_domain.py <gribfile> [map]') |
---|
37 | print('The map will only be shown if the `map` keyword is present.\n') |
---|
38 | quit() |
---|
39 | else: |
---|
40 | fname=sys.argv[1] |
---|
41 | |
---|
42 | Map=False |
---|
43 | if len(sys.argv)==3: |
---|
44 | if sys.argv[2]=='map': Map=True |
---|
45 | |
---|
46 | en=pygrib.open(fname) |
---|
47 | lats, lons = en[1].latlons() |
---|
48 | ymin=lats.min() |
---|
49 | ymax=lats.max() |
---|
50 | xmin=lons.min() |
---|
51 | xmax=lons.max() |
---|
52 | |
---|
53 | ym = 0.5*(ymin+ymax) |
---|
54 | xm = 0.5*(xmin+xmax) |
---|
55 | numx=lats.shape[1]-1 |
---|
56 | numy=lats.shape[0]-1 |
---|
57 | dx=(xmax-xmin)/numx |
---|
58 | dy=(ymax-ymin)/numy |
---|
59 | levels=[] |
---|
60 | print('Fields found in',fname,':') |
---|
61 | for field in en: |
---|
62 | content = field.__repr__() |
---|
63 | print(field) |
---|
64 | if 'U component of wind' in content: |
---|
65 | levels.append(content.split(':')[5].split(' ')[1]) |
---|
66 | if len(levels) == 0: |
---|
67 | print(fname+': ','x=',xmin,xmax,dx,numx+1, ' y=',ymin,ymax,dy,numy+1,\ |
---|
68 | 'nuvz=',len(levels)) |
---|
69 | else: |
---|
70 | print(fname+': ','x/xmin/xmax/dx/numx=',xmin,xmax,dx,numx+1, '; y=',ymin,ymax,dy,numy+1,\ |
---|
71 | '; nuvz (min max) =',len(levels),'(',min(levels),max(levels),')') |
---|
72 | |
---|
73 | if Map==True: |
---|
74 | |
---|
75 | # lon_0 is central longitude of projection. |
---|
76 | # resolution = 'c' means use crude resolution coastlines. |
---|
77 | #m = Basemap(projection='robin',lon_0=45.,resolution='c') |
---|
78 | #m = Basemap(projection='ortho',lat_0=ym,lon_0=xm,resolution='l') |
---|
79 | xmin5=max(xmin-5.,-180.) |
---|
80 | xmax5=min(xmax+5.,180.) |
---|
81 | ymin5=max(ymin-5.,-90.) |
---|
82 | ymax5=min(ymax+5.,90.) |
---|
83 | xmin10=max(xmin-10.,-180.) |
---|
84 | xmax10=min(xmax+10.,180.) |
---|
85 | ymin10=max(ymin-10.,-90.) |
---|
86 | ymax10=min(ymax+10.,90.) |
---|
87 | |
---|
88 | m = Basemap(llcrnrlon=xmin5,llcrnrlat=ymin5, |
---|
89 | urcrnrlon=xmax5,urcrnrlat=ymax5, |
---|
90 | projection='mill',lat_0=ym,lon_0=xm, |
---|
91 | resolution ='l',area_thresh=10000.) |
---|
92 | m.drawcoastlines(linewidth=.5) |
---|
93 | m.drawcountries(color='w') |
---|
94 | m.fillcontinents() |
---|
95 | # draw parallels and meridians. |
---|
96 | m.drawparallels(np.arange(ymin10,ymax10,5.),linewidth=1.,labels=[1,0,0,0]) |
---|
97 | m.drawmeridians(np.arange(xmin10,xmax10,5.),linewidth=1.,latmax=90., |
---|
98 | labels=[0,0,0,1]) |
---|
99 | m.plot(lons,lats,'b+',ms=2.,latlon=True) |
---|
100 | plt.title('DX='+str(dx)+' DY='+str(dy),color='b') |
---|
101 | #m.etopo(scale=0.25) |
---|
102 | plt.show() |
---|
103 | print('done') |
---|