簡體   English   中英

python 使用形狀文件屏蔽 netcdf 文件並處理 LAT LONG 度數與 LAT LONG 米數

[英]python mask netcdf file with a shape file and dealing with LAT LONG in degree vs LAT LONG in meters

我正在努力掩蓋我的 netcdf 數據集。 我已經設法做某事,但不是以正確的方式。

基本上,我有一個形狀文件和一個 netcdf 數據集。

我閱讀 shapefile 如下:

import geopandas  as gpd
shp_noce       = gpd.read_file(shapefile_path)

內容如下:

  DN                                           geometry
0   1  POLYGON ((660074.143 5155942.267, 660172.884 5...

然后,我將文件讀取為

rain = xarray.open_dataset(ncfile_path)

這是結果:

<xarray.Dataset>
Dimensions:              (DATE: 14245, x: 641, y: 643)
Coordinates:
  * DATE                 (DATE) datetime64[ns] 1980-01-01T12:00:00 ... 2018-1...
  * x                    (x) float64 6.058e+05 6.061e+05 ... 7.656e+05 7.658e+05
  * y                    (y) float64 5.06e+06 5.06e+06 ... 5.22e+06 5.22e+06
Data variables:
    transverse_mercator  |S1 ...
    precipitation        (DATE, y, x) float32 ...
Attributes:
    CDI:          Climate Data Interface version 1.9.9 (https://mpimet.mpg.de...
    Conventions:  CF-1.5
    Title:        Daily total precipitation Trentino-South Tyrol 250-meter re...
    Created on:   Fri Feb 26 21:30:51 2021
    history:      Fri Feb 26 23:31:30 2021: cdo -z zip -mergetime DAILYPCP_19...
    CDO:          Climate Data Operators version 1.9.9 (https://mpimet.mpg.de..

我試圖遵循來自其他帖子的一些建議。 首先,我試過這個,它是基於rioarray的。 內容如下:

rain.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)

這是結果:

    raise MissingSpatialDimensionError(

MissingSpatialDimensionError: x dimension (lon) not found.

據我了解,由於投影單元,連接形狀文件和 netcdf 數據集可能存在問題。 所以,按照這里的報道,我做了以下事情:

shp_noce.to_crs("epsg:3395")

但是,我得到同樣的錯誤。 我想是因為 netcdf 數據集中的字段被命名為 x 和 y。

你有什么建議? 我應該重命名這些字段嗎? 我應該將“set_spatial_dims”設置為 x 和 y 嗎?

如果您的數據和 shapefile 在同一個 CRS(墨卡托)中,您需要做的就是告訴 rioxarray 您的空間暗淡是xy

rain.rio.set_spatial_dims(x_dim="x", y_dim="y", inplace=True)

有關 ds.rio.set_spatial_dims 的信息,請參閱ds.rio.set_spatial_dims API 文檔:

set_spatial_dims (x_dim: str, y_dim: str, inplace: bool = True)Union[xarray.core.dataset.Dataset, xarray.core.dataarray.DataArray]

這設置了數據集的空間維度。

參數

  • x_dim (str) – x 維度的名稱。
  • y_dim (str) – y 維度的名稱。
  • inplace (bool, optional) – 如果為真,它將在原地修改 dataframe。 否則它將返回修改后的副本。

退貨

  • 具有空間維度集的數據集。

返回類型

  • xarray.Dataset | xarray.DataArray

您告訴它查找名為“lon”的維度,它告訴您在數據集中找不到 lon。 那是因為 x 維度被命名為"x" :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM