Fill a custom field with a value from the datasets parameter section

FillCustomFieldWithParameterValue.py

from LOGS import LOGS
from LOGS.Entities import (
    DatasetRequestParameter,
)
#optional for a nice progress bar:
from tqdm import tqdm 

# Define the Dataset CustomType and CustomFieldId for the operation
# !! This is different on each LOGS instance, so you need to check your LOGS instance for the correct IDs.
# The Dataset CustomType ID can be found in the LOGS web interface under "More" -> "Custom Types" -> "Your Custom Type Name" -> Upper right corner of the detail page below Last modified
# The Custom Field ID can be found in the LOGS web interface under "More" -> "Custom Fields" -> "Your Custom Field Name" -> Upper right corner of the detail page below Last modified
DatasetCustomTypeId = 17
CustomFieldId = 65

# Connect to LOGS
logs = LOGS()

# The DatasetRequestParameter allows us to filter datasets based on specific criteria.
# Here: Get all datasets of the specified format and custom type 
dataset_iterator = logs.datasets(
    DatasetRequestParameter(formatIds=["BrukerNMR"], customTypeIds=[DatasetCustomTypeId])
    )

print(f"Found {dataset_iterator.count} datasets.")

# Use a nice progress bar to show the progress of processing datasets
for dataset in tqdm(dataset_iterator, total=dataset_iterator.count, desc="Processing datasets"):
# Without progress bar it would be:
# for dataset in dataset_iterator:

    # Fetch all parameters for the dataset in order to access them
    dataset.fetchParameters() 
    # Get the value of the "Duration" parameter from the dataset via the Section/Parameter path. This is the same path as seen in the LOGS web interface.
    duration = dataset.getParameter("General information/Duration") 

    # Check if 'duration' is a non-empty string and not None
    if isinstance(duration, str) and duration.strip():
        # Set the value of the custom field in the dataset to the duration value
        dataset.customValues.customField(CustomFieldId).value = duration.strip()

        # Save the changes to the dataset
        dataset.update() 
        #print(f"Dataset '{dataset.name}' (ID: {dataset.id}) updated with duration: '{duration.strip()}'.")

print("All datasets processed.")