# Path: astroNN/apogee/apogee_shared.py
# def apogee_env():
#     """
#     Get APOGEE environment variable
# 
#     :return: path to APOGEE local dir
#     :rtype: str
#     :History: 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#     """
#     from astroNN.config import ENVVAR_WARN_FLAG
#     _APOGEE = os.getenv('SDSS_LOCAL_SAS_MIRROR')
#     if _APOGEE is None and ENVVAR_WARN_FLAG is True:
#         print("WARNING! APOGEE environment variable SDSS_LOCAL_SAS_MIRROR not set")
# 
#     return _APOGEE
# 
# def apogee_default_dr(dr=None):
#     """
#     Check if dr argument is provided, if none then use default
# 
#     :param dr: APOGEE DR
#     :type dr: int
#     :return: APOGEE DR
#     :rtype: int
#     :History:
#         | 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#         | 2018-Sept-08 - Updated - Henry Leung (University of Toronto)
#     """
#     if dr == 15:
#         print("SDSS APOGEE DR15 is equivalent to DR14, so astroNN is using DR14 even you set DR15")
#         dr = 14
# 
#     if dr is None:
#         try:
#             redux_ver = os.environ["RESULTS_VERS"]  # RESULTS_VERS is from Jo Bovy APOGEE Tool
#             if redux_ver == "v402":
#                 dr = 11
#             elif redux_ver == "v603":
#                 dr = 12
#             elif redux_ver == "l30e.2":
#                 dr = 13
#             elif redux_ver == "l31c.2":
#                 dr = 14
#             elif redux_ver == "l33":
#                 dr = 16
#         except KeyError:
#             pass
# 
#         if dr is None:  # if it is still None
#             dr = 16
#             print(f'dr is not provided, using default dr={dr}')
#     else:
#         pass
# 
#     return dr
# 
# Path: astroNN/shared/downloader_tools.py
# class TqdmUpTo(tqdm):
#     """
#     NAME:
#         sha256_checksum
#     PURPOSE:
#         Provides `update_to(n)` which uses `tqdm.update(delta_n)`.
#     INPUT:
#     OUTPUT:
#     HISTORY:
#         2017-Oct-25 - Written - Henry Leung (University of Toronto)
#     """
# 
#     def update_to(self, b=1, bsize=1, tsize=None):
#         """
#         b  : int, optional
#             Number of blocks transferred so far [default: 1].
#         bsize  : int, optional
#             Size of each block (in tqdm units) [default: 1].
#         tsize  : int, optional
#             Total size (in tqdm units). If [default: None] remains unchanged.
#         """
#         if tsize is not None:
#             self.total = tsize
#         self.update(b * bsize - self.n)  # will also set self.n = b * bsize
# 
# def filehash(filename, block_size=65536, algorithm='sha256'):
#     """
#     Computes the hash value for a file by using a specified hash algorithm.
# 
#     :param filename: filename
#     :type filename: str
#     :param block_size: blocksize used to compute file hash
#     :type block_size: int
#     :param algorithm: hash algorithms like 'sha256' or 'md5' etc.
#     :type algorithm: str
#     :return: None
#     :History: 2019-Mar-12 - Written - Henry Leung (University of Toronto)
#     """
#     algorithm = algorithm.lower()
#     if algorithm not in hashlib.algorithms_guaranteed:
#         raise ValueError(f"{algorithm} is an unsupported hashing algorithm")
# 
#     func_algorithm = getattr(hashlib, algorithm)()
#     with open(filename, 'rb') as f:
#         for block in iter(lambda: f.read(block_size), b''):
#             func_algorithm.update(block)
#     return func_algorithm.hexdigest()
# 
# Path: astroNN/shared/logging.py
# def get_logger():
# def log(level, msg, *args, **kwargs):
# def debug(msg, *args, **kwargs):
# def error(msg, *args, **kwargs):
# def fatal(msg, *args, **kwargs):
# def info(msg, *args, **kwargs):
# def warn(msg, *args, **kwargs):


import getpass
import os
import urllib.request, urllib.error
import warnings
import numpy as np
from astroNN.apogee.apogee_shared import apogee_env, apogee_default_dr
from astroNN.shared.downloader_tools import TqdmUpTo, filehash
from astropy.io import fits
from astroNN.shared import logging as logging
        filename = f"apogee-rc-DR{dr}.fits"
        urlstr = str1 + filename
        fullfoldername = os.path.join(apogee_env(), "dr17/apogee/vac/apogee-rc/cat/")
        if not os.path.exists(fullfoldername):
            os.makedirs(fullfoldername)
        fullfilename = os.path.join(fullfoldername, filename)

    else:
        raise ValueError("apogee_rc() only supports APOGEE DR13-DR17")

    # check file integrity
    if os.path.isfile(fullfilename) and flag is None:
        checksum = filehash(fullfilename, algorithm="sha1")
        if checksum != file_hash.lower():
            warnings.warn(
                "File corruption detected, astroNN is attempting to download again"
            )
            apogee_rc(dr=dr, flag=1)
        else:
            logging.info(fullfilename + " was found!")

    elif not os.path.isfile(fullfilename) or flag == 1:
        try:
            with TqdmUpTo(
                unit="B", unit_scale=True, miniters=1, desc=urlstr.split("/")[-1]
            ) as t:
                urllib.request.urlretrieve(urlstr, fullfilename, reporthook=t.update_to)
                logging.info(
                    f"Downloaded DR{dr} Red Clumps Catalog successfully to {fullfilename}"
                )

