QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: SMcNeill on December 24, 2020, 10:47:16 pm

Title: A quick program for folks to test
Post by: SMcNeill on December 24, 2020, 10:47:16 pm
Code: QB64: [Select]
  1.  
  2. ip$ = GetPublicIP$
  3. PRINT "Your IP Address is:"; ip$
  4. Lat_Long ip$, lat, lon
  5. PRINT "Your Latitude and Longitude is: "; lat, lon
  6. PRINT "For your location, the following is true for Xmas day:"
  7. SunStuff lat, lon, 12, 25, 2020
  8.  
  9.  
  10. FUNCTION GetPublicIP$
  11.     f = FREEFILE
  12.     OPEN "tempPIP.txt" FOR OUTPUT AS #f: CLOSE f
  13.     SHELL _HIDE "cmd /c nslookup myip.opendns.com resolver1.opendns.com>tempPIP.txt"
  14.  
  15.     OPEN "tempPIP.txt" FOR INPUT AS #f
  16.     IF LOF(f) THEN
  17.         DO
  18.             LINE INPUT #f, temp$
  19.             IF temp$ <> "" THEN last$ = temp$ 'there's a blank line after the data we need.
  20.             '                                 Ignore it.  What we want is the last line of info generated here.
  21.         LOOP UNTIL EOF(1)
  22.     END IF
  23.     CLOSE f
  24.     l = _INSTRREV(last$, "Address:")
  25.     IF l THEN GetPublicIP$ = MID$(last$, l + 10)
  26.     KILL "tempPIP.txt"
  27.  
  28. SUB Lat_Long (ip$, lat, lon)
  29.     out$ = "powershell.exe -c " + CHR$(34) + "Invoke-Webrequest 'ip-api.com/line/"
  30.     out$ = out$ + ip$
  31.     out$ = out$ + "?fields=lat,lon' -OutFile '.\temp.txt'" + CHR$(34)
  32.     SHELL _HIDE out$
  33.     OPEN "temp.txt" FOR INPUT AS #1
  34.     INPUT #1, lat
  35.     INPUT #1, lon
  36.     CLOSE 1
  37.  
  38. SUB SunStuff (lat, lon, month, day, year)
  39.     out$ = "powershell.exe -c " + CHR$(34) + "Invoke-Webrequest 'https://api.sunrise-sunset.org/json?lat="
  40.     out$ = out$ + _TRIM$(STR$(lat)) + "&lng="
  41.     out$ = out$ + _TRIM$(STR$(lon)) + "&date="
  42.     d$ = _TRIM$(STR$(year)) + _TRIM$(STR$(month)) + _TRIM$(STR$(day))
  43.     out$ = out$ + d$ + "'   -OutFile '.\temp.txt'"
  44.     SHELL out$
  45.     OPEN "temp.txt" FOR BINARY AS #1
  46.     t$ = SPACE$(LOF(1))
  47.     GET #1, 1, t$
  48.     'strip off unwanted stuff
  49.     l = INSTR(t$, ":{"): t$ = MID$(t$, l + 2)
  50.     DO
  51.         l = INSTR(t$, CHR$(34))
  52.         t$ = LEFT$(t$, l - 1) + MID$(t$, l + 1)
  53.     LOOP UNTIL l = 0
  54.     DO
  55.         l = INSTR(t$, "_")
  56.         t$ = LEFT$(t$, l - 1) + " " + MID$(t$, l + 1)
  57.     LOOP UNTIL l = 0
  58.     t$ = _TRIM$(t$)
  59.     t$ = LEFT$(t$, LEN(t$) - 1)
  60.  
  61.     DO
  62.         l = INSTR(t$, ",")
  63.         IF l = 0 THEN EXIT DO
  64.         whole$ = LEFT$(t$, l)
  65.         l$ = LEFT$(whole$, INSTR(whole$, ":") - 1)
  66.         r$ = MID$(whole$, INSTR(whole$, ":") + 1)
  67.         r$ = LEFT$(r$, LEN(r$) - 1)
  68.         PRINT l$; " is "; r$
  69.         t$ = MID$(t$, l + 1)
  70.     LOOP
  71.     CLOSE 1
  72.  

Output should look like the below (more, or less, changed by your location):

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

Let me know if this fails to work for any of you Windows folk, and let me know what it does do (if anything at all), so I can work on this.  I'm hoping to add this into my little calendar program so that it'll always be able to give me sunrise and sunset times for my location in the world.  :D
Title: Re: A quick program for folks to test
Post by: STxAxTIC on December 24, 2020, 10:51:40 pm
Works awesome!

