Skip to content

Commit 3e4ed73

Browse files
authored
Fix: Do not attempt to write when knob isn't found in LSA optics (#101)
1 parent 7fb7ae0 commit 3e4ed73

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

pylhc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
__title__ = "pylhc"
1111
__description__ = "An accelerator physics script collection for the OMC team at CERN."
1212
__url__ = "https://github.com/pylhc/pylhc"
13-
__version__ = "0.7.0"
13+
__version__ = "0.7.1"
1414
__author__ = "pylhc"
1515
__author_email__ = "pylhc@github.com"
1616
__license__ = "MIT"

pylhc/lsa_to_madx.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,20 @@ def main():
222222
madx_commands_string = get_madx_script_from_definition_dataframe(
223223
deltas_df=knob_definition, lsa_knob=lsa_knob, trim=trim_value
224224
)
225-
except (OSError, IOError):
225+
226+
except (OSError, IOError): # raised by pjlsa if knob not found
226227
LOG.warning(f"Could not find knob '{lsa_knob}' in the given optics '{lsa_optics}' - skipping")
227228
unfound_knobs.append(lsa_knob)
228229

229-
definition_file = f"{lsa_knob.replace('/', '_')}_definition.tfs"
230-
LOG.debug(f"Writing knob definition TFS file at '{definition_file}'")
231-
tfs.write(definition_file, knob_definition, save_index=True)
230+
else: # we've managed to find knobs
231+
# Don't write this in the try block, as it could raise IOError when failing
232+
definition_file = f"{lsa_knob.replace('/', '_')}_definition.tfs"
233+
LOG.debug(f"Writing knob definition TFS file at '{definition_file}'")
234+
tfs.write(definition_file, knob_definition, save_index=True)
232235

233-
madx_file = f"{lsa_knob.replace('/', '_')}_knob.madx"
234-
LOG.debug(f"Writing MAD-X commands to file '{madx_file}'")
235-
Path(madx_file).write_text(madx_commands_string)
236+
madx_file = f"{lsa_knob.replace('/', '_')}_knob.madx"
237+
LOG.debug(f"Writing MAD-X commands to file '{madx_file}'")
238+
Path(madx_file).write_text(madx_commands_string)
236239

237240
if unfound_knobs:
238241
LOG.info(f"The following knobs could not be found in the '{lsa_optics}' optics: \n\t\t" + "\n\t\t".join(unfound_knobs))

0 commit comments

Comments
 (0)