# -*- coding: utf-8 -*-
#
# polar2grid documentation build configuration file, created by
# sphinx-quickstart on Thu Apr 19 23:17:38 2012.
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
"""Polar2Grid project's sphinx documentation configuration."""

import ftplib
import os
import sys
import urllib.request
import warnings
from datetime import datetime
from shutil import copyfileobj

import yaml
from pyproj import CRS

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# sys.path.insert(0, os.path.abspath('.'))
print("Adding the following directories to PYTHONPATH:")
BASE_PATH = "../../../"
for dirname in [
    x for x in os.listdir(BASE_PATH) if os.path.isdir(os.path.join(BASE_PATH, x)) and x.startswith("polar2grid")
]:
    print("\t ", os.path.realpath(os.path.join(BASE_PATH, dirname)))
    sys.path.insert(0, os.path.abspath(os.path.join(BASE_PATH, dirname)))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

# Handle building documentation for polar2grid or geo2grid
is_geo2grid = "geo" in os.getenv("POLAR2GRID_DOC", "polar").lower()

# Hack to download example images instead of storing them in git
images = (
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/amsr2/images_basic/gcom-w1_amsr2_btemp_36.5h_20160719_190300_wgs84_fit.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/amsr2/images_nrl/gcom-w1_amsr2_btemp_89.0ah_20160719_190300_lcc_fit.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/amsr2/images_nrl/gcom-w1_amsr2_btemp_89.0ah_20160719_190300_lcc_fit.basic_overlay_example.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/amsr2/images_nrl/gcom-w1_amsr2_btemp_89.0ah_20160719_190300_lcc_fit.advanced_overlay.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/viirs/overlay/npp_viirs_true_color_20170305_193251_lcc_fit_overlay.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/viirs/images_basic/noaa20_viirs_true_color_20220919_175331_lcc_fit.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/viirs/overlay/noaa20_viirs_true_color_20220919_175331_lcc_fit_overlay.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/viirs/dnb/VIIRS_DNB_Enhancement_Comparison.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/viirs/images_basic/npp_viirs_true_color_20170319_183246_miami.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/modis/images_basic/terra_modis_false_color_20170319_163000_miami.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/modis/images_basic/terra_modis_false_color_20170319_163000_miami_p2g_v3.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/modis/awips/SSEC_AWIPS_aqua_modis_bt28_211e_20170308_181800.nc.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/modis/awips/SSEC_AWIPS_aqua_modis_vis02_211e_20170308_181800.nc.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/amsr2/images_nrl/gcom-w1_amsr2_btemp_89.0ah_20160719_190300_lcc_fit_color.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/viirs/merge/VIIRS_False_Color_Side_by_Side_Example_P2G.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/viirs/overlay/VIIRS_true_color_in_google_earth.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/viirs/images_basic/noaa20_viirs_true_color_20220919_175331_lcc_fit_in_google_earth.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/viirs/merge/my_false_color.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/modis/awips/modis_true_color_awips_20170308_1818.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_LCC.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_Mercator.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_Pacific.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_Polar.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_GOES_EAST.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_GOES_STORE.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_GOES_TEST.png",
    "https://www.ssec.wisc.edu/~davidh/polar2grid/scmi_grids/scmi_grid_GOES_WEST.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/viirs/dnb/HNCC_DNB_Band_Example.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/modis/awips/modis_true_color_example.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/modis/awips/modis_vis02_example.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/acspo/VIIRS_I02_ACSPO_SST_composite_example.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/acspo/noaa20_viirs_sst_20220810_184327_great_lakes.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/acspo/noaa20_viirs_sst_20220810_184327_great_lakes_wcolor.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/acspo/noaa20_viirs_sst_20220810_184327_great_lakes_rescaled_wcolor.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/acspo/noaa20_viirs_sst_20220810_184327_great_lakes_rescaled_wcolor_colortable_resize.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/acspo/noaa20_viirs_sst_20220810_184327_great_lakes_sst_final_resize.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/asci/VIIRS_AOD_example.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/asci/VIIRS_AOD_example_with_overlays.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/asci/VIIRS_CTT_example_with_overlays.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/asci/VIIRS_NDVI_example_with_overlays.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/asci/VIIRS_SurfReflectance_True_Color_example_with_overlays.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/acspo/npp_viirs_sst_20191216_072134_acspo_sst_rescaled_wcolor.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/acspo/npp_viirs_sst_20191216_072134_acspo_sst_rescaled_wcolor_colortable_resize.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/acspo/npp_viirs_sst_20191216_072134_acspo_sst_final_resize.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/amsr2/gcom-w1_amsr2_btemp_89.0ah_20220910_233500_wgs84_fit.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_3_0_examples/amsr2/gcom-w1_amsr2_btemp_89.0ah_20220910_233500_wgs84_lcc_fit_rescaled_and_colorized.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/ahi/HIMAWARI-8_AHI_true_color_20181112_233020_perth_example.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/ahi/HIMAWARI-8_AHI_true_color_20181112_233020_perth.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/abi_20181219_1745_montage.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/abi_20181219_1745_montage.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-16_ABI_RadC_natural_color_20181219_174215_GOES-East.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-18_ABI_RadF_natural_color_20221115_183020_GOES-West_cutout.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-16_ABI_RadC_true_color_20190104_195718_GOES-East.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-16_ABI_RadF_true_color_night_20181112_123034_GOES-East.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-16_ABI_RadF_true_color_night_20181112_123034_GOES-East_new.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-16_ABI_RadF_true_color_night_microphysics_20181112_123034_GOES-East_newenhancement.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-16_ABI_RadF_true_color_night_microphysics_20181112_123034_GOES-East.jpg",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/GOES-16_ABI_RadF_true_color_night_microphysics_20181112_123034_GOES-East_new.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/my_goes16_abi_naturalcolor.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi/my_goes18_abi_naturalcolor.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi_l2/GOES-17_ABI_TEMP_20221123_183117_GOES-West_original.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi_l2/goes17_overlay_true_color_cloud_temperature.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi_l2/my_goes17_abi_ctt.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi_l2/my_goes17_abi_ctt1.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi_l2/GOES-16_ABI_AOD_20240429_225020_GOES-East.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/g2g_examples/abi_l2/GOES-16_ABI_AOD_20240429_225020_GOES-East_woverlays.png",
    "https://bin.ssec.wisc.edu/pub/CSPP/p2g_v_2_1_examples/flood/Flood_Legend.png",
)
script_path = os.path.dirname(os.path.realpath(__file__))
image_dst = os.path.join(script_path, "_static", "example_images")
os.makedirs(image_dst, exist_ok=True)