I was thinking about two things for the calendar, too. Might as well state them here.

1) Names of the days of the week across the top. I'm sure you kinda get used to the days not being there, but it would be a little more complete that way.

2) Externalizing holiday data as an external file. This opens up a big world, in the sense that if you let certain RSS feeds add to that data file, this calendar can archive or predict all kinds of stuff.
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 24, 2020, 10:52:37 pm
NOTE: All times are in UTC and summer time adjustments are not included in the returned data.
Title: Re: A quick program for folks to test
Post by: STxAxTIC on December 24, 2020, 11:07:03 pm
Oh and - if you completely generalize the routine that undresses t$, you will have a JSON parser.
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 24, 2020, 11:14:51 pm
Oh and - if you completely generalize the routine that undresses t$, you will have a JSON parser.

That's a job for another day.  ;)

As for days, that's easy enough to insert.  I'll add a CONST as a toggle for folks who want them, or not.

For the external holidays, I'm still sorting out the format I want to add for future versions.  I'm thinking something like:

Exact, 12,25, Christmas
Last, 12, 5, Foo_day
First, 12, 2, Fee_day

12/25 is Christmas
Last Thursday (5) in December (12) is Foo_day
First Monday (2) in December is Fee_day

Flexible enough for mouse holidays, and not so complex as to be indecipherable.
Title: Re: A quick program for folks to test
Post by: Bert22306 on December 24, 2020, 11:19:46 pm
In my attempt, it said that it could not find the file in line 38. That's where you tell it to open temp.txt for input. And it's supposed to contain lat and long? Weird. I'll bet my enterprise net is blocking whatever you're trying to do here.

But, it did print out my IP address. Of course, that's an address behind the NAT, so it doesn't really help pinpoint my location. Wha' happened?
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 24, 2020, 11:23:07 pm
In my attempt, it said that it could not find the file in line 38. That's where you tell it to input temp.txt.

But, it did print out my IP address. Of course, that's an address behind the NAT, so it doesn't really help pinpoint my location. Wha' happened?

Remove the _HIDE on line 37 and see what the output in the console looks like. 

Did it generate a lat/long value for you?

And you need permissions to run powershell scripts on your PC, as well, if that's an issue.
Title: Re: A quick program for folks to test
Post by: Bert22306 on December 24, 2020, 11:28:20 pm
No. It says something about authentication required to reach the site, but it did show a public IP address, at least, as opposed to the one behind my NAT. I'm doing this test from my corporate enterprise net, which is almost certainly why these problems exist.
Title: 🎄Re: A quick program for folks to test🎄
Post by: Richard on December 24, 2020, 11:37:00 pm
@Steve
🎄
  [ This attachment cannot be displayed inline in 'Print Page' view ]  

So in Australia we have longer Xmas days than you.
Title: Re: A quick program for folks to test
Post by: SpriggsySpriggs on December 25, 2020, 12:11:17 am
I shall give this a try and use my pipecom library to get rid of the file operations.
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 25, 2020, 12:34:51 am
I shall give this a try and use my pipecom library to get rid of the file operations.

Personally, I prefer the file operations.  With them, I can check to see if the file exists, and if so, don't need to call the functions again.  ;)
Title: Re: A quick program for folks to test
Post by: johnno56 on December 25, 2020, 01:10:06 am
Hi Steve,

Mine didn't run. Linux Mint 20. "Line: 38 (in main module). File not found". "temp.txt"

J
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 25, 2020, 01:18:38 am
Hi Steve,

Mine didn't run. Linux Mint 20. "Line: 38 (in main module). File not found". "temp.txt"

J

Yours won't run, as it relies on window's Powershell to download info from the web.  I'll need to add a Linux method to do the same, and then it'll work for you.
Title: Re: A quick program for folks to test
Post by: johnno56 on December 25, 2020, 04:06:02 am
Thanks Steve... No rush... Just curious to see if it would work....
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 25, 2020, 04:16:49 am
Thanks Steve... No rush... Just curious to see if it would work....

Linux would probably just need to use wget instead.

SHELL "wget http://example.com/file.tar -O /path/to/dir/file.tar"

With the syntax above, used with the links from the powershell code, you should be able to download the same results on Linux as on windows.  I'll see about putting up some code for you to try later, so you can be my test monkey with it.  I don't usually try and code much for Linux, as all my personal stuff is windows, but this is easy enough to account for that I don't mind trying it blindfolded.  ;)
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 25, 2020, 06:02:01 am
@Bert22306, @johnno56

Give the following version a run, guys, and see what you get:

