=============================
SFalmanac Project Description
=============================

.. |nbsp| unicode:: 0xA0
   :trim:

.. |emsp| unicode:: U+2003
   :trim:

.. |smiley| image:: https://github.githubassets.com/images/icons/emoji/unicode/1f603.png
   :height: 24 px
   :width:  24 px

SFalmanac is a **Python 3** script that creates the daily pages of the Nautical Almanac (based on the UT timescale).
The generated tables are needed for celestial navigation with a sextant.
Although you are strongly advised to purchase the official Nautical Almanac, this program will reproduce the tables with no warranty or guarantee of accuracy.

-------------------------------------------------------------------------------------------------------------------------
|emsp| |emsp| |emsp| |emsp| |emsp| |smiley| |emsp| **Multiprocessing version for increased performance!** |emsp| |smiley|
-------------------------------------------------------------------------------------------------------------------------

SFalmanac-Py3 now employs the capability of multiprocessing (if your processor has multiple cores), making it acceptably fast. Single-processing is also optionally available in case one wants to compare the results.
Testing has been successfully performed on Windows 10 and Ubuntu 20.04 LTS. (No testing done on Mac OS.) Compared to single-processing, data processing (excluding conversion to PDF)

* ... of a 6-day Nautical Almanac is **4x faster** on Windows 10; **2x faster** on Linux.
* ... of 6-day Event Time Tables is almost **5x faster** on Windows 10; **3x faster** on Linux.

Windows 10 uses up to 8 threads; Linux uses up to 12 threads in parallel. Testing was performed on a PC with an AMD Ryzen 7 3700X 8-Core (16 threads) Processor. Windows & Mac OS spawn new processes; Linux forks new processes (the code is compatible with both techniques and will also run on CPUs with fewer cores/threads).

This performance gain infers that there is practically no justification in using Skyalmanac, which was an interim solution to overcome the poor single-processing performance in SFalmanac at the cost of marginally poorer accuracy in event times (sunset/twilight/sunrise; moonrise/moonset).

Quick Overview
--------------

This is the **PyPI edition** of `SFalmanac-Py3 <https://github.com/aendie/SFalmanac-Py3>`_ (a Changelog can be viewed here). Version numbering started from 1.0 as the previous well-tested versions that are on github since 2015 were never published in PyPI. Version numbering follows the scheme *Major.Minor.Patch*, whereby the *Patch* number represents some small correction to the intended release.

Two astronomical libraries are employed: `Skyfield <https://rhodesmill.org/skyfield/>`_ and `Ephem <https://rhodesmill.org/pyephem/>`_.
Ephem is only used to calculate a few planet magnitudes (Venus and Jupiter are available in Skyfield).

SFalmanac uses the Hipparcos catalog as its star database. If a current version of Skyfield (>= 1.31) is used, you have two options (which one, you specify manually by editing *config.py*): 

* if "useIERS = False", the built-in UT1 tables in the installed version of Skyfield will be employed.
* if "useIERS = True", for optimal accuracy (especially for all GHA data), Earth orientation data from IERS (International Earth Rotation and Reference Systems Service) is downloaded and then used until it 'expires'. It expires after a chosen number of days (also specifiable in *config.py*). Note that IERS specifies the range of Earth Orientation Parameter (EOP) data currently as "from 2nd January 1973 to 8th January 2023 (continuously advancing)". Refer to the `IERS web site <https://www.iers.org/IERS/EN/Home/home_node.html>`_ for current information.

If your Skyfield version is somewhat older (<= 1.30), Skyfield will have downloaded the older files it then used: *deltat.data, deltat.preds* and *Leap_Second.dat*, which are slightly less accurate than the IERS EOP data (which is updated weekly).

Software Requirements
=====================

|
| Nearly all of the astronomical computation is done by the free Skyfield library.
| Typesetting is done typically by MiKTeX or TeX Live.
| Here are the requirements/recommendations:

* `python <https://www.python.org/downloads/>`_ >= 3.4 (v3.9.x is recommended)
* `skyfield <https://pypi.org/project/skyfield/>`__ >= v1.15 (the latest is recommended; see the `Skyfield Changelog <https://rhodesmill.org/skyfield/installation.html#changelog>`_)
* `pandas <https://pandas.pydata.org/>`_ >= 1.0 (to decode the Hipparcos catalog)
* `ephem <https://pypi.org/project/ephem/>`__ >= 3.7.6
* `MiKTeX <https://miktex.org/>`_ |nbsp| |nbsp| or |nbsp| |nbsp| `TeX Live <http://www.tug.org/texlive/>`_

Installation
============

Install a TeX/LaTeX program on your operating system so that 'pdflatex' is available.

Ensure that the `pip Python installer tool <https://pip.pypa.io/en/latest/installation/>`_ is installed. 
Then ensure that old versions of PyEphem, Ephem and Skyalmanac are not installed before installing SkyAlmanac from PyPI::

  python -m pip uninstall pyephem ephem sfalmanac
  python -m pip install sfalmanac

Installing SFalmanac ensures that Ephem, Skyfield and Pandas (and their dependencies) are also installed. If previous versions of SFalmanac were installed, consider upgrading Skyfield and Pandas::

  python -m pip install --upgrade skyfield pandas

Thereafter run it with::

  python -m sfalmanac

On a POSIX system (Linux or Mac OS), use ``python3`` instead of ``python`` in the commands above.

This PyPI edition also supports installing and running in a `venv <https://docs.python.org/3/library/venv.html>`_ virtual environment.
Finally check or change the settings in *config.py*.
It's location is printed immediately whenever Skyalmanac runs.

Guidelines for Linux & Mac OS
-----------------------------

Quote from `Chris Johnson <https://stackoverflow.com/users/763269/chris-johnson>`_:

It's best to not use the system-provided Python directly. Leave that one alone since the OS can change it in undesired ways.

The best practice is to configure your own Python version(s) and manage them on a per-project basis using ``venv`` (for Python 3). This eliminates all dependency on the system-provided Python version, and also isolates each project from other projects on the machine.

Each project can have a different Python point version if needed, and gets its own site_packages directory so pip-installed libraries can also have different versions by project. This approach is a major problem-avoider.

Troubleshooting
---------------

If using MiKTeX 21 or higher, executing 'option 7' (Increments and Corrections) will probably fail with::

    ! TeX capacity exceeded, sorry [main memory size=3000000].

To resolve this problem (assuming MiKTeX has been installed for all users),
open a Command Prompt as Administrator and enter: ::

    initexmf --admin --edit-config-file=pdflatex

This opens pdflatex.ini in Notepad. Add the following line: ::

    extra_mem_top = 1000000

and save the file. Problem solved. For more details look `here <https://tex.stackexchange.com/questions/438902/how-to-increase-memory-size-for-xelatex-in-miktex/438911#438911>`_.