Trixology

WeatherCat => WeatherCat General Discussion => Topic started by: wurzelmac on October 23, 2018, 07:24:15 PM

Title: AppleScript - Help needed ...
Post by: wurzelmac on October 23, 2018, 07:24:15 PM
Hello,
this topic is dedicated to our famous AppleScripter Edouard.  ::)
Can you help me out this problem:
The all new supersonic snow sensor measures in mm (variable x). Is there a possibility to round this variable beneath 5 down and above 5 up? So for example if "x" records 73 the script should tell 70, on the other side if it records 77 it should output 80. The thought behind is that I would not record every millimeter, but only every centimeter.
How can I format the "x" to do this?
 :-[

Thanks in advance,
Title: Re: AppleScript - Help needed ...
Post by: xairbusdriver on October 23, 2018, 09:33:48 PM
I think your number can be in the hundreds, if I remember your description of your sensor. If it goes above 999, you'll need to add another "if then" below.

I think the AppleScript term you need is "round" plus a 'direction'. In this case, it looks like the "to nearest" would work the way you want.

The pseudo code:
Code: [Select]
snowRAW = your raw sensor number [753mm]
if (snowRAW >99) {
   snowCent = snowRAW / 100 [75.3]
} else { [if snowRAW was 56mm, for example]
   snowCent = snowRAW / 10 [5.6]
}
snowFinal = round to nearest(snowCent)
In the first case it should be 75. In the second case it should be 6.

However, I don't use AppleScript, so I have no idea if this is how it works. I'm sure there is a more efficient method that the AS expert can provide.
Title: Best to convert to an integer in cm (Re: AppleScript - Help needed ...)
Post by: elagache on October 23, 2018, 10:27:51 PM
Dear Reinhard, X-Air, and WeatherCat scripters,

this topic is dedicated to our famous AppleScripter Edouard.  ::)
Can you help me out this problem:
The all new supersonic snow sensor measures in mm (variable x). Is there a possibility to round this variable beneath 5 down and above 5 up? So for example if "x" records 73 the script should tell 70, on the other side if it records 77 it should output 80. The thought behind is that I would not record every millimeter, but only every centimeter.
How can I format the "x" to do this?

This is an old problem that goes all the way back to FORTRAN (at least).  The simplest solution is use a trick about how computers convert real numbers into integers.  Basically you want to round the number to the next highest power of ten.  Here is a solution that assumes you want the output to be literally in cm instead of mm:

Code: [Select]
set snow_mm to 77

set snow_cm to (snow_mm + 5) / 10 as integer

If you want the result to be 80 instead of 8, multiply the variable snow_cm by 10 and that will be the answer you are looking for.

The trick is that when you convert a value to an integer it simply drops any decimal that might have been there.  When I divide by 10 I convert the number from an integer to a floating point number.  Before dividing by 10, I add 5.  That shifts everything by effectively 1/2.  If the value was 74, it becomes 79.   When I divide by 10 it becomes 7.9.  However when converting to an integer the floating point is simply dropped, so when it converts 7.9 to an integer it simply becomes 7.  On the other hand, suppose the value was 76.  Add 5 to get that it becomes 81.  Divide by 10 it becomes 8.1.  However once again everything after the decimal point is dropped.  That leaves you with the 8 cm that you were aiming for.

I hope that's clear.  You can paste the code into your script editor and play around with it for yourself.

Cheers, Edouard  [cheers1]
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 24, 2018, 09:37:10 AM
Thanks folks, I will try... and report back!
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 24, 2018, 10:11:12 AM
Hello again,
just tried Edouards code and it is working fine so far:

Code: [Select]
set msg to do shell script "/usr/local/bin/python2.7 /Users/reinhard/schnee_client.py"

set height to 2030 #hight of sensor above ground

considering case
try
set x to msg as number
set x to height - x
if x is less than 0 then
set x to 0
end if
set snow_mm to x
set snow_mm to (snow_mm + 5) / 10 as integer
set snow_mm to snow_mm * 10
return snow_mm
end try
end considering

Thanks again!
Title: Glad it is working as advertised. (Re: AppleScript - Help needed ...)
Post by: elagache on October 24, 2018, 09:56:02 PM
Dear Reinhard and WeatherCat scripters,

just tried Edouards code and it is working fine so far:

I'm glad it appears to have solved your problem.  I'm just passing on this solution.  I don't remember when I personally became aware of this trick, but it originally dates to around 1960!