Code: QB64: [Select]
  1.  
  2. ip$ = GetPublicIP$
  3. PRINT "Your IP Address is:"; ip$
  4. Lat_Long ip$, lat, lon
  5. PRINT "Your Latitude and Longitude is: "; lat, lon
  6. PRINT "For your location, the following is true for Xmas day:"
  7. SunStuff lat, lon, 12, 25, 2020
  8.  
  9.  
  10. SUB DownloadURL (link$, file$)
  11.     f = FREEFILE
  12.     OPEN file$ FOR OUTPUT AS #f: CLOSE #f 'erase any old file of the same name
  13.     $IF WIN THEN
  14.         out$ = "powershell.exe -c " + CHR$(34) + "Invoke-Webrequest '" + link$ + "' -OutFile '" + file$ + "'" + CHR$(34)
  15.     $ELSE
  16.         out$ = "wget " + chr$(34) + link$ + " -O " + file$ + chr$(34)
  17.     $END IF
  18.     SHELL _HIDE out$
  19.  
  20.  
  21. FUNCTION GetPublicIP$
  22.     f = FREEFILE
  23.     OPEN "tempPIP.txt" FOR OUTPUT AS #f: CLOSE f
  24.     SHELL _HIDE "cmd /c nslookup myip.opendns.com resolver1.opendns.com>tempPIP.txt"
  25.  
  26.     OPEN "tempPIP.txt" FOR INPUT AS #f
  27.     IF LOF(f) THEN
  28.         DO
  29.             LINE INPUT #f, temp$
  30.             IF temp$ <> "" THEN last$ = temp$ 'there's a blank line after the data we need.
  31.             '                                 Ignore it.  What we want is the last line of info generated here.
  32.         LOOP UNTIL EOF(1)
  33.     END IF
  34.     CLOSE f
  35.     l = _INSTRREV(last$, "Address:")
  36.     IF l THEN GetPublicIP$ = MID$(last$, l + 10)
  37.     KILL "tempPIP.txt"
  38.  
  39. SUB Lat_Long (ip$, lat, lon)
  40.     f = FREEFILE
  41.     DownloadURL "ip-api.com/line/" + ip$ + "?fields=lat,lon", "temp.txt"
  42.     OPEN "temp.txt" FOR INPUT AS #f
  43.  
  44.     IF LOF(f) = 0 THEN CLOSE f: EXIT SUB 'something didn't download.  this info isn't available to parse.
  45.  
  46.     INPUT #f, lat
  47.     INPUT #f, lon
  48.     CLOSE f
  49.  
  50. SUB SunStuff (lat, lon, month, day, year)
  51.     d$ = _TRIM$(STR$(year)) + _TRIM$(STR$(month)) + _TRIM$(STR$(day))
  52.     link$ = "https://api.sunrise-sunset.org/json?lat=" + _TRIM$(STR$(lat)) + "&lng="
  53.     link$ = link$ + _TRIM$(STR$(lon)) + "&date=" + d$
  54.     DownloadURL link$, "temp.txt"
  55.     f = FREEFILE
  56.     OPEN "temp.txt" FOR BINARY AS #f
  57.  
  58.     IF LOF(f) = 0 THEN CLOSE f: EXIT SUB 'something didn't download.  this info isn't available to parse.
  59.  
  60.     t$ = SPACE$(LOF(f))
  61.     GET #1, 1, t$
  62.     CLOSE f
  63.  
  64.     'strip off unwanted stuff
  65.     l = INSTR(t$, ":{"): t$ = MID$(t$, l + 2)
  66.     DO
  67.         l = INSTR(t$, CHR$(34))
  68.         t$ = LEFT$(t$, l - 1) + MID$(t$, l + 1)
  69.     LOOP UNTIL l = 0
  70.     DO
  71.         l = INSTR(t$, "_")
  72.         t$ = LEFT$(t$, l - 1) + " " + MID$(t$, l + 1)
  73.     LOOP UNTIL l = 0
  74.     t$ = _TRIM$(t$)
  75.     t$ = LEFT$(t$, LEN(t$) - 1)
  76.  
  77.     DO
  78.         l = INSTR(t$, ",")
  79.         IF l = 0 THEN EXIT DO
  80.         whole$ = LEFT$(t$, l)
  81.         l$ = LEFT$(whole$, INSTR(whole$, ":") - 1)
  82.         r$ = MID$(whole$, INSTR(whole$, ":") + 1)
  83.         r$ = LEFT$(r$, LEN(r$) - 1)
  84.         PRINT l$; " is "; r$
  85.         t$ = MID$(t$, l + 1)
  86.     LOOP
  87.  

