简体   繁体   English

如何使用python对numpy数组(来自netCDF文件)执行比较?

[英]How to perform comparisons on numpy arrays (from netCDF files) using python?

I'm writing a python script that reads two different netCDF files and, after performing an evaluation on the variables makes some calculation, the idea is (an example of the idea of the code the real one is too long): 我正在编写一个python脚本,该脚本读取两个不同的netCDF文件,并且在对变量执行评估后进行了一些计算后,其想法是(代码想法的示例实在太长了):

import netCDF4
import numpy as np
#other modules loaded...

#Values
a = 2
b = 4
c = 1

def srf(r, h):
    if r[:] == 2:
        if h[:] > 0:
            surf = 1 + b
        else:
            surf = a + b 
    else:
        surf = a - c

return surf

path_file : /home/file.nc
fhp = Dataset(path_file, r+)
ra = fhp.variables['VAR'][:]
path_file2 : /home/file2.nc
fhp2 = Dataset(path_file2, r+)
hu = fhp2.variables['VAR2'][:]   

#Call the Function
srf(ra, hu)       

The netCDF files each have 3 dimensions, if I try to run this code I get this error 每个netCDF文件都具有3个维度,如果我尝试运行此代码,则会收到此错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I need the function to be perform in the las two dimensions that are the ones containing the information to check in a domain [TSTEP, ROW, COL]. 我需要在两个维度上执行该功能,即包含要在域[TSTEP,ROW,COL]中检查的信息的两个维度。 So I would need to loop through the dimensions, but I'm not sure how to do it and I don't know if maybe there is an easiest way. 因此,我需要遍历各个维度,但是我不确定该怎么做,我也不知道是否有最简单的方法。 Thanks 谢谢

This question is unrelated to netCDF, but is rather about the manipulation numpy arrays, obtained from a netCDF file. 这个问题与netCDF无关,但是与从netCDF文件获得的操作numpy数组有关。

The problem is that given a numpy vector r , r[:] == 2 (or r[:] > 0 for that matter) returns a boolean array and not a single boolean. 问题在于给定一个numpy向量rr[:] == 2 (或者r[:] > 0 )返回一个布尔数组而不是一个布尔值。 It should therefore not be used in an if construction. 因此,不应在if结构中使用它。 Depending on what you are trying to accomplish you can use following approaches, 根据您要完成的工作,可以使用以下方法,

  • use (r[:] == 2).any() or (r[:] == 2).all() instead 使用(r[:] == 2).any()(r[:] == 2).all()
  • use more complex indexing, for instance, 例如,使用更复杂的索引

     import numpy as np def srf(r, h): mask_r = (r[:] == 2) mask_h = (h[:] > 0) surf = np.ones(r.shape)*(ac) surf[mask_r&mask_h] = 1 + b surf[mask_r&(~mask_h)] = a + b return surf 

please refer to the numpy documentation about advanced indexing for more details. 请参阅有关高级索引的numpy文档以获取更多详细信息。 This approach is significantly more efficient then loops over indices in python and should be used instead when possible. 与循环遍历python中的索引相比,此方法明显更有效,应尽可能使用它。

Also, if you want to apply a function along a particular axis, you can use numpy.apply_along_axis. 另外,如果要沿特定轴应用功能,则可以使用numpy.apply_along_axis。

For example, if you wanted to apply function srf along the time axis, you would do 例如,如果您想沿时间轴应用srf函数,则可以

import numpy as np srf_arr = np.apply_along_axis(srf, axisnumber, arrayname) 导入numpy为np srf_arr = np.apply_along_axis(srf,axisnumber,arrayname)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM