In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to draw a weather chart with Python". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to draw a weather chart with Python".
#! / bin/env python#coding=utf-8import codecsimport numpy as npimport datetimeimport reimport pandas as pdimport cartopy.crs as ccrsimport cartopy.feature as cfeaturefrom metpy.calc import wind_componentsfrom metpy.plots import add_metpy_logo, current_weather, sky_cover, StationPlot, wx_code_mapfrom cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatterfrom metpy.units import unitsimport matplotlib.pyplot as pltfrom matplotlib.patches import Polygonimport cartopy.io.shapereader as shpreader
Def func_return_self (x): return x
Def split (str_in, sep=None, convert_func=None): ss = str_in.split (sep) nn = len (ss) rst = [None] * nn if convert_func is None: cf = [func_return_self] * nn else: cf = [_ for _ in convert_func] # print (nn Len (cf)) for i in range (nn): xcf = func_return_self if cf [I] is None else cf [I] rst [I] = xcf (ss[ I]) return rst
Def d1_parse (filename,encoding= "gb2312"): "" read micaps type 1 data file to memory: return: "" with codecs.open (filename, mode='r', encoding=encoding) as fid: contents = fid.readlines () dataflag, style, title = split (contents [0], None,None)
Nn = len (contents) nx = int ((nn-2) / 2)
Data = {"stid": [None] * nx, "lon": [None] * nx, "lat": [None] * nx, "height": [None] * nx, "stlevel": [None] * nx, "cloud": [None] * nx, "wd": [None] * nx, "ws": [None] * nx, "psfc": [None] * nx "pd3": [None] * nx, "pw1": [None] * nx, "pw2": [None] * nx, "prc6": [None] * nx, "lc1": [None] * nx, "lc2": [None] * nx, "lc3": [None] * nx, "dp": [None] * nx, "njd": [None] * nx "cw": [None] * nx, "tc": [None] * nx, "mc": [None] * nx, "hc": [None] * nx, "flag1": [None] * nx, "flag2": [None] * nx, "dtc24": [None] * nx, "dp24": [None] * nx} data = pd.DataFrame (data)
Iline1 = contents [I] iline2 = contents [I] iline = ".join ([iline1,iline2]) # for iline in contents [2:]: data.stid [j], data.lon [j], data.lat [j], data.height [j], data.stlevel [j], data.cloud [j], data.wd [j], data.ws [j] Data.psfc [j], data.pd3 [j], data.pw1 [j], data.pw2 [j],\ data.prc6 [j], data.lc1 [j], data.lc2 [j], data.lc3 [j], data.dp [j], data.njd [j], data.cw [j], data.tc [j], data.mc [j], data.hc [j], data.flag1 [j], data.flag2 [j] Data.dtc24 [j], data.dp24 [j]\ = split (iline,None, [int,float,float,float, int,float,float,float,float,float,int,int,\ float,float,float,float,float,float,int, float,float,float,int,int, float,float]) jackers 1 # data.append ([stid, lon, lat, height, stlevel,cloud,wd,ws Psfc,pd3, pw1, pw2,\ # prc6, lc1,lc2,lc3, dp, njd, cw, tc, mc, hc, flag1, flag2, dtc24,dp24]) # print (data) return dataflag, style, title,datadef d4_parse (filename,encoding= "gb2312"): "read micaps type 4 data file to memory: return:"try: file_object = codecs.open (filename) Mode='r', encoding=encoding) all_the_text = file_object.read () .strip () file_object.close () contents = re.split ('[\ s] +' All_the_text) if len (contents) < 23: return dataflag = contents [0] .strip style = contents [1] .strip () title = contents [2] .strip () yy = int (contents [3] .strip () mm = int (contents [4] .strip ()) dd = int (contents [5] .strip ()) Hh = int (contents [6] .strip () forehh = int (contents [7] .strip ()) level = contents [8] .strip () deltalon = float (contents [9] .strip ()) deltalat = float (contents [10] .strip ()) beginlon = float (contents [11] .strip () endlon = float (contents [12] .strip ()) beginlat = float (contents [13] .strip () endlat = float (contents [14] .strip ()) sumlon = int (contents [15] .strip ()) sumlat = int (contents [16] .strip ()) distance = float (contents [17] .strip () min = float (contents [18] .strip ()) max = float (contents [19] .strip ()) def1 = contents [ 20] .strip () def2 = contents [21] .strip () x = np.arange (beginlon) Endlon + 0.9*deltalon, deltalon) y = np.arange (beginlat, endlat + 0.9*deltalat, deltalat) X, Y = np.meshgrid (x, y) if dataflag = 'diamond' and style = =' 4mm: begin = 22 Z = np.zeros ((sumlat) Sumlon)) for i in range (sumlon): for j in range (sumlat): Z [j, I] = float (contents [begin + j * sumlon + I]) return dataflag, style,title,yy,mm,dd,hh,forehh,level,X,Y,Z,distance,min,max,def1 Def2 except Exception as err: print (u'[{0}] {1}-{2} '.format (filename, err, datetime.datetime.now ()
If _ _ name__ = = "_ main__": dataflag, style, title, yy, mm, dd, hh, forehh, level, X, Y, p0_data, distance, dmin, dmax, def1, def2 = d4_parse ("/ home/hliang/share/share/plotest/20190126/p0/19012602.000") station_dataflag, staiton_style, station_title Station_data = d1_parse ("/ home/hliang/share/share/plotest/20190126/plot/19012108.000") station_data = station_data.replace (9999 JNP.nan) station_data = station_ data [station _ data.stlevel==2] # .dropna () lat_max=50 lat_min=15 lon_max=130 lon_min=100 proj = ccrs.LambertCylindrical (central_longitude= (lon_max+lon_min) / 2.0) # ccrs.LambertConformal (central_longitude= (lon_max+lon_min) / 2.0) Central_latitude= (lat_max+lat_min) / 2.0 standardstandard paralleles = [(lat_max+lat_min) / 2.0]) fig = plt.figure (figsize= (10,10)) ax = fig.add_subplot (1,1,1, projection=proj) ax.set_extent ([lon_min,lon_max,lat_min,lat_max], proj) ax.set_xticks (range (lon_min,lon_max+2,10), crs=proj) ax.set_yticks (range (lat_min) Lat_max+2,10), crs=proj) shp = shpreader.Reader ('/ home/hliang/share/share/plotest/shp_resources/CHN_adm/CHN_adm1.shp') ax.add_geometries (shp.geometries (), crs=proj, facecolor= "none", edgecolor= "grey", lw=0.8)
Contours = ax.contour (dmin,dmax+0.9*distance,distance), colors= "black", transform=proj,linewidths=0.5) plt.clabel (contours, fontsize=8,fmt='%.1f') stationplot = StationPlot (ax, station_data ['lon'] .values, station_data [' lat'] .values, clip_on=True, transform=proj, fontsize=6) u V = wind_components (station_data ['ws']. Values * units (' m _ wd''), station_data ['wd']. Values * units.degree) stationplot.plot_parameter (' NW', station_data ['tc'], color='red') stationplot.plot_barb (u, vmae sizesdict (emptybarb=0.15))
Cw = station_data.cw.replace (np.nan,0) .astype (np.int) pw1 = station_data.pw1.replace (np.nan,0) .astype (np.int) stationplot.plot_symbol ('web, cw, current_weather,color='fuchsia') stationplot.plot_symbol (' SE', pw1, current_weather,color='y')
For record,state in zip (shp.records (), shp.geometries ()): name= record.attributes ["NAME_1"] if name== "Jiangsu": ax.add_geometries (state,crs=proj,facecolor= "none", edgecolor= "grey", lw=0.5)''plt.savefig ('. / fig1.png',dpi=300) # plt.show () Thank you for your reading The above is the content of "how to draw weather charts with Python". After the study of this article, I believe you have a deeper understanding of how to draw weather charts with Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.