def secure_filename(filename):
    r"""Borrowed from :mod:`werkzeug.utils`, under the BSD 3-clause license.

    Pass it a filename and it will return a secure version of it.  This
    filename can then safely be stored on a regular file system and passed
    to :func:`os.path.join`.  The filename returned is an ASCII only string
    for maximum portability.

    On windows systems the function also makes sure that the file is not
    named after one of the special device files.

    >>> secure_filename("My cool movie.mov")
    'My_cool_movie.mov'
    >>> secure_filename("../../../etc/passwd")
    'etc_passwd'
    >>> secure_filename(u'i contain cool \xfcml\xe4uts.txt')
    'i_contain_cool_umlauts.txt'

    The function might return an empty filename.  It's your responsibility
    to ensure that the filename is unique and that you generate random
    filename if the function returned an empty one.

    :param filename: the filename to secure
    """
    _filename_ascii_strip_re = re.compile(r'[^A-Za-z0-9_.-]')
    _windows_device_files = ('CON', 'AUX', 'COM1', 'COM2', 'COM3', 'COM4',
                             'LPT1', 'LPT2', 'LPT3', 'PRN', 'NUL')
    if isinstance(filename, text_type):
        from unicodedata import normalize
        filename = normalize('NFKD', filename).encode('ascii', 'ignore')
        if not PY2:
            filename = filename.decode('ascii')
    for sep in os.path.sep, os.path.altsep:
        if sep:
            filename = filename.replace(sep, ' ')
    filename = str(_filename_ascii_strip_re.sub('', '_'.join(
                   filename.split()))).strip('._')

    # on nt a couple of special files are present in each folder.  We
    # have to ensure that the target file is not such a filename.  In
    # this case we prepend an underline
    if os.name == 'nt' and filename and \
       filename.split('.')[0].upper() in _windows_device_files:
        filename = '_' + filename

    return filename
