# Path: astroNN/gaia/gaia_shared.py
# def gaia_env():
#     """
#     Get Gaia environment variable
# 
#     :return: Path to Gaia Data
#     :rtype: str
#     :History: 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#     """
#     from astroNN.config import ENVVAR_WARN_FLAG
#     _GAIA = os.getenv('GAIA_TOOLS_DATA')
#     if _GAIA is None and ENVVAR_WARN_FLAG is True:
#         print("WARNING! Gaia environment variable GAIA_TOOLS_DATA not set")
#     return _GAIA
# 
# def gaia_default_dr(dr=None):
#     """
#     Check if dr argument is provided, if none then use default
# 
#     :param dr: Gaia DR
#     :type dr: Union[NoneType, int]
#     :return: Gaia DR
#     :rtype: int
#     :History: 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#     """
#     if dr is None:
#         dr = 2
#         print(f'dr is not provided, using default dr={dr}')
#     else:
#         pass
#     return dr
# 
# Path: astroNN/shared/warnings.py
# def deprecated(func):
#     """
#     This is a decorator which can be used to mark functions
#     as deprecated. It will result in a warning being emitted
#     when the function is used.
#     """
# 
#     @functools.wraps(func)
#     def new_func(*args, **kwargs):
#         if inspect.isclass(func):
#             warnings.warn(f"The use of class {func.__name__}() is deprecated. It will be removed in future",
#                           category=DeprecationWarning, stacklevel=2)
#         else:
#             warnings.warn(f"Call to function {func.__name__}() is deprecated. It will be removed in future",
#                           category=DeprecationWarning, stacklevel=2)
#         return func(*args, **kwargs)
# 
#     return new_func
# 
# 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()


import os
import urllib.request
import numpy as np
import astroNN
import astroNN.data
from astropy.io import fits
from astroNN.gaia.gaia_shared import gaia_env, gaia_default_dr
from astroNN.shared.warnings import deprecated
from astroNN.shared.downloader_tools import TqdmUpTo, filehash
    """
    dr = gaia_default_dr(dr=dr)
    fulllist = []

    if dr == 1:

        # Check if directory exists
        folderpath = os.path.join(gaia_env(), 'Gaia/gdr1/gaia_source/fits/')
        urlbase = 'http://cdn.gea.esac.esa.int/Gaia/gdr1/gaia_source/fits/'

        if not os.path.exists(folderpath):
            os.makedirs(folderpath)

        hash_filename = 'MD5SUM.txt'
        full_hash_filename = os.path.join(folderpath, hash_filename)
        if not os.path.isfile(full_hash_filename):
            urllib.request.urlretrieve(urlbase + hash_filename, full_hash_filename)

        hash_list = np.loadtxt(full_hash_filename, dtype='str').T

        for j in range(0, 20, 1):
            for i in range(0, 256, 1):
                filename = f'GaiaSource_000-0{j:0{2}d}-{i:0{3}d}.fits'
                urlstr = urlbase + filename

                fullfilename = os.path.join(folderpath, filename)
                file_hash = (hash_list[0])[np.argwhere(hash_list[1] == filename)]

                # Check if files exists
                if os.path.isfile(fullfilename) and flag is None:

# Path: astroNN/gaia/gaia_shared.py
# def gaia_env():
#     """
#     Get Gaia environment variable
# 
#     :return: Path to Gaia Data
#     :rtype: str
#     :History: 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#     """
#     from astroNN.config import ENVVAR_WARN_FLAG
#     _GAIA = os.getenv('GAIA_TOOLS_DATA')
#     if _GAIA is None and ENVVAR_WARN_FLAG is True:
#         print("WARNING! Gaia environment variable GAIA_TOOLS_DATA not set")
#     return _GAIA
# 
# def gaia_default_dr(dr=None):
#     """
#     Check if dr argument is provided, if none then use default
# 
#     :param dr: Gaia DR
#     :type dr: Union[NoneType, int]
#     :return: Gaia DR
#     :rtype: int
#     :History: 2017-Oct-26 - Written - Henry Leung (University of Toronto)
#     """
#     if dr is None:
#         dr = 2
#         print(f'dr is not provided, using default dr={dr}')
#     else:
#         pass
#     return dr
# 
# Path: astroNN/shared/warnings.py
# def deprecated(func):
#     """
#     This is a decorator which can be used to mark functions
#     as deprecated. It will result in a warning being emitted
#     when the function is used.
#     """
# 
#     @functools.wraps(func)
#     def new_func(*args, **kwargs):
#         if inspect.isclass(func):
#             warnings.warn(f"The use of class {func.__name__}() is deprecated. It will be removed in future",
#                           category=DeprecationWarning, stacklevel=2)
#         else:
#             warnings.warn(f"Call to function {func.__name__}() is deprecated. It will be removed in future",
#                           category=DeprecationWarning, stacklevel=2)
#         return func(*args, **kwargs)
# 
#     return new_func
# 
# 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/gaia/downloader.py
import os
import urllib.request
import numpy as np
import astroNN
import astroNN.data
from astropy.io import fits
from astroNN.gaia.gaia_shared import gaia_env, gaia_default_dr
from astroNN.shared.warnings import deprecated
from astroNN.shared.downloader_tools import TqdmUpTo, filehash

    """
    dr = gaia_default_dr(dr=dr)
    fulllist = []

    if dr == 1:

        # Check if directory exists
        folderpath = os.path.join(gaia_env(), 'Gaia/gdr1/gaia_source/fits/')
        urlbase = 'http://cdn.gea.esac.esa.int/Gaia/gdr1/gaia_source/fits/'

        if not os.path.exists(folderpath):
            os.makedirs(folderpath)

        hash_filename = 'MD5SUM.txt'
        full_hash_filename = os.path.join(folderpath, hash_filename)
        if not os.path.isfile(full_hash_filename):
            urllib.request.urlretrieve(urlbase + hash_filename, full_hash_filename)

        hash_list = np.loadtxt(full_hash_filename, dtype='str').T

        for j in range(0, 20, 1):
            for i in range(0, 256, 1):
                filename = f'GaiaSource_000-0{j:0{2}d}-{i:0{3}d}.fits'
                urlstr = urlbase + filename

                fullfilename = os.path.join(folderpath, filename)
                file_hash = (hash_list[0])[np.argwhere(hash_list[1] == filename)]

                # Check if files exists
                if os.path.isfile(fullfilename) and flag is None:

                    checksum = filehash(fullfilename, algorithm='md5')