Author Topic: AppleScript - Help needed ...  (Read 758 times)

wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
AppleScript - Help needed ...
« 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,
Reinhard


xairbusdriver

  • Storm
  • *****
  • Posts: 2174
    • EW7115 (E7115)
    • KTNGERMA20
    • Mid-South Weather
  • Station Details: Davis VP2 wireless + remote Anemometer/2014 Mac min - 10.13.4/WC 3.0
Re: AppleScript - Help needed ...
« Reply #1 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.

elagache

  • Global Moderator
  • Storm
  • *****
  • Posts: 4968
    • DW3835
    • KCAORIND10
    • Canebas Weather
  • Station Details: Davis Vantage Pro-2, MacBook Pro (Early 2011), macOS 10.13.6, WeatherCat 3
Best to convert to an integer in cm (Re: AppleScript - Help needed ...)
« Reply #2 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]

wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #3 on: October 24, 2018, 09:37:10 AM »
Thanks folks, I will try... and report back!
Reinhard


wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #4 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!
Reinhard


elagache

  • Global Moderator
  • Storm
  • *****
  • Posts: 4968
    • DW3835
    • KCAORIND10
    • Canebas Weather
  • Station Details: Davis Vantage Pro-2, MacBook Pro (Early 2011), macOS 10.13.6, WeatherCat 3
Glad it is working as advertised. (Re: AppleScript - Help needed ...)
« Reply #5 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]

Blicj11

  • Storm
  • *****
  • Posts: 2996
    • EW3808
    • KUTHEBER6
    • Timber Lakes Utah
  • Station Details: Davis Vantage Pro2 Plus | WeatherLinkIP™ Data Logger | iMac (late 2013), 3.5 GHz Intel Core i7, 24 GB RAM, macOS High Sierra | Sharx SCNC2900 Webcam | Supportive Wife
Re: AppleScript - Help needed ...
« Reply #6 on: October 24, 2018, 10:39:22 PM »
I like two things about this so far:
  • Edouard is right, as usual
  • Reinhard looks like he and his genius son have got themselves a terrific new weather toy
Blick


wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #7 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]
Reinhard


wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #8 on: October 25, 2018, 06:32:29 PM »
...I will post a link to the graph...

Why wait? The measures are test measures, see attached picture.

Reinhard


Blicj11

  • Storm
  • *****
  • Posts: 2996
    • EW3808
    • KUTHEBER6
    • Timber Lakes Utah
  • Station Details: Davis Vantage Pro2 Plus | WeatherLinkIP™ Data Logger | iMac (late 2013), 3.5 GHz Intel Core i7, 24 GB RAM, macOS High Sierra | Sharx SCNC2900 Webcam | Supportive Wife
Re: AppleScript - Help needed ...
« Reply #9 on: October 25, 2018, 08:48:45 PM »
Love the plastic milk carton at the base!
Blick


elagache

  • Global Moderator
  • Storm
  • *****
  • Posts: 4968
    • DW3835
    • KCAORIND10
    • Canebas Weather
  • Station Details: Davis Vantage Pro-2, MacBook Pro (Early 2011), macOS 10.13.6, WeatherCat 3
Interested to see how it works over time! (Re: AppleScript - Help needed ...)
« Reply #10 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]

wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
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,
Reinhard


wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: AppleScript - Help needed ...
« Reply #12 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,
Reinhard


elagache

  • Global Moderator
  • Storm
  • *****
  • Posts: 4968
    • DW3835
    • KCAORIND10
    • Canebas Weather
  • Station Details: Davis Vantage Pro-2, MacBook Pro (Early 2011), macOS 10.13.6, WeatherCat 3
Washing car before AppleScripts (Re: AppleScript - Help needed ...)
« Reply #13 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

wurzelmac

  • Storm
  • *****
  • Posts: 1165
    • ITIROLPR2
    • Wetterstation Prägraten am Großvenediger
  • Station Details: Davis Vantage PRO2 Plus (24h fan aspirated, wireless) with UV/Solar | Weather Envoy Data Logger | Mac mini Mid 2011 2,7 GHz Intel Core i7 with 12 GB of RAM | 2x Canon EOS 1100D | macOS 10.13.6 HighSierra
Re: Washing car before AppleScripts (Re: AppleScript - Help needed ...)
« Reply #14 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!
Reinhard