# Path: astroNN/apogee/apogee_shared.py
# def apogee_env():
#     """
#     Get APOGEE environment variable
# 
#     :return: path to APOGEE local dir
#     :rtype: str
#     :History: 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#     """
#     from astroNN.config import ENVVAR_WARN_FLAG
#     _APOGEE = os.getenv('SDSS_LOCAL_SAS_MIRROR')
#     if _APOGEE is None and ENVVAR_WARN_FLAG is True:
#         print("WARNING! APOGEE environment variable SDSS_LOCAL_SAS_MIRROR not set")
# 
#     return _APOGEE
# 
# def apogee_default_dr(dr=None):
#     """
#     Check if dr argument is provided, if none then use default
# 
#     :param dr: APOGEE DR
#     :type dr: int
#     :return: APOGEE DR
#     :rtype: int
#     :History:
#         | 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#         | 2018-Sept-08 - Updated - Henry Leung (University of Toronto)
#     """
#     if dr == 15:
#         print("SDSS APOGEE DR15 is equivalent to DR14, so astroNN is using DR14 even you set DR15")
#         dr = 14
# 
#     if dr is None:
#         try:
#             redux_ver = os.environ["RESULTS_VERS"]  # RESULTS_VERS is from Jo Bovy APOGEE Tool
#             if redux_ver == "v402":
#                 dr = 11
#             elif redux_ver == "v603":
#                 dr = 12
#             elif redux_ver == "l30e.2":
#                 dr = 13
#             elif redux_ver == "l31c.2":
#                 dr = 14
#             elif redux_ver == "l33":
#                 dr = 16
#         except KeyError:
#             pass
# 
#         if dr is None:  # if it is still None
#             dr = 16
#             print(f'dr is not provided, using default dr={dr}')
#     else:
#         pass
# 
#     return dr
# 
# Path: astroNN/shared/downloader_tools.py
# class TqdmUpTo(tqdm):
#     """
#     NAME:
#         sha256_checksum
#     PURPOSE:
#         Provides `update_to(n)` which uses `tqdm.update(delta_n)`.
#     INPUT:
#     OUTPUT:
#     HISTORY:
#         2017-Oct-25 - Written - Henry Leung (University of Toronto)
#     """
# 
#     def update_to(self, b=1, bsize=1, tsize=None):
#         """
#         b  : int, optional
#             Number of blocks transferred so far [default: 1].
#         bsize  : int, optional
#             Size of each block (in tqdm units) [default: 1].
#         tsize  : int, optional
#             Total size (in tqdm units). If [default: None] remains unchanged.
#         """
#         if tsize is not None:
#             self.total = tsize
#         self.update(b * bsize - self.n)  # will also set self.n = b * bsize
# 
# def filehash(filename, block_size=65536, algorithm='sha256'):
#     """
#     Computes the hash value for a file by using a specified hash algorithm.
# 
#     :param filename: filename
#     :type filename: str
#     :param block_size: blocksize used to compute file hash
#     :type block_size: int
#     :param algorithm: hash algorithms like 'sha256' or 'md5' etc.
#     :type algorithm: str
#     :return: None
#     :History: 2019-Mar-12 - Written - Henry Leung (University of Toronto)
#     """
#     algorithm = algorithm.lower()
#     if algorithm not in hashlib.algorithms_guaranteed:
#         raise ValueError(f"{algorithm} is an unsupported hashing algorithm")
# 
#     func_algorithm = getattr(hashlib, algorithm)()
#     with open(filename, 'rb') as f:
#         for block in iter(lambda: f.read(block_size), b''):
#             func_algorithm.update(block)
#     return func_algorithm.hexdigest()
# 
# Path: astroNN/shared/logging.py
# def get_logger():
# def log(level, msg, *args, **kwargs):
# def debug(msg, *args, **kwargs):
# def error(msg, *args, **kwargs):
# def fatal(msg, *args, **kwargs):
# def info(msg, *args, **kwargs):
# def warn(msg, *args, **kwargs):


# Path: astroNN/apogee/downloader.py
import getpass
import os
import urllib.request, urllib.error
import warnings
import numpy as np
from astroNN.apogee.apogee_shared import apogee_env, apogee_default_dr
from astroNN.shared.downloader_tools import TqdmUpTo, filehash
from astropy.io import fits
from astroNN.shared import logging as logging

        filename = f"apogee-rc-DR{dr}.fits"
        urlstr = str1 + filename
        fullfoldername = os.path.join(apogee_env(), "dr17/apogee/vac/apogee-rc/cat/")
        if not os.path.exists(fullfoldername):
            os.makedirs(fullfoldername)
        fullfilename = os.path.join(fullfoldername, filename)

    else:
        raise ValueError("apogee_rc() only supports APOGEE DR13-DR17")

    # check file integrity
    if os.path.isfile(fullfilename) and flag is None:
        checksum = filehash(fullfilename, algorithm="sha1")
        if checksum != file_hash.lower():
            warnings.warn(
                "File corruption detected, astroNN is attempting to download again"
            )
            apogee_rc(dr=dr, flag=1)
        else:
            logging.info(fullfilename + " was found!")

    elif not os.path.isfile(fullfilename) or flag == 1:
        try:
            with TqdmUpTo(
                unit="B", unit_scale=True, miniters=1, desc=urlstr.split("/")[-1]
            ) as t:
                urllib.request.urlretrieve(urlstr, fullfilename, reporthook=t.update_to)
                logging.info(
                    f"Downloaded DR{dr} Red Clumps Catalog successfully to {fullfilename}"
                )

                checksum = filehash(fullfilename, algorithm="sha1")