百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

「实用」气象Python学习手册 by Unidata

toyiye 2024-08-25 15:48 5 浏览 0 评论

?1.Unidata Python Gallery


基础库安装到位,运行脚本即可出图

脚本可点击文末阅读原文下载

页面链接:https://unidata.github.io/python-gallery/examples/index.html


2.Useful Python Tools

This is a list of useful and/or new Python tools that the Unidata Python Team and community are keeping an eye on or using.

Unidata Projects

  • MetPy - General meteorological toolkit
  • siphon - Remote data access
  • netCDF4-python - netCDF4 API

Meteorology Specific

  • PyART - Python ARM Radar Toolkit

Data Wrangling

  • pandas - Easy tabular data manipulation
  • xarray - Gridded/labeled multidimensional data

Plotting

  • matplotlib - Beautiful publication quality graphics
  • Bokeh - Interactive web graphics
  • Cartopy - Plotting maps

Core Python/Interface

  • iPython - Interactive Python shell
  • Jupyter - Notebooks and the new Jupyter Lab
  • pathlib - Easy file path manipulation

Education

  • nbgrader - An automatic homework grader for notebooks

Performance

  • Numba - JIT compiler
  • Dask - Distributed computing


3.实例



  1from?datetime?import?datetime  2  3import?matplotlib.pyplot?as?plt  4import?metpy.calc?as?mpcalc  5from?metpy.units?import?units  6import?numpy?as?np  7from?pyproj?import?Geod  8from?scipy.interpolate?import?griddata  9from?scipy.ndimage?import?gaussian_filter 10from?siphon.simplewebservice.wyoming?import?WyomingUpperAir 11 12def?vertical_interpolate(vcoord_data,?interp_var,?interp_levels): 13????"""A?function?to?interpolate?sounding?data?from?each?station?to 14????every?millibar.?Assumes?a?log-linear?relationship. 15 16????Input 17????----- 18????vcoord_data?:?A?1D?array?of?vertical?level?values?(e.g.,?pressure?from?a?radiosonde) 19????interp_var?:?A?1D?array?of?the?variable?to?be?interpolated?to?all?pressure?levels 20????vcoord_interp_levels?:?A?1D?array?containing?veritcal?levels?to?interpolate?to 21 22????Return 23????------ 24????interp_data?:?A?1D?array?that?contains?the?interpolated?variable?on?the?interp_levels 25????""" 26 27????#?Make?veritcal?coordinate?data?and?grid?level?log?variables 28????lnp?=?np.log(vcoord_data) 29????lnp_intervals?=?np.log(interp_levels) 30 31????#?Use?numpy?to?interpolate?from?observed?levels?to?grid?levels 32????interp_data?=?np.interp(lnp_intervals[::-1],?lnp[::-1],?interp_var[::-1])[::-1] 33 34????#?Mask?for?missing?data?(generally?only?near?the?surface) 35????mask_low?=?interp_levels?>?vcoord_data[0] 36????mask_high?=?interp_levels?<?vcoord_data[-1] 37????interp_data[mask_low]?=?interp_var[0] 38????interp_data[mask_high]?=?interp_var[-1] 39 40????return?interp_data 41 42def?radisonde_cross_section(stns,?data,?start=1000,?end=100,?step=10): 43????"""This?function?takes?a?list?of?radiosonde?observation?sites?with?a 44????dictionary?of?Pandas?Dataframes?with?the?requesite?data?for?each?station. 45 46????Input 47????----- 48????stns?:?List?of?statition?three-letter?identifiers 49????data?:?A?dictionary?of?Pandas?Dataframes?containing?the?radiosonde?observations 50????for?the?stations 51????start?:?interpolation?start?value,?optional?(default?=?1000?hPa) 52????end?:?Interpolation?end?value,?optional?(default?=?100?hPa) 53????step?:?Interpolation?interval,?option?(default?=?10?hPa) 54 55????Return 56????------ 57????cross_section?:?A?dictionary?that?contains?the?following?variables 58 59????????grid_data?:?An?interpolated?grid?with?100?points?between?the?first?and?last?station, 60????????with?the?corresponding?number?of?vertical?points?based?on?start,?end,?and?interval 61????????(default?is?90) 62????????obs_distance?:?An?array?of?distances?between?each?radiosonde?observation?location 63????????x_grid?:?A?2D?array?of?horizontal?direction?grid?points 64????????p_grid?:?A?2D?array?of?vertical?pressure?levels 65????????ground_elevation?:?A?representation?of?the?terrain?between?radiosonde?observation?sites 66????????based?on?the?elevation?of?each?station?converted?to?pressure?using?the?standard 67????????atmosphere 68 69????""" 70????#?Set?up?vertical?grid,?largest?value?first?(high?pressure?nearest?surface) 71????vertical_levels?=?np.arange(start,?end-1,?-step) 72 73????#?Number?of?vertical?levels?and?stations 74????plevs?=?len(vertical_levels) 75????nstns?=?len(stns) 76 77????#?Create?dictionary?of?interpolated?values?and?include?neccsary?attribute?data 78????#?including?lat,?lon,?and?elevation?of?each?station 79????lats?=?[] 80????lons?=?[] 81????elev?=?[] 82????keys?=?data[stns[0]].keys()[:8] 83????tmp_grid?=?dict.fromkeys(keys) 84 85????#?Interpolate?all?variables?for?each?radiosonde?observation 86????#?Temperature,?Dewpoint,?U-wind,?V-wind 87????for?key?in?tmp_grid.keys(): 88????????tmp_grid[key]?=?np.empty((nstns,?plevs)) 89????????for?station,?loc?in?zip(stns,?range(nstns)): 90????????????if?key?==?'pressure': 91????????????????lats.append(data[station].latitude[0]) 92????????????????lons.append(data[station].longitude[0]) 93????????????????elev.append(data[station].elevation[0]) 94????????????????tmp_grid[key][loc,?:]?=?vertical_levels 95????????????else: 96????????????????tmp_grid[key][loc,?:]?=?vertical_interpolate( 97????????????????????data[station]['pressure'].values,?data[station][key].values, 98????????????????????vertical_levels) 99100????#?Compute?distance?between?each?station?using?Pyproj101????g?=?Geod(ellps='sphere')102????_,?_,?dist?=?g.inv(nstns*[lons[0]],?nstns*[lats[0]],?lons[:],?lats[:])103104????#?Compute?sudo?ground?elevation?in?pressure?from?standard?atmsophere?and?the?elevation105????#?of?each?station106????ground_elevation?=?mpcalc.height_to_pressure_std(np.array(elev)?*?units('meters'))107108????#?Set?up?grid?for?2D?interpolation109????grid?=?dict.fromkeys(keys)110????x?=?np.linspace(dist[0],?dist[-1],?100)111????nx?=?len(x)112113????pp,?xx?=?np.meshgrid(vertical_levels,?x)114????pdist,?ddist?=?np.meshgrid(vertical_levels,?dist)115116????#?Interpolate?to?2D?grid?using?scipy.interpolate.griddata117????for?key?in?grid.keys():118????????grid[key]?=?np.empty((nx,?plevs))119????????grid[key][:]?=?griddata((ddist.flatten(),?pdist.flatten()),120????????????????????????????????tmp_grid[key][:].flatten(),121????????????????????????????????(xx,?pp),122????????????????????????????????method='cubic')123124????#?Gather?needed?data?in?dictionary?for?return125????cross_section?=?{'grid_data':?grid,?'obs_distance':?dist,126?????????????????????'x_grid':?xx,?'p_grid':?pp,?'elevation':?ground_elevation}127????return?cross_section128#?A?roughly?east-west?cross?section129stn_list?=?['DNR',?'LBF',?'OAX',?'DVN',?'DTX',?'BUF']130131#?Set?a?date?and?hour?of?your?choosing132date?=?datetime(2019,?12,?20,?0)133134df?=?{}135136#?Loop?over?stations?to?get?data?and?put?into?dictionary137for?station?in?stn_list:138????df[station]?=?WyomingUpperAir.request_data(date,?station)139xsect?=?radisonde_cross_section(stn_list,?df)140141potemp?=?mpcalc.potential_temperature(142????xsect['p_grid']?*?units('hPa'),?xsect['grid_data']['temperature']?*?units('degC'))143144relhum?=?mpcalc.relative_humidity_from_dewpoint(145????xsect['grid_data']['temperature']?*?units('degC'),146????xsect['grid_data']['dewpoint']?*?units('degC'))147148mixrat?=?mpcalc.mixing_ratio_from_relative_humidity(relhum,149????????????????????????????????????????????????????xsect['grid_data']['temperature']?*150????????????????????????????????????????????????????units('degC'),151????????????????????????????????????????????????????xsect['p_grid']?*?units('hPa'))152153fig?=?plt.figure(figsize=(17,?11))154155#?Specify?plotting?axis?(single?panel)156ax?=?plt.subplot(111)157158#?Set?y-scale?to?be?log?since?pressure?decreases?exponentially?with?height159ax.set_yscale('log')160161#?Set?limits,?tickmarks,?and?ticklabels?for?y-axis162ax.set_ylim([1030,?101])163ax.set_yticks(range(1000,?101,?-100))164ax.set_yticklabels(range(1000,?101,?-100))165166#?Invert?the?y-axis?since?pressure?decreases?with?increasing?height167ax.invert_yaxis()168169#?Plot?the?sudo?elevation?on?the?cross?section170ax.fill_between(xsect['obs_distance'],?xsect['elevation'].m,?1030,171????????????????where=xsect['elevation'].m?<=?1030,?facecolor='lightgrey',172????????????????interpolate=True,?zorder=10)173#?Don't?plot?xticks174plt.xticks([],?[])175176#?Plot?wind?barbs?for?each?sounding?location177for?stn,?stn_name?in?zip(range(len(stn_list)),?stn_list):178????ax.axvline(xsect['obs_distance'][stn],?ymin=0,?ymax=1,179???????????????linewidth=2,?color='blue',?zorder=11)180????ax.text(xsect['obs_distance'][stn],?1100,?stn_name,?ha='center',?color='blue')181????ax.barbs(xsect['obs_distance'][stn],?df[stn_name]['pressure'][::2],182?????????????df[stn_name]['u_wind'][::2,?None],183?????????????df[stn_name]['v_wind'][::2,?None],?zorder=15)184185#?Plot?smoothed?potential?temperature?grid?(K)186cs?=?ax.contour(xsect['x_grid'],?xsect['p_grid'],?gaussian_filter(187????potemp,?sigma=1.0),?range(0,?500,?5),?colors='red')188ax.clabel(cs,?fmt='%i')189190#?Plot?smoothed?mixing?ratio?grid?(g/kg)191cs?=?ax.contour(xsect['x_grid'],?xsect['p_grid'],?gaussian_filter(192????mixrat*1000,?sigma=2.0),?range(0,?41,?2),?colors='tab:green')193ax.clabel(cs,?fmt='%i')194195#?Add?some?informative?titles196plt.title('Cross-Section?from?DNR?to?BUF?Potential?Temp.?'197??????????'(K;?red)?and?Mix.?Rat.?(g/kg;?green)',?loc='left')198plt.title(date,?loc='right')199200plt.show()

