当前位置: 首页 > news >正文

python中的.nc文件处理 | 05 NetCDF数据的进一步分析


NetCDF数据的进一步分析
比较不同数据集、不同季节的气候数据
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import seaborn as sns
import geopandas as gpd
import earthpy as et
import xarray as xr
import regionmask

绘制设置

sns.set(font_scale=1.3) # 字号
sns.set_style(“white”,{‘font.family’: ‘Times New Roman’}) # 主题

读取数据集
2006-2099年月最高气温
data_path_monthly = ‘http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_rcp45_2006_2099_CONUS_monthly.nc’

with xr.open_dataset(data_path_monthly) as file_nc:
monthly_forecast_temp_xr=file_nc

monthly_forecast_temp_xr

读取矢量数据

states_path = “ne_50m_admin_1_states_provinces_lakes”
states_path = os.path.join(
states_path, “ne_50m_admin_1_states_provinces_lakes.shp”)

states_gdf=gpd.read_file(states_path)
cali_aoi=states_gdf[states_gdf.name==“California”]

获取aoi的左下-右上经纬度坐标

def get_aoi(shp, world=True):
“”“Takes a geopandas object and converts it to a lat/ lon
extent “””

lon_lat = {}
# Get lat min, max
aoi_lat = [float(shp.total_bounds[1]), float(shp.total_bounds[3])]
aoi_lon = [float(shp.total_bounds[0]), float(shp.total_bounds[2])]# Handle the 0-360 lon values
if world:aoi_lon[0] = aoi_lon[0] + 360aoi_lon[1] = aoi_lon[1] + 360
lon_lat["lon"] = aoi_lon
lon_lat["lat"] = aoi_lat
return lon_lat

cali_bounds=get_aoi(cali_aoi)

数据切片

start_date=“2059-12-15”
end_date=“2099-12-15”

cali_temp=monthly_forecast_temp_xr[“air_temperature”].sel(
time=slice(start_date,end_date),
lon=slice(cali_bounds[“lon”][0],cali_bounds[“lon”][1]),
lat=slice(cali_bounds[“lat”][0],cali_bounds[“lat”][1]))
cali_temp

print("Time Period start: ", cali_temp.time.min().values)
print("Time Period end: ", cali_temp.time.max().values)

Time Period start: 2059-12-15 00:00:00
Time Period end: 2099-12-15 00:00:00

掩膜

cali_mask=regionmask.mask_3D_geopandas(cali_aoi,
monthly_forecast_temp_xr.lon,
monthly_forecast_temp_xr.lat)
cali_temp_masked=cali_temp.where(cali_mask)
cali_temp_masked.dims

(‘time’, ‘lat’, ‘lon’, ‘region’)

cali_temp_masked.shape

(481, 227, 246, 1)

计算每个季节的平均最高温

groupby()参数:time.season time.month time.year

cali_season_summary=cali_temp_masked.groupby(
“time.season”).mean(“time”,skipna=True)
cali_season_summary.shape # 汇总成四个季节

(4, 227, 246, 1)

(DJF=Dec-Feb, MAM=Mar-May,. JJA=Jun-Aug, SON=Sep-Nov)

cali_season_summary.plot(col=“season”,col_wrap=2,figsize=(10,10))
plt.suptitle(“Mean Temperature Across All Selected Years By Season \n California, USA”,
y=1.05)

plt.show()

计算每年的每个季节的均值变化
String in the ‘#offset’ to specify the step-size along the resampled dimension

‘AS’: year start
‘QS-DEC’: quarterly, starting on December 1
‘MS’: month start
‘D’: day
‘H’: hour
‘Min’: minute

Resample the data by season across all years

cali_season_mean_all_years = cali_temp_masked.resample(
time=‘QS-DEC’, keep_attrs=True).mean()
cali_season_mean_all_years.shape

(161, 227, 246, 1)

Summarize each array into one single (mean) value

cali_seasonal_mean = cali_season_mean_all_years.groupby(‘time’).mean([
“lat”, “lon”])
cali_seasonal_mean.shape

(161, 1)

f, ax = plt.subplots(figsize=(10, 4))
cali_seasonal_mean.plot(marker=“o”,
color=“grey”,
markerfacecolor=“purple”,
markeredgecolor=“purple”)
ax.set(title=“Seasonal Mean Temperature”)
plt.show()

转换成pd格式

cali_seasonal_mean_df = cali_seasonal_mean.to_dataframe()
cali_seasonal_mean_df.head()

导出为csv

cali_seasonal_mean_df.to_csv(“cali-seasonal-temp.csv”)

分季节绘制均值变化
colors={3:“grey”,6:“lightgreen”,9:“green”,12:“purple”}
seasons={3:“spring”,6:“summer”,9:“fall”,12:“winter”}

f,ax=plt.subplots(figsize=(10,7))
for month,arr in cali_seasonal_mean.groupby(“time.month”):
arr.plot(ax=ax,
color=“grey”,
marker=“o”,
markerfacecolor=colors[month],
markeredgecolor=colors[month],
label=seasons[month])
ax.legend(bbox_to_anchor=(1.05,1),loc=“upper left”)
ax.set(title=“Seasonal Change in Mean Temperature Over Time”)
plt.show()

参考链接:
https://www.earthdatascience.org/courses/use-data-open-source-python/hierarchical-data-formats-hdf/summarize-climate-data-by-season/

https://xarray.pydata.org/en/v0.8.2/generated/xarray.Dataset.resample.html

https://gitee.com/jiangroubao/learning/tree/master/NetCDF4

http://www.lryc.cn/news/9901.html

相关文章:

  • GGX发布全新路线图,揭示具备 Layer0 特性且可编程的跨链基建生态
  • taro+vue3 搭建一套框架,适用于微信小程序和H5
  • C++:模板初阶(泛型编程、函数模板、类模板)
  • 把数组排成最小的数 AcWing(JAVA)
  • 4.3 PBR
  • hmac — 加密消息签名和验证
  • AWS攻略——使用ACL限制访问
  • 【已解决】关于 luckysheet 设置纯文本,解决日期格式回显错误的办法
  • Jackson
  • 字节软件测试岗:惨不忍睹的三面,幸好做足了准备,月薪19k,已拿offer
  • vue使用axios发送post请求携带json body参数,后端使用@RequestBody进行接收
  • 【python百炼成魔】python之列表详解
  • 如何学习 Web3
  • 大数据框架之Hadoop:MapReduce(一)MapReduce概述
  • 一文搞定python语法进阶
  • 2019蓝桥杯真题数列求值(填空题) C语言/C++
  • spring中@Autowire和@Resource的区别在哪里?
  • 算法训练营DAY54|583. 两个字符串的删除操作、72. 编辑距离
  • 【Ctfshow_Web】信息收集和爆破
  • 基于机器学习的推荐算法研究与实现
  • (二十四)ATP应用测试平台——springboot集成fastdfs上传与下载功能
  • linux好用命令+vs快捷键
  • Git 构建分布式版本控制系统
  • Day891.一主多从的切换正确性 -MySQL实战
  • 【论文笔记】图像修复Learning Joint Spatial-Temporal Transformations for Video Inpainting
  • 代码随想录算法训练营第二天 | 977.有序数组的平方 、209.长度最小的子数组 、59.螺旋矩阵II、总结
  • Python pickle模块:实现Python对象的持久化存储
  • 【C++】C/C++内存管理
  • 【测试】自动化测试02
  • Python空间分析| 02 利用Python计算空间局部自相关(LISA)