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 reduce_examples_3d_to_2d(example_dict, list_of_operation_dicts):
    """Reduces examples from 3-D to 2-D.

    If the examples contain both 2-D azimuthal-shear images and 3-D
    reflectivity images:

    - Keys "reflectivity_image_matrix_dbz" and "az_shear_image_matrix_s01" are
      required.
    - "radar_heights_m_agl" should contain only reflectivity heights.
    - "radar_field_names" should contain only the names of azimuthal-shear
      fields.

    If the examples contain 3-D radar images and no 2-D images:

    - Key "radar_image_matrix" is required.
    - Each field in "radar_field_names" appears at each height in
      "radar_heights_m_agl".
    - Thus, there are F_r elements in "radar_field_names", H_r elements in
      "radar_heights_m_agl", and F_r * H_r field-height pairs.

    After dimensionality reduction (from 3-D to 2-D):

    - Keys "reflectivity_image_matrix_dbz", "az_shear_image_matrix_s01", and
      "radar_heights_m_agl" will be absent.
    - Key "radar_image_matrix" will be present.  The dimensions will be
      E x M x N x C.
    - Key "radar_field_names" will be a length-C list, where the [j]th item is
      the field name for the [j]th channel of radar_image_matrix
      (radar_image_matrix[..., j]).
    - Key "min_radar_heights_m_agl" will be a length-C numpy array, where the
      [j]th item is the MINIMUM height for the [j]th channel of
      radar_image_matrix.
    - Key "max_radar_heights_m_agl" will be a length-C numpy array, where the
      [j]th item is the MAX height for the [j]th channel of radar_image_matrix.
    - Key "radar_layer_operation_names" will be a length-C list, where the [j]th
      item is the name of the operation used to create the [j]th channel of
      radar_image_matrix.

    :param example_dict: See doc for `write_example_file`.
    :param list_of_operation_dicts: See doc for `_check_layer_operation`.
    :return: example_dict: See general discussion above, for how the input
        `example_dict` is changed to the output `example_dict`.
    """

    if RADAR_IMAGE_MATRIX_KEY in example_dict:
        num_radar_dimensions = len(
            example_dict[RADAR_IMAGE_MATRIX_KEY].shape
        ) - 2

        assert num_radar_dimensions == 3

    new_radar_image_matrix = None
    new_field_names = []
    new_min_heights_m_agl = []
    new_max_heights_m_agl = []
    new_operation_names = []

    if AZ_SHEAR_IMAGE_MATRIX_KEY in example_dict:
        new_radar_image_matrix = example_dict[AZ_SHEAR_IMAGE_MATRIX_KEY] + 0.

        for this_field_name in example_dict[RADAR_FIELDS_KEY]:
            new_field_names.append(this_field_name)
            new_operation_names.append(MAX_OPERATION_NAME)

            if this_field_name == radar_utils.LOW_LEVEL_SHEAR_NAME:
                new_min_heights_m_agl.append(0)
                new_max_heights_m_agl.append(2000)
            else:
                new_min_heights_m_agl.append(3000)
                new_max_heights_m_agl.append(6000)

    for this_operation_dict in list_of_operation_dicts:
        this_new_matrix, this_operation_dict = _apply_layer_operation(
            example_dict=example_dict, operation_dict=this_operation_dict)

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

        if new_radar_image_matrix is None:
            new_radar_image_matrix = this_new_matrix + 0.
        else:
            new_radar_image_matrix = numpy.concatenate(
                (new_radar_image_matrix, this_new_matrix), axis=-1
            )

        new_field_names.append(this_operation_dict[RADAR_FIELD_KEY])
        new_min_heights_m_agl.append(this_operation_dict[MIN_HEIGHT_KEY])
        new_max_heights_m_agl.append(this_operation_dict[MAX_HEIGHT_KEY])
        new_operation_names.append(this_operation_dict[OPERATION_NAME_KEY])

    example_dict.pop(REFL_IMAGE_MATRIX_KEY, None)
    example_dict.pop(AZ_SHEAR_IMAGE_MATRIX_KEY, None)
    example_dict.pop(RADAR_HEIGHTS_KEY, None)

    example_dict[RADAR_IMAGE_MATRIX_KEY] = new_radar_image_matrix
    example_dict[RADAR_FIELDS_KEY] = new_field_names
    example_dict[MIN_RADAR_HEIGHTS_KEY] = numpy.array(
        new_min_heights_m_agl, dtype=int)
    example_dict[MAX_RADAR_HEIGHTS_KEY] = numpy.array(
        new_max_heights_m_agl, dtype=int)
    example_dict[RADAR_LAYER_OPERATION_NAMES_KEY] = new_operation_names

    return example_dict