相关推荐

# Python 3 # Python 3字典Dictionary(1)

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中,格式如...

Python第八课:数据类型中的字典及其函数与方法

Python3字典字典是另一种可变容器模型,且可存储任意类型对象。字典的每个键值...

Python中字典详解(python 中字典)

字典是Python中使用键进行索引的重要数据结构。它们是无序的项序列(键值对),这意味着顺序不被保留。键是不可变的。与列表一样,字典的值可以保存异构数据,即整数、浮点、字符串、NaN、布尔值、列表、数...

Python3.9又更新了:dict内置新功能,正式版十月见面

机器之心报道参与:一鸣、JaminPython3.8的热乎劲还没过去,Python就又双叒叕要更新了。近日,3.9版本的第四个alpha版已经开源。从文档中,我们可以看到官方透露的对dic...

Python3 基本数据类型详解(python三种基本数据类型)

文章来源:加米谷大数据Python中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。在Python中,变量就是变量,它没有类型,我们所说的"类型"是变...

一文掌握Python的字典(python字典用法大全)

字典是Python中最强大、最灵活的内置数据结构之一。它们允许存储键值对,从而实现高效的数据检索、操作和组织。本文深入探讨了字典,涵盖了它们的创建、操作和高级用法,以帮助中级Python开发...