for image_url in images:
    image_fn = os.path.basename(image_url)
    image_pathname = os.path.join(image_dst, image_fn)
    if os.path.isfile(image_pathname):
        continue
    elif image_url.startswith("http://") or image_url.startswith("https://"):  # or image_url.startswith('ftp://'):
        print("Downloading example image: {}".format(image_url))
        with urllib.request.urlopen(image_url) as remote_img, open(image_pathname, "wb") as local_img:
            copyfileobj(remote_img, local_img)
    elif image_url.startswith("ftp://"):
        print("Downloading example image: {}".format(image_url))
        parts = image_url.split("/")
        server = parts[2]
        ftp_fn = "/".join(parts[3:])
        ftp = ftplib.FTP(server, user="ftp")  # hope for anonymous
        out_file = open(image_pathname, "wb")
        ftp.retrbinary("RETR {}".format(ftp_fn), out_file.write)
    else:
        raise ValueError("Not sure how to download image: {}".format(image_url))

# -- Customize setup -----------------------------------------------------------


def setup(app):
    app.add_css_file("prettytables.css")
    app.add_config_value("is_geo2grid", is_geo2grid, "env")


# -- General configuration -----------------------------------------------------
rst_epilog = """
.. |ssec| replace:: :abbr:`SSEC (Space Science and Engineering Center)`
.. |cspp| replace:: :abbr:`CSPP (Community Satellite Processing Package)`
.. |viirs| replace:: :abbr:`VIIRS (Visible/Infrared Imager Radiometer Suite)`
"""