For Bert, I'm hoping that you'll basically run but see a whole lot of nothing, as we should error trap now for non-existent files.  You could always try running it as admin and see if that makes a difference on your work machine, but I imagine it's some sort of system limitation.  Powershell is only available from windows 7 up, so if your enterprise machines don't give proper permissions, or run on an older version of windows, then they're definitely not going to work.  A test just to make certain things fail properly, without crashing, would be nice in this case.  :)

For Johnno, I'm hoping that you get the same results now that everyone else does.  If not, uncomment out that _HIDE statement in the download SUB and let me know what the output is that you see.  wget comes with Linux (I think), so unless my syntax is wrong, or something, I'm thinking it should work for you.  If it does, I'll have a nice simple tool to add to my toolbox for downloading stuff from the web, which works with https files and is cross-compatible with our various OSes.  :D

For everyone else, it should run as before, without any changes.  At least, I hope it does...
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 25, 2020, 06:52:49 am
And if all works well, the following version should now make adjustments to the time to account for your local time zone and current daylight saving time. 

Code: QB64: [Select]
  1. TYPE Sun_Data_type
  2.     value AS STRING
  3.  
  4. REDIM SHARED Sun(0) AS Sun_Data_type
  5.  
  6.  
  7.  
  8.  
  9. ip$ = GetPublicIP$
  10. PRINT "Your IP Address is:"; ip$
  11. Lat_Long ip$, lat, lon
  12. PRINT "Your Latitude and Longitude is: "; lat, lon
  13.  
  14. SunStuff lat, lon, 12, 25, 2020
  15. PRINT "For your location, the following is true for Xmas day:"
  16. FOR i = 1 TO UBOUND(Sun)
  17.     PRINT Sun(i).field; " = "; Sun(i).value
  18.  
  19.  
  20. FUNCTION GetHour (fromTime$)
  21.     'time should be hh:mm:ss WhateverM
  22.     l = INSTR(fromTime$, ":") '1st :
  23.     GetHour = VAL(LEFT$(fromTime$, l))
  24.  
  25. FUNCTION GetMinute (fromTime$)
  26.     'time should be hh:mm:ss WhateverM
  27.     l = INSTR(fromTime$, ":") '1st :
  28.     GetMinute = VAL(MID$(fromTime$, l + 1))
  29.  
  30.  
  31. FUNCTION GetSecond (fromTime$)
  32.     'time should be hh:mm:ss WhateverM
  33.     l = INSTR(fromTime$, ":") '1st :
  34.     l = INSTR(l + 1, fromTime$, ":") '2nd :
  35.     GetSecond = VAL(MID$(fromTime$, l + 1))
  36.  
  37.  
  38. FUNCTION GetTimeOffset
  39.     $IF WIN THEN
  40.         TYPE SYSTEMTIME
  41.             wYear AS _UNSIGNED INTEGER
  42.             wMonth AS _UNSIGNED INTEGER
  43.             wDayOfWeek AS _UNSIGNED INTEGER
  44.             wDay AS _UNSIGNED INTEGER
  45.             wHour AS _UNSIGNED INTEGER
  46.             wMinute AS _UNSIGNED INTEGER
  47.             wSecond AS _UNSIGNED INTEGER
  48.             wMilliseconds AS _UNSIGNED INTEGER
  49.         END TYPE
  50.  
  51.         TYPE TIME_ZONE_INFORMATION
  52.             Bias AS LONG
  53.             StandardName AS STRING * 64 'WCHAR      StandardName[32];
  54.             StandardDate AS SYSTEMTIME
  55.             StandardBias AS LONG
  56.             DaylightName AS STRING * 64 'WCHAR      DaylightName[32];
  57.             DaylightDate AS SYSTEMTIME
  58.             DaylightBias AS LONG
  59.         END TYPE
  60.  
  61.         DECLARE DYNAMIC LIBRARY "Kernel32"
  62.             SUB GetTimeZoneInformation (t AS TIME_ZONE_INFORMATION)
  63.         END DECLARE
  64.  
  65.         DIM t AS TIME_ZONE_INFORMATION
  66.         GetTimeZoneInformation t
  67.         GetTimeOffset = t.Bias
  68.     $END IF
  69.  
  70.  
  71. SUB DownloadURL (link$, file$)
  72.     f = FREEFILE
  73.     OPEN file$ FOR OUTPUT AS #f: CLOSE #f 'erase any old file of the same name
  74.     $IF WIN THEN
  75.         out$ = "powershell.exe -c " + CHR$(34) + "Invoke-Webrequest '" + link$ + "' -OutFile '" + file$ + "'" + CHR$(34)
  76.     $ELSE
  77.         out$ = "wget " + chr$(34) + link$ + " -O " + file$ + chr$(34)
  78.     $END IF
  79.     SHELL _HIDE out$
  80.  
  81.  
  82. FUNCTION GetPublicIP$
  83.     f = FREEFILE
  84.     OPEN "tempPIP.txt" FOR OUTPUT AS #f: CLOSE f
  85.     SHELL _HIDE "cmd /c nslookup myip.opendns.com resolver1.opendns.com>tempPIP.txt"
  86.  
  87.     OPEN "tempPIP.txt" FOR INPUT AS #f
  88.     IF LOF(f) THEN
  89.         DO
  90.             LINE INPUT #f, temp$
  91.             IF temp$ <> "" THEN last$ = temp$ 'there's a blank line after the data we need.
  92.             '                                 Ignore it.  What we want is the last line of info generated here.
  93.         LOOP UNTIL EOF(1)
  94.         l = _INSTRREV(last$, "Address:")
  95.         IF l THEN GetPublicIP$ = MID$(last$, l + 10)
  96.     END IF
  97.     CLOSE f
  98.     KILL "tempPIP.txt"
  99.  
  100. SUB Lat_Long (ip$, lat, lon)
  101.     f = FREEFILE
  102.     DownloadURL "ip-api.com/line/" + ip$ + "?fields=lat,lon", "temp.txt"
  103.     OPEN "temp.txt" FOR INPUT AS #f
  104.  
  105.     IF LOF(f) = 0 THEN CLOSE f: EXIT SUB 'something didn't download.  this info isn't available to parse.
  106.  
  107.     INPUT #f, lat
  108.     INPUT #f, lon
  109.     CLOSE f
  110.  
  111. SUB SunStuff (lat, lon, month, day, year)
  112.     d$ = _TRIM$(STR$(year)) + _TRIM$(STR$(month)) + _TRIM$(STR$(day))
  113.     link$ = "https://api.sunrise-sunset.org/json?lat=" + _TRIM$(STR$(lat)) + "&lng="
  114.     link$ = link$ + _TRIM$(STR$(lon)) + "&date=" + d$
  115.     DownloadURL link$, "temp.txt"
  116.     f = FREEFILE
  117.     OPEN "temp.txt" FOR BINARY AS #f
  118.  
  119.     IF LOF(f) = 0 THEN CLOSE f: EXIT SUB 'something didn't download.  this info isn't available to parse.
  120.  
  121.     t$ = SPACE$(LOF(f))
  122.     GET #1, 1, t$
  123.     CLOSE f
  124.  
  125.     'strip off unwanted stuff
  126.     l = INSTR(t$, ":{"): t$ = MID$(t$, l + 2) 'junk left of our initial data
  127.     DO
  128.         l = INSTR(t$, CHR$(34))
  129.         t$ = LEFT$(t$, l - 1) + MID$(t$, l + 1) 'remove all quotes completely from this data
  130.     LOOP UNTIL l = 0
  131.     DO
  132.         l = INSTR(t$, "_")
  133.         t$ = LEFT$(t$, l - 1) + " " + MID$(t$, l + 1) 'change all underscores to spaces in this data
  134.     LOOP UNTIL l = 0
  135.     t$ = _TRIM$(t$)
  136.     t$ = LEFT$(t$, LEN(t$) - 12) 'remove the last end of data }
  137.     PRINT t$
  138.  
  139.     'parse it down to field, data
  140.     DO
  141.         l = INSTR(t$, ",")
  142.         IF l = 0 THEN EXIT DO
  143.         count = count + 1
  144.         REDIM _PRESERVE Sun(count) AS Sun_Data_type
  145.         whole$ = LEFT$(t$, l)
  146.         Sun(count).field = LEFT$(whole$, INSTR(whole$, ":") - 1)
  147.         r$ = MID$(whole$, INSTR(whole$, ":") + 1)
  148.         r$ = LEFT$(r$, LEN(r$) - 1)
  149.         IF RIGHT$(r$, 1) = "M" THEN m$ = RIGHT$(r$, 3) ELSE m$ = ""
  150.         h = GetHour(r$) - GetTimeOffset / 60: m = GetMinute(r$): s = GetSecond(r$)
  151.         IF h < 1 THEN h = h + 12 'adjust for AM/PM difference, if ever necessary
  152.         IF h > 12 THEN h = h - 12 'adjust for AM/PM difference, if ever necessary
  153.         Sun(count).value = _TRIM$(STR$(h)) + ":" + _TRIM$(STR$(m)) + ":" + _TRIM$(STR$(s)) + m$
  154.         t$ = MID$(t$, l + 1)
  155.     LOOP
  156.  