超级完整|Python字典详解(python字典的方法或操作)

一、字典概述01字典的格式Python字典是一种可变容器模型,且可存储任意类型对象,如字符串、数字、元组等其他容器模型。字典的每个键值key=>value对用冒号:分割,每个对之间用逗号,...

Python3.9版本新特性:字典合并操作的详细解读

处于测试阶段的Python3.9版本中有一个新特性:我们在使用Python字典时,将能够编写出更可读、更紧凑的代码啦!Python版本你现在使用哪种版本的Python?3.7分?3.5分?还是2.7...

python 自学,字典3(一些例子)(python字典有哪些基本操作)

例子11;如何批量复制字典里的内容2;如何批量修改字典的内容3;如何批量修改字典里某些指定的内容...

Python3.9中的字典合并和更新,几乎影响了所有Python程序员

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

Python3大字典:《Python3自学速查手册.pdf》限时下载中

最近有人会想了,2022了,想学Python晚不晚,学习python有前途吗?IT行业行业薪资高,发展前景好,是很多求职群里严重的香饽饽,而要进入这个高薪行业,也不是那么轻而易举的,拿信工专业的大学生...

python学习——字典(python字典基本操作)

字典Python的字典数据类型是基于hash散列算法实现的,采用键值对(key:value)的形式,根据key的值计算value的地址,具有非常快的查取和插入速度。但它是无序的,包含的元素个数不限,值...

324页清华教授撰写【Python 3 菜鸟查询手册】火了,小白入门字典

如何入门学习python...

Python3.9中的字典合并和更新,了解一下

全文共2837字,预计学习时长9分钟Python3.9正在积极开发,并计划于今年10月发布。2月26日,开发团队发布了alpha4版本。该版本引入了新的合并(|)和更新(|=)运算符,这个新特性几乎...

python3基础之字典(python中字典的基本操作)

字典和列表一样,也是python内置的一种数据结构。字典的结构如下图:列表用中括号[]把元素包起来,而字典是用大括号{}把元素包起来,只不过字典的每一个元素都包含键和值两部分。键和值是一一对应的...

取消回复欢迎 发表评论:

请填写验证码