# Path: oggm/exceptions.py
# class InvalidParamsError(ValueError):
#     pass
# 
# class NoInternetException(Exception):
#     pass
# 
# class DownloadVerificationFailedException(Exception):
#     def __init__(self, msg=None, path=None):
#         self.msg = msg
#         self.path = path
# 
# class DownloadCredentialsMissingException(Exception):
#     pass
# 
# class HttpDownloadError(Exception):
#     def __init__(self, code, url):
#         self.code = code
#         self.url = url
# 
# class HttpContentTooShortError(Exception):
#     pass
# 
# class InvalidDEMError(RuntimeError):
#     pass
# 
# class FTPSDownloadError(Exception):
#     def __init__(self, orgerr):
#         self.orgerr = orgerr


import glob
import os
import gzip
import bz2
import hashlib
import shutil
import zipfile
import sys
import math
import logging
import time
import fnmatch
import urllib.request
import urllib.error
import socket
import multiprocessing
import ftplib
import ssl
import tarfile
import pandas as pd
import numpy as np
import shapely.geometry as shpg
import requests
    import geopandas as gpd
    import salem
    import rasterio
import oggm.cfg as cfg
from functools import partial, wraps
from urllib.parse import urlparse
from netrc import netrc
    from salem import wgs84
        from rasterio.merge import merge as merge_tool
        from rasterio.tools.merge import merge as merge_tool
from oggm.exceptions import (InvalidParamsError, NoInternetException,
                             DownloadVerificationFailedException,
                             DownloadCredentialsMissingException,
                             HttpDownloadError, HttpContentTooShortError,
                             InvalidDEMError, FTPSDownloadError)
        from progressbar import DataTransferBar, UnknownLength
        from oggm.utils import robust_tar_extract
    from oggm.utils import tolist
    from oggm.utils import tolist
        if wd is not None and os.path.isdir(wd):
            fb_cache_dir = os.path.join(wd, 'cache')
        else:
            fb_cache_dir = os.path.join(cfg.CACHE_DIR, 'cache')
        check_fb_dir = True

    if not cache_dir:
        # Defaults to working directory: it must be set!
        if not cfg.PATHS['working_dir']:
            raise InvalidParamsError("Need a valid PATHS['working_dir']!")
        cache_dir = fb_cache_dir
        cache_ro = False

    fb_path = os.path.join(fb_cache_dir, cache_obj_name)
    if not reset and os.path.isfile(fb_path):
        return fb_path

    cache_path = os.path.join(cache_dir, cache_obj_name)
    if not reset and os.path.isfile(cache_path):
        return cache_path

    if cache_ro:
        if check_fb_dir:
            # Add a manual check that we are caching sample data download
            if 'oggm-sample-data' not in fb_path:
                raise InvalidParamsError('Attempting to download something '
                                         'with invalid global settings.')
        cache_path = fb_path

    if not cfg.PARAMS['has_internet']:

# Path: oggm/exceptions.py
# class InvalidParamsError(ValueError):
#     pass
# 
# class NoInternetException(Exception):
#     pass
# 
# class DownloadVerificationFailedException(Exception):
#     def __init__(self, msg=None, path=None):
#         self.msg = msg
#         self.path = path
# 
# class DownloadCredentialsMissingException(Exception):
#     pass
# 
# class HttpDownloadError(Exception):
#     def __init__(self, code, url):
#         self.code = code
#         self.url = url
# 
# class HttpContentTooShortError(Exception):
#     pass
# 
# class InvalidDEMError(RuntimeError):
#     pass
# 
# class FTPSDownloadError(Exception):
#     def __init__(self, orgerr):
#         self.orgerr = orgerr


# Path: oggm/utils/_downloads.py
import glob
import os
import gzip
import bz2
import hashlib
import shutil
import zipfile
import sys
import math
import logging
import time
import fnmatch
import urllib.request
import urllib.error
import socket
import multiprocessing
import ftplib
import ssl
import tarfile
import pandas as pd
import numpy as np
import shapely.geometry as shpg
import requests
    import geopandas as gpd
    import salem
    import rasterio
import oggm.cfg as cfg
from functools import partial, wraps
from urllib.parse import urlparse
from netrc import netrc
    from salem import wgs84
        from rasterio.merge import merge as merge_tool
        from rasterio.tools.merge import merge as merge_tool
from oggm.exceptions import (InvalidParamsError, NoInternetException,
                             DownloadVerificationFailedException,
                             DownloadCredentialsMissingException,
                             HttpDownloadError, HttpContentTooShortError,
                             InvalidDEMError, FTPSDownloadError)
        from progressbar import DataTransferBar, UnknownLength
        from oggm.utils import robust_tar_extract
    from oggm.utils import tolist
    from oggm.utils import tolist

        if wd is not None and os.path.isdir(wd):
            fb_cache_dir = os.path.join(wd, 'cache')
        else:
            fb_cache_dir = os.path.join(cfg.CACHE_DIR, 'cache')
        check_fb_dir = True

    if not cache_dir:
        # Defaults to working directory: it must be set!
        if not cfg.PATHS['working_dir']:
            raise InvalidParamsError("Need a valid PATHS['working_dir']!")
        cache_dir = fb_cache_dir
        cache_ro = False

    fb_path = os.path.join(fb_cache_dir, cache_obj_name)
    if not reset and os.path.isfile(fb_path):
        return fb_path

    cache_path = os.path.join(cache_dir, cache_obj_name)
    if not reset and os.path.isfile(cache_path):
        return cache_path

    if cache_ro:
        if check_fb_dir:
            # Add a manual check that we are caching sample data download
            if 'oggm-sample-data' not in fb_path:
                raise InvalidParamsError('Attempting to download something '
                                         'with invalid global settings.')
        cache_path = fb_path

    if not cfg.PARAMS['has_internet']:

        raise NoInternetException("Download required, but "