# If your documentation needs a minimal Sphinx version, state it here.
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.autosummary",
    "sphinx.ext.graphviz",
    "sphinx.ext.todo",
    "sphinx.ext.coverage",
    "sphinx.ext.imgmath",
    "sphinx.ext.intersphinx",
    "sphinx.ext.napoleon",
    "sphinx.ext.ifconfig",
    "sphinx.ext.viewcode",
    "sphinxarg.ext",
    "doi_role",
    "toctree_filter",
    "sphinxcontrib.apidoc",
]

# API docs
apidoc_module_dir = "../../polar2grid"
apidoc_output_dir = "dev_guide/api"
if is_geo2grid:
    apidoc_excluded_paths = ["readers/amsr2_l1b.py"]
else:
    apidoc_excluded_paths = []
apidoc_separate_modules = True
apidoc_extra_args = ["-P"]

# Autodoc
autodoc_mock_imports = []

numfig = True

# graphviz_dot = 'dot'

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# The suffix of source filenames.
source_suffix = ".rst"

# The encoding of source files.
# source_encoding = 'utf-8-sig'

# The master toctree document.
master_doc = "index"

# General information about the project.
if is_geo2grid:
    project = "Geo2Grid"
    version = "1.2"
    release = "1.2"
else:
    project = "Polar2Grid"

    # The version info for the project you're documenting, acts as replacement for
    # |version| and |release|, also used in various other places throughout the
    # built documents.
    #
    # The short X.Y version.
    version = "3.1"

    # The full version, including alpha/beta/rc tags.
    release = "3.1"

# Tell Polar2Grid scripts which system we are running
os.environ["USE_POLAR2GRID_DEFAULTS"] = str(int(not is_geo2grid))

rst_epilog += """
.. |project| replace:: {}
.. |script| replace:: {}.sh
.. |script_literal| replace:: ``{}.sh``
.. |project_env| replace:: ${}_HOME
.. |cspp_abbr| replace:: {}
.. |cspp_title| replace:: {}
""".format(
    project,
    project.lower(),
    project.lower(),
    project.upper(),
    "CSPP Geo" if is_geo2grid else "CSPP LEO",
    "CSPP for Geostationary Satellites" if is_geo2grid else "CSPP for Low Earth Orbiter Satellites",
)

# Custom TOCTree filtering
toc_filter_exclude = ["polar2grid"] if is_geo2grid else ["geo2grid"]

copyright = "2012-{:%Y}, University of Wisconsin SSEC".format(datetime.utcnow())

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
# language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
# today = ''
# Else, today_fmt is used as the format for a strftime call.
# today_fmt = '%B %d, %Y'

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = [
    "readers/viirs_edr_flood.rst",  # not advertised
    "grids_list.rst",  # included directly in grids.rst
]
if is_geo2grid:
    exclude_patterns.extend(
        [
            "examples/acspo_example.rst",
            "examples/amsr2_example.rst",
            "examples/modis_example.rst",
            "examples/viirs_example.rst",
            "examples/asci_example.rst",
            "readers/acspo.rst",
            "readers/amsr2_l1b.rst",
            "readers/avhrr.rst",
            "readers/clavrx.rst",
            "readers/mersi2_l1b.rst",
            "readers/mersi_ll_l1b.rst",
            "readers/mirs.rst",
            "readers/modis_l1b.rst",
            "readers/modis_l2.rst",
            "readers/nucaps.rst",
            "readers/viirs_edr_active_fires.rst",
            "readers/viirs_l1b.rst",
            "readers/viirs_sdr.rst",
            "readers/viirs_edr.rst",
            "readers/virr_l1b.rst",
            "verification/modis_verification.rst",
            "verification/viirs_verification.rst",
            "viirs_day_night_band.rst",
            "writers/awips_tiled.rst",
            "writers/binary.rst",
            "writers/hdf5.rst",
            "misc_recipes.rst",
        ]
    )
else:
    exclude_patterns.extend(
        [
            "compositors.rst",
            "data_access.rst",
            "examples/abi_example.rst",
            "examples/abi_l2_example.rst",
            "examples/ahi_example.rst",
            "examples/creating_animations_example.rst",
            "readers/abi_l1b.rst",
            "readers/abi_l2_nc.rst",
            "readers/agri_fy4a_l1.rst",
            "readers/agri_fy4b_l1.rst",
            "readers/ahi_hrit.rst",
            "readers/ahi_hsd.rst",
            "readers/ami_l1b.rst",
            "readers/glm_l2.rst",
            "readers/fci_l1c_nc.rst",
            "verification/abi_verification.rst",
        ]
    )

