

def main():
    fsloutputdir = 'export FSLOUTPUTTYPE=NIFTI_GZ; '
    file_ordering = 'alternate'
    start_time = time.time()
    parser = Parser(__file__)
    parser.usage.set_description('Convert dcm2nii using nin sequence from J. Finsterbusch. Requires the software dcm2nii (from mricron).')
    parser.add_option(name='-i', type_value='folder', description='Path to dicom data.', mandatory=True, example='data/my_data')
    parser.add_option(name='-ord', type_value='multiple_choice', description='File ordering: \nalternate: spine,brain,spine,brain... (with custom coil)\nbloc: spine,spine... brain,brain... (with head-neck coil)\n', mandatory=False, default_value='bloc', example=['alternate', 'bloc'])
    parser.add_option(name='-r', type_value='multiple_choice', description='Remove temporary files.', mandatory=False, default_value='1', example=['0', '1'])
    parser.add_option(name='-v', type_value='multiple_choice', description='Verbose.', mandatory=False, default_value='1', example=['0', '1', '2'])
    arguments = parser.parse(sys.argv[1:])
    path_data = sct.slash_at_the_end(arguments['-i'], 1)
    if ('-ord' in arguments):
        file_ordering = arguments['-ord']
    remove_temp_files = int(arguments['-r'])
    verbose = int(arguments['-v'])
    fname_data = os.listdir(path_data)
    sct.printv('\nCreate temporary folder...', verbose)
    path_tmp = ('tmp.' + time.strftime('%y%m%d%H%M%S'))
    (status, output) = sct.run(('mkdir ' + path_tmp), verbose)
    os.chdir(path_tmp)
    file_data_list = os.listdir(path_data)
    sct.run('mkdir tmp', verbose)
    file_nii = []
    i = 0
    for i_file in file_data_list:
        (status, output) = sct.run((('dcm2nii -o tmp/ -v n ' + path_data) + i_file), verbose)
        file_nii.append((('data_' + str(i).zfill(4)) + '.nii.gz'))
        sct.run(('mv tmp/*.nii.gz ' + file_nii[i]))
        i = (i + 1)
    nb_files = len(file_data_list)
    if (file_ordering == 'alternate'):
        sct.run(((fsloutputdir + 'fslmerge -t data_spine ') + ' '.join([file_nii[i] for i in range(0, fname_data_brain, 2)])))
        sct.run(((fsloutputdir + 'fslmerge -t data_brain ') + ' '.join([file_nii[i] for i in range(1, nb_files, 2)])))
    if (file_ordering == 'bloc'):
        sct.run(((fsloutputdir + 'fslmerge -t data_spine ') + ' '.join([file_nii[i] for i in range(0, (nb_files / 2))])))
        sct.run(((fsloutputdir + 'fslmerge -t data_brain ') + ' '.join([file_nii[i] for i in range(((nb_files / 2) + 1), nb_files)])))
    os.chdir('..')
    sct.printv('\nGenerate output files...', verbose)
    fname_data_spine = sct.generate_output_file((path_tmp + '/data_spine.nii.gz'), 'data_spine.nii.gz', verbose)
    fname_data_brain = sct.generate_output_file((path_tmp + '/data_brain.nii.gz'), 'data_brain.nii.gz', verbose)
    if remove_temp_files:
        sct.printv('\nRemove temporary files...', verbose)
        sct.run(('rm -rf ' + path_tmp), verbose)
    elapsed_time = (time.time() - start_time)
    sct.printv((('\nFinished! Elapsed time: ' + str(int(round(elapsed_time)))) + 's'), verbose)
    sct.printv('\nTo view results, type:', verbose)
    sct.printv('fslview data_spine &', verbose, 'info')
    sct.printv('fslview data_brain &\n', verbose, 'info')

dataset/ETHPy150Open neuropoly/spinalcordtoolbox/dev/sct_nin_convert_dcm2nii.py main/VarMisuse@436/815 `nb_files`->`fname_data_brain`