Cheers, Edouard  [cheers1]
Title: Re: AppleScript - Help needed ...
Post by: Blicj11 on October 24, 2018, 10:39:22 PM
I like two things about this so far:
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 25, 2018, 06:22:08 PM
First: I re-did the script to show cm with one comma ( Example: 23.7 instead of 237. The advantage is that the graph is now readable, because it begins at [maybe] 20 and ends at [maybe] 30. Not at 220 and 300.) If all is in a workflow, I will post a link to the graph.
Second: YESSSS, that is a really terrific new toy to play with.  :)
Third: If all goes well this winter I can imagine to share all technical things step by step - if the genius behind me allows it...  :D

 [cheers1]
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 25, 2018, 06:32:29 PM
...I will post a link to the graph...

Why wait? The measures (https://wetter.unterwurzacher.at/schnee_test.html) are test measures, see attached picture.

Title: Re: AppleScript - Help needed ...
Post by: Blicj11 on October 25, 2018, 08:48:45 PM
Love the plastic milk carton at the base!
Title: Interested to see how it works over time! (Re: AppleScript - Help needed ...)
Post by: elagache on October 25, 2018, 11:22:26 PM
Dear Reinhard, Blick, and WeatherCat fans of weather geeky toys,

First: I re-did the script to show cm with one comma ( Example: 23.7 instead of 237. The advantage is that the graph is now readable, because it begins at [maybe] 20 and ends at [maybe] 30. Not at 220 and 300.) If all is in a workflow, I will post a link to the graph.

Yes, that is what I was trying to suggest to you when I originally wrote up my example.  Your example now actually does measure in cm.  What you had before was snow in mm that might be overkill.  Are you sure you want to include a full decimal place?  That is once more basically trying to measure snowfall to the nearest mm.  You know snow a lot better than I do, but I would imagine that blowing snow would cause your readings to jump around a lot.  Might it not be more realistic to measure to the nearest 1/2 cm?

Second: YESSSS, that is a really terrific new toy to play with.  :)
Third: If all goes well this winter I can imagine to share all technical things step by step - if the genius behind me allows it...  :D

I'll only be an interested observer since snow - isn't a problem in coastal California!  However, there are a number of WeatherCatters who might love to add a device like this to their weather instrumentation.  I strongly encourage you to document what you have done so that others might join in your new enterprise!

Cheers, Edouard  [cheers1]
Title: Re: Interested to see how it works over time! (Re: AppleScript - Help needed ...)
Post by: wurzelmac on October 26, 2018, 05:50:24 AM
Might it not be more realistic to measure to the nearest 1/2 cm?
Yes, indeed - but since I wasn't able to round it to at least 1 cm, how should I be able to round it to the nearest ? cm? Ahh I know, Edouard will point me into the right direction...  [bounce]

Love the plastic milk carton at the base!
Me, too!  :D

Cheers,
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 26, 2018, 06:10:39 PM
@ Edouard:
Sorry to bother you again after asking on how to do ? cm possible...  :-[
Another thing that could be possible via AppleScript is to "set" the value to zero ( "0" ) every one minute after midnight? With this trick I can force the graph to begin at zero. Possible?
Thanks in advance,
Title: Washing car before AppleScripts (Re: AppleScript - Help needed ...)
Post by: elagache on October 27, 2018, 12:44:36 AM
Dear Reinhard and WeatherCat scripters,

Sorry, but today was the day I set aside to wash and wax our 2000 Buick Century: Coquette.  I started before 10am and didn't finish until after 4pm.  As a result I'm rather worn out!  I'll have to get back to your questions tomorrow.

Cheers, Edouard
Title: Re: Washing car before AppleScripts (Re: AppleScript - Help needed ...)
Post by: wurzelmac on October 27, 2018, 05:42:48 AM
I'll have to get back to your questions tomorrow.

Would be great, Edouard! Thanks for taking the time for a reply!
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 27, 2018, 08:30:02 AM
Dear Edouard and scripters  [computer],

after taking some time to dive into AppleScript a bit deeper and using Dr. Google to look around a bit on the web i found the Introduction to AppleScript Language Guide (https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html) and here is what I need.  :)
So the script I am running at the moment is working and doing what I want so far:

Code: [Select]
set msg to do shell script "/usr/local/bin/python2.7 /Users/reinhard/schnee_client.py"

set height to 2030 #height of sensor above ground