Since the plan is to only have this information available on a TODAY basis, and not for any given day, I'm not going to try and detect for what days are modified by daylight savings time, and what aren't.  I'm just going to use the overall modifier that our system gives us, and go with that.  If you need something different, then I'll leave it up to you to sort out when to apply the DST modifier and when not to.
Title: Re: A quick program for folks to test
Post by: jack on December 25, 2020, 07:49:14 am
seems to work ok
but I would prefer calculating the sunrise/set locally instead of accessing the web, also, I am not fond of using the shell and temporary files
here's code that calculates the sunrise/set https://skyandtelescope.org/wp-content/uploads/sunup.bas
Title: Re: 🎄Re: A quick program for folks to test🎄
Post by: doppler on December 25, 2020, 10:18:02 am
So in Australia we have longer Xmas days than you.
Yea, but everything is upside down.
Title: Re: A quick program for folks to test
Post by: jack on December 25, 2020, 10:49:28 am
I refactored the code
Code: QB64: [Select]
  1. '
  2. '   This program by Roger W. Sinnott calculates the times of sunrise
  3. '   and sunset on any date, accurate to the minute within several
  4. '   centuries of the present.  It correctly describes what happens in the
  5. '   arctic and antarctic regions, where the Sun may not rise or set on
  6. '   a given date.  Enter north latitudes positive, west longitudes
  7. '   negative.  For the time zone, enter the number of hours west of
  8. '   Greenwich (e.g., 5 for EST, 4 for EDT).  The calculation is
  9. '   discussed in Sky & Telescope for August 1994, page 84.
  10.  
  11. Declare Sub FA(Byref X As Single, Byref Y As Single, Byref T As Single)
  12. '        Sunrise-Sunset
  13. '        Constants
  14. DIM A(2) AS SINGLE, D(2) AS SINGLE, DR AS SINGLE, K1 AS SINGLE
  15. P1 = 3.14159265
  16. P2 = 2 * P1
  17. DR = P1 / 180
  18. K1 = 15 * DR * 1.0027379
  19. S1 = "Sunset at  "
  20. R1 = "Sunrise at "
  21. M1 = "No sunrise this date"
  22. M2 = "No sunset this date"
  23. M3s = "Sun down all day"
  24. M4 = "Sun up all day"
  25.  
  26. INPUT "Lat, Long (deg)"; B5, L5
  27. INPUT "Time zone (hrs)"; H
  28. L5 = L5 / 360
  29. Z0 = H / 24
  30. '
  31. '     Calendar --> JD
  32. Y = 2020
  33. M = 12
  34. D3 = 25 'INPUT "Year, Month, Day";Y,M,D3
  35. G = 1
  36. IF Y < 1583 THEN G = 0
  37. D1 = INT(D3)
  38. F = D3 - D1 - .5
  39. J = -INT(7 * (INT((M + 9) / 12) + Y) / 4)
  40. IF G <> 0 THEN
  41.     S = SGN(M - 9)
  42.     A1 = ABS(M - 9)
  43.     J3 = INT(Y + S * INT(A1 / 7))
  44.     J3 = -INT((INT(J3 / 100) + 1) * 3 / 4)
  45. J = J + INT(275 * M / 9) + D1 + G * J3
  46. J = J + 1721027 + 2 * G + 367 * Y
  47. IF F < 0 THEN
  48.     F = F + 1
  49.     J = J - 1
  50. T = (J - 2451545) + F
  51. TT = T / 36525 + 1 ' TT = centuries
  52. '               from 1900.0
  53. '     LST at 0h zone time
  54. T0 = T / 36525
  55. S = 24110.5 + 8640184.813 * T0
  56. S = S + 86636.6 * Z0 + 86400 * L5
  57. S = S / 86400
  58. S = S - INT(S)
  59. T0 = S * 360 * DR
  60. T = T + Z0
  61. '
  62. '       Get Sun's Position
  63. CALL FA(A(1), D(1), T)
  64. T = T + 1
  65. CALL FA(A(2), D(2), T)
  66. IF A(2) < A(1) THEN A(2) = A(2) + P2
  67. Z1 = DR * 90.833 ' Zenith dist.
  68. S = SIN(B5 * DR)
  69. C = COS(B5 * DR)
  70. Z = COS(Z1)
  71. M8 = 0
  72. W8 = 0
  73. A0 = A(1)
  74. D0 = D(1)
  75. DA = A(2) - A(1)
  76. DD = D(2) - D(1)
  77. FOR C0 = 0 TO 23
  78.     P = (C0 + 1) / 24
  79.     A2 = A(1) + P * DA
  80.     D2 = D(1) + P * DD
  81.     '
  82.     '  Test an hour for an event
  83.     L0 = T0 + C0 * K1
  84.     L2 = L0 + K1
  85.     H0 = L0 - A0
  86.     H2 = L2 - A2
  87.     H1 = (H2 + H0) / 2 '  Hour angle,
  88.     D1 = (D2 + D0) / 2 '  declination,
  89.     '                at half hour
  90.     IF C0 <= 0 THEN
  91.         V0 = S * SIN(D0) + C * COS(D0) * COS(H0) - Z
  92.     END IF
  93.     V2 = S * SIN(D2) + C * COS(D2) * COS(H2) - Z
  94.     IF SGN(V0) <> SGN(V2) THEN
  95.         V1 = S * SIN(D1) + C * COS(D1) * COS(H1) - Z
  96.         A1 = 2 * V2 - 4 * V1 + 2 * V0
  97.         B = 4 * V1 - 3 * V0 - V2
  98.         D3 = B * B - 4 * A1 * V0
  99.         IF D3 >= 0 THEN
  100.             D3 = SQR(D3)
  101.             IF V0 < 0 AND V2 > 0 THEN PRINT R1;
  102.             IF V0 < 0 AND V2 > 0 THEN M8 = 1
  103.             IF V0 > 0 AND V2 < 0 THEN PRINT S1;
  104.             IF V0 > 0 AND V2 < 0 THEN W8 = 1
  105.             E = (-B + D3) / (2 * A1)
  106.             IF E > 1 OR E < 0 THEN E = (-B - D3) / (2 * A1)
  107.             T3 = C0 + E + 1 / 120: ' Round off
  108.             H3 = INT(T3)
  109.             M3 = INT((T3 - H3) * 60)
  110.             PRINT USING "##:##"; H3; M3;
  111.             H7 = H0 + E * (H2 - H0)
  112.             N7 = -COS(D1) * SIN(H7)
  113.             D7 = C * SIN(D1) - S * COS(D1) * COS(H7)
  114.             AZ = ATN(N7 / D7) / DR
  115.             IF D7 < 0 THEN AZ = AZ + 180
  116.             IF AZ < 0 THEN AZ = AZ + 360
  117.             IF AZ > 360 THEN AZ = AZ - 360
  118.             PRINT USING ",  azimuth ###.#"; AZ
  119.         END IF
  120.     END IF
  121.     A0 = A2
  122.     D0 = D2
  123.     V0 = V2
  124. '  Special msg?
  125. '
  126. '   Special-message routine
  127. IF M8 <> 0 AND W8 <> 0 THEN
  128.     IF M8 = 0 THEN PRINT M1
  129.     IF W8 = 0 THEN PRINT M2
  130.     IF V2 < 0 THEN PRINT M3s
  131.     IF V2 > 0 THEN PRINT M4
  132. '
  133.  
  134. SUB FA (A5 AS SINGLE, D5 AS SINGLE, T AS SINGLE)
  135.     '   Fundamental arguments
  136.     '     (Van Flandern &
  137.     '     Pulkkinen, 1979)
  138.     DIM P2 AS SINGLE, L AS SINGLE, V AS SINGLE, U AS SINGLE
  139.     DIM W AS SINGLE, S AS SINGLE, R5 AS SINGLE, G AS SINGLE
  140.     DIM TT AS SINGLE
  141.     P2 = 6.283185307179586
  142.     L = .779072 + .00273790931 * T
  143.     G = .993126 + .0027377785 * T
  144.     L = L - INT(L): G = G - INT(G)
  145.     L = L * P2: G = G * P2
  146.     V = .39785 * SIN(L)
  147.     V = V - .01000 * SIN(L - G)
  148.     V = V + .00333 * SIN(L + G)
  149.     V = V - .00021 * TT * SIN(L)
  150.     U = 1 - .03349 * COS(G)
  151.     U = U - .00014 * COS(2 * L)
  152.     U = U + .00008 * COS(L)
  153.     W = -.00010 - .04129 * SIN(2 * L)
  154.     W = W + .03211 * SIN(G)
  155.     W = W + .00104 * SIN(2 * L - G)
  156.     W = W - .00035 * SIN(2 * L + G)
  157.     W = W - .00008 * TT * SIN(G)
  158.     '
  159.     '    Compute Sun's RA and Dec
  160.     S = W / SQR(U - V * V)
  161.     A5 = L + ATN(S / SQR(1 - S * S))
  162.     S = V / SQR(U): D5 = ATN(S / SQR(1 - S * S))
  163.     R5 = 1.00021 * SQR(U)
  164.  
