1818import sys
1919from pathlib import Path
2020
21+ from diffpy .pdfmorph import __save_morph_as__
2122from diffpy .pdfmorph .version import __version__
2223import diffpy .pdfmorph .tools as tools
2324import diffpy .pdfmorph .pdfplot as pdfplot
2425import diffpy .pdfmorph .morphs as morphs
2526import diffpy .pdfmorph .morph_helpers as helpers
2627import diffpy .pdfmorph .refine as refine
27- from diffpy .pdfmorph .pdfmorph_io import single_morph_output , multiple_morph_output , tabulate_results
28+ import diffpy .pdfmorph .pdfmorph_io as io
2829
2930
3031def create_option_parser ():
@@ -71,13 +72,13 @@ def custom_error(self, msg):
7172 '--snf' ,
7273 '--save-names-file' ,
7374 metavar = "NAMESFILE" ,
74- dest = "snfile " ,
75- help = """Used only when both -s and --multiple are enabled.
75+ dest = "snamesfile " ,
76+ help = f """Used only when both -s and --multiple are enabled.
7677Specify names for each manipulated PDF when saving (see -s) using a serial file
7778NAMESFILE. The format of NAMESFILE should be as follows: each target PDF
78- is an entry in NAMESFILE. For each entry, there should be a key 'save_morph_as'
79+ is an entry in NAMESFILE. For each entry, there should be a key { __save_morph_as__ }
7980whose value specifies the name to save the manipulated PDF as.
80- (See sample names files in the pdfmorph tutorial).""" ,
81+ (See sample names files in the PDFmorph tutorial).""" ,
8182 )
8283 parser .add_option (
8384 '-v' ,
@@ -407,15 +408,14 @@ def single_morph(parser, opts, pargs, stdout_flag=True):
407408
408409 # Print summary to terminal and save morph to file if requested
409410 try :
410- single_morph_output (morph_inputs , morph_results ,
411- save_file = opts .slocation , morph_file = pargs [0 ],
412- xy_out = [chain .x_morph_out , chain .y_morph_out ],
413- verbose = opts .verbose , stdout_flag = stdout_flag )
411+ io . single_morph_output (morph_inputs , morph_results ,
412+ save_file = opts .slocation , morph_file = pargs [0 ],
413+ xy_out = [chain .x_morph_out , chain .y_morph_out ],
414+ verbose = opts .verbose , stdout_flag = stdout_flag )
414415
415- except FileNotFoundError as e :
416+ except ( FileNotFoundError , RuntimeError ) as e :
416417 save_fail_message = "Unable to save to designated location."
417- print (save_fail_message )
418- parser .custom_error (str (e ))
418+ parser .custom_error (save_fail_message )
419419
420420 if opts .plot :
421421 pairlist = [chain .xy_morph_out , chain .xy_target_out ]
@@ -449,7 +449,6 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True):
449449
450450 # Parse paths
451451 morph_file = Path (pargs [0 ])
452- target_directory = Path (pargs [1 ])
453452 if not morph_file .is_file ():
454453 parser .custom_error (f"{ morph_file } is not a file. Go to --help for usage." )
455454 target_directory = Path (pargs [1 ])
@@ -489,41 +488,34 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True):
489488 opts .plot = False
490489
491490 # Set up saving
492- save_directory = opts .slocation
493- save_names = {}
494- save_morphs_here = ""
491+ save_directory = opts .slocation # User-given directory for saves
492+ save_names_file = opts .snamesfile # User-given serialfile with names for each morph
493+ save_morphs_here = None # Subdirectory for saving morphed PDFs
494+ save_names = {} # Dictionary of names to save each morph as
495495 if save_directory is not None :
496496 try :
497- # Make directory to save files in if it does not already exist
498- Path (save_directory ).mkdir (parents = True , exist_ok = True )
499-
500- # Morphs will be saved in the subdirectory "Morphs"
501- save_morphs_here = Path (save_directory ).joinpath ("Morphs" )
502- save_morphs_here .mkdir (exist_ok = True )
503-
504- # Get names for the saved morphs
505- if opts .snfile is not None :
506- # Names should be stored properly in opts.snfile
507- save_names = tools .deserialize (opts .snfile )
508- # Default naming scheme
509- else :
510- for target_file in target_list :
511- save_names .update ({target_file .name : {"save_morph_as" :
512- f"Morph_with_Target_{ target_file .stem } .cgr" }})
497+ save_morphs_here = io .create_morphs_directory (save_directory )
513498
514- # Could not create directory
515- except FileNotFoundError as e :
499+ # Could not create directory or find names to save morphs as
500+ except ( FileNotFoundError , RuntimeError ) as e :
516501 save_fail_message = "\n Unable to create directory"
517- print (save_fail_message )
518- parser .custom_error (str (e ))
502+ parser .custom_error (save_fail_message )
503+
504+ try :
505+ save_names = io .get_multisave_names (target_list , save_names_file = save_names_file )
506+ # Could not create directory or find names to save morphs as
507+ except FileNotFoundError :
508+ save_fail_message = "\n Unable to read from save names file"
509+ parser .custom_error (save_fail_message )
519510
520511 # Morph morph_file against all other files in target_directory
521512 morph_results = {}
522513 for target_file in target_list :
523514 if target_file .is_file :
524515 # Set the save file destination to be a file within the SLOC directory
525516 if save_directory is not None :
526- opts .slocation = Path (save_morphs_here ).joinpath (save_names .get (target_file .name ).get ("save_morph_as" ))
517+ save_as = save_names [target_file .name ][__save_morph_as__ ]
518+ opts .slocation = Path (save_morphs_here ).joinpath (save_as )
527519 # Perform a morph of morph_file against target_file
528520 pargs = [morph_file , target_file ]
529521 morph_results .update ({
@@ -539,18 +531,18 @@ def multiple_morphs(parser, opts, pargs, stdout_flag=True):
539531
540532 try :
541533 # Print summary of morphs to terminal and to file (if requested)
542- multiple_morph_output (morph_inputs , morph_results , target_file_names ,
543- save_directory = save_directory , morph_file = morph_file , target_directory = target_directory ,
544- field = field , field_list = field_list , verbose = opts .verbose , stdout_flag = stdout_flag )
545- except FileNotFoundError as e :
534+ io .multiple_morph_output (morph_inputs , morph_results , target_file_names ,
535+ save_directory = save_directory , morph_file = morph_file ,
536+ target_directory = target_directory , field = field , field_list = field_list ,
537+ verbose = opts .verbose , stdout_flag = stdout_flag )
538+ except (FileNotFoundError , RuntimeError ) as e :
546539 save_fail_message = "Unable to save summary to directory."
547- print (save_fail_message )
548- parser .custom_error (str (e ))
540+ parser .custom_error (save_fail_message )
549541
550542 # Plot the rw values for each target if requested
551543 # FIXME: create functionality to plot other data (scale, stretch, smear, etc.)
552544 if plot_opt :
553- plot_results = tabulate_results (morph_results )
545+ plot_results = io . tabulate_results (morph_results )
554546 if field_list is not None :
555547 pdfplot .plot_rws (field_list , plot_results ["Rw" ], field )
556548 else :
0 commit comments