xarray

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

元气小坏坏 提交于 2021-01-09 23:54:35
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

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

半世苍凉 提交于 2021-01-08 21:43:29
NetCDF4文件处理 下载MACA v2的 netcdf4 格式数据 使用 xarray 读取和处理 netcdf4 格式数据 将 netcdf4 格式数据导出为 .csv 格式 将 netcdf4 格式数据导出为 .tif 格式 参考链接 import os import 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

megrid和meshgrid

房东的猫 提交于 2020-11-21 12:49:46
一、meshgrid函数 meshgrid函数通常使用在数据的矢量化上。 它适用于生成网格型数据,可以接受两个一维数组生成两个二维矩阵,对应两个数组中所有的(x,y)对。 示例展示: 由上面的示例展示可以看出,meshgrid的 作用 是: 根据传入的两个一维数组参数生成两个数组元素的列表。 如果第一个参数是xarray,维度是xdimesion, 第二个参数是yarray,维度是ydimesion。 那么生成的第一个二维数组是以xarray为行,共ydimesion行的向量; 而第二个二维数组是以yarray的转置为列,共xdimesion列的向量。 二、 mgrid函数 用法:返回多维结构,常见的如2D图形,3D图形。对比np.meshgrid,在处理大数据时速度更快,且能处理多维(np.meshgrid只能处理2维) ret = np.mgrid[ 第1维,第2维 ,第3维 , …] 返回多值,以多个矩阵的形式返回, 第1返回值为第1维数据在最终结构中的分布, 第2返回值为第2维数据在最终结构中的分布,以此类推。(分布以矩阵形式呈现) 例如np.mgrid[X , Y] 样本(i,j)的坐标为 (X[i,j] ,Y[i,j]),X代表第1维,Y代表第2维,在此例中分别为横纵坐标。 例如1D结构(array),如下: In [2]: import numpy as np In

Xarray: slice coordinates with no dimensions

岁酱吖の 提交于 2020-06-16 08:15:11
问题 I am having difficultly with this topic, even though it seems like it should be rather simple. I want to slice an xarray dataset using a set of latitude and longitude coordinates. Here is what my dataset looks like: In [31]: data = xr.open_mfdataset(open_file, decode_cf=True) In [32]: data Out[32]: <xarray.Dataset> Dimensions: (time: 108120, x: 349, y: 277) Coordinates: lons (y, x) float64 -145.5 -145.3 -145.1 -144.9 -144.8 -144.6 -144.4 ... lats (y, x) float64 1.0 1.104 1.208 1.312 1.416 1

Take maximum rainfall value for each season over a time period (xarray)

…衆ロ難τιáo~ 提交于 2020-04-18 06:08:42
问题 I'm trying to find the maximum rainfall value for each season (DJF, MAM, JJA, SON) over a 10 year period. I am using netcdf data and xarray to try and do this. The data consists of rainfall (recorded every 3 hours), lat, and lon data. Right now I have the following code: ds.groupby('time.season).max('time') However, when I do it this way the output has a shape of (4,145,192) indicating that it's taking the maximum value for each season over the entire period. I would like the maximum for each

Plotting 2D data using Xarray takes a surprisingly long time?

半腔热情 提交于 2020-01-25 07:12:41
问题 I am reading NetCDF files using xarray. Each variable have 4 dimensions ( Times, lev, y, x ). After reading the variable, I calculate the mean of the variable QVAPOR along ( Times,lev ) dimensions. After calculation I get variable QVAPOR_mean which is a 2D variable with shape ( y: 699, x: 639 ). Xarray took only 10micro seconds to read the data with shape ( Times:2918, lev:36, y:699, x:639 ); but took more than 60 minutes to plot the filled contour of the data of shape ( y: 699, x: 639 ). I

How can I find the maximum across all variables corrresponding to the max in one variable?

拟墨画扇 提交于 2020-01-05 01:32:44
问题 I have an xarray of daily data with a number of variables. I want to extract the maximum q_routed every year and the corresponding values of other variables on the day that the maximum q_routed happens. <xarray.Dataset> Dimensions: (latitude: 1, longitude: 1, param_set: 1, time: 17167) Coordinates: * time (time) datetime64[ns] 1970-01-01 ... * latitude (latitude) float32 44.5118 * longitude (longitude) float32 -111.435 * param_set (param_set) |S1 b'' Data variables: ppt (time, param_set,

How can I find the maximum across all variables corrresponding to the max in one variable?

荒凉一梦 提交于 2020-01-05 01:32:20
问题 I have an xarray of daily data with a number of variables. I want to extract the maximum q_routed every year and the corresponding values of other variables on the day that the maximum q_routed happens. <xarray.Dataset> Dimensions: (latitude: 1, longitude: 1, param_set: 1, time: 17167) Coordinates: * time (time) datetime64[ns] 1970-01-01 ... * latitude (latitude) float32 44.5118 * longitude (longitude) float32 -111.435 * param_set (param_set) |S1 b'' Data variables: ppt (time, param_set,

replace values in xarray dataset with None

风流意气都作罢 提交于 2019-12-23 10:25:48
问题 I want to replace values in a variable in an xarray dataset with None. I tried this approach but it did not work: da[da['var'] == -9999.]['var'] = None I get this error: *** TypeError: unhashable type: 'numpy.ndarray' Is there something like numpy replace that I could use here? da is xarray dataset. here is what da looks like: <xarray.Dataset> Dimensions: (band: 1, time: 3, x: 4258, y: 2334) Coordinates: * band (band) int32 1 * y (y) float64 4.406e+06 4.406e+06 4.406e+06 4.406e+06 4.406e+06 .

Get hourly average for each month from a netcdf file

五迷三道 提交于 2019-12-13 12:57:10
问题 I have a netCDF file with the time dimension containing data by the hour for 2 years. I want to average it to get an hourly average for each hour of the day for each month. I tried this: import xarray as xr ds = xr.open_mfdataset('ecmwf_usa_2015.nc') ds.groupby(['time.month', 'time.hour']).mean('time') but I get this error: *** TypeError: `group` must be an xarray.DataArray or the name of an xarray variable or dimension How can I fix this? If I do this: ds.groupby('time.month', 'time.hour')