considering case
try
set x to msg as number
set x to height - x
#resetting to zero to avoid negative numbers
if x is less than 0 then
set x to 0
end if
set snow_cm to x
#converting from mm to cm
set snow_cm to snow_cm / 10
#rounding to nearest ? cm
set quantum to 0.5
set snow_cm to (round snow_cm / quantum) * quantum
set t to (time string of (current date))
#resetting to zero to force floating graphdraw beginning at 0
#but leaving gaugedraw with a true daily Hi Lo value till short before midnight
if t is greater than "23:59:00" and t is less than "23:59:59" then
set snow_cm to 0
end if
return snow_cm
end try
end considering
Title: Re: AppleScript - Help needed ...
Post by: xairbusdriver on October 27, 2018, 05:05:03 PM
Sounds familiar... ;)
Title: Two answers (Re: AppleScript - Help needed ...)
Post by: elagache on October 27, 2018, 10:21:17 PM
Dear Reinhard, X-Air, and WeatherCat scripters,

Well I'm still sore and it didn't help that I had to continue trimming our hedge of Oleanders this afternoon, but hopefully I can get back to your questions today.

Yes, indeed - but since I wasn't able to round it to at least 1 cm, how should I be able to round it to the nearest ? cm? Ahh I know, Edouard will point me into the right direction...  [bounce]

Well, I had to search the web for a solution myself, but it turns out to be extremely simple.  The goal is to get a value to the nearest ? value.  So first multiply by 2.   Then round the result to the nearest whole integer.  Then divide by 2 (of course as a floating point operation!)  Here is a little AppleScript function that does the trick:

Code: [Select]
set snow_mm to 75.26
set snow_mm to roundToHalf(snow_mm)

on roundToHalf(numberToRound)
set numberToRound to (round (numberToRound * 2)) as real
return (numberToRound / 2)
end roundToHalf

Another thing that could be possible via AppleScript is to "set" the value to zero ( "0" ) every one minute after midnight? With this trick I can force the graph to begin at zero. Possible?

I see that you have already come up with an alternative, but in my AppleScripts I do this differently.  There is a way to get AppleScript to return the number of seconds since midnight and that's what I've use.  Here is a snippet that shows the basic idea:

Code: [Select]
set daySeconds to time of (current date) -- get number of seconds since midnight

if (daySeconds ≤ 60) then -- If midnight occurred less than a minute ago
set dailySnowfall to 0
end if

I took a look at your script.  So I assume that you are putting all this AppleScript into one of WeatherCat's synthetic channels?  This might be a bit risky because WeatherCat has to wait for the script to complete before it can do anything else in that particular thread.  I don't remember exactly, but this might be the main thread.  Stu would know for sure.

I have a similar situation for WeatherCat memory.  I collect that memory data in an independent AppleScript that creates a tiny file with just memory used as a single number.  The synthetic channel therefore only needs to read the number and can immediately move on.  Perhaps you should ask Stu if what you are doing is safe.  If it is a bit of a risk, I can explain how to move the processing of the data from the ultrasonic sensor out of the synthetic channel and just have a tiny bit of AppleScript that reads the snow level from a file.

Cheers, Edouard
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 28, 2018, 06:01:22 AM
Hello Edouard,

thanks for looking into my script and your thoughts about it - much appreciated!
Title: Re: AppleScript - Help needed ...
Post by: wurzelmac on October 28, 2018, 09:50:00 AM
Sounds familiar... ;)

Yess, indeed.  :D
Title: Re: Two answers (Re: AppleScript - Help needed ...)
Post by: wurzelmac on November 26, 2018, 01:01:53 PM
This might be a bit risky because WeatherCat has to wait for the script to complete before it can do anything else in that particular thread.

Hi Edouard,
since executing the whole script in a synthetic channel (for almost a month now) I did not notice one single problem.  [bounce] Just want to let you know!
Cheers,
Title: Re: AppleScript - Help needed ...
Post by: Blicj11 on November 26, 2018, 06:19:43 PM
I love it when this stuff works.
Title: Glad to hear it!! (Re: AppleScript - Help needed ...)
Post by: elagache on November 27, 2018, 12:18:30 AM
Dear Reinhard, Blick, and WeatherCat scripters,

since executing the whole script in a synthetic channel (for almost a month now) I did not notice one single problem.  [bounce] Just want to let you know!

Glad to hear it!   :)  Honestly I was a bit concerned!  :-\

Cheers, Edouard