# The reST default role (used for this markup: `text`) to use for all documents.
# default_role = None

# If true, '()' will be appended to :func: etc. cross-reference text.
# add_function_parentheses = True

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
# add_module_names = True

# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
# show_authors = False

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = "sphinx"

# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []


# -- Options for HTML output ---------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
html_theme = "alabaster"

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
# html_theme_options = {}

# Add any paths that contain custom themes here, relative to this directory.
# html_theme_path = []

# The name for this set of Sphinx documents.  If None, it defaults to
# "<project> v<release> documentation".
# html_title = None

# A shorter title for the navigation bar.  Default is the same as html_title.
# html_short_title = None

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
html_logo = "_static/SSEC_logo_small24.png"

# The name of an image file (within the static path) to use as favicon of the
# docs.  This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
html_favicon = "_static/favicon.ico"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
html_last_updated_fmt = "%b %d, %Y"

# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
html_use_smartypants = False

# Custom sidebar templates, maps document names to template names.
# html_sidebars = {}

# Additional templates that should be rendered to pages, maps page names to
# template names.
# html_additional_pages = {}

# If false, no module index is generated.
# html_domain_indices = True

# If false, no index is generated.
# html_use_index = True

# If true, the index is split into individual pages for each letter.
# html_split_index = False

# If true, links to the reST sources are added to the pages.
# html_show_sourcelink = True

# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
# html_show_sphinx = True

# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
# html_show_copyright = True

# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it.  The value of this option must be the
# base URL from which the finished HTML is served.
# html_use_opensearch = ''

# This is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = None

# Output file base name for HTML help builder.
htmlhelp_basename = project.lower() + "doc"


# -- Options for LaTeX output --------------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    # 'papersize': 'letterpaper',
    # The font size ('10pt', '11pt' or '12pt').
    # 'pointsize': '10pt',
    # Additional stuff for the LaTeX preamble.
    "preamble": r"""
\setcounter{tocdepth}{1}
\usepackage{pdflscape}

\def\ttl@save@mkschap #1{\vspace *{-20\p@ }{\parindent \z@ \raggedright
    \normalfont \interlinepenalty \@M \DOTIS {#1} \vskip -20\p@ }}
\makeatother

""",
    "classoptions": ",openany,oneside",
    "babel": "\\usepackage[english]{babel}",
    "printindex": "",
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]), toctree_only.
if is_geo2grid:
    latex_documents = [
        (
            "index",
            "{}_Documentation_{}.tex".format(project, version),
            "{} Documentation".format(project),
            "NOAA Community Satellite Processing Package \\and for Geostationary Satellites (CSPP Geo)",
            "manual",
            True,
        ),
    ]
else:
    latex_documents = [
        (
            "index",
            "{}_Documentation_{}.tex".format(project, version),
            "{} Documentation".format(project),
            "Released through the \\and NOAA Community Satellite Processing Package (CSPP)",
            "manual",
            True,
        ),
    ]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
latex_logo = "_static/{}2G_PDF_Logos.png".format("G" if is_geo2grid else "P")

# latex_toplevel_sectioning = 'section'

# If true, show page references after internal links.
# latex_show_pagerefs = False

# If true, show URL addresses after external links.
# latex_show_urls = False

# Documents to append as an appendix to all manuals.
if is_geo2grid:
    latex_appendices = [
        # "misc_recipes",
        "design_overview",
    ]
else:
    latex_appendices = [
        "version3_implementation",
        "misc_recipes",
        "design_overview",
        "viirs_day_night_band",
    ]

# If false, no module index is generated.
latex_domain_indices = False

latex_engine = "xelatex"


# -- Options for manual page output --------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [("index", project.lower(), "{} Documentation".format(project), ["David Hoese"], 1)]

# If true, show URL addresses after external links.
# man_show_urls = False


# -- Options for Texinfo output ------------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (
        "index",
        project.lower(),
        "{} Documentation".format(project),
        "David Hoese",
        project.lower(),
        "One line description of project.",
        "Miscellaneous",
    ),
]

