日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中的.nc文件处理 | 03 指定位置的数据切片及可视化

發布時間:2024/1/8 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中的.nc文件处理 | 03 指定位置的数据切片及可视化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

NetCDF4文件處理

  • 下載MACA v2的 netcdf4 格式數據
  • 使用 xarray 讀取和處理 netcdf4 格式數據
  • 將 netcdf4 格式數據導出為 .csv 格式
  • 將 netcdf4 格式數據導出為 .tif 格式

參考鏈接

import osimport numpy as np import pandas as pd import matplotlib.pyplot as plt # 處理netcdf4文件所要用到的包 import xarray as xr import rioxarray import cartopy.crs as ccrs import cartopy.feature as cfeature import seaborn as sns import geopandas as gpd import earthpy as et# 統計圖繪制選項 sns.set(font_scale=1.3) sns.set_style("white")

文件讀取

.nc文件名的含義

agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_historical_1950_2005_CONUS_monthly

  • agg_macav2metdata :MACA v2版本,降尺度到美國大陸
  • tasmax :數據項為最高溫度
  • BNU-ESM :產生該原始數據的模式名稱
  • historical :數據為1950-2005年的歷史預測數據
  • CONUS :數據范圍為美國(CONtinental United States boundary)
  • monthly :數據的時間分辨率為月份
# MACAv2數據連接 data_path = "http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_tasmax_BNU-ESM_r1i1p1_historical_1950_2005_CONUS_monthly.nc"# 打開數據 with xr.open_dataset(data_path) as file_nc:# 使用 rio.write _ crs,確保數據的坐標系在整個分析過程中都保持不變max_temp_xr = file_nc.rio.write_crs(file_nc.rio.crs, inplace=True)# 查看數據對象 max_temp_xr # 讀取數據的坐標系統信息 climate_crs = max_temp_xr.rio.crs climate_crs CRS.from_wkt('GEOGCS["undefined",DATUM["undefined",SPHEROID["undefined",6378137,298.257223563]],PRIMEM["undefined",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Longitude",EAST],AXIS["Latitude",NORTH]]') # 查看前五行數據max_temp_xr['air_temperature']['lat'].values[:5]# 輸出經緯度的最大值和最小值 print("The min and max latitude values in the data is:", max_temp_xr["air_temperature"]["lat"].values.min(), max_temp_xr["air_temperature"]["lat"].values.max()) print("The min and max longitude values in the data is:", max_temp_xr["air_temperature"]["lon"].values.min(), max_temp_xr["air_temperature"]["lon"].values.max()) The min and max latitude values in the data is: 25.063077926635742 49.39602279663086 The min and max longitude values in the data is: 235.22784423828125 292.93524169921875 # 查看數據的時間范圍 print("The earliest date in the data is:", max_temp_xr["air_temperature"]["time"].values.min()) print("The latest date in the data is:", max_temp_xr["air_temperature"]["time"].values.max()) The earliest date in the data is: 1950-01-15 00:00:00 The latest date in the data is: 2005-12-15 00:00:00 # 查看數據記錄的總條數,本數據集共672條,即672個月 max_temp_xr["air_temperature"]["time"].values.shape (672,) # 查看數據的坐標系統信息 max_temp_xr["crs"] # 查看數據的元數據 metadata = max_temp_xr.attrs metadata {'description': 'Multivariate Adaptive Constructed Analogs (MACA) method, version 2.3,Dec 2013.','id': 'MACAv2-METDATA','naming_authority': 'edu.uidaho.reacch','Metadata_Conventions': 'Unidata Dataset Discovery v1.0','Metadata_Link': '','cdm_data_type': 'FLOAT','title': 'Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the historical scenario.','summary': 'This archive contains monthly downscaled meteorological and hydrological projections for the Conterminous United States at 1/24-deg resolution. These monthly values are obtained by aggregating the daily values obtained from the downscaling using the Multivariate Adaptive Constructed Analogs (MACA, Abatzoglou, 2012) statistical downscaling method with the METDATA (Abatzoglou,2013) training dataset. The downscaled meteorological variables are maximum/minimum temperature(tasmax/tasmin), maximum/minimum relative humidity (rhsmax/rhsmin),precipitation amount(pr), downward shortwave solar radiation(rsds), eastward wind(uas), northward wind(vas), and specific humidity(huss). The downscaling is based on the 365-day model outputs from different global climate models (GCMs) from Phase 5 of the Coupled Model Inter-comparison Project (CMIP3) utlizing the historical (1950-2005) and future RCP4.5/8.5(2006-2099) scenarios. ','keywords': 'monthly, precipitation, maximum temperature, minimum temperature, downward shortwave solar radiation, specific humidity, wind velocity, CMIP5, Gridded Meteorological Data','keywords_vocabulary': '','standard_name_vocabulary': 'CF-1.0','history': 'No revisions.','comment': '','geospatial_bounds': 'POLYGON((-124.7722 25.0631,-124.7722 49.3960, -67.0648 49.3960,-67.0648, 25.0631, -124.7722,25.0631))','geospatial_lat_min': '25.0631','geospatial_lat_max': '49.3960','geospatial_lon_min': '-124.7722','geospatial_lon_max': '-67.0648','geospatial_lat_units': 'decimal degrees north','geospatial_lon_units': 'decimal degrees east','geospatial_lat_resolution': '0.0417','geospatial_lon_resolution': '0.0417','geospatial_vertical_min': 0.0,'geospatial_vertical_max': 0.0,'geospatial_vertical_resolution': 0.0,'geospatial_vertical_positive': 'up','time_coverage_start': '2000-01-01T00:0','time_coverage_end': '2004-12-31T00:00','time_coverage_duration': 'P5Y','time_coverage_resolution': 'P1M','date_created': '2014-05-15','date_modified': '2014-05-15','date_issued': '2014-05-15','creator_name': 'John Abatzoglou','creator_url': 'http://maca.northwestknowledge.net','creator_email': 'jabatzoglou@uidaho.edu','institution': 'University of Idaho','processing_level': 'GRID','project': '','contributor_name': 'Katherine C. Hegewisch','contributor_role': 'Postdoctoral Fellow','publisher_name': 'REACCH','publisher_email': 'reacch@uidaho.edu','publisher_url': 'http://www.reacchpna.org/','license': 'Creative Commons CC0 1.0 Universal Dedication(http://creativecommons.org/publicdomain/zero/1.0/legalcode)','coordinate_system': 'WGS84,EPSG:4326','grid_mapping': 'crs'} # 可以以調用字典的方式,查看元數據中的數據項目 # 如查看數據標題 metadata["title"] 'Monthly aggregation of downscaled daily meteorological data of Monthly Average of Daily Maximum Near-Surface Air Temperature from College of Global Change and Earth System Science, Beijing Normal University (BNU-ESM) using the run r1i1p1 of the historical scenario.'

