def test_get_all_elections_xlsx(self):
        response = self.client.get(reverse('results-export'))

        self.assertEqual(response.status_code, 200)

        self.assertEqual(
            response['Content-Disposition'],
            'attachment; filename="Election Results.xlsx"'
        )

        wb = openpyxl.load_workbook(io.BytesIO(response.content))

        self.assertEqual(len(wb.worksheets), 2)

        # Check first worksheet.
        ws = wb.worksheets[0]

        self.assertEqual(wb.sheetnames[0], 'Election 0')

        row_count = ws.max_row
        col_count = ws.max_column
        self.assertEqual(row_count, 25)
        self.assertEqual(col_count, 5)

        self.assertEqual(str(ws.cell(1, 1).value), 'Election 0 Results')

        self.assertEqual(str(ws.cell(2, 1).value), 'Candidates')

        cellContents = [
            'Position 0',
            'Party 0',
            '0, 0',
            'Party 1',
            '3, 3',
            'Party 2',
            'None',
            'Position 1',
            'Party 0',
            '1, 1',
            'Party 1',
            '4, 4',
            'Party 2',
            'None',
            'Position 2',
            'Party 0',
            '2, 2',
            'Party 1',
            '5, 5',
            'Party 2',
            'None'
        ]
        for cellIndex, content in enumerate(cellContents, 5):
            self.assertEqual(str(ws.cell(cellIndex, 1).value), content) 

        self.assertEqual(str(ws.cell(2, 2).value), 'Number of Votes')

        self.assertEqual(str(ws.cell(3, 2).value), '0')

        self.assertEqual(str(ws.cell(4, 2).value), '0') # Section

        self.assertEqual(str(ws.cell(7, 2).value), '1')
        self.assertEqual(str(ws.cell(9, 2).value), '0')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 2).value), '2')
        self.assertEqual(str(ws.cell(16, 2).value), '0')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 2).value), '0')
        self.assertEqual(str(ws.cell(23, 2).value), '0')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')

        self.assertEqual(str(ws.cell(4, 3).value), '1') # Section

        self.assertEqual(str(ws.cell(7, 3).value), '0')
        self.assertEqual(str(ws.cell(9, 3).value), '1')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 3).value), '0')
        self.assertEqual(str(ws.cell(16, 3).value), '0')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 3).value), '1')
        self.assertEqual(str(ws.cell(23, 3).value), '0')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')

        self.assertEqual(str(ws.cell(3, 4).value), '1')

        self.assertEqual(str(ws.cell(4, 4).value), '2') # Section

        self.assertEqual(str(ws.cell(7, 4).value), '0')
        self.assertEqual(str(ws.cell(9, 4).value), '0')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 4).value), '0')
        self.assertEqual(str(ws.cell(16, 4).value), '1')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 4).value), '0')
        self.assertEqual(str(ws.cell(23, 4).value), '1')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')

        self.assertEqual(str(ws.cell(3, 5).value), 'Total Votes')
        self.assertEqual(str(ws.cell(7, 5).value), '1')
        self.assertEqual(str(ws.cell(9, 5).value), '1')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 5).value), '2')
        self.assertEqual(str(ws.cell(16, 5).value), '1')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 5).value), '1')
        self.assertEqual(str(ws.cell(23, 5).value), '1')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')

        # Check second worksheet.
        ws = wb.worksheets[1]

        self.assertEqual(wb.sheetnames[1], 'Election 1')

        row_count = ws.max_row
        col_count = ws.max_column
        self.assertEqual(row_count, 25)
        self.assertEqual(col_count, 5)

        self.assertEqual(str(ws.cell(1, 1).value), 'Election 1 Results')

        self.assertEqual(str(ws.cell(2, 1).value), 'Candidates')

        self.assertEqual(str(ws.cell(2, 1).value), 'Candidates')

        cellContents = [
            'Position 3',
            'Party 3',
            '6, 6',
            'Party 4',
            '9, 9',
            'Party 5',
            'None',
            'Position 4',
            'Party 3',
            '7, 7',
            'Party 4',
            '10, 10',
            'Party 5',
            'None',
            'Position 5',
            'Party 3',
            '8, 8',
            'Party 4',
            '11, 11',
            'Party 5',
            'None'
        ]
        for cellIndex, content in enumerate(cellContents, 5):
            self.assertEqual(str(ws.cell(cellIndex, 1).value), content) 

        self.assertEqual(str(ws.cell(2, 2).value), 'Number of Votes')

        self.assertEqual(str(ws.cell(3, 2).value), '2')

        self.assertEqual(str(ws.cell(4, 2).value), '3') # Section

        self.assertEqual(str(ws.cell(7, 2).value), '1')
        self.assertEqual(str(ws.cell(9, 2).value), '0')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 2).value), '1')
        self.assertEqual(str(ws.cell(16, 2).value), '0')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 2).value), '0')
        self.assertEqual(str(ws.cell(23, 2).value), '0')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')

        self.assertEqual(str(ws.cell(4, 3).value), '4') # Section

        self.assertEqual(str(ws.cell(7, 3).value), '0')
        self.assertEqual(str(ws.cell(9, 3).value), '1')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 3).value), '0')
        self.assertEqual(str(ws.cell(16, 3).value), '0')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 3).value), '1')
        self.assertEqual(str(ws.cell(23, 3).value), '0')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')

        self.assertEqual(str(ws.cell(3, 4).value), '3')

        self.assertEqual(str(ws.cell(4, 4).value), '5') # Section

        self.assertEqual(str(ws.cell(7, 4).value), '0')
        self.assertEqual(str(ws.cell(9, 4).value), '0')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 4).value), '0')
        self.assertEqual(str(ws.cell(16, 4).value), '1')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 4).value), '0')
        self.assertEqual(str(ws.cell(23, 4).value), '1')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')

        self.assertEqual(str(ws.cell(3, 5).value), 'Total Votes')
        self.assertEqual(str(ws.cell(7, 5).value), '1')
        self.assertEqual(str(ws.cell(9, 5).value), '1')
        self.assertEqual(str(ws.cell(11, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(14, 5).value), '1')
        self.assertEqual(str(ws.cell(16, 5).value), '1')
        self.assertEqual(str(ws.cell(18, 2).value), 'N/A')
        self.assertEqual(str(ws.cell(21, 5).value), '1')
        self.assertEqual(str(ws.cell(23, 5).value), '1')
        self.assertEqual(str(ws.cell(25, 2).value), 'N/A')
def _create_2d_examples(
        radar_file_names, full_id_strings, storm_times_unix_sec,
        target_matrix, sounding_file_name=None, sounding_field_names=None):
    """Creates 2-D examples for one file time.

    E = number of desired examples (storm objects)
    e = number of examples returned
    T = number of target variables

    :param radar_file_names: length-C list of paths to storm-centered radar
        images.  Files will be read by `storm_images.read_storm_images`.
    :param full_id_strings: length-E list with full IDs of storm objects to
        return.
    :param storm_times_unix_sec: length-E numpy array with valid times of storm
        objects to return.
    :param target_matrix: E-by-T numpy array of target values (integer class
        labels).
    :param sounding_file_name: Path to sounding file (will be read by
        `soundings.read_soundings`).  If `sounding_file_name is None`, examples
        will not include soundings.
    :param sounding_field_names: See doc for `soundings.read_soundings`.
    :return: example_dict: Same as input for `write_example_file`, but without
        key "target_names".
    """

    orig_full_id_strings = copy.deepcopy(full_id_strings)
    orig_storm_times_unix_sec = storm_times_unix_sec + 0

    print('Reading data from: "{0:s}"...'.format(radar_file_names[0]))
    this_radar_image_dict = storm_images.read_storm_images(
        netcdf_file_name=radar_file_names[0],
        full_id_strings_to_keep=full_id_strings,
        valid_times_to_keep_unix_sec=storm_times_unix_sec)

    if this_radar_image_dict is None:
        return None

    if sounding_file_name is None:
        sounding_matrix = None
        sounding_field_names = None
        sounding_heights_m_agl = None
    else:
        sounding_dict, this_radar_image_dict = _read_soundings(
            sounding_file_name=sounding_file_name,
            sounding_field_names=sounding_field_names,
            radar_image_dict=this_radar_image_dict)

        if this_radar_image_dict is None:
            return None
        if len(this_radar_image_dict[storm_images.FULL_IDS_KEY]) == 0:
            return None

        sounding_matrix = sounding_dict[soundings.SOUNDING_MATRIX_KEY]
        sounding_field_names = sounding_dict[soundings.FIELD_NAMES_KEY]
        sounding_heights_m_agl = sounding_dict[soundings.HEIGHT_LEVELS_KEY]

    full_id_strings = this_radar_image_dict[storm_images.FULL_IDS_KEY]
    storm_times_unix_sec = this_radar_image_dict[storm_images.VALID_TIMES_KEY]

    these_indices = tracking_utils.find_storm_objects(
        all_id_strings=orig_full_id_strings,
        all_times_unix_sec=orig_storm_times_unix_sec,
        id_strings_to_keep=full_id_strings,
        times_to_keep_unix_sec=storm_times_unix_sec, allow_missing=False)

    target_matrix = target_matrix[these_indices, :]

    num_channels = len(radar_file_names)
    tuple_of_image_matrices = ()

    for j in range(num_channels):
        if j != 0:
            print('Reading data from: "{0:s}"...'.format(radar_file_names[j]))
            this_radar_image_dict = storm_images.read_storm_images(
                netcdf_file_name=radar_file_names[j],
                full_id_strings_to_keep=full_id_strings,
                valid_times_to_keep_unix_sec=storm_times_unix_sec)

        tuple_of_image_matrices += (
            this_radar_image_dict[storm_images.STORM_IMAGE_MATRIX_KEY],
        )

    radar_field_names = [
        storm_images.image_file_name_to_field(f) for f in radar_file_names
    ]
    radar_heights_m_agl = numpy.array(
        [storm_images.image_file_name_to_height(f) for f in radar_file_names],
        dtype=int
    )

    example_dict = {
        FULL_IDS_KEY: full_id_strings,
        STORM_TIMES_KEY: storm_times_unix_sec,
        RADAR_FIELDS_KEY: radar_field_names,
        RADAR_HEIGHTS_KEY: radar_heights_m_agl,
        ROTATED_GRIDS_KEY:
            this_radar_image_dict[storm_images.ROTATED_GRIDS_KEY],
        ROTATED_GRID_SPACING_KEY:
            this_radar_image_dict[storm_images.ROTATED_GRID_SPACING_KEY],
        RADAR_IMAGE_MATRIX_KEY: dl_utils.stack_radar_fields(
            tuple_of_image_matrices),
        TARGET_MATRIX_KEY: target_matrix
    }

    if sounding_file_name is not None:
        example_dict.update({
            SOUNDING_FIELDS_KEY: sounding_field_names,
            SOUNDING_HEIGHTS_KEY: sounding_heights_m_agl,
            SOUNDING_MATRIX_KEY: sounding_matrix
        })

    return example_dict