# lux, lumen and candela math

Hi,
i want to do some math in applescript or shell (i don’t like plugins), but these formulas are far from my comprehension: http://www.compuphase.com/electronics/candela_lumen.htm

1. lux to lumen
2. lumen to candela and vice versa

Hello!

The formulas are actually in the page, if you use view source. You need Satimage.osax for the cos function, when you implement it in applescript.

Just ignore the greek characters, (except for pi).

lux to lumen given surface area in square meters:
= lumen * surface area in square meters (you have to have lumen up front)

lumen to candela given apex angle

a) compute ((2 *pi) * ( 1 - (cos ( 0.5 * apex angle)))

b.) check that the result of a is not 0

c.) compute lumen/ (result of a)

candela to lumen given apex angle

result = ((2picandela)( 1 - cos(0.5apex angle)))

Edit
Angles are taken as radians for the cos function of Satimage.Osax, ( in stead of degrees), the dictionary describes how to perform the conversion.
Hopes this helps

Angle in radians = ((pi * angle in degrees) / 180 )

Alternative is using AWK

``````set degree to "70"
set cd to "1.5"

do shell script "awk 'BEGIN {pi=atan2(0, -1);print 2*pi*" & cd & "*(1-cos(" & degree & "*pi/360))}'"

``````

EDIT: Edited the code a but more user friendly

Hello!

When I was this far, I just wrote them out for you, you have to test them, as I haven’t time at the moment!

``````
to getLumen from lux by surfArea
if surfArea is 0 then error

return lux * surfArea

end getLumen

to convertToCandela from lumen by apexAngleInDegrees

if apexAngleInDegrees = 0 then set apexAngleInDegrees to 360

set circleInRads to 2 * pi

set halfApexAngleInRads to ((pi * apexAngleInDegrees) / 180) * 0.5

end convertToCandela

to convertToLumen from candela by apexAngleInDegrees

if apexAngleInDegrees = 0 then set apexAngleInDegrees to 360

set circleInRads to 2 * pi

set halfApexAngleInRads to ((pi * apexAngleInDegrees) / 180) * 0.5

end convertToLumen

``````

Yes Bazzie Wazzie, awk is a handy tool, but if you use these functions just alittle, the overhead by the do shell script soon becomes noticeable.

I know, 200 calculations a second, but was just to show that you won’t need some extra software installed. TS said ‘applescript or shell (i don’t like plugins)’, not everybody wants to install all kind of OSAX on their machine (me included). And when you put this code behind a dialog or something the speed difference isn’t noticeable.

Oh well. Satimage.osax is a plugin isn’t it?

But the OP can save the script containing the bundle, and stuff Satimage.osax into the Resources/Scripting Additions folder, or use your solution. ``````

to _cos for angleInDegrees
local _pi
set _pi to pi as text
set _pi to "3." & text 3 thru -1 of _pi as text
return (do shell script "awk 'BEGIN {print cos((" & angleInDegrees & "*" & _pi & ")/180)  }'")
end _cos

return (do shell script "awk 'BEGIN {print cos(" & angleInRads & ")  }'")
end __cos

``````

Where do you get a bytcoded awk by the way, I am curious to try it!

Well that’s one option. You could also use ASObjC Runner:

``````tell application "ASObjC Runner" to set x to do trig on (45 / 180 * pi) using function "cos"

``````

Or vanilla AppleScript:

``````on getCosine(x)
set x to x * (2 * pi) / 360
-- cosine = Ï€/2 - sine, so...
set x to pi / 2 - x
set numerator to x
set denominator to 1
set factor to -(x ^ 2)

repeat with i from 3 to 40 by 2
set numerator to numerator * factor
set denominator to denominator * i * (i - 1)
end repeat

end getCosine

``````

I haven’t got that far in the dictionary of AsObjC Runner yet Shane! AsObjC Runner isn’t a plugin! It is a faceless app, that I like!

I like your solution by using the Taylor (or is it McLaurin ) series expansion? but hopefully the tables are precomputed in AsObjC Runner, awk and Satimage.Osax! I’ll suggest we continue in our previous topic about AWK.

Hi
a big ‘thank you’ to everybody! i tried for myself :

``````set angle to 60 -- in degree
set lumen to 0.11 --125 candela

--lumen to candela
set cmd to "awk 'BEGIN{calc= (2*pi) * (1- (cos (0.5 * (" & angle & "*degree))))}'"
set lum_cand to (do shell script cmd) as real
if lum_cand â‰  0 then set lum_cand to 0.11 / lum_cand
``````

but my calculation might not be correct.
Anyway, my problem is solved.

This is a little better. pi is not a constant in awk.

``````
set angle to 60 -- in degree
set lumen to 0.11 --125 candela

set angle to (((pi * angle) / 180) * 0.5) as text

set ofs to offset of "," in angle

set angle to text 1 thru (ofs - 1) of angle & "." & text (ofs + 1) thru -1 of angle

--lumen to candela
set cmd to "awk 'BEGIN{print (1- (cos (" & angle & ")))}'"
set lum_cand to (do shell script cmd)
set lum_cand to 2 * pi * lum_cand
if lum_cand â‰  0 then set theCand to lumen / lum_cand
theCand
”> 0,13

``````

It should be

``````set angle to "60"
set lumen to "0.11"

do shell script "awk 'BEGIN {pi=atan2(0, -1);print " & lumen & "/(2*pi*(1-cos(" & angle & "*pi/360)))}'"
``````

@McUsr: When you’re result gets higher, for example lumen 125, it returns the wrong results.

Well, my awk version returns 0.00045 less than my applescript version. Given the magnitude of the numbers (148.493135931979 and 148.493583473672) I find that to be perfectly acceptable! When I fill in 125 lumen it will return 1.48493135931979E-4 which is 0.000148… and it should be 1.48493135931979E+2.

I don’t know how you do that!

I copied the code from the post, when I run it, it returns 148.493135931979

Here is the code I used:

``````
set angle to 60 -- in degree
set lumen to 125 --125 candela

set angle to (((pi * angle) / 180) * 0.5) as text

set ofs to offset of "," in angle

set angle to text 1 thru (ofs - 1) of angle & "." & text (ofs + 1) thru -1 of angle

--lumen to candela
set cmd to "awk 'BEGIN{print (1- (cos (" & angle & ")))}'"
set lum_cand to (do shell script cmd)
set lum_cand to 2 * pi * lum_cand
if lum_cand â‰  0 then set theCand to lumen / lum_cand
theCand
--> 0,13

``````

I’d also like to add that it differs 0,036 from the AS version when given 10000 lumen

Be aware that coercing floating point numbers to text have different results depending on the current decimal separator. Your code assumes the european comma separator, on an US system you get the proper shell compatible number with period separator

I know now.

Haven’t payed much attention but numbers needs to have always a comma when system language is Dutch. the string “0.133975” return by the shell will be coerced to integer 133975.

Safest way from a string to a number is using

``````
set stringNumber to "0.133975"
set num to run script stringNumber
``````

So when I put run script before the do shell script I get the expected results.

Yes! I was making a solution that was to work for Joy really, and I also figured that Bazzie Wazzie being Dutch, wouldn’t have any problems with it.

From now onwards, I’ll do the conversion the proper way! I’ll just have to figure out how! So you didn’t run the code as I wrote it then, did you? ``````
--coerce between numbers and string without considering localization
set x to "1.12"
set x to run script x --coerce to a number
set x to do shell script "osascript -e '\"" & x & "\" as number'" --coerce to a string; dirty but does the job
``````

no I had to change set lum_cand to (do shell script cmd) to set lum_cand to run script (do shell script cmd) to make it work.