View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Using known arrays to calculate an array of new values

Brett,
If I understand you correctly, the code below should work.
If you need a different method than linear to get the projected final
quantity, you'll need to change that part.
I have tested much, so ther may be some error(s), but this should get you
started.

NickHK
P.S. Not sure if this should be called "NormaliseArray", but you know what I
mean.

Private Sub CommandButton1_Click()
Dim RetVal As Variant
Dim OutputArray As Variant

Dim Data(1 To 4, 1 To 2) As Variant

'Sample data
Data(1, 1) = "10:00"
Data(1, 2) = 1
Data(2, 1) = "10:08"
Data(2, 2) = 1.5
Data(3, 1) = "10:16"
Data(3, 2) = 2
Data(4, 1) = "10:33"
Data(4, 2) = 2.5

RetVal = NormaliseArray(Data, CDate("10:00"), 15, OutputArray)
If IsNumeric(RetVal) = True Then
MsgBox "Normalised to " & RetVal & " elements"
Else
MsgBox "Error : " & RetVal
End If

End Sub

Private Function NormaliseArray(ByVal InputData As Variant, _
StartTime As Date, _
IntervalMinutes As Long, _
ByRef OutData As Variant, _
Optional BaseQty As Single = 1) _
As Variant

Dim TimeTaken As Double
Dim QuantityCollected As Single
Dim MinElement As Long
Dim MaxElement As Long
Dim NewElementCount As Long
Dim CalcQtyInterval As Single
Dim Counter As Long

Const OneMinute As Single = 1 / 24 / 60
Const ERR_SUBSCRIPTOUTOFRANGE As Long = 9
Const ERR_ARRAYCANNOTRESIZE As Long = 10

'Make sure the input is an array
If IsArray(InputData) = False Then
NormaliseArray = "Not array input"
Exit Function
End If

MinElement = LBound(InputData, 1)
MaxElement = UBound(InputData, 1)

On Error Resume Next
'Make sure we have a 2-D array
'This will error if there is no 2nd dimension
QuantityCollected = InputData(MaxElement, 2) - InputData(MinElement, 2)

If Err.Number = ERR_SUBSCRIPTOUTOFRANGE Then
NormaliseArray = "No 2nd dimension"
Exit Function
End If
On Error GoTo 0

'The time taken for the original data
TimeTaken = CDate(InputData(MaxElement, 1)) - CDate(InputData(MinElement,
1))

'The number of elements required to cover the data, with the new time
interval
NewElementCount = RoundUp(TimeTaken / (IntervalMinutes * OneMinute))

On Error Resume Next

'Resize the the OutputData variable, including the Start element
ReDim OutData(0 To NewElementCount, 1 To 2)

If Err.Number = ERR_ARRAYCANNOTRESIZE Then
NormaliseArray = "Cannot resize output array"
Exit Function
End If
On Error GoTo 0

'Calculate the projected new total quantity
QuantityCollected = QuantityCollected * (NewElementCount * IntervalMinutes *
OneMinute) / TimeTaken

CalcQtyInterval = QuantityCollected / (NewElementCount)

For Counter = 0 To NewElementCount
OutData(Counter, 1) = DateAdd("n", IntervalMinutes * (Counter),
StartTime)
OutData(Counter, 2) = BaseQty + CalcQtyInterval * (Counter)
Debug.Print OutData(Counter, 1), OutData(Counter, 2)
Next

NormaliseArray = NewElementCount + 1

End Function

Private Function RoundUp(sngInBound) As Long
RoundUp = CLng(sngInBound + 0.5)
End Function


"Brett" wrote in message
ups.com...
Here is my problem,
I have rain gage data at varying time intervals with a cumulative
precip. amount at that time interval making a 2-d array. For example
10:00 1
10:08 1.5
10:16 2
10:33 2.5
From this array I want to create another 2-d array at fixed time

intervals in this case it could be 15 min.
The 2nd column of array two would be created from the first array using
linear interpolation or some other rule the result would be
10:00 1
10:15 ?
10:30 ?

stepping forward in time to fill the second array.
any ideas?