def download_file(url, dest=None, chunk_size=1024, replace="ask",
                  label="Downloading {dest_basename} ({size:.2f}MB)",
                  expected_extension=None):
    """Download a file from a given URL and display progress.

    :param dest: If the destination exists and is a directory, the filename
        will be guessed from the Content-Disposition header. If the destination
        is an existing file, the user will either be prompted to overwrite, or
        the file will be replaced (depending on the value of **replace**). If
        the destination does not exist, it will be used as the filename.
    :param int chunk_size: bytes read in at a time.
    :param replace: If `False`, an existing destination file will not be
        overwritten.
    :param label: a string which is formatted and displayed as the progress bar
        label. Variables provided include *dest_basename*, *dest*, and *size*.
    :param expected_extension: if set, the filename will be sanitized to ensure
        it has the given extension. The extension should not start with a dot
        (`.`).
    """
    dest = Path(dest or url.split("/")[-1])
    response = get(url, stream=True)
    if (dest.exists()
            and dest.is_dir()
            and "Content-Disposition" in response.headers):
        content_disposition = rfc6266.parse_requests_response(response)
        if expected_extension is not None:
            filename = content_disposition.filename_sanitized(
                expected_extension)
        filename = secure_filename(filename)
        dest = dest / filename
    if dest.exists() and not dest.is_dir():
        if (replace is False
                or replace == "ask"
                and not click.confirm("Replace {}?".format(dest))):
            return str(dest)
    size = int(response.headers.get("content-length", 0))
    label = label.format(dest=dest, dest_basename=dest.name,
                         size=size/1024.0/1024)
    with click.open_file(str(dest), "wb") as f:
        content_iter = response.iter_content(chunk_size=chunk_size)
        with click.progressbar(content_iter, length=size/1024,
                               label=label) as bar:
            for chunk in bar:
                if chunk:
                    f.write(chunk)
                    f.flush()
    return str(dest)
def _read_soundings(sounding_file_name, sounding_field_names, radar_image_dict):
    """Reads storm-centered soundings and matches w storm-centered radar imgs.

    :param sounding_file_name: Path to input file (will be read by
        `soundings.read_soundings`).
    :param sounding_field_names: See doc for `soundings.read_soundings`.
    :param radar_image_dict: Dictionary created by
        `storm_images.read_storm_images`.
    :return: sounding_dict: Dictionary created by `soundings.read_soundings`.
    :return: radar_image_dict: Same as input, but excluding storm objects with
        no sounding.
    """

    print('Reading data from: "{0:s}"...'.format(sounding_file_name))
    sounding_dict, _ = soundings.read_soundings(
        netcdf_file_name=sounding_file_name,
        field_names_to_keep=sounding_field_names,
        full_id_strings_to_keep=radar_image_dict[storm_images.FULL_IDS_KEY],
        init_times_to_keep_unix_sec=radar_image_dict[
            storm_images.VALID_TIMES_KEY]
    )

    num_examples_with_soundings = len(sounding_dict[soundings.FULL_IDS_KEY])
    if num_examples_with_soundings == 0:
        return None, None

    radar_full_id_strings = numpy.array(
        radar_image_dict[storm_images.FULL_IDS_KEY]
    )
    orig_storm_times_unix_sec = (
        radar_image_dict[storm_images.VALID_TIMES_KEY] + 0
    )

    indices_to_keep = []

    for i in range(num_examples_with_soundings):
        this_index = numpy.where(numpy.logical_and(
            radar_full_id_strings == sounding_dict[soundings.FULL_IDS_KEY][i],
            orig_storm_times_unix_sec ==
            sounding_dict[soundings.INITIAL_TIMES_KEY][i]
        ))[0][0]

        indices_to_keep.append(this_index)

    indices_to_keep = numpy.array(indices_to_keep, dtype=int)
    radar_image_dict[storm_images.STORM_IMAGE_MATRIX_KEY] = radar_image_dict[
        storm_images.STORM_IMAGE_MATRIX_KEY
    ][indices_to_keep, ...]

    radar_image_dict[storm_images.FULL_IDS_KEY] = sounding_dict[
        soundings.FULL_IDS_KEY
    ]
    radar_image_dict[storm_images.VALID_TIMES_KEY] = sounding_dict[
        soundings.INITIAL_TIMES_KEY
    ]

    return sounding_dict, radar_image_dict