Skip to content

Commit

Permalink
adjustements to include .rod downloads and parsing other rod files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Hildebrandt authored and Ron Hildebrandt committed Dec 13, 2024
1 parent 2241edf commit e092d14
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
34 changes: 34 additions & 0 deletions src/download_rod_files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Downloading multiple .rod files

## download_rods_script.sh

Adjust the file `download_rods_script.sh`to the range of download you want.
Default start is `1` and default end is `3`.
Be careful: Do not trigger unneccsary amounts of downloads.

Take a look [here](https://solsa.crystallography.net/rod/1000679.html), to get valid .rod IDs. For this example its e.g. `1000679`.

## Add the command as script

`chmod +x download_rods_script.sh`

## Exectutute the script

`./download_rods_script.sh`


## Convert the downloaded .rod files

via the pynxtools-raman command:

`dataconverter <PATH_TO>/1000679.rod src/pynxtools_raman/config/config_file_rod.json --reader raman --nxdl NXraman --output rod_example_neuxs.nxs`


# Automatec conversion of all .rod files to .nxs files

## Add the command as script
`chmod +x convert_all_rod_to_nxs.sh`

## Call the script
`./src/download_rod_files/convert_all_rod_to_nxs.sh`

14 changes: 14 additions & 0 deletions src/download_rod_files/convert_all_rod_to_nxs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Define the folder containing the .rod files
folder_path="./src/download_rod_files"

# Loop over all .rod files in the folder
for file in "$folder_path"/*.rod; do
# Extract the base name (without extension)
base_name=$(basename "$file" .rod)

# Execute the command with the base name
dataconverter "$file" src/pynxtools_raman/config/config_file_rod.json \
--reader raman --nxdl NXraman --output "${base_name}.nxs"
done
6 changes: 6 additions & 0 deletions src/download_rod_files/download_rods_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

#for X in {1..3}; do
for X in {1000004..1000005}; do
download_rod_file "$X"
done
4 changes: 2 additions & 2 deletions src/pynxtools_raman/config/config_file_rod.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"/ENTRY[entry]/INSTRUMENT[instrument]/LENS_OPT[objective_lens]/numerical_aperture": "@data:_raman_measurement_device.microscope_numerical_aperture",
"/ENTRY[entry]/INSTRUMENT[instrument]/MONOCHROMATOR[monochromator]/grating/period": "@data:_raman_measurement_device.diffraction_grating",
"/ENTRY[entry]/INSTRUMENT[instrument]/MONOCHROMATOR[monochromator]/grating/period/@units": "lines/mm",
"/ENTRY[entry]/SAMPLE[sample]/name":"@data:_chemical_name_systematic",
"/ENTRY[entry]/SAMPLE[sample]/name":"@data:_chemical_name_mineral",
"/ENTRY[entry]/SAMPLE[sample]/physical_form":"@data:_[local]_chemical_compound_state",
"/ENTRY[entry]/SAMPLE[sample]/chemical_formula":"@data:_chemical_formula_structural",
"/ENTRY[entry]/SAMPLE[sample]/ENVIRONMENT[medium]/sample_medium":"@data:_raman_measurement.environment",
Expand All @@ -31,7 +31,7 @@
"/ENTRY[entry]/definition/@url": "Remove_this_if_pynxtools_issue_#469_is_solved",
"/ENTRY[entry]/experiment_type": "Raman spectroscopy",
"/ENTRY[entry]/raman_experiment_type": "other",
"/ENTRY[entry]/title": "@data:_chemical_name_mineral",
"/ENTRY[entry]/title": "@data:_cod_original_formula_sum",
"/ENTRY[entry]/start_time": "@data:_raman_measurement.datetime_initiated",
"/ENTRY[entry]/@default": "data",
"/ENTRY[entry]/DATA[data]/@signal": "y_values",
Expand Down
32 changes: 21 additions & 11 deletions src/pynxtools_raman/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def handle_rod_file(self, filepath) -> Dict[str, Any]:
# get the key and value pairs from the rod file
self.raman_data = rod.extract_keys_and_values_from_cif()

# replace the [ and ] to avoid confliucts in processing with pynxtools NXclass assignments
self.raman_data = {
key.replace("_[local]_", "_local_"): value
for key, value in self.raman_data.items()
}

self.missing_meta_data = copy.deepcopy(self.raman_data)

# This changes all uppercase string elements to lowercase string elements for the given key, within a given key value pair
Expand All @@ -101,16 +107,17 @@ def handle_rod_file(self, filepath) -> Dict[str, Any]:
# transform the string into a datetime object
time_key = "_raman_measurement.datetime_initiated"
date_time_str = self.raman_data.get(time_key)
date_time_obj = datetime.datetime.strptime(date_time_str, "%Y-%m-%d")
# assume UTC for .rod data, as this is not specified in detail
tzinfo = datetime.timezone.utc
if isinstance(date_time_obj, datetime.datetime):
if tzinfo is not None:
# Apply the specified timezone to the datetime object
date_time_obj = date_time_obj.replace(tzinfo=tzinfo)

# assign the dictionary the corrrected date format
self.raman_data[time_key] = date_time_obj.isoformat()
if date_time_str is not None:
date_time_obj = datetime.datetime.strptime(date_time_str, "%Y-%m-%d")
# assume UTC for .rod data, as this is not specified in detail
tzinfo = datetime.timezone.utc
if isinstance(date_time_obj, datetime.datetime):
if tzinfo is not None:
# Apply the specified timezone to the datetime object
date_time_obj = date_time_obj.replace(tzinfo=tzinfo)

# assign the dictionary the corrrected date format
self.raman_data[time_key] = date_time_obj.isoformat()

# remove capitalization
objective_type_key = "_raman_measurement_device.optics_type"
Expand Down Expand Up @@ -199,7 +206,10 @@ def get_data(self, key: str, path: str) -> Any:
# this filters out the meta data, which is up to now only created for .rod files

if self.missing_meta_data:
del self.missing_meta_data[path]
# this if condition is required, to only delete keys which are abaialble by the data.
# e.g. is defined to extract it via config.json, but there is no value in meta data
if path in self.missing_meta_data.keys():
del self.missing_meta_data[path]

if value is not None:
try:
Expand Down
Binary file modified tests/data/rod/example.nxs
Binary file not shown.
Binary file modified tests/data/witec/example.nxs
Binary file not shown.

0 comments on commit e092d14

Please sign in to comment.