Skip to content

Commit

Permalink
add rasx for new rigaku xrd
Browse files Browse the repository at this point in the history
  • Loading branch information
qwe789qwec committed Feb 19, 2025
1 parent 6508ebb commit c1145cc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,8 @@ fabric.properties
# git update-index --assume-unchanged Xerus/settings/config.conf

#test file
/Examples/Xerustest.ipynb
/Examples/Xerustest.ipynb

#user file
/results_tmp/
/tmp/
28 changes: 25 additions & 3 deletions Xerus/readers/dataextensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import pandas as pd
import numpy as np
import os

import zipfile

class DataExtension:
"""
Expand Down Expand Up @@ -144,7 +144,7 @@ def read_data(self, file: os.PathLike) -> None:
self.tmin = np.round(xrd.theta.min(), 2)
self.tmax = np.round(xrd.theta.max(), 2)
self.step = np.round(xrd.theta.iat[1] - xrd.theta.iat[0], 4)
print("Sucessfuly read datafile {}".format(file))
print("Successfully read datafile {}".format(file))


class CSVFile(DataExtension):
Expand Down Expand Up @@ -175,5 +175,27 @@ def read_data(self, file: str) -> None:
self.tmin = np.round(xrd.theta.min(), 2)
self.tmax = np.round(xrd.theta.max(), 2)
self.step = np.round(xrd.theta.iat[1] - xrd.theta.iat[0], 4)
print("Sucessfuly read datafile {}".format(file))
print("Successfully read datafile {}".format(file))


class RASXFile(DataExtension):

def __init__(self, fmt="rasx"):
super().__init__()
self.format = fmt

def read_data(self, file: os.PathLike) -> None:
try:
with zipfile.ZipFile(file, "r") as z:
with z.open(z.namelist()[0]) as f:
xrd = pd.read_table(f, sep="\t", names=["theta", "int", "extra"])
xrd = xrd.iloc[:, :2]
except Exception as e:
print("Error reading file: {}".format(e))
xrd = pd.read_table(file, sep=",", names=["theta", "int"])
xrd['filename'] = os.path.basename(file)
self.xrd_df = xrd
self.tmin = np.round(xrd.theta.min(), 2)
self.tmax = np.round(xrd.theta.max(), 2)
self.step = np.round(xrd.theta.iat[1] - xrd.theta.iat[0], 4)
print("Successfully read datafile {}".format(file))
3 changes: 2 additions & 1 deletion Xerus/readers/datareader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
readers = {
"xy": XYFile(),
"ras": RASFile(),
"csv": CSVFile()
"csv": CSVFile(),
"rasx": RASXFile(),
} # supported formats

class DataReader:
Expand Down
23 changes: 17 additions & 6 deletions app/ui_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import codecs
import io
import os
from typing import List, Union

import pandas as pd

import zipfile
from typing import List, Union
from conf import AppSettings


Expand All @@ -21,13 +20,25 @@ def read_input(file: io.BytesIO) -> str:
_type_
Nothing. Save files into the temporary folder.
"""
stringio = io.StringIO(file.getvalue().decode("shift-jis"))
print(file)
if file.name.endswith(".rasx"):
with open(os.path.join(AppSettings.TMP_FOLDER, file.name), "wb") as f:
f.write(file.getvalue())
with zipfile.ZipFile(os.path.join(AppSettings.TMP_FOLDER, file.name), "r") as z:
with z.open(z.namelist()[0]) as f:
xrd = pd.read_table(f, sep="\t")
# Save the dataframe as a CSV to the TMP_FOLDER
csv_path = os.path.join(AppSettings.TMP_FOLDER, file.name.replace(".rasx", ".csv"))
xrd = xrd.iloc[:, :2] # Keep only the first two columns
stringio = io.StringIO(xrd.to_csv(index=False))
else:
stringio = io.StringIO(file.getvalue().decode("shift-jis"))

# Save
# Save the string data with shift-jis encoding
with codecs.open(os.path.join(AppSettings.TMP_FOLDER, file.name), "w", "shift-jis") as f:
f.write(stringio.read())

# Return path
# Return the full path of the saved file
return os.path.join(AppSettings.TMP_FOLDER, file.name)


Expand Down

0 comments on commit c1145cc

Please sign in to comment.