beam-tracing / Scotty

Beam tracing code for diagnostics
https://scotty.readthedocs.io/en/latest/
GNU General Public License v3.0
7 stars 4 forks source link

Add option to choose customise filename for find_B_method='omfit' #85

Closed valerian-chen closed 1 year ago

valerian-chen commented 1 year ago

Right now, find_B_method='omfit' does not allow for custom filenames, unlike the other methods, which allow an input_filename_suffix

Sorry @ZedThree, this should be quite easy to change but I don't quite understand the notation: f"topfile{input_filename_suffix}"

The new way of doing paths does boggle me a little, embarrassingly.

ZedThree commented 1 year ago

This is using an f-string (official docs, tutorial), it's basically a fancier way of doing "topfile" + input_filename_suffix.

It's maybe a little overkill here, but they're really handy for more complex cases like (made up example):

filename = "data_" + suffix + "_" + str(shot_number) + "_" + str(launch_freq_GHz) + "GHz"

and using an f-string:

filename = f"data_{suffix}_{shot_number}_{launch_freq_GHz}GHz"

Hopefully you'll agree the second one is easier to read -- it also happens to be significantly faster, although that only matters if you're doing this in a big loop for some reason.

Basically, if you need to construct a string, f-strings are almost always the best choice (the exception is if you want a template to fill in later).

I guess here you probably want:

@@ -2393,7 +2393,7 @@ def create_magnetic_geometry(

     elif find_B_method == "omfit":
         print("Using OMFIT JSON Torbeam file for B and poloidal flux")
-        topfile_filename = magnetic_data_path / "topfile.json"
+        topfile_filename = magnetic_data_path / f"topfile{input_filename_suffix}.json"

         with open(topfile_filename) as f:
             data = json.load(f)
valerian-chen commented 1 year ago

Ah wait that's how I've been formatting filenames with floats. Didn't realise it worked with strings too. This is great, thank you so much!

Definitely agree this is nicer.