diff --git a/notebooks/LPD/LPDChar_Darks_NBC.ipynb b/notebooks/LPD/LPDChar_Darks_NBC.ipynb index 0248b247b329ba4e2e4901a218d408f1f8ec5c24..37e6585363ffe8b3404d5c8ec6c9640290cd128f 100644 --- a/notebooks/LPD/LPDChar_Darks_NBC.ipynb +++ b/notebooks/LPD/LPDChar_Darks_NBC.ipynb @@ -118,6 +118,7 @@ " map_gain_stages,\n", " module_index_to_qm,\n", " parse_runs,\n", + " raw_data_location_string,\n", " reorder_axes,\n", " run_prop_seq_from_path,\n", " save_const_to_h5,\n", @@ -343,7 +344,7 @@ "source": [ "# Read report path and create file location tuple to add with the injection\n", "proposal = list(filter(None, in_folder.strip('/').split('/')))[-2]\n", - "file_loc = 'proposal:{} runs:{}'.format(proposal, \", \".join(map(str, run_nums))) \n", + "file_loc = raw_data_location_string(proposal, run_nums)\n", "\n", "report = get_report(metadata_folder)" ] diff --git a/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb b/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb index c226964bed398d41355ffa2db17e88e53162f3f5..f3ccaa445187d055eac71d04cee66cebc6df8554 100644 --- a/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb +++ b/notebooks/LPDMini/LPD_Mini_Char_Darks_NBC.ipynb @@ -98,6 +98,7 @@ " calcat_creation_time,\n", " get_from_db,\n", " get_report,\n", + " raw_data_location_string,\n", " run_prop_seq_from_path,\n", " save_const_to_h5,\n", " send_to_db,\n", @@ -341,7 +342,7 @@ "source": [ "# Read report path and create file location tuple to add with the injection\n", "proposal = list(filter(None, in_folder.strip('/').split('/')))[-2]\n", - "file_loc = 'proposal:{} runs:{}'.format(proposal, \", \".join(map(str, run_nums)))\n", + "file_loc = raw_data_location_string(proposal, run_nums)\n", "\n", "report = get_report(metadata_folder)" ] diff --git a/src/cal_tools/tools.py b/src/cal_tools/tools.py index 88ab28d1e03c23264bb77c1c9a0e6b707a1535a1..d9e5e5f9e420a41dfff546b38e95a5fbd81c7bf8 100644 --- a/src/cal_tools/tools.py +++ b/src/cal_tools/tools.py @@ -1044,3 +1044,24 @@ def reorder_axes(a, from_order, to_order): from_order = list(from_order) order = tuple([from_order.index(lbl) for lbl in to_order]) return a.transpose(order) + + +def raw_data_location_string(proposal: str, runs: List[int]): + """Create RAW data location string to inject as a + metadata along with the calibration parameters. + + Args: + proposal (string): The proposal number including the preceding `p`. + e.g. p900203 + runs (list): A list of the run numbers + + Returns: + str: The string for raw data_location. + e.g. `proposal p900203 runs: 9008, 9009, 90010` + """ + if not isinstance(proposal, str) or proposal[0] != "p": + raise ValueError( + "Invalid proposal format. The proposal should be a string with" + " a preceding 'p'. Example: 'p900203'") + + return f"proposal:{proposal} runs:{' '.join(map(str, runs))}" diff --git a/tests/test_cal_tools.py b/tests/test_cal_tools.py index 3eb962b0ec1a12c29efd03584b77d5340cd277cf..577a82a4f57122b6153cd4ef92e5f548a424f1de 100644 --- a/tests/test_cal_tools.py +++ b/tests/test_cal_tools.py @@ -18,6 +18,7 @@ from cal_tools.tools import ( get_pdu_from_db, map_seq_files, module_index_to_qm, + raw_data_location_string, recursive_update, send_to_db, write_constants_fragment, @@ -567,3 +568,16 @@ def test_reorder_axes(): to_order = ('gain', 'fast_scan', 'slow_scan', 'cells') assert reorder_axes(a, from_order, to_order).shape == (3, 256, 32, 10) + + +def test_raw_data_location_string(): + raw_data_loc = raw_data_location_string("p900203", [9008, 9009, 9010]) + assert raw_data_loc == "proposal:p900203 runs:9008 9009 9010" + + +def test_raise_raw_data_location_string(): + with pytest.raises(ValueError): + raw_data_location_string(900203, [9008, 9009, 9010]) + + with pytest.raises(ValueError): + raw_data_location_string("900203", [9008, 9009, 9010])