mireianievas / PySQM

Python client for the Sky Quality Meter
https://guaix.fis.ucm.es/splpr/SQM-REECL
GNU General Public License v3.0
4 stars 12 forks source link

CircumPolarError handling for twilight, fixes #8 and #11 #9

Closed jscheidtmann closed 2 years ago

jscheidtmann commented 2 years ago

This pull requests adds handling of CircumPolarError handling to Ephem.calculate_twilight and plotting.

This code has not been tested.

jscheidtmann commented 2 years ago

@Tartouffe provided a data file to me, that did not create a graph before and errored out as described in issue #8. The current version of this pull request creates the respective plot using:

python plot.py -i <name of data file>

even if the config.py lat/lon is so high, that astronomical night does not happen (tested with lat=51.09, lat=9.57 - no that is not my coordinates, nor Tartouffe's). Please review and merge.

jscheidtmann commented 2 years ago

BTW, I tested the plot.py in python 3.8.10 (in Ubuntu on WSL) and had to silence a couple of warnings and work around a couple of error messages.

Tartouffe commented 2 years ago

Hello Mireia,

here is the fix. If you like, please exchange the whole calculate_twilight fuction with this (starting at line 114):

` def calculate_twilight(self,thedate,twilight=-18):

Changing the horizon forces ephem to calculate different types of twilights:

    # 0: polar regions
    # -6: civil
    # -12: nautical
    # -18: astronomical
    # Because of different locations, not all twilight zones exists, so when a
    # ephem.CircumpolarError (catched NeverUpError or AlwaysUpError) happended in
    # summer time (Jun/Jul) interate through the zones up to: sun <= 0 degrees,
    # instead of calculating the place and check for possible existing/non existing nights

    interate = 0
    for interate in range (4):
        if interate == 0: twilight=-18
        if interate == 1: twilight=-12
        if interate == 2: twilight=-9
        if interate == 3: twilight=0

        self.Observatory.horizon = str(twilight)
        self.Observatory.date = str(self.end_of_the_day(thedate))

        try:
            self.twilight_prev_rise = self.ephem_date_to_datetime(\
             self.Observatory.previous_rising(ephem.Sun(),use_center=True))
            self.twilight_prev_set = self.ephem_date_to_datetime(\
             self.Observatory.previous_setting(ephem.Sun(),use_center=True))
            self.twilight_next_rise = self.ephem_date_to_datetime(\
             self.Observatory.next_rising(ephem.Sun(),use_center=True))
            self.twilight_next_set = self.ephem_date_to_datetime(\
             self.Observatory.next_setting(ephem.Sun(),use_center=True))                

            break  # all okay, no error, exit for-loop!

        except ephem.CircumpolarError:
            # circumpolar error happened, switch to the next nautical zone
            # --> simply stay in the for-loop 
            pass

`

The fix is really easy in its logic: When the user uses the SQM in an area where some twilight effects are current, then a ephem.CircumpolarError (NeverUpError or AlwaysUpError) error will happen in summertime. If this exception has occured, then the program switches to the next higher nautical zone, to get its variables filled and some legal data, being able to continue and able to plot the graph. E.g. Living at the height of Paris: In Jun/Jul the sun never reached -18 degress at night. Then we use instead of the astronomical (-18°) night the next zone, the nautical night at -12°. If the location is higher, lets say Nordkap, then it excepts up to the civil night at -6°, up to 0 degrees (near Polar regions). The program does its calculation again and again, triggert by the CircumpolarError until 0 degrees were reached (four calculations in total) or in the middle of the function no exception happend any more.

With this PySQM now can be used even at the North/South Pole regions and produces plots even when the sun did not full lowers below the well knows night zones. The method with catching the exception is much more elegant than doing some complicated position calculations, which nautical zone is legal at the users location.

The second change has to be done at the docs, that the graphs dashed line now were not ONLY dawn and dusk of the astromical night, they change on these events to nautical night/civil night or none (based on the position of the SQM/User) to get a plot.

I'm really proud to have one of my first python codes done ^^ Sorry for not pasting the code with such a beauty like Jens, because i do not know how i should do this here. As i said, i'm no programmer, neither knowledgeable with the special code fuctions here.

Tartouffe commented 2 years ago

Arrr, the code starts at "def calculate_twilight". The add code fuction here at browser has a bug, also...

Take this: PySQM-ExchangeCode.txt

Tartouffe commented 2 years ago

Just wanted to give feedback after 5 weeks of running with the fix, that the code above worked without errors over the time of the missing astronomical night and that a graphic was generated every night as usual.

If, sometimes, mireia will use the fix and close this here?!