Unverified Commit e78eb6cc authored by Raj Giridharagopal's avatar Raj Giridharagopal Committed by GitHub
Browse files

Merge pull request #202 from pycroscopy/ginger_lab

Updates to PiFM/Igor translator
parents 7480f993 2ef83e78
......@@ -16,6 +16,7 @@ from . import df_utils
from . import beps_data_generator
from . import nanonis
from . import image
from . import pifm
from .be_odf import BEodfTranslator
from .be_odf_relaxation import BEodfRelaxationTranslator
......@@ -37,9 +38,11 @@ from .beps_data_generator import FakeBEPSGenerator
from .labview_h5_patcher import LabViewH5Patcher
from .nanonis import NanonisTranslator
from .image import ImageTranslator
from .pifm import PiFMTranslator
__all__ = ['BEodfTranslator', 'BEPSndfTranslator', 'BEodfRelaxationTranslator',
'GIVTranslator', 'GLineTranslator', 'GTuneTranslator', 'GDMTranslator',
'SporcTranslator', 'MovieTranslator', 'IgorIBWTranslator',
'OneViewTranslator', 'NDataTranslator', 'FakeBEPSGenerator', 'PtychographyTranslator',
'LabViewH5Patcher', 'TRKPFMTranslator', 'BrukerAFMTranslator', 'ImageTranslator']
'LabViewH5Patcher', 'TRKPFMTranslator', 'BrukerAFMTranslator', 'ImageTranslator',
'PiFMTranslator', 'NanonisTranslator']
\ No newline at end of file
......@@ -13,7 +13,7 @@ from igor import binarywave as bw
from pyUSID.io.translator import Translator, \
generate_dummy_main_parms # Because this class extends the abstract Translator class
from pyUSID.io.write_utils import VALUES_DTYPE, Dimension, clean_string_att
from pyUSID.io.write_utils import VALUES_DTYPE, Dimension
from pyUSID.io.hdf_utils import create_indexed_group, write_main_dataset, write_simple_attrs, write_ind_val_dsets
......@@ -22,7 +22,8 @@ class IgorIBWTranslator(Translator):
Translates Igor Binary Wave (.ibw) files containing images or force curves to .h5
"""
def translate(self, file_path, verbose=False, parm_encoding='utf-8'):
def translate(self, file_path, verbose=False, append_path='',
grp_name='Measurement', parm_encoding='utf-8'):
"""
Translates the provided file to .h5
......@@ -32,6 +33,10 @@ class IgorIBWTranslator(Translator):
Absolute path of the .ibw file
verbose : Boolean (Optional)
Whether or not to show print statements for debugging
append_path : string (Optional)
h5_file to add these data to, must be a path to the h5_file on disk
grp_name : string (Optional)
Change from default "Measurement" name to something specific
parm_encoding : str, optional
Codec to be used to decode the bytestrings into Python strings if needed.
Default 'utf-8'
......@@ -45,11 +50,18 @@ class IgorIBWTranslator(Translator):
# Prepare the .h5 file:
folder_path, base_name = path.split(file_path)
base_name = base_name[:-4]
h5_path = path.join(folder_path, base_name + '.h5')
if path.exists(h5_path):
remove(h5_path)
h5_file = h5py.File(h5_path, 'w')
if not append_path:
h5_path = path.join(folder_path, base_name + '.h5')
if path.exists(h5_path):
remove(h5_path)
h5_file = h5py.File(h5_path, 'w')
else:
h5_path = append_path
if not path.exists(append_path):
raise Exception('File does not exist. Check pathname.')
h5_file = h5py.File(h5_path, 'r+')
# Load the ibw file first
ibw_obj = bw.load(file_path)
......@@ -111,7 +123,7 @@ class IgorIBWTranslator(Translator):
spec_desc = Dimension('Z', 'm', spec_data)
# Create measurement group
meas_grp = create_indexed_group(h5_file, 'Measurement')
meas_grp = create_indexed_group(h5_file, grp_name)
# Write file and measurement level parameters
global_parms = generate_dummy_main_parms()
......
......@@ -3,19 +3,54 @@ import numpy as np
from pyUSID.io.translator import Translator
from pyUSID.io import write_utils
from pyUSID import USIDataset
import pyUSID as usid
import h5py
class PiFMTranslator(Translator):
"""
Class that writes images, spectrograms, point spectra and associated ancillary data sets to h5 file in pyUSID data
structure.
"""
def __init__(self, path=None):
self.path = path
# super(HyperspectralTranslator, self).__init__(*args, **kwargs)
def get_path(self):
def translate(self, path, append_path='', grp_name='Measurement'):
"""
Parameters
----------
file_path : String / unicode
Absolute path of the .ibw file
verbose : Boolean (Optional)
Whether or not to show print statements for debugging
append_path : string (Optional)
h5_file to add these data to, must be a path to the h5_file on disk
parm_encoding : str, optional
Codec to be used to decode the bytestrings into Python strings if needed.
Default 'utf-8'
Returns
-------
h5_path : String / unicode
Absolute path of the .h5 file
"""
self.get_path(path)
self.read_anfatec_params()
self.read_file_desc()
self.read_spectrograms()
self.read_imgs()
self.read_spectra()
self.make_pos_vals_inds_dims()
self.create_hdf5_file(append_path, grp_name)
self.write_spectrograms()
self.write_images()
self.write_spectra()
return self.h5_f
def get_path(self, path):
"""writes full path, directory, and file name as attributes to class"""
# get paths/get params dictionary, img/spectrogram/spectrum descriptions
self.path = path
full_path = os.path.realpath(self.path)
directory = os.path.dirname(full_path)
# file name
......@@ -149,16 +184,26 @@ class PiFMTranslator(Translator):
usid.write_utils.Dimension('Y', self.params_dictionary['YPhysUnit'].replace('\xb5', 'u'), self.y_len)]
self.pos_ind, self.pos_val, self.pos_dims = pos_ind, pos_val, pos_dims
def create_hdf5_file(self):
h5_path = os.path.join(self.directory, self.basename.replace('.txt', '.h5'))
try:
self.h5_f = h5py.File(h5_path, mode='w')
#if file already exists. (maybe there is a better way to check for this)
except OSError:
self.h5_f = h5py.File(h5_path, mode='r+')
self.h5_meas_grp = usid.hdf_utils.create_indexed_group(self.h5_f, 'Measurement_')
def create_hdf5_file(self, append_path='', grp_name='Measurement'):
if not append_path:
h5_path = os.path.join(self.directory, self.basename.replace('.txt', '.h5'))
if os.path.exists(h5_path):
raise FileExistsError
#if file already exists. (maybe there is a better way to check for this)
else:
self.h5_f = h5py.File(h5_path, mode='w')
else:
if not os.path.exists(append_path):
raise Exception('File does not exist. Check pathname.')
self.h5_f = h5py.File(append_path, mode='r+')
self.h5_meas_grp = usid.hdf_utils.create_indexed_group(self.h5_f, grp_name)
usid.hdf_utils.write_simple_attrs(self.h5_meas_grp, self.params_dictionary)
return
def write_spectrograms(self):
if bool(self.spectrogram_desc):
for spectrogram_f, descriptors in self.spectrogram_desc.items():
......@@ -288,19 +333,3 @@ class PiFMTranslator(Translator):
'YLoc': descriptors[1]})
h5_raw[:, :] = self.spectra[spec_f].reshape(h5_raw.shape)
def translate(self):
"""
:return: h5 file.
"""
self.get_path()
self.read_anfatec_params()
self.read_file_desc()
self.read_spectrograms()
self.read_imgs()
self.read_spectra()
self.make_pos_vals_inds_dims()
self.create_hdf5_file()
self.write_spectrograms()
self.write_images()
self.write_spectra()
return self.h5_f
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment