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

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

toyiye 2024-06-21 12:30 8 浏览 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()

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码