Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

More simple way


Makada Apr 30, 2020 02:23 AM

Hi,

I would like to know if there is a more simple way of coding this piece?

I have 5 tables with different time intervals and the total crbasic code is getting very long when i add this piece to all corresponding tables.

In this case its the one minute table, with WS_kph.

But then i have WS_kph10, WS_kph60, WS_kph1d, WS_kph1m.

Select Case WS_kph
Case Is <0.8
Beaufort = 0
Case Is <5.5
Beaufort = 1
Case Is <12
Beaufort = 2
Case Is <19.5
Beaufort = 3
Case Is <28.7
Beaufort = 4
Case Is <38.6
Beaufort = 5
Case Is <49.8
Beaufort = 6
Case Is <61.7
Beaufort = 7
Case Is <74.6
Beaufort = 8
Case Is <87.9
Beaufort = 9
Case Is <102.3
Beaufort = 10
Case Is <117.5
Beaufort = 11
Case Is <132.8
Beaufort = 12
EndSelect


Makada Apr 30, 2020 03:55 AM

I tried different settings in the "select case" statement.

Comma, AND, OR...

None seem to work.

Select Case WS_kph/ minute_avg_WS_kph/ ten_minute_avg_WS_kph/ sixtie_minute_avg_WS_kph/ day_avg_WS_kph/ month_avg_WS_kph
Case Is <0.8
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 0
Case Is <5.5
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 1
Case Is <12
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 2
Case Is <19.5
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 3
Case Is <28.7
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 4
Case Is <38.6
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 5
Case Is <49.8
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 6
Case Is <61.7
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 7
Case Is <74.6
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 8
Case Is <87.9
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 9
Case Is <102.3
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 10
Case Is <117.5
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 11
Case Is <132.8
Beaufort OR Beaufort_1min OR Beaufort_10min OR Beaufort_60min OR Beaufort_day OR Beaufort_month= 12
EndSelect


nsw Apr 30, 2020 05:04 AM

Have a look at the CRBasic Function instruction. This does exactly what you want to do.

In fact, in the CRBasic Help, CR1000Xlogger, current OS, there is an example link which shows how a function with a case statement in it is called from the main program.

This matches pretty much exactly what you want to do. Example below :-

Public WS_KPH, Beaufort, BF

Function (BeaufortScale) WS_KPH
  Select Case WS_kph
  Case Is <0.8
    Beaufort = 0
  Case Is <5.5
    Beaufort = 1
  Case Is <12
    Beaufort = 2
  Case Is <19.5
    Beaufort = 3
  Case Is <28.7
    Beaufort = 4
  Case Is <38.6
    Beaufort = 5
  Case Is <49.8
    Beaufort = 6
  Case Is <61.7
    Beaufort = 7
  Case Is <74.6
    Beaufort = 8
  Case Is <87.9
    Beaufort = 9
  Case Is <102.3
    Beaufort = 10
  Case Is <117.5
    Beaufort = 11
  Case Is <132.8
    Beaufort = 12
  EndSelect
  Return Beaufort
EndFunction

'Main Program
BeginProg
  Scan (15,Sec,0,0)
    WS_KPH = WS_KPH+1
    BF = BeaufortScale(WS_KPH)
    CallTable Test
  NextScan
EndProg


Makada Apr 30, 2020 05:37 AM

Hi nsw, 

Thanks for your reply .

Im not sure how to use it when i have, for example, WS_KPH, WS_KPH 1min, WS_KPH 10min, WS_KPH60min, WS_KPH1hour etc.

I can use just the one piece of code you mention for all those data ?

With kind regards .


nsw Apr 30, 2020 06:42 AM

Yes, that is one of the main reaons for Function. Try it out and you will see.


Makada Apr 30, 2020 08:48 AM

Ok ill try.

Thanks for your help nsw:)


Makada Apr 30, 2020 09:41 AM

Yes thats working great.

Maybe the following can be achieved easier to...:)

I got it from this great piece of info from Campbell Scientific:

https://www.campbellsci.es/blog/convert-wind-directions

My attempt to run this in 1 minute (Table1), 10 minute (Table2), 60 minute (Table3), daily (Table4) and monthly (Table5) tables.

Dim Sector(17) As String * 3 = {"N","NNO","NO","ONO","O","OZO","ZO","ZZO","Z","ZZW","ZW","WZW","W","WNW","NW","NNW","N"}