Title: Re: A quick program for folks to test
Post by: Bert22306 on December 26, 2020, 07:36:53 pm
@Bert22306, @johnno56

Give the following version a run, guys, and see what you get:

. . .

For Bert, I'm hoping that you'll basically run but see a whole lot of nothing, as we should error trap now for non-existent files.  You could always try running it as admin and see if that makes a difference on your work machine, but I imagine it's some sort of system limitation.  Powershell is only available from windows 7 up, so if your enterprise machines don't give proper permissions, or run on an older version of windows, then they're definitely not going to work.  A test just to make certain things fail properly, without crashing, would be nice in this case.  :)

Sorry for the delay in responding. Wife kept me busy for Christmas.

This time, I again got the private IP address behind my NAT, as opposed to the public IP address facing the enterprise. But not much more than that:

"Your latitude and longitude is 0 0

"For your location, the following is true for Christmas day:"

"Press any key to continue"

Of course, with Christmas day having passed, I'm not sure what to expect. But you said it. These corporate machines are pretty tightly locked up. They used to trust me with admin privileges, until this last refresh. No more. I should try it on my own machine, when I get a chance.
Title: Re: A quick program for folks to test
Post by: johnno56 on December 26, 2020, 08:38:30 pm
Steve,

The 'file not found' went away. But did not display anything. Commented out the first two lines (console) and reran.

