我试图通过从另一个NetCDF文件(“NetCDf”文件)中获取值来填充NetCDf文件中的nan值(让我们调用是“目标”文件)。the two example files can be downloaded [from here] I正在考虑使用以下框架在python中执行此操作:
步骤1-标识目标文件中的nan值,提取位置(lat/long),存储在数据文件中
步骤2-从源文件中提取存储的lat/long的相应值
步骤3-将这些值写入目标文件
我想出了以下代码:
import pandas as pd
import xarray as xr
import numpy as np
Source = xr.open_dataset("Source.nc")
Target = xr.open_dataset("Target.nc")
#Step 1
df = Target.to_dataframe()
df=df.reset_index()
df2=(df.loc[df['ET'].isin([32767,'nan'])])
#Step2
lat = df2["lat"]
lon = df2["lon"]
point_list = zip(lat,lon)
Newdf = pd.DataFrame([])
for i, j in point_list:
dsloc = Source.sel(lat=i,lon=j,method='nearest')
DT=dsloc.to_dataframe()
Newdf=Newdf.append(DT,sort=True)这有三个问题: 1-我不知道如何做第三步
第二步要花很长时间才能完成,因为可能有很多遗漏点。
3-这只是一段时间的步骤!使用这两个文件。
因此,我相信在python或cdo/Nco…中可能有更好的方法,更容易更快地做到这一点。任何想法和解决方案都欢迎…谢谢你,…请注意,这两个NC文件具有不同的空间分辨率(维度)。
发布于 2019-11-19 00:22:37
为此您可以使用Xarray's where method。如果您关心的是效率,那么您确实希望远离pythonfor循环。下面是一个如何工作的例子:
# these are the points you want to keep
# you can fine tune this further (exclude values over a threshold)
condition = target.notnull()
# fill the values where condition is false
target_filled = target.where(condition, source)https://stackoverflow.com/questions/58924759
复制相似问题