Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix behaviour of deerload with 2D-datasets #83

Merged
merged 11 commits into from
Mar 11, 2021
48 changes: 30 additions & 18 deletions deerlab/deerload.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def deerload(fullbasename, plot=False, full_output=False, *args,**kwargs):
parameters = read_description_file(filename_dsc)
parDESC = parameters["DESC"]
parSPL = parameters["SPL"]

print( int(parDESC['XPTS']))
laenan8466 marked this conversation as resolved.
Show resolved Hide resolved
# XPTS, YPTS, ZPTS specify the number of data points along x, y and z.
if 'XPTS' in parDESC:
nx = int(parDESC['XPTS'])
Expand Down Expand Up @@ -167,12 +167,9 @@ def deerload(fullbasename, plot=False, full_output=False, *args,**kwargs):
abscissa[:Dimensions[index],index] = np.linspace(minimum,minimum+width,npts)
if axistype == 'NTUP':
raise ValueError('Cannot read data with NTUP axes.')

# In case of column filled with NaN, erase the column in the array
abscissa = abscissa[:,~np.isnan(abscissa).all(axis=0)]

dt_data = dt_spc
dt_spc = dt_spc.newbyteorder(byteorder)

# Read data matrix and separate complex case from real case.
data = np.full((nx,ny,nz),np.nan)
# reorganize the data in a "complex" way as the real part and the imaginary part are separated
Expand All @@ -182,11 +179,10 @@ def deerload(fullbasename, plot=False, full_output=False, *args,**kwargs):
if parDESC['IKKF'] == 'REAL':
data = np.full((nx,ny,nz),np.nan)
with open(filename_dta,'rb') as fp:
data = np.frombuffer(fp.read(),dtype=dt_spc).reshape(nx,ny,nz)
data = np.frombuffer(fp.read(),dtype=dt_spc)
data = np.copy(data)
elif parDESC['IKKF'] == 'CPLX':
dt_new = np.dtype('complex')
data = np.full((2*nx*ny*nz,1),np.nan)
with open(filename_dta,'rb') as fp:
data = np.frombuffer(fp.read(),dtype=dt_spc)
# Check if there is multiple harmonics (High field ESR quadrature detection)
Expand All @@ -199,25 +195,41 @@ def deerload(fullbasename, plot=False, full_output=False, *args,**kwargs):
n_harmonics = sum(harmonics)[0]
if n_harmonics != 0:
ny = int(len(data)/nx/n_harmonics)

# copy the data to a writable numpy array
data = np.copy(data.astype(dtype=dt_data).view(dtype=dt_new).reshape(nx,ny,nz))
data = np.copy(data.astype(dtype=dt_data).view(dtype=dt_new))
else:
raise ValueError("Unknown value for keyword IKKF in .DSC file!")
else:
warn("Keyword IKKF not found in .DSC file! Assuming IKKF=REAL.")

if nz == 1:
data = data.reshape(nx,ny)
# ns -> us converesion
abscissa /= 1e3
# Split 1D-array into 3D-array according to XPTS/YPTS/ZPTS
data = np.array_split(data,nz)
data = np.array(data).T
data = np.array_split(data,ny)
data = np.array(data).T

# Ensue proper numpy formatting
abscissa,data = np.atleast_1d(abscissa,data)
abscissa = np.squeeze(abscissa)
data = np.atleast_1d(data)
data = np.squeeze(data)

# Abscissa formatting
abscissa = np.atleast_1d(abscissa)
abscissa = np.squeeze(abscissa)
abscissas = []
# Convert to list of abscissas
for absc in abscissa.T:
# Do not include abcissas full of NaNs
if not all(np.isnan(absc)):
# ns -> us converesion
absc /= 1e3
# Remove nan values to ensure proper length of abscissa
abscissas.append(absc[~np.isnan(absc)])
# If 1D-dataset, return array instead of single-element list
print(len(abscissas))
laenan8466 marked this conversation as resolved.
Show resolved Hide resolved
if len(abscissas)==1:
abscissas = abscissas[0]

if plot:
plt.plot(abscissa,np.real(data),abscissa,np.imag(data))
plt.xlabel("Time (μs)")
Expand All @@ -226,9 +238,9 @@ def deerload(fullbasename, plot=False, full_output=False, *args,**kwargs):
plt.show()

if full_output:
return abscissa, data, parameters
return abscissas, data, parameters
else:
return abscissa, data
return abscissas, data


def read_description_file(DSCFileName):
Expand Down