print('\n'.join(file_ns))

save_folders = []
for ch_n in ch_ns:
    curr_save_folder = os.path.join(data_folder, file_identifier, ch_n)
    if not os.path.isdir(curr_save_folder):
        os.makedirs(curr_save_folder)
    save_folders.append(curr_save_folder)

curr_step = 0

for file_n in file_ns:
    curr_mov = tf.imread(os.path.join(data_folder, file_n))

    # reorient movie
    curr_mov = ia.rigid_transform_cv2_2d(curr_mov[:, ::-1, :], rotation=135)

    curr_frame_num = curr_mov.shape[0] / len(ch_ns)

    if curr_frame_num % frames_per_step != 0:
        raise ValueError(
            '{}: total frame number is not divisible by frames per step.'.
            format(file_n))

    curr_mov_chs = []
    for ch_i in range(len(ch_ns)):
        curr_mov_chs.append(curr_mov[ch_i::len(ch_ns)])

    steps = curr_frame_num // frames_per_step
    for step_ind in range(steps):
                curr_vasmap_ch = (curr_vasmap_ch * 255).astype(np.uint8)
                curr_vasmap_ch = cv2.equalizeHist(curr_vasmap_ch).astype(
                    np.float32)
            vasmaps[ch_n].append(curr_vasmap_ch)

for ch_n, ch_vasmap in vasmaps.items():
    # save_vasmap = np.concatenate(ch_vasmap, axis=0)
    # print(save_vasmap.shape)
    # save_vasmap = ia.array_nor(np.mean(save_vasmap, axis=0))
    # print(save_vasmap.shape)

    save_vasmap = ia.array_nor(np.mean(ch_vasmap, axis=0))

    if scope == 'scientifica':
        save_vasmap_r = save_vasmap[::-1, :]
        save_vasmap_r = ia.rigid_transform_cv2_2d(save_vasmap_r, rotation=135)
    elif scope == 'sutter':
        save_vasmap_r = save_vasmap.transpose()[::-1, :]
    elif scope == 'deepscope':
        save_vasmap_r = ia.rigid_transform_cv2(save_vasmap,
                                               rotation=140)[:, ::-1]
    else:
        raise LookupError(
            "Do not understand scope type. Should be 'sutter' or 'deepscope' or 'scientifica'."
        )

    tf.imsave('{}_{}.tif'.format(identifier, ch_n),
              save_vasmap.astype(np.float32))
    tf.imsave('{}_{}_rotated.tif'.format(identifier, ch_n),
              save_vasmap_r.astype(np.float32))