diff --git a/nxtomomill/app/z_concatenate_scans.py b/nxtomomill/app/z_concatenate_scans.py index 63f20088194426e1fd5edc78159e8200e480aa01..68bb0e61d744a4897af1c29adcc55e1800c813e9 100644 --- a/nxtomomill/app/z_concatenate_scans.py +++ b/nxtomomill/app/z_concatenate_scans.py @@ -152,18 +152,21 @@ def get_arguments(argv): ps = pattern.findall(args.filename_template) ls = list(map(len, ps)) if len(ls) < 1: - message = f" The argument for filename_template , which was '{args.filename_template}' does not seem to contain a pattern with multiple X for the numerical part" - raise ValueError(message) + args.name_template = args.filename_template - idx = np.argmax(ls) - if len(ps[idx]) < 2: - message = f""" The argument filename_template should contain a substring formed by at least two 'X' - The filename_template was {args.filename_template} - """ - raise ValueError(message) - args.name_template = args.filename_template.replace( - ps[idx], "{i_stage:" + "0" + str(ls[idx]) + "d}" - ) + if args.first_stage != args.last_stage: + message = f" The argument for filename_template , which was '{args.filename_template}' does not seem to contain a pattern with multiple X for the numerical part" + raise ValueError(message) + else: + idx = np.argmax(ls) + if len(ps[idx]) < 2: + message = f""" The argument filename_template should contain a substring formed by at least two 'X' + The filename_template was {args.filename_template} + """ + raise ValueError(message) + args.name_template = args.filename_template.replace( + ps[idx], "{i_stage:" + "0" + str(ls[idx]) + "d}" + ) args.file_list = [] for i_stage in range(args.first_stage, args.last_stage + 1): diff --git a/nxtomomill/app/zstages2nxs.py b/nxtomomill/app/zstages2nxs.py index 36320f1a8ca3f81d1388fcb13f46bd27184a9e45..102bd76054defb934fa42ad268057c01ab28b0b6 100644 --- a/nxtomomill/app/zstages2nxs.py +++ b/nxtomomill/app/zstages2nxs.py @@ -39,12 +39,21 @@ def get_arguments(user_args): ) parser.add_argument( "--filename_template", - required=True, - help="""The filename template. For zstage it must contain one or more segments equal to "X"*ndigits which will be replaced by the stage number, for the scans, and, for the reference scans, by the begin/end prefixes""", + required=False, + default=None, + help="""The filename template. To be used for multiples zstages it must contain one or more segments equal to "X"*ndigits which will be replaced by the stage number, for the scans, and, for the reference scans, by the begin/end prefixes""", + ) + + parser.add_argument( + "--filename", + required=False, + default=None, + help="""The filename. To be used for a single scan""", ) parser.add_argument( "--output_filename_template", required=False, + default=None, help="""Optional, default to a name deduced from filename_template. The output filename template. It must contain one or more segments equal to "X"*ndigits which will be replaced by the stage number""", ) parser.add_argument( @@ -80,7 +89,13 @@ def get_arguments(user_args): required=False, help="Optional. If given the reference scans are used for the extraction of the flats/dark. The reference scans are obtained using the ref postfixes", ) - + parser.add_argument( + "--extracted_reference_target_dir", + type=str, + default=None, + required=None, + help="Optional. By default the extracted reference will be written in the same directory as the nexus scan. As the extraction procedure is time consuming they can be written instead to a common directory", + ) parser.add_argument( "--ref_scan_begin", type=str, @@ -140,15 +155,30 @@ def _convert_bliss2nx(bliss_ref_name, nexus_name): def main(argv): args = get_arguments(argv[1:]) - name_template_for_numeric = template_to_format_string(args.filename_template) - if args.output_filename_template is not None: - args.output_refname_template = template_to_format_string( - args.output_filename_template, literal=True - ) - args.output_filename_template = template_to_format_string( - args.output_filename_template, literal=False + if args.filename_template is not None: + name_template_for_numeric = template_to_format_string(args.filename_template) + else: + name_template_for_numeric = args.filename + + if name_template_for_numeric is None: + raise ValueError( + " Either filename_template of filename must be given as arguments" ) + + if args.output_filename_template is not None: + args.output_refname_template = None + if args.filename is None: + if args.extracted_reference_target_dir is None: + args.output_refname_template = template_to_format_string( + args.output_filename_template, literal=True + ) + else: + args.output_refname_template = None + args.output_filename_template = template_to_format_string( + args.output_filename_template, literal=False + ) else: + # will be deduced at output time args.output_refname_template = None if args.do_references: @@ -158,8 +188,13 @@ def main(argv): (args.ref_scan_end, "end"), ): if args.output_refname_template is None: + if args.extracted_reference_target_dir is None: + extraction_target_dir = args.target_directory + else: + extraction_target_dir = args.extracted_reference_target_dir + nexus_name = os.path.join( - args.target_directory, + extraction_target_dir, strip_extension(os.path.basename(bliss_ref_name), _logger) + ".nx", ) else: @@ -205,6 +240,8 @@ def template_to_format_string(template, literal=False): # warning: If the dataset base names contains several X substrings the longest ones will be taken. ps = pattern.findall(template) ls = list(map(len, ps)) + if len(ls) == 0: + raise ValueError("The template argument does not contain XX.. segments") idx = np.argmax(ls) if len(ps[idx]) < 2: message = f""" The argument template should contain a substring formed by at least two 'X'