# Documents to append as an appendix to all manuals.
# texinfo_appendices = []

# If false, no module index is generated.
# texinfo_domain_indices = True

# How to display URL addresses: 'footnote', 'no', or 'inline'.
# texinfo_show_urls = 'footnote'

intersphinx_mapping = {
    "python": ("https://docs.python.org/3", None),
    "numpy": ("https://numpy.org/doc/stable", None),
    "scipy": ("https://docs.scipy.org/doc/scipy", None),
    "xarray": ("https://docs.xarray.dev/en/stable", None),
    "dask": ("https://docs.dask.org/en/latest", None),
    "pyresample": ("https://pyresample.readthedocs.io/en/stable", None),
    "trollsift": ("https://trollsift.readthedocs.io/en/stable", None),
    "satpy": ("https://satpy.readthedocs.io/en/stable", None),
    "trollimage": ("https://trollimage.readthedocs.io/en/stable", None),
}

# Generate builtin grids list

grid_titles = {
    "wgs84_fit": "WGS84 Dynamic Fit",
    "wgs84_fit_250": "WGS84 Dynamic Fit 250m",
    "lcc_fit": "Lambert Conic Conformal Dynamic Fit",
    "lcc_fit_hr": "High Resolution Lambert Conic Conformal Dynamic Fit",
    "lcc_sa": "Lambert Conic Conformal - South America Centered",
    "lcc_eu": "Lambert Conic Conformal - Europe Centered",
    "lcc_south_africa": "Lambert Conic Conformal - South Africa Centered",
    "lcc_aus": "Lambert Conic Conformal - Australia Centered",
    "lcc_asia": "Lambert Conic Conformal - Asia Centered",
    "polar_north_pacific": "Polar-Stereographic North Pacific",
    "polar_south_pacific": "Polar-Stereographic South Pacific",
    "polar_alaska": "Polar-Stereographic Alaska",
    "polar_canada": "Polar-Stereographic Canada",
    "polar_russia": "Polar-Stereographic Russia",
    "eqc_fit": "Equirectangular Fit",
    "goes_east_1km": "GOES-East 1km",
    "goes_east_4km": "GOES-East 4km",
    "goes_east_8km": "GOES-East 8km",
    "goes_east_10km": "GOES-East 10km",
    "goes_west_1km": "GOES-West 1km",
    "goes_west_4km": "GOES-West 4km",
    "goes_west_8km": "GOES-West 8km",
    "goes_west_10km": "GOES-West 10km",
}

warnings.filterwarnings("ignore", module="pyproj", category=UserWarning)
builtin_areas_filename = os.path.join(script_path, "..", "..", "polar2grid", "grids", "grids.yaml")
with open(builtin_areas_filename, "r") as yaml_file:
    areas_dict = yaml.load(yaml_file, Loader=yaml.SafeLoader)
# builtin_areas = parse_area_file(os.path.join(script_path, "..", "..", "polar2grid", "grids", "grids.yaml"))
grids_list_filename = os.path.join(script_path, "grids_list.rst")
with open(grids_list_filename, "w") as grids_list_file:
    for area_name, area_dict in areas_dict.items():
        area_title = grid_titles.get(area_name)
        if area_title is None:
            continue
        proj = area_dict["projection"]
        crs = CRS.from_user_input(proj["EPSG"] if "EPSG" in proj else proj)
        title_underline = "^" * len(area_title)
        rst_str = f"""
.. _grid_{area_name}:

{area_title}
{title_underline}

:Grid Name: {area_name}
:Description: {area_dict['description']}
:Projection: {crs.to_string()}
"""

        if "resolution" in area_dict:
            res = area_dict["resolution"]
            xres = res["dx"]
            yres = res["dy"]
            def_units = "degrees" if crs.is_geographic else "meters"
            units = res.get("units", def_units)
            if xres != yres:
                rst_str += f":Resolution (X): {xres} {units}\n"
                rst_str += f":Resolution (Y): {yres} {units}\n"
            else:
                rst_str += f":Resolution: {xres} {units}\n"
        if "area_extent" in area_dict:
            rst_str += f":Extent: {area_dict['area_extent']}\n"
        grids_list_file.write(rst_str)