在xarray數據中,使用 .sel() 方法可以快速提取數據子集

# 根據經緯度進行取值 key=400 longitude = max_temp_xr["air_temperature"]["lon"].values[key] latitude = max_temp_xr["air_temperature"]["lat"].values[key]print("Long, Lat values:", longitude, latitude) Long, Lat values: 251.89422607421875 41.72947692871094

在地圖上顯示選取點的位置

extent = [-120, -70, 24, 50.5] central_lon = np.mean(extent[:2]) central_lat = np.mean(extent[2:])f, ax = plt.subplots(figsize=(12, 6),subplot_kw={'projection': ccrs.AlbersEqualArea(central_lon, central_lat)}) ax.coastlines() # Plot the selected location ax.plot(longitude-360, latitude, '^', transform=ccrs.PlateCarree(),color="r", markersize=15)ax.set_extent(extent) ax.set(title="Location of the Latitude / Longitude Being Used To to Slice Your netcdf Climate Data File")# Adds continent boundaries to the map ax.add_feature(cfeature.LAND, edgecolor='black')ax.gridlines() plt.show()

# 使用.sel()方法提取對應經緯度位置的數據 one_point=max_temp_xr["air_temperature"].sel(lat=latitude,lon=longitude)one_point # 提取具體點位置的數據結果為該點的時間序列數據 one_point.shape (672,) # 查看該點上的前五條數據 one_point.values[:5] array([271.11615, 274.05585, 279.538 , 284.42365, 294.1337 ],dtype=float32) # 直接使用xarray直接繪制該點數據的時間序列折線圖 one_point.plot() plt.show()

