在VBA中创建反距离加权函数

时间:2017-10-08 20:01:03

标签: python vba

我想估算降雨量的反距离加权(IDW)插值。 我确定了到所需站点最近的三个站点以获得插值。

我有下表:

enter image description here

那么,IDW的等式:

[(Station_valu1/Dist1^2)+(Station_valu2/Dist2^2)+(Station_valu3/Dist3^2)]
—————————————————————————————————————————————————————————————————————————
1/Dist1^2 + 1/Dist2^2 + 1/Dist3^2

在这个等式中,有几种情况试图解决它们:

1)如果Value1Dist1为空且Value2Dist2Value3Dist3不是

然后,忽略等式中的Value1Dist1,并仅考虑Value2Dist2Value3Dist3

IDW的结果将是:

[Station_valu2/Dist2^2)+(Station_valu3/Dist3^2)] 
————————————————————————————————————————————————
Dist2^2 + 1/Dist3^2

我们将使用Value2Dist2Value3Dist3的相同方案,如果其中任何人的值为空。

我想出了这段代码:

Function IDWW(Value1, Value2, Value3, Dist1,Dist2,Dist3)
Dim a1 As Variant
Dim b1 As Variant

Dim a2 As Variant
Dim b2 As Variant

Dim a3 As Variant
Dim b3 As Variant





    If Value1 <> "" And Dist1 <> "" Then
        a1 = Value1 / (Dist1) ^ 2
        b1 = 1 / (Dist1) ^ 2

        ElseIF Value1 = "" OR Dist1 = "" Then
        a1 = ""
        b1 = ""

    End If


    If Value2 <> "" And Dist2 <> "" Then
        a2 = Value2 / (Dist2) ^ 2
        b2 = 1 / (Dist2) ^ 2

        ElseIF Value1 = "" OR Dist1 = "" Then
        a2 = ""
        b2 = ""

    End If


    If Value3 <> "" And Dist3 <> "" Then
        a3 = Value3 / (Dist3) ^ 2
        b3 = 1 / (Dist3) ^ 2

        ElseIF Value3 = "" OR Dist3 = "" Then
        a3 = ""
        b3 = ""

    End If




    IDWW = (a1+a2+a3) / (b1+b2+b3)



End Function

拜托,我需要你的帮助来解决这个问题!

1 个答案:

答案 0 :(得分:0)

据我所知,如果它们为空,你想要做的就是设置为零:

Function IDWW(Value1, Value2, Value3, Dist1,Dist2,Dist3)
    Dim a1 As Variant
    Dim b1 As Variant
    Dim a2 As Variant
    Dim b2 As Variant
    Dim a3 As Variant
    Dim b3 As Variant

    If Value1 <> "" And Dist1 <> "" Then
        a1 = Value1 / (Dist1) ^ 2
        b1 = 1 / (Dist1) ^ 2
    Else
        a1 = 0
        b1 = 0
    End If

    If Value2 <> "" And Dist2 <> "" Then
        a2 = Value2 / (Dist2) ^ 2
        b2 = 1 / (Dist2) ^ 2
    Else
        a2 = 0
        b2 = 0
    End If

    If Value3 <> "" And Dist3 <> "" Then
        a3 = Value3 / (Dist3) ^ 2
        b3 = 1 / (Dist3) ^ 2
    Else
        a3 = 0
        b3 = 0
    End If

    'Avoid a problem if all 3 distances are empty
    If b1 + b2 + b3 = 0 Then
        IDWW = 0
    Else
        IDWW = (a1+a2+a3) / (b1+b2+b3)
    End If
End Function