def _subset_radar_data(
        example_dict, netcdf_dataset_object, example_indices_to_keep,
        field_names_to_keep, heights_to_keep_m_agl, num_rows_to_keep,
        num_columns_to_keep):
    """Subsets radar data by field, height, and horizontal extent.

    If the file contains both 2-D shear images and 3-D reflectivity images (like
    MYRORSS data):

    - `field_names_to_keep` will be interpreted as a list of shear fields to
      keep.  If None, all shear fields will be kept.
    - `heights_to_keep_m_agl` will be interpreted as a list of reflectivity
      heights to keep.  If None, all reflectivity heights will be kept.

    If the file contains only 2-D images, `field_names_to_keep` and
    `heights_to_keep_m_agl` will be considered together, as a list of
    field/height pairs to keep.  If either argument is None, then all
    field-height pairs will be kept.

    If the file contains only 3-D images, `field_names_to_keep` and
    `heights_to_keep_m_agl` will be considered separately:

    - `field_names_to_keep` will be interpreted as a list of fields to keep.  If
        None, all fields will be kept.
    - `heights_to_keep_m_agl` will be interpreted as a list of heights to keep.
      If None, all heights will be kept.

    :param example_dict: See output doc for `_read_metadata_from_example_file`.
    :param netcdf_dataset_object: Same.
    :param example_indices_to_keep: 1-D numpy array with indices of examples
        (storm objects) to keep.  These are examples in `netcdf_dataset_object`
        for which radar data will be added to `example_dict`.
    :param field_names_to_keep: See discussion above.
    :param heights_to_keep_m_agl: See discussion above.
    :param num_rows_to_keep: Number of grid rows to keep.  Images will be
        center-cropped (i.e., rows will be removed from the edges) to meet the
        desired number of rows.  If None, all rows will be kept.
    :param num_columns_to_keep: Same as above but for columns.
    :return: example_dict: Same as input but with the following exceptions.

    [1] Keys "radar_field_names" and "radar_heights_m_agl" may have different
        values.
    [2] If file contains both 2-D and 3-D images, dictionary now contains keys
        "reflectivity_image_matrix_dbz" and "az_shear_image_matrix_s01".
    [3] If file contains only 2-D or only 3-D images, dictionary now contains
        key "radar_image_matrix".
    """

    if field_names_to_keep is None:
        field_names_to_keep = copy.deepcopy(example_dict[RADAR_FIELDS_KEY])
    if heights_to_keep_m_agl is None:
        heights_to_keep_m_agl = example_dict[RADAR_HEIGHTS_KEY] + 0

    error_checking.assert_is_numpy_array(
        numpy.array(field_names_to_keep), num_dimensions=1
    )

    heights_to_keep_m_agl = numpy.round(heights_to_keep_m_agl).astype(int)
    error_checking.assert_is_numpy_array(
        heights_to_keep_m_agl, num_dimensions=1)

    if RADAR_IMAGE_MATRIX_KEY in netcdf_dataset_object.variables:
        radar_matrix = numpy.array(
            netcdf_dataset_object.variables[RADAR_IMAGE_MATRIX_KEY][
                example_indices_to_keep, ...
            ],
            dtype=float
        )

        num_radar_dimensions = len(radar_matrix.shape) - 2

        if num_radar_dimensions == 2:
            these_indices = [
                numpy.where(numpy.logical_and(
                    example_dict[RADAR_FIELDS_KEY] == f,
                    example_dict[RADAR_HEIGHTS_KEY] == h
                ))[0][0]
                for f, h in zip(field_names_to_keep, heights_to_keep_m_agl)
            ]

            these_indices = numpy.array(these_indices, dtype=int)
            radar_matrix = radar_matrix[..., these_indices]
        else:
            these_field_indices = numpy.array([
                example_dict[RADAR_FIELDS_KEY].index(f)
                for f in field_names_to_keep
            ], dtype=int)

            radar_matrix = radar_matrix[..., these_field_indices]

            these_height_indices = numpy.array([
                numpy.where(example_dict[RADAR_HEIGHTS_KEY] == h)[0][0]
                for h in heights_to_keep_m_agl
            ], dtype=int)

            radar_matrix = radar_matrix[..., these_height_indices, :]

        radar_matrix = storm_images.downsize_storm_images(
            storm_image_matrix=radar_matrix,
            radar_field_name=field_names_to_keep[0],
            num_rows_to_keep=num_rows_to_keep,
            num_columns_to_keep=num_columns_to_keep)

        example_dict[RADAR_IMAGE_MATRIX_KEY] = radar_matrix

    else:
        reflectivity_matrix_dbz = numpy.array(
            netcdf_dataset_object.variables[REFL_IMAGE_MATRIX_KEY][
                example_indices_to_keep, ...
            ],
            dtype=float
        )

        reflectivity_matrix_dbz = numpy.expand_dims(
            reflectivity_matrix_dbz, axis=-1
        )

        azimuthal_shear_matrix_s01 = numpy.array(
            netcdf_dataset_object.variables[AZ_SHEAR_IMAGE_MATRIX_KEY][
                example_indices_to_keep, ...
            ],
            dtype=float
        )

        these_height_indices = numpy.array([
            numpy.where(example_dict[RADAR_HEIGHTS_KEY] == h)[0][0]
            for h in heights_to_keep_m_agl
        ], dtype=int)

        reflectivity_matrix_dbz = reflectivity_matrix_dbz[
            ..., these_height_indices, :]

        these_field_indices = numpy.array([
            example_dict[RADAR_FIELDS_KEY].index(f)
            for f in field_names_to_keep
        ], dtype=int)

        azimuthal_shear_matrix_s01 = azimuthal_shear_matrix_s01[
            ..., these_field_indices]

        reflectivity_matrix_dbz = storm_images.downsize_storm_images(
            storm_image_matrix=reflectivity_matrix_dbz,
            radar_field_name=radar_utils.REFL_NAME,
            num_rows_to_keep=num_rows_to_keep,
            num_columns_to_keep=num_columns_to_keep)

        azimuthal_shear_matrix_s01 = storm_images.downsize_storm_images(
            storm_image_matrix=azimuthal_shear_matrix_s01,
            radar_field_name=field_names_to_keep[0],
            num_rows_to_keep=num_rows_to_keep,
            num_columns_to_keep=num_columns_to_keep)

        example_dict[REFL_IMAGE_MATRIX_KEY] = reflectivity_matrix_dbz
        example_dict[AZ_SHEAR_IMAGE_MATRIX_KEY] = azimuthal_shear_matrix_s01

    example_dict[RADAR_FIELDS_KEY] = field_names_to_keep
    example_dict[RADAR_HEIGHTS_KEY] = heights_to_keep_m_agl
    return example_dict