标签: netcdf

重新采样 xarray 对象以降低空间分辨率

使用 xarray 重新采样以降低空间分辨率

我想将我的 xarray 对象重新采样到较低的空间分辨率(LESS PIXELS)。

import pandas as pd
import numpy as np
import xarray as xr

time = pd.date_range(np.datetime64('1998-01-02T00:00:00.000000000'), np.datetime64('2005-12-28T00:00:00.000000000'), freq='8D')
x = np.arange(1200)
y = np.arange(1200)

latitude = np.linspace(40,50,1200)
longitude = np.linspace(0,15.5572382,1200)

latitude, longitude = np.meshgrid(latitude, longitude)

BHR_SW = np.ones((365, 1200, 1200))

output_da = xr.DataArray(BHR_SW, coords=[time, y, x])
latitude_da = xr.DataArray(latitude, coords=[y, x])
longitude_da = xr.DataArray(longitude, coords=[y, x])

output_da = output_da.rename({'dim_0':'time','dim_1':'y','dim_2':'x'})
latitude_da = latitude_da.rename({'dim_0':'y','dim_1':'x'})
longitude_da = longitude_da.rename({'dim_0':'y','dim_1':'x'})

output_ds = output_da.to_dataset(name='BHR_SW')
output_ds = output_ds.assign({'latitude':latitude_da, 'longitude':longitude_da})

print(output_ds)

<xarray.Dataset> …
Run Code Online (Sandbox Code Playgroud)

python resampling netcdf python-xarray

7
推荐指数
2
解决办法
4468
查看次数

如何将 x,y 坐标投影到 netcdf 文件中的纬度/经度

我已从 CCI 网站下载了格陵兰冰盖的速度场 NetCDF 文件。但是,投影给出如下(见下文,其中 x 范围在 [-639750,855750] 和 y [-655750,-3355750] 之间)

如何将这些数据投影到 NetCDF 文件中的实际纬度/经度坐标?已经谢谢了!对于感兴趣的人:可以在此处下载该文件:http ://products.esa-icesheets-cci.org/products/downloadlist/IV/

Variables:
    crs                                
           Size:       1x1
           Dimensions: 
           Datatype:   int32
           Attributes:
                       grid_mapping_name                     = 'polar_stereographic'
                       standard_parallel                     = 70
                       straight_vertical_longitude_from_pole = -45
                       false_easting                         = 0
                       false_northing                        = 0
                       unit                                  = 'meter'
                       latitude_of_projection_origin         = 90
                       spatial_ref                           = 'PROJCS["WGS 84 / NSIDC Sea Ice Polar Stereographic North",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Polar_Stereographic"],PARAMETER["latitude_of_origin",70],PARAMETER["central_meridian",-45],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],AUTHORITY["EPSG","3413"]]'
    y                                  
           Size:       5401x1
           Dimensions: y
           Datatype:   double
           Attributes:
                       units         = 'm'
                       axis          = 'Y'
                       long_name     = 'y coordinate of projection'
                       standard_name …
Run Code Online (Sandbox Code Playgroud)

python projection netcdf coordinate-transformation nco

7
推荐指数
1
解决办法
5055
查看次数

R CMD SHLIB Fortran 90 文件,使用 NetCDF

我想编译一个使用 NetCDF 的 fortran 90 文件。我已经安装的NetCDF,Fortran语言,如图所示这里,对文件进行编译test_nc.f90:

program test_nc
    use netcdf
    implicit none
    integer :: ncid, nc_err

    nc_err = nf90_open('test.nc', nf90_nowrite, ncid)
    nc_err = nf90_close(ncid)
end program test_nc
Run Code Online (Sandbox Code Playgroud)

用 gfortran 编译是

gfortran test_nc.f90 -o test_nc `nf-config --fflags --flibs`
Run Code Online (Sandbox Code Playgroud)

在哪里nf-config --fflags --flibs

-I/usr/include
-L/usr/lib -lnetcdff -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -lnetcdf -lnetcdf -ldl -lz -lcurl -lm
Run Code Online (Sandbox Code Playgroud)

用子程序替换程序是