See attached for output.

Regards

J

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 26, 2020, 09:00:03 pm
Steve,

The 'file not found' went away. But did not display anything. Commented out the first two lines (console) and reran.


Does this work from the command line for you:

nslookup myip.opendns.com resolver1.opendns.com

It should give you a couple of lines of information about what your public IP address is.  On windows, we get 4 lines and a blank line.  What's the output look like for Linux?



Second question: Do you have wget installed?  Even if it doesn't find your IP, you should still be able to get sun data for Lat/Long 0, 0.

Make one quick change and test it for me.  In the Download routine, change:

        OUT$ = "wget " + CHR$(34) + link$ + " -O " + file$ + CHR$(34)

To:

        OUT$ = "wget " + CHR$(34) + link$ + CHR$(34) + " -O "+ CHR$(34) + file$ + CHR$(34)

As long as you have wget on your machine, I'm thinking the problem is probably something simple which we might be able to save with quotes in the proper place to deliminate our fields.

Heck, you might even try:

        OUT$ = "wget "  + link$  + " -O " + file$

I'm not on a Linux system, so I can't readily test things for them, but I imagine the glitch here is really something rather simple.  It's not that hard to shell out of wget and have it download a webpage to a file for you.  At least, not usually.  :P
Title: Re: A quick program for folks to test
Post by: Bert22306 on December 27, 2020, 04:34:12 am
And if all works well, the following version should now make adjustments to the time to account for your local time zone and current daylight saving time.

