def downsample_for_multiprocessing(params):

    nwb_path, dset_path, frame_start_i, frame_end_i, dr = params

    print('\tdownsampling frame {} - {}'.format(frame_start_i, frame_end_i))

    ff = h5py.File(nwb_path, 'r')
    chunk = ff[dset_path][frame_start_i:frame_end_i, :, :]
    ff.close()
    chunk_d = ia.z_downsample(chunk, downSampleRate=dr, is_verbose=False)
    return chunk_d
    f_ns.sort()
    print('\n'.join(f_ns))

    mov_join = []
    for f_n in f_ns:
        print('processing plane: {}; file: {} ...'.format(plane_n, f_n))

        curr_mov = tf.imread(os.path.join(plane_folder, f_n))

        if curr_mov.shape[0] % t_downsample_rate != 0:
            print(
                'the frame number of {} ({}) is not divisible by t_downsample_rate ({}).'
                .format(f_n, curr_mov.shape[0], t_downsample_rate))

        curr_mov_d = ia.z_downsample(curr_mov,
                                     downSampleRate=t_downsample_rate,
                                     is_verbose=False)
        mov_join.append(curr_mov_d)

    mov_join = np.concatenate(mov_join, axis=0)

    save_name = '{}_{}_{}_{}_downsampled_for_caiman.hdf5'.format(
        date_recorded, mouse_id, sess_id, plane_n)
    save_f = h5py.File(os.path.join(plane_folder, save_name))
    save_f.create_dataset('mov', data=mov_join)
    save_f.close()

    # save_name = '{}_d1_{}_d2_{}_d3_1_order_C_frames_{}_.mmap'\
    #     .format(base_name, mov_join.shape[2], mov_join.shape[1], mov_join.shape[0])
    #
    # mov_join = mov_join.reshape((mov_join.shape[0], mov_join.shape[1] * mov_join.shape[2]), order='F').transpose()
                                                             td_rate)

            for save_file_id in range(num_file_to_save):
                save_chunk = total_movs[ch_i][save_file_id *
                                              (frames_per_file *
                                               td_rate):(save_file_id + 1) *
                                              (frames_per_file * td_rate)]
                save_path = os.path.join(
                    save_folders[ch_i],
                    '{}_{:05d}_reorged.tif'.format(file_identifier,
                                                   save_ids[ch_i]))
                if td_rate != 1:
                    print('\tdown sampling for {} ...'.format(
                        os.path.split(save_path)[1]))
                    save_chunk = ia.z_downsample(save_chunk,
                                                 downSampleRate=td_rate,
                                                 is_verbose=False)

                print('\tsaving {} ...'.format(os.path.split(save_path)[1]))
                tf.imsave(save_path, save_chunk)
                save_ids[ch_i] = save_ids[ch_i] + 1

            if total_movs[ch_i].shape[0] % (frames_per_file * td_rate) == 0:
                total_movs[ch_i] = None
            else:
                frame_num_left = total_movs[ch_i].shape[0] % (frames_per_file *
                                                              td_rate)
                total_movs[ch_i] = total_movs[ch_i][-frame_num_left:]

print('\nprocessing residual frames ...')
def downsample_folder(working_folder,
                      td_rate,
                      file_identifier,
                      frames_per_file=500):

    file_list = [f for f in os.listdir(working_folder) if file_identifier in f and f[-14:] == '_corrected.tif']
    file_list.sort()
    print('\t\tall files:')
    print('\n'.join(['\t\t' + f for f in file_list]))

    print('\n\t\tmoving files to "not_downsampled" folder:')
    file_paths = [os.path.join(working_folder, f) for f in file_list]
    print

    not_downsampled_folder = os.path.join(working_folder, 'not_downsampled')
    os.mkdir(not_downsampled_folder)
    for file_path in file_paths:
        fn = os.path.split(file_path)[1]
        shutil.move(file_path, os.path.join(not_downsampled_folder, fn))

    file_paths_original = [os.path.join(not_downsampled_folder, fn) for fn in file_list]
    file_paths_original.sort()


    save_id = 0
    total_mov = None
    for file_path_o in file_paths_original:
        print('\t\tprocessing {} ...'.format(os.path.split(file_path_o)[1]))
        curr_mov = tf.imread(file_path_o)

        if total_mov is None:
            total_mov = curr_mov
        else:
            total_mov = np.concatenate((total_mov, curr_mov), axis=0)

        while total_mov is not None and \
                (total_mov.shape[0] >= frames_per_file * td_rate):

            num_file_to_save = total_mov.shape[0] // (frames_per_file * td_rate)

            for save_file_id in range(num_file_to_save):
                save_chunk = total_mov[save_file_id * (frames_per_file * td_rate) :
                                       (save_file_id + 1) * (frames_per_file * td_rate)]
                save_path = os.path.join(working_folder, '{}_{:05d}_corrected_downsampled.tif'.format(file_identifier,
                                                                                                      save_id))
                save_chunk = ia.z_downsample(save_chunk, downSampleRate=td_rate, is_verbose=False)

                print('\t\t\tsaving {} ...'.format(os.path.split(save_path)[1]))
                tf.imsave(save_path, save_chunk)
                save_id = save_id + 1

            if total_mov.shape[0] % (frames_per_file * td_rate) == 0:
                total_mov = None
            else:
                frame_num_left = total_mov.shape[0] % (frames_per_file * td_rate)
                total_mov = total_mov[-frame_num_left:]

    if total_mov is not None:
        save_path = os.path.join(working_folder, '{}_{:05d}_corrected_downsampled.tif'.format(file_identifier, save_id))
        save_chunk = ia.z_downsample(total_mov, downSampleRate=td_rate, is_verbose=False)
        print('\t\t\tsaving {} ...'.format(os.path.split(save_path)[1]))
        tf.imsave(save_path, save_chunk)

    return