|
28 | 28 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
29 | 29 | ) |
30 | 30 | parser.add_argument( |
31 | | - "-r", |
32 | | - "--run", |
33 | | - default=None, |
34 | | - help="process a specific run.", |
35 | | - type=str, |
| 31 | + "-f", |
| 32 | + "--force", |
| 33 | + default=False, |
| 34 | + action="store_true", |
| 35 | + help="if this run is already in the DB, force re-parsing its DQM output again.", |
36 | 36 | ) |
37 | 37 | parser.add_argument( |
38 | 38 | "-p", |
|
41 | 41 | help="path on DIRAC where to grab DQM outputs (optional).", |
42 | 42 | type=str, |
43 | 43 | ) |
| 44 | +parser.add_argument( |
| 45 | + "-r", |
| 46 | + "--runs", |
| 47 | + nargs="+", |
| 48 | + default=None, |
| 49 | + help="process a specific run or a list of runs.", |
| 50 | +) |
44 | 51 | args = parser.parse_args() |
45 | 52 |
|
46 | | -if args.run is None: |
47 | | - logger.critical("A run number should be provided.") |
| 53 | +if args.runs is None: |
| 54 | + logger.critical("At least one run number should be provided.") |
48 | 55 | sys.exit(1) |
49 | 56 |
|
50 | | -lfn = f"{args.path}/NectarCAM_DQM_Run{args.run}.tar.gz" |
51 | | - |
52 | | -if not os.path.exists(os.path.basename(lfn)): |
53 | | - DIRAC.initialize() |
54 | | - |
55 | | - dirac = Dirac() |
56 | | - |
57 | | - dirac.getFile( |
58 | | - lfn=lfn, |
59 | | - destDir=f".", |
60 | | - printOutput=True, |
61 | | - ) |
62 | | - |
63 | | -with tarfile.open(os.path.basename(lfn), "r") as tar: |
64 | | - tar.extractall(".") |
65 | | - |
66 | | -fits_file = ( |
67 | | - f"./NectarCAM_DQM_Run{args.run}/output/NectarCAM_Run{args.run}/" |
68 | | - f"NectarCAM_Run{args.run}_calib/NectarCAM_Run{args.run}_Results.fits" |
69 | | -) |
70 | | - |
71 | | -hdu = fits.open(fits_file) |
72 | | - |
73 | | -# Explore FITS file structure |
74 | | -hdu.info() |
75 | | - |
76 | | -outdict = dict() |
77 | | - |
78 | | -for h in range(1, len(hdu)): |
79 | | - extname = hdu[h].header["EXTNAME"] |
80 | | - outdict[extname] = dict() |
81 | | - for i in range(hdu[extname].header["TFIELDS"]): |
82 | | - keyname = hdu[extname].header[f"TTYPE{i+1}"] |
83 | | - outdict[extname][keyname] = hdu[extname].data[keyname] |
84 | | - |
85 | | -try: |
86 | | - db = DQMDB(read_only=False) |
87 | | - db.insert(f"NectarCAM_Run{args.run}", outdict) |
88 | | - db.commit_and_close() |
89 | | -except ZEO.Exceptions.ClientDisconnected as e: |
90 | | - logger.critical(f"Impossible to feed the ZODB data base. Received error: {e}") |
91 | | - |
92 | | -# Remove DQM archive file and directory |
93 | | -try: |
94 | | - os.remove(f"NectarCAM_DQM_Run{args.run}.tar.gz") |
95 | | -except OSError: |
96 | | - logger.warning( |
97 | | - f"Could not remove NectarCAM_DQM_Run{args.run}.tar.gz or it does not exist" |
| 57 | +db_read = DQMDB(read_only=True) |
| 58 | +db_read_keys = list(db_read.root.keys()) |
| 59 | +db_read.abort_and_close() |
| 60 | + |
| 61 | +for run in args.runs: |
| 62 | + if not args.force and f"NectarCAM_Run{run}" in db_read_keys: |
| 63 | + logger.warning( |
| 64 | + f'The run {run} is already present in the DB, will not parse this DQM run, or consider forcing it with the "--force" option.' |
| 65 | + ) |
| 66 | + continue |
| 67 | + |
| 68 | + lfn = f"{args.path}/NectarCAM_DQM_Run{run}.tar.gz" |
| 69 | + |
| 70 | + if not os.path.exists(os.path.basename(lfn)): |
| 71 | + DIRAC.initialize() |
| 72 | + |
| 73 | + dirac = Dirac() |
| 74 | + |
| 75 | + dirac.getFile( |
| 76 | + lfn=lfn, |
| 77 | + destDir=f".", |
| 78 | + printOutput=True, |
| 79 | + ) |
| 80 | + |
| 81 | + try: |
| 82 | + with tarfile.open(os.path.basename(lfn), "r") as tar: |
| 83 | + tar.extractall(".") |
| 84 | + except FileNotFoundError as e: |
| 85 | + logger.warning( |
| 86 | + f"Could not fetch DQM results from DIRAC for run {run}, received error {e}, skipping this run..." |
| 87 | + ) |
| 88 | + continue |
| 89 | + |
| 90 | + fits_file = ( |
| 91 | + f"./NectarCAM_DQM_Run{run}/output/NectarCAM_Run{run}/" |
| 92 | + f"NectarCAM_Run{run}_calib/NectarCAM_Run{run}_Results.fits" |
98 | 93 | ) |
99 | 94 |
|
100 | | -dirpath = Path(f"./NectarCAM_DQM_Run{args.run}") |
101 | | -if dirpath.exists() and dirpath.is_dir(): |
102 | | - shutil.rmtree(dirpath) |
| 95 | + hdu = fits.open(fits_file) |
| 96 | + |
| 97 | + # Explore FITS file structure |
| 98 | + hdu.info() |
| 99 | + |
| 100 | + outdict = dict() |
| 101 | + |
| 102 | + for h in range(1, len(hdu)): |
| 103 | + extname = hdu[h].header["EXTNAME"] |
| 104 | + outdict[extname] = dict() |
| 105 | + for i in range(hdu[extname].header["TFIELDS"]): |
| 106 | + keyname = hdu[extname].header[f"TTYPE{i+1}"] |
| 107 | + outdict[extname][keyname] = hdu[extname].data[keyname] |
| 108 | + |
| 109 | + try: |
| 110 | + db = DQMDB(read_only=False) |
| 111 | + db.insert(f"NectarCAM_Run{run}", outdict) |
| 112 | + db.commit_and_close() |
| 113 | + except ZEO.Exceptions.ClientDisconnected as e: |
| 114 | + logger.critical(f"Impossible to feed the ZODB data base. Received error: {e}") |
| 115 | + |
| 116 | + # Remove DQM archive file and directory |
| 117 | + try: |
| 118 | + os.remove(f"NectarCAM_DQM_Run{run}.tar.gz") |
| 119 | + except OSError: |
| 120 | + logger.warning( |
| 121 | + f"Could not remove NectarCAM_DQM_Run{run}.tar.gz or it does not exist" |
| 122 | + ) |
| 123 | + |
| 124 | + dirpath = Path(f"./NectarCAM_DQM_Run{run}") |
| 125 | + if dirpath.exists() and dirpath.is_dir(): |
| 126 | + shutil.rmtree(dirpath) |
0 commit comments