subroutine test_nc
    use netcdf
    implicit none
    integer :: ncid, nc_err

    nc_err = nf90_open('test.nc', nf90_nowrite, ncid)
    nc_err = nf90_close(ncid)
end subroutine test_nc …
Run Code Online (Sandbox Code Playgroud)

fortran r netcdf

7
推荐指数
1
解决办法
309
查看次数

从 NETCDF 文件中提取数据的有效方法

我有许多坐标(大约 20000 个),我需要从许多 NetCDF 文件中提取数据,每个文件大约有 30000 个时间步长(未来的气候情景)。使用此处的解决方案效率不高,原因是每个 i,j 将“dsloc”转换为“dataframe”所花费的时间(请查看下面的代码)。** 可以从此处下载 NetCDF 文件示例**

import pandas as pd
import xarray as xr
import time

#Generate some coordinates
coords_data = [{'lat': 68.04, 'lon': 15.20, 'stid':1},
    {'lat':67.96, 'lon': 14.95, 'stid': 2}]
crd= pd.DataFrame(coords_data)
lat = crd["lat"]
lon = crd["lon"]
stid=crd["stid"]

NC = xr.open_dataset(nc_file)
point_list = zip(lat,lon,stid)
start_time = time.time()
for i,j,id in point_list:
    print(i,j)
    dsloc = NC.sel(lat=i,lon=j,method='nearest')
    print("--- %s seconds ---" % (time.time() - start_time))
    DT=dsloc.to_dataframe()
    DT.insert(loc=0,column="station",value=id)
    DT.reset_index(inplace=True)
    temp=temp.append(DT,sort=True)
    print("--- …
Run Code Online (Sandbox Code Playgroud)

python netcdf nco python-xarray cdo-climate

7
推荐指数
1
解决办法
2535
查看次数

libtool:归档中的对象名称冲突(NETCDF + MinGW)

我将在Windows中使用NetCDF,我认为它必须用MinGW编译,因为我的主程序和所有其他库已经用MinGW编译.

但是当我使用MinGW(gcc版本4.6.2)时.我收到一些错误消息:

    Making all in liblib
    make[2]: Entering directory `/c/Users/ylylyl/Documents/CB/NETCDF/netcdf-4.2.1.1/liblib'
    /bin/sh ../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I..  -I../include  -DDLL_NETCDF   -DDLL_EXPORT   -g -O2 -MT libnetcdf_la-stub.lo -MD -MP -MF .deps/libnetcdf_la-stub.Tpo -c -o libnetcdf_la-stub.lo `test -f 'stub.c' ||echo './'`stub.c
    libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -I../include -DDLL_NETCDF -DDLL_EXPORT -g -O2 -MT libnetcdf_la-stub.lo -MD -MP -MF .deps/libnetcdf_la-stub.Tpo -c stub.c  -DDLL_EXPORT -DPIC -o .libs/libnetcdf_la-stub.o
    libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -I../include -DDLL_NETCDF -DDLL_EXPORT -g -O2 -MT libnetcdf_la-stub.lo -MD -MP -MF .deps/libnetcdf_la-stub.Tpo -c stub.c -o …
Run Code Online (Sandbox Code Playgroud)

conflict mingw libtool netcdf

6
推荐指数
1
解决办法
1597
查看次数

将 grib 转换为 netcdf 文件

有没有办法在 Windows 上将 grib 文件转换为 NetCDF 格式?
我使用了一个名为的软件,tkdegrib但它通过一个参数捕获一个参数,而我希望所有 grib 的参数都在同一个文件中。不幸的是,我不能使用 Linux。

netcdf grib cdo-climate

6
推荐指数
1
解决办法
7063
查看次数

在xarray中导入和解码数据集,以避免_FillValue和missing_value冲突

当使用xarray open_dataset或open_mfdataset加载NARR netcdf数据集(例如ftp://ftp.cdc.noaa.gov/Datasets/NARR/monolevel/air.2m.2010.nc)时,xarray返回有关“与_FillValue和missing_values”。

输入:

ds = xarray.open_dataset('air.2m.2010.nc')

产生此错误:

ValueError: ('Discovered conflicting _FillValue and missing_value. Considering opening the offending dataset using decode_cf=False, corrected the attributes', 'and decoding explicitly using xray.conventions.decode_cf(ds)')

使用建议打开时:

ds = xarray.open_dataset('air.2m.2010.nc',decode_cf=False)

数据集已打开,但变量,时间,坐标等未解码(显然)。xarray.decode_cf(ds)显式使用似乎无法成功解码数据集,因为会遇到相同的错误。

我相信会出现此错误,因为NARR数据集是Lambert Conformal,因此由于xarray打开网格时的网格形状而导致一些缺失值,并且由于某些原因,这与填充值冲突。

在xarray中打开和解码此文件的最佳方法是什么?

注意:我已经能够使用netcdf4-python打开和解码,但是我希望能够在xarray中执行此操作,以利用dask提供的核心计算功能。

python netcdf python-xarray

6
推荐指数
2
解决办法
642
查看次数

如何计算bash中netcdf文件中随时间维度求和的缺失值数量

我有一个 netcdf 文件,其中的数据是 lon、lat 和 time 的函数。我想计算在时间维度上求和的每个网格单元中缺失条目的总数,最好使用 CDO 或 NCO,因此我不需要调用 R、python 等。

我知道如何获得缺失值的总数

ncap2 -s "nmiss=var.number_miss()" in.nc out.nc
Run Code Online (Sandbox Code Playgroud)

正如我对这个相关问题的回答: count number of missing values in netcdf file - R

并且 CDO 可以告诉我空间总和

cdo info in.nc
Run Code Online (Sandbox Code Playgroud)

但我不知道如何随着时间的推移求和。例如,有没有一种方法可以指定要在 ncap2 中使用 number_miss 求和的维度?

bash netcdf missing-data nco cdo-climate

6
推荐指数
1
解决办法
1067
查看次数

使用 xarray 打开 netcdf 文件时如何禁用缓存?

我正在尝试设置一个性能测试来重复读取具有不同分块配置的 netcdf 文件,以最终确定特定用例的最佳块大小。我遇到的一个问题是,当使用xarray.open_dataset()读取文件时,即使缓存设置为 False,它仍然以某种方式将缓存存储在内存中。我知道这是基于两个指标的情况:

  • 第一次运行时读取总是很慢。
  • 使用RamMap应用程序,我看到即使关闭数据集,打开的文件仍在内存中。

这是我运行的代码:

ds = xr.open_dataset("path/to/netcdf/file", engine='h5netcdf', cache=False)

lat_dim = 2160
lon_dim = 4320
time_dim = 46
read_chunk_size = 2160

data = np.empty((time_dim, lat_dim, lon_dim))
data[0:time_dim, 0:read_chunk_size, 0:read_chunk_size] = \
ds['value'][0:time_dim, 0:read_chunk_size, 0:read_chunk_size]

ds.close()
Run Code Online (Sandbox Code Playgroud)

很明显,我对 xarray 中缓存的理解非常少。因此,如果有人能向我解释它的实际工作原理,以及如何在多运行性能测试中利用它,我将不胜感激。

python caching performance-testing netcdf python-xarray

6
推荐指数
0
解决办法
356
查看次数

使用 xarray 在 python 中打开 .nc 文件时出现 HDF5 错误

我正在尝试使用 xarray 打开 MERRA-2 文件,正如我的标题所示。当我尝试使用 print 语句查看某个变量中的值时,会发生我遇到的特定错误。错误如下:

HDF5-DIAG: Error detected in HDF5 (1.12.2) thread 5:
  #000: H5A.c line 528 in H5Aopen_by_name(): can't open attribute
    major: Attribute
    minor: Can't open object
  #001: H5VLcallback.c line 1091 in H5VL_attr_open(): attribute open failed
    major: Virtual Object Layer
    minor: Can't open object
  #002: H5VLcallback.c line 1058 in H5VL__attr_open(): attribute open failed
    major: Virtual Object Layer
    minor: Can't open object
  #003: H5VLnative_attr.c line 130 in H5VL__native_attr_open(): can't open attribute
    major: Attribute
    minor: Can't open object
  #004: …
Run Code Online (Sandbox Code Playgroud)

python hdf5 netcdf python-xarray

6
推荐指数
1
解决办法
2911
查看次数