Public WindDir As Float
Public CompassDir As String * 3
Public CompassDir1Min As String * 3
Public CompassDir10Min As String * 3
Public CompassDir60Min As String * 3
Public CompassDirday As String * 3
Public CompassDirmonth As String * 3

Public minute_avg_WindDir As Float
Public ten_minute_avg_WindDir As Float
Public sixtie_minute_avg_WindDir As Float

Public day_avg_WindDir As Float
Public month_avg_WindDir As Float

minute_avg_WindDir = Table1.WindDir_D1_WVT(1,1)
ten_minute_avg_WindDir = Table2.WindDir_D1_WVT(1,1)
sixtie_minute_avg_WindDir = Table3.WindDir_D1_WVT(1,1)
day_avg_WindDir = Table4.WindDir_D1_WVT(1,1)
month_avg_WindDir = Table5.WindDir_D1_WVT(1,1)

CompassDir = Sector(Round((WindDir MOD 360)/ 22.5,0)+1)
CompassDir1Min = Sector(Round((minute_avg_WindDir MOD 360)/ 22.5,0)+1)
CompassDir10Min = Sector(Round((ten_minute_avg_WindDir MOD 360)/ 22.5,0)+1)
CompassDir60Min = Sector(Round((sixtie_minute_avg_WindDir MOD 360)/ 22.5,0)+1)
CompassDirday = Sector(Round((day_avg_WindDir MOD 360)/ 22.5,0)+1)
CompassDirmonth = Sector(Round((month_avg_WindDir MOD 360)/ 22.5,0)+1

DataTable(Table1,True,-1)
DataInterval(0,1,Min,10)
Sample(1,CompassDir1Min,String,False)

DataTable(Table1,True,-1)
DataInterval(0,10,Min,10)
Sample(1,CompassDir10Min,String,False)

...and so on.

With kind regards. 


nsw May 1, 2020 07:28 AM

Yes, absolutely right. As the calclation is something you do regularly, it can be done as a Function. See the Help in CRBasic for Function which shows some examples which should help you.


Makada May 1, 2020 09:09 AM

Hi nsw, 

With the code above i get an "Variable out of bounds:102 - There is a program error."

When i choose "Reset" , nothing happens.

With kind regards .


Makada May 2, 2020 01:29 AM

Hi nsw, 

How do i use the BF in your code in my 5 different tables?

Making a Tablename.fieldname from the windspeed windvector?

And then Function (BeaufortScale) the windvector windspeed from each table seperately?

Table1, 1 minute table the minute average BF from windvector speed

Table2, 10 minute table the 10 minute average BF from windvector speed

Table3, 1 hour table 1 hour average BF from windvector speed

And so on.

I am currently running a test with the above script but i see the windvector speed is one minute (in minute table 1) behind the function output.

Or do you have a better solution?

With kind regards. 

https://ibb.co/Npy378w


Makada May 3, 2020 07:11 AM

Hi,

It looks like when i make a public value from the windspeed from the windvector with tablename.fieldname (minute_avg_WS_kph = Table1.WS_KPH_S_WVT(1)) and use the output in the function:

BF1 = BeaufortScale (minute_avg_WS_kph)

With this function:

Function (BeaufortScale) WS_KPH
Select Case WS_KPH
Case Is <0.8
Beaufort = 0
Case Is <5.5
Beaufort = 1
Case Is <12
Beaufort = 2
Case Is <19.5
Beaufort = 3
Case Is <28.7
Beaufort = 4
Case Is <38.6
Beaufort = 5
Case Is <49.8
Beaufort = 6
Case Is <61.7
Beaufort = 7
Case Is <74.6
Beaufort = 8
Case Is <87.9
Beaufort = 9
Case Is <102.3
Beaufort = 10
Case Is <117.5
Beaufort = 11
Case Is <132.8
Beaufort = 12
EndSelect
Return Beaufort
EndFunction

...the output BF1 in the one minute datatable is always one minute late of the WS_KPH_S_WVT output.

As seen in the link in previous post.

With kind regards 


Makada May 4, 2020 11:26 AM

Hi nsw, 

Am i doing something wrong regarding the code above?

With kind regards .

Log in or register to post/reply in the forum.