On my own Win10 PC, this works. Up top, the times (datybreak, llocal noon, etc.) seem to be given in GMT (roughly 5 hours ahead). I'll nit: the top info, before the Christmas Day info, would probably benefit from line breaks.
Title: Re: A quick program for folks to test
Post by: SMcNeill on December 27, 2020, 05:09:33 am
On my own Win10 PC, this works. Up top, the times (datybreak, llocal noon, etc.) seem to be given in GMT (roughly 5 hours ahead). I'll nit: the top info, before the Christmas Day info, would probably benefit from line breaks.

Did you try the version at Reply #16?  It should detect your PC's time offset and adjust accordingly for it.  ;)

I'm thinking most of the Windows issues have been sorted out.  Now, if I could only figure out what the heck was wrong with it on Linux for johnno.
Title: Re: A quick program for folks to test
Post by: johnno56 on December 29, 2020, 05:08:12 am
Steve,

Limited success.

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

wget was one of the first programs installed when I upgraded the OS.

  [ This attachment cannot be displayed inline in 'Print Page' view ]  

Actual location is: -38.0828828   145.281849
Title: Re: A quick program for folks to test
Post by: zaadstra on January 02, 2021, 11:18:32 am
Yours won't run, as it relies on window's Powershell to download info from the web.  I'll need to add a Linux method to do the same, and then it'll work for you.

Wget (command line) exists for Windows too - I use it a lot in QB64 to grab a webpage.   If you need to rely on an external program this might be wget, it makes your code a bit more platform independent.

Here is a site with some explanation and a download link to a recent version: https://builtvisible.com/download-your-website-with-wget/ (https://builtvisible.com/download-your-website-with-wget/)