# 使用matplotlib繪制統計圖 f, ax = plt.subplots(figsize=(12, 6)) one_point.plot.line(hue='lat',marker="o",ax=ax,color="grey",markerfacecolor="purple",markeredgecolor="purple") ax.set(title="Time Series For a Single Lat / Lon Location")# 導出為png格式 # plt.savefig("single_point_timeseries.png") plt.show()

將 xarray 數據轉換成 Pandas DataFrame 格式并導出為 csv 文件

# 轉換為DataFrame one_point_df = one_point.to_dataframe() # View just the first 5 rows of the data one_point_df.head()

# pd導出為.csv文件 one_point_df.to_csv("one-location.csv")

根據時間和地點對數據進行切片

start_date="2000-01-01" end_date="2005-01-01"temp_2000_2005=max_temp_xr['air_temperature'].sel(time=slice(start_date,end_date),lat=45.02109146118164,lon=243.01937866210938) temp_2000_2005

查看切片得到的數據信息

temp_2000_2005.shape (60,)

切片時間段內的時間序列數據顯示

# Plot the data just like you did above f, ax = plt.subplots(figsize=(12, 6)) temp_2000_2005.plot.line(hue='lat',marker="o",ax=ax,color="grey",markerfacecolor="purple",markeredgecolor="purple") ax.set(title="A 5 Year Time Series of Temperature Data For A Single Location") plt.show()

對特定時間段以及指定的空間范圍內的數據進行切片

# 設定起始和結束時間 start_date = "1950-01-15" end_date = "1950-02-15"two_months_conus = max_temp_xr["air_temperature"].sel(time=slice(start_date, end_date)) # 查看切片后的數據信息 two_months_conus.shape (2, 585, 1386) # two_months_conus.plot() # plt.show()

數據的空間可視化

# 使用xarray.plot()可以對數據進行快速可視化 two_months_conus.plot(x="lon",y="lat",col="time", # 屬性值col_wrap=1) # 按列繪制 plt.suptitle("Two Time Steps of Monthly Average Temp", y=1.03) plt.show()

two_months_conus.plot(x="lon",y="lat",col="time",col_wrap=2)# 按行繪制 plt.show()

# 使用matplotlib繪制地圖 central_lat = 37.5 central_long = 96 extent = [-120, -70, 20, 55.5] # CONUSmap_proj = ccrs.AlbersEqualArea(central_longitude=central_lon,central_latitude=central_lat)aspect = two_months_conus.shape[2] / two_months_conus.shape[1] p = two_months_conus.plot(transform=ccrs.PlateCarree(), # the data's projectioncol='time', col_wrap=1,aspect=aspect,figsize=(10, 10),subplot_kws={'projection': map_proj}) # the plot's projectionplt.suptitle("Two Time Steps of CONUS Historic Temperature Data", y=1) # Add the coastlines to each axis object and set extent for ax in p.axes.flat:ax.coastlines()ax.set_extent(extent) d:\ProgramData\Anaconda3\envs\pygis\lib\site-packages\xarray\plot\facetgrid.py:373: UserWarning: Tight layout not applied. The left and right margins cannot be made large enough to accommodate all axes decorations. self.fig.tight_layout()

將數據導出為Geotiff文件

  • 使用 rioxarray 將數據導出為 geotiff文件格式
# 確保導出的數據帶有坐標信息 two_months_conus.rio.crs CRS.from_wkt('GEOGCS["undefined",DATUM["undefined",SPHEROID["undefined",6378137,298.257223563]],PRIMEM["undefined",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Longitude",EAST],AXIS["Latitude",NORTH]]') # 導出為tif文件 file_path = "two_months_temp_data.tif" two_months_conus.rio.to_raster(file_path) # 讀取導出的tif文件 two_months_tiff = xr.open_rasterio(file_path) two_months_tiff # 查看數據的波段信息 two_months_tiff.plot() plt.show()

# 繪制數據 two_months_tiff.plot(col="band") plt.show()

two_months_tiff.rio.nodata -9999.0 # 使用.where() to mask函數進行掩膜 two_months_tiff = xr.open_rasterio(file_path)two_months_clean = two_months_tiff.where(two_months_tiff != two_months_tiff.rio.nodata)two_months_clean.plot(col="band") plt.show()

文章代碼:

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

總結

以上是生活随笔為你收集整理的python中的.nc文件处理 | 03 指定位置的数据切片及可视化的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。