def main():

    # Initialization
    fsloutputdir = 'export FSLOUTPUTTYPE=NIFTI_GZ; '
    file_ordering = 'alternate'
    start_time = time.time()

    # # get path of the toolbox
    # status, path_sct = commands.getstatusoutput('echo $SCT_DIR')

    # Initialize the parser
    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:])

    # get arguments
    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'])

    # list all DICOM files
    fname_data = os.listdir(path_data)

    # create temporary folder
    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)

    # go to temporary folder
    os.chdir(path_tmp)

    # list DICOM files
    file_data_list = os.listdir(path_data)

    # create another temp folder for conversion
    sct.run('mkdir tmp', verbose)

    # loop across files
    file_nii = []
    i = 0
    for i_file in file_data_list:
        # convert dicom to nifti and put in temporary folder
        status, output = sct.run('dcm2nii -o tmp/ -v n '+path_data+i_file, verbose)
        # change file name
        file_nii.append('data_'+str(i).zfill(4)+'.nii.gz')
        sct.run('mv tmp/*.nii.gz '+file_nii[i])
        # increment file index
        i = i+1

    # Merge data
    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, nb_files, 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)]))

    # come back to parent folder
    os.chdir('..')

    # Generate output files
    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)

    # Delete temporary files
    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')


# START PROGRAM
# ==========================================================================================
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/dev/sct_nin_convert_dcm2nii.py/main/original