-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathDlgDefineClimaticData.vb
407 lines (353 loc) · 20.1 KB
/
DlgDefineClimaticData.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
' R- Instat
' Copyright (C) 2015-2017
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
Imports instat.Translations
Imports System.Text.RegularExpressions
Imports RDotNet
Public Class DlgDefineClimaticData
Public bFirstLoad As Boolean = True
Private bReset As Boolean = True
Private clsTypesFunction, clsNewTypesFunction As New RFunction
Private lstReceivers As New List(Of ucrReceiverSingle)
Private lstNewReceivers As New List(Of ucrReceiverSingle)
Private lstRecognisedTypes As New List(Of KeyValuePair(Of String, List(Of String)))
Private lstNewRecognisedTypes As New List(Of KeyValuePair(Of String, List(Of String)))
Private clsDefaultFunction, clsNewDefautFunction As New RFunction
Private clsAnyDuplicatesFunction, clsConcFunction, clsNewConcFunction, clsGetColFunction, clsDummyFunction As New RFunction
Private strCurrentDataframeName As String
Private bIsUnique As Boolean = True
Private Sub DlgDefineClimaticData_Load(sender As Object, e As EventArgs) Handles MyBase.Load
autoTranslate(Me)
If bFirstLoad Then
InitialiseDialog()
bFirstLoad = False
End If
If bReset Then
SetDefaults()
End If
SetRCodeForControls(bReset)
bReset = False
TestOKEnabled()
End Sub
Private Sub InitialiseDialog()
ucrBase.iHelpTopicID = 328
Dim kvpRain As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("rain", {"rain", "prec", "rr", "prcp"}.ToList())
Dim kvpDate As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("date", {"date", "record"}.ToList())
Dim kvpStation As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("station", {"station", "id", "name"}.ToList())
Dim kvpAltitude As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("alt", {"alt", "altitude", "elevation", "elev"}.ToList())
Dim kvpLongitude As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("lon", {"lon", "lont", "longitude"}.ToList())
Dim kvpLatitude As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("lat", {"lat", "latitude"}.ToList())
Dim kvpCloudCover As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("cloud_cover", {"cloud"}.ToList())
Dim kvpTempMax As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("temp_max", {"tmax", "tx", "tempmax", "tmp_max", "tmpmax"}.ToList())
Dim kvpTempMin As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("temp_min", {"tmin", "tn", "tempmin", "tmp_min", "tmpmin"}.ToList())
Dim kvpRadiation As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("radiation", {"radiation", "rad"}.ToList())
Dim kvpSunshineHours As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("sunshine_hours", {"sunshine", "sunh", "sunhrs"}.ToList())
Dim kvpWindDirection As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("wind_direction", {"winddirection"}.ToList())
Dim kvpWindSpeed As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("wind_speed", {"windspeed"}.ToList())
Dim kvpYear As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("year", {"year"}.ToList())
Dim kvpMonth As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("month", {"month"}.ToList())
Dim kvpDay As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("day", {"day"}.ToList())
Dim kvpDOY As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("doy", {"doy", "doy_366"}.ToList())
Dim kvpMinRH As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("hum_min", {"minhum", "hmin", "hn"}.ToList())
Dim kvpMaxRH As KeyValuePair(Of String, List(Of String)) = New KeyValuePair(Of String, List(Of String))("hum_max", {"maxhum", "hmax", "hx"}.ToList())
lstRecognisedTypes.AddRange({kvpRain, kvpCloudCover, kvpTempMax, kvpTempMin, kvpRadiation, kvpSunshineHours, kvpStation, kvpAltitude, kvpLatitude, kvpLongitude,
kvpWindDirection, kvpWindSpeed, kvpYear, kvpMonth, kvpDay, kvpDOY, kvpDate, kvpMinRH, kvpMaxRH})
lstNewRecognisedTypes.AddRange({kvpStation, kvpAltitude, kvpLatitude, kvpLongitude})
lstReceivers.AddRange({ucrReceiverCloudCover, ucrReceiverDay, ucrReceiverMaxTemp, ucrReceiverMinTemp, ucrReceiverMonth, ucrReceiverRadiation,
ucrReceiverRain, ucrReceiverStation, ucrReceiverAltitude, ucrReceiverLatitude, ucrReceiverLongitude, ucrReceiverSunshine,
ucrReceiverWindDirection, ucrReceiverWindSpeed, ucrReceiverYear, ucrReceiverDOY, ucrReceiverDate, ucrReceiverMinRH, ucrReceiverMaxRH})
lstNewReceivers.AddRange({ucrReceiverStationMeta, ucrReceiverAltMeta, ucrReceiverLatMeta, ucrReceiverLonMeta})
ucrSelectorDefineClimaticData.SetParameter(New RParameter("data_name", 0))
ucrSelectorDefineClimaticData.SetParameterIsString()
ucrSelectorLinkedDataFrame.SetParameter(New RParameter("data_name", 1))
ucrSelectorLinkedDataFrame.SetParameterIsString()
ucrReceiverDate.Tag = "date"
ucrReceiverCloudCover.Tag = "cloud_cover"
ucrReceiverStation.Tag = "station"
ucrReceiverAltitude.Tag = "alt"
ucrReceiverLongitude.Tag = "lon"
ucrReceiverLatitude.Tag = "lat"
ucrReceiverStationMeta.Tag = "station"
ucrReceiverAltMeta.Tag = "alt"
ucrReceiverLonMeta.Tag = "lon"
ucrReceiverLatMeta.Tag = "lat"
ucrReceiverMaxTemp.Tag = "temp_max"
ucrReceiverMinTemp.Tag = "temp_min"
ucrReceiverRadiation.Tag = "radiation"
ucrReceiverRain.Tag = "rain"
ucrReceiverSunshine.Tag = "sunshine_hours"
ucrReceiverWindDirection.Tag = "wind_direction"
ucrReceiverWindSpeed.Tag = "wind_speed"
ucrReceiverYear.Tag = "year"
ucrReceiverMonth.Tag = "month"
ucrReceiverDay.Tag = "day"
ucrReceiverDOY.Tag = "doy"
ucrReceiverMinRH.Tag = "hum_min"
ucrReceiverMaxRH.Tag = "hum_max"
ucrInputCheckInput.IsReadOnly = True
ucrReceiverDate.SetIncludedDataTypes({"Date"})
SetRSelector()
NewSetRSelector()
ucrChkLinkedMetaData.SetText("Linked Meta Data")
ucrChkLinkedMetaData.SetParameter(New RParameter("check", 0))
ucrChkLinkedMetaData.SetValuesCheckedAndUnchecked("True", "False")
ucrBase.clsRsyntax.iCallType = 2
End Sub
Private Sub SetDefaults()
clsDefaultFunction = New RFunction
clsGetColFunction = New RFunction
clsAnyDuplicatesFunction = New RFunction
clsConcFunction = New RFunction
clsNewConcFunction = New RFunction
clsDummyFunction = New RFunction
clsNewDefautFunction = New RFunction
ucrSelectorDefineClimaticData.Reset()
ucrSelectorLinkedDataFrame.Reset()
ucrInputCheckInput.Reset()
ucrReceiverDate.SetMeAsReceiver()
ucrSelectorLinkedDataFrame.Visible = False
grpMeta.Visible = False
ucrChkLinkedMetaData.SetParameter(New RParameter("y", 0))
clsDefaultFunction.SetRCommand(frmMain.clsRLink.strInstatDataObject & "$define_as_climatic")
clsDefaultFunction.AddParameter("types", clsRFunctionParameter:=clsTypesFunction)
clsDefaultFunction.AddParameter("key_col_names", clsRFunctionParameter:=clsConcFunction, iPosition:=2)
clsNewDefautFunction.SetRCommand(frmMain.clsRLink.strInstatDataObject & "$define_as_climatic")
clsNewDefautFunction.AddParameter("types", clsRFunctionParameter:=clsNewTypesFunction)
clsNewDefautFunction.AddParameter("key_col_names", clsRFunctionParameter:=clsNewConcFunction, iPosition:=2)
clsDummyFunction.AddParameter("checked", "FALSE", iPosition:=0)
clsTypesFunction.SetRCommand("c")
clsNewTypesFunction.SetRCommand("c")
clsConcFunction.SetRCommand("c")
clsNewConcFunction.SetRCommand("c")
clsAnyDuplicatesFunction.SetRCommand("anyDuplicated")
clsAnyDuplicatesFunction.AddParameter("x", clsRFunctionParameter:=clsGetColFunction)
ucrBase.clsRsyntax.ClearCodes()
clsGetColFunction.SetRCommand(frmMain.clsRLink.strInstatDataObject & "$get_columns_from_data")
clsGetColFunction.AddParameter("data_name", Chr(34) & strCurrentDataframeName & Chr(34))
clsGetColFunction.AddParameter("col_names", clsRFunctionParameter:=clsConcFunction)
ucrBase.clsRsyntax.SetBaseRFunction(clsDefaultFunction)
ucrBase.clsRsyntax.bSeparateThread = False
AutoFillReceivers()
EnableDisableCheckUniqueBtn()
End Sub
Private Sub SetRCodeForControls(bReset As Boolean)
If bReset Then
ucrSelectorDefineClimaticData.SetRCode(clsDefaultFunction, bReset)
ucrSelectorLinkedDataFrame.SetRCode(clsNewDefautFunction, bReset)
End If
SetRCodesforReceivers(bReset)
ucrChkLinkedMetaData.SetRCode(clsDummyFunction, bReset)
End Sub
Private Sub TestOKEnabled()
If Not ucrReceiverDate.IsEmpty AndAlso bIsUnique Then
ucrBase.OKEnabled(True)
Else
ucrBase.OKEnabled(False)
End If
End Sub
Private Sub ucrBase_ClickReset(sender As Object, e As EventArgs) Handles ucrBase.ClickReset
SetDefaults()
SetRCodeForControls(True)
TestOKEnabled()
End Sub
Private Sub SetRCodesforReceivers(bReset As Boolean)
Dim ucrTempReceiver As ucrReceiver
For Each ucrTempReceiver In lstReceivers
ucrTempReceiver.SetRCode(clsTypesFunction, bReset)
Next
For Each ucrTempReceiver In lstNewReceivers
ucrTempReceiver.SetRCode(clsNewTypesFunction, bReset)
Next
End Sub
Private Sub SetRSelector()
Dim ucrTempReceiver As ucrReceiver
For Each ucrTempReceiver In lstReceivers
ucrTempReceiver.SetParameter(New RParameter(ucrTempReceiver.Tag))
ucrTempReceiver.Selector = ucrSelectorDefineClimaticData
ucrTempReceiver.SetParameterIsString()
ucrTempReceiver.bExcludeFromSelector = True
Next
End Sub
Private Sub NewSetRSelector()
Dim ucrTempReceiver As ucrReceiver
For Each ucrTempReceiver In lstNewReceivers
ucrTempReceiver.SetParameter(New RParameter(ucrTempReceiver.Tag))
ucrTempReceiver.Selector = ucrSelectorLinkedDataFrame
ucrTempReceiver.SetParameterIsString()
ucrTempReceiver.bExcludeFromSelector = True
Next
End Sub
Private Sub AutoFillReceivers()
Dim lstRecognisedValues As List(Of String)
Dim ucrCurrentReceiver As ucrReceiver
Dim bFound As Boolean = False
ucrCurrentReceiver = ucrSelectorDefineClimaticData.CurrentReceiver
For Each ucrTempReceiver As ucrReceiver In lstReceivers
ucrTempReceiver.SetMeAsReceiver()
lstRecognisedValues = GetRecognisedValues(ucrTempReceiver.Tag)
If lstRecognisedValues.Count > 0 Then
For Each lviTempVariable As ListViewItem In ucrSelectorDefineClimaticData.lstAvailableVariable.Items
For Each strValue As String In lstRecognisedValues
If Regex.Replace(lviTempVariable.Text.ToLower(), "[^\w]|_", String.Empty).Contains(strValue) Then
ucrTempReceiver.Add(lviTempVariable.Text, ucrSelectorDefineClimaticData.ucrAvailableDataFrames.cboAvailableDataFrames.Text)
bFound = True
Exit For
End If
Next
If bFound Then
bFound = False
Exit For
End If
Next
End If
Next
If ucrCurrentReceiver IsNot Nothing Then
ucrCurrentReceiver.SetMeAsReceiver()
End If
End Sub
Private Sub NewAutoFillReceivers()
Dim lstRecognisedValues As List(Of String)
Dim ucrCurrentReceiver As ucrReceiver
Dim bFound As Boolean = False
ucrCurrentReceiver = ucrSelectorLinkedDataFrame.CurrentReceiver
For Each ucrTempReceiver As ucrReceiver In lstNewReceivers
ucrTempReceiver.SetMeAsReceiver()
lstRecognisedValues = GetNewRecognisedValues(ucrTempReceiver.Tag)
If lstRecognisedValues.Count > 0 Then
For Each lviTempVariable As ListViewItem In ucrSelectorLinkedDataFrame.lstAvailableVariable.Items
For Each strValue As String In lstRecognisedValues
If Regex.Replace(lviTempVariable.Text.ToLower(), "[^\w]|_", String.Empty).Contains(strValue) Then
ucrTempReceiver.Add(lviTempVariable.Text, ucrSelectorLinkedDataFrame.ucrAvailableDataFrames.cboAvailableDataFrames.Text)
bFound = True
Exit For
End If
Next
If bFound Then
bFound = False
Exit For
End If
Next
End If
Next
If ucrCurrentReceiver IsNot Nothing Then
ucrCurrentReceiver.SetMeAsReceiver()
End If
End Sub
Private Function GetRecognisedValues(strVariable As String) As List(Of String)
Dim lstValues As New List(Of String)
For Each kvpTemp As KeyValuePair(Of String, List(Of String)) In lstRecognisedTypes
If kvpTemp.Key = strVariable Then
lstValues = kvpTemp.Value
Exit For
End If
Next
Return lstValues
End Function
Private Function GetNewRecognisedValues(strVariable As String) As List(Of String)
Dim lstValues As New List(Of String)
For Each kvpTemp As KeyValuePair(Of String, List(Of String)) In lstNewRecognisedTypes
If kvpTemp.Key = strVariable Then
lstValues = kvpTemp.Value
Exit For
End If
Next
Return lstValues
End Function
Private Sub cmdCheckUnique_Click(sender As Object, e As EventArgs) Handles cmdCheckUnique.Click
Dim iAnyDuplicated As Integer
Try
iAnyDuplicated = frmMain.clsRLink.RunInternalScriptGetValue(clsAnyDuplicatesFunction.ToScript()).AsInteger(0)
Catch ex As Exception
iAnyDuplicated = -1
End Try
If iAnyDuplicated = -1 Then
ucrInputCheckInput.SetName("Developer error! Could not check uniqueness.")
ucrInputCheckInput.txtInput.BackColor = Color.Yellow
bIsUnique = False
ElseIf iAnyDuplicated > 0 Then
ucrInputCheckInput.SetName("")
ucrInputCheckInput.txtInput.BackColor = Color.LightCoral
bIsUnique = False
If ucrReceiverStation.IsEmpty Then
ucrInputCheckInput.SetName("Duplicate dates found.")
MsgBox("You have multiple rows with the same dates. Did you forget to add the station column? Otherwise, use the Climatic > Tidy and Examine > Duplicates dialog to investigate these issues.", MsgBoxStyle.Information, Title:="Duplicates")
Else
ucrInputCheckInput.SetName("Duplicate dates for station(s) were found.")
MsgBox("You have multiple rows with the same dates for one or more stations. Use the Climatic > Tidy and Examine > Duplicates dialog to investigate these issues.", MsgBoxStyle.Information, Title:="Duplicates")
End If
Else
ucrInputCheckInput.SetName("No duplicate dates.")
ucrInputCheckInput.txtInput.BackColor = Color.LightGreen
bIsUnique = True
End If
TestOKEnabled()
End Sub
Private Sub EnableDisableCheckUniqueBtn()
If ucrReceiverDate.IsEmpty Then
cmdCheckUnique.Enabled = False
Else
cmdCheckUnique.Enabled = True
End If
ucrInputCheckInput.SetName("")
ucrInputCheckInput.txtInput.BackColor = SystemColors.Window
bIsUnique = True
End Sub
Private Sub ucrReceiverDate_ControlValueChanged(ucrChangedControl As ucrCore) Handles ucrReceiverDate.ControlValueChanged, ucrReceiverStation.ControlContentsChanged
EnableDisableCheckUniqueBtn()
If Not ucrReceiverStation.IsEmpty Then
clsConcFunction.AddParameter("x1", ucrReceiverStation.GetVariableNames, bIncludeArgumentName:=False)
Else
clsConcFunction.RemoveParameterByName("x1")
End If
If Not ucrReceiverDate.IsEmpty Then
clsConcFunction.AddParameter("x2", ucrReceiverDate.GetVariableNames, bIncludeArgumentName:=False)
Else
clsConcFunction.RemoveParameterByName("x2")
End If
End Sub
Private Sub ucrSelectorDefineClimaticData_ControlValueChanged(ucrChangedControl As ucrCore) Handles ucrSelectorDefineClimaticData.ControlValueChanged
strCurrentDataframeName = ucrSelectorDefineClimaticData.strCurrentDataFrame
clsGetColFunction.AddParameter("data_name", Chr(34) & strCurrentDataframeName & Chr(34), iPosition:=0)
AutoFillReceivers()
SetRSelector()
End Sub
Private Sub ucrSelectorLinkedDataFrame_ControlValueChanged(ucrChangedControl As ucrCore) Handles ucrSelectorLinkedDataFrame.ControlValueChanged
clsGetColFunction.AddParameter("data_name", Chr(34) & ucrSelectorLinkedDataFrame.strCurrentDataFrame & Chr(34), iPosition:=1)
NewAutoFillReceivers()
NewSetRSelector()
End Sub
Private Sub Controls_ControlContentsChanged(ucrChangedControl As ucrCore) Handles ucrReceiverDate.ControlContentsChanged
TestOKEnabled()
End Sub
Private Sub ucrChkLinkedMetaData_ControlValueChanged(ucrChangedControl As ucrCore) Handles ucrChkLinkedMetaData.ControlValueChanged, ucrReceiverAltMeta.ControlValueChanged, ucrReceiverLatMeta.ControlValueChanged, ucrReceiverLonMeta.ControlValueChanged, ucrReceiverStationMeta.ControlValueChanged
If ucrChkLinkedMetaData.Checked Then
ucrSelectorLinkedDataFrame.Visible = True
grpMeta.Visible = True
grpStation.Visible = False
ucrBase.clsRsyntax.AddToAfterCodes(clsNewDefautFunction, iPosition:=0)
clsNewDefautFunction.iCallType = 2
Else
ucrSelectorLinkedDataFrame.Visible = False
grpMeta.Visible = False
grpStation.Visible = True
ucrBase.clsRsyntax.RemoveFromAfterCodes(clsNewDefautFunction)
End If
If Not ucrReceiverStationMeta.IsEmpty Then
clsNewConcFunction.AddParameter("x1", ucrReceiverStationMeta.GetVariableNames, bIncludeArgumentName:=False)
Else
clsNewConcFunction.RemoveParameterByName("x1")
End If
End Sub
End Class