1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
# -*- coding: utf-8 -*-
# Copyright 2007-2023 The HyperSpy developers
#
# This file is part of RosettaSciIO.
#
# RosettaSciIO is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RosettaSciIO is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RosettaSciIO. If not, see <https://www.gnu.org/licenses/#GPL>.
import logging
import os
import numpy as np
from rsciio._docstrings import FILENAME_DOC, LAZY_UNSUPPORTED_DOC, RETURNS_DOC
_logger = logging.getLogger(__name__)
try:
from netCDF4 import Dataset as netcdf_file_reader
netcdf_reader = "netCDF4"
except Exception:
try:
from scipy.io import netcdf_file as netcdf_file_reader
netcdf_reader = "scipy"
except Exception:
netcdf_reader = None
attrib2netcdf = {
"energyorigin": "energy_origin",
"energyscale": "energy_scale",
"energyunits": "energy_units",
"xorigin": "x_origin",
"xscale": "x_scale",
"xunits": "x_units",
"yorigin": "y_origin",
"yscale": "y_scale",
"yunits": "y_units",
"zorigin": "z_origin",
"zscale": "z_scale",
"zunits": "z_units",
"exposure": "exposure",
"title": "title",
"binning": "binning",
"readout_frequency": "readout_frequency",
"ccd_height": "ccd_height",
"blanking": "blanking",
}
acquisition2netcdf = {
"exposure": "exposure",
"binning": "binning",
"readout_frequency": "readout_frequency",
"ccd_height": "ccd_height",
"blanking": "blanking",
"gain": "gain",
"pppc": "pppc",
}
treatments2netcdf = {
"dark_current": "dark_current",
"readout": "readout",
}
def file_reader(filename, lazy=False):
"""
Read netCDF ``.nc`` files saved using the HyperSpy predecessor EELSlab.
Parameters
----------
%s
%s
%s
"""
if netcdf_reader is None:
raise ImportError(
"No netCDF library installed. "
"To read EELSLab netcdf files install "
"one of the following packages:"
"netCDF4 or scipy."
)
if lazy is not False:
raise NotImplementedError("Lazy loading is not supported.")
ncfile = netcdf_file_reader(filename, "r")
if (
hasattr(ncfile, "file_format_version")
and ncfile.file_format_version == "EELSLab 0.1"
):
dictionary = nc_hyperspy_reader_0dot1(ncfile, filename)
else:
ncfile.close()
raise IOError("Unsupported netCDF file")
return (dictionary,)
file_reader.__doc__ %= (FILENAME_DOC, LAZY_UNSUPPORTED_DOC, RETURNS_DOC)
def nc_hyperspy_reader_0dot1(ncfile, filename):
calibration_dict, acquisition_dict, treatments_dict = {}, {}, {}
dc = ncfile.variables["data_cube"]
data = dc[:]
if "history" in calibration_dict:
calibration_dict["history"] = eval(ncfile.history)
for attrib in attrib2netcdf.items():
if hasattr(dc, attrib[1]):
value = eval("dc." + attrib[1])
if isinstance(value, np.ndarray):
calibration_dict[attrib[0]] = value[0]
else:
calibration_dict[attrib[0]] = value
else:
_logger.warning(
"Warning: the attribute '%s' is not defined in " "the file '%s'",
attrib[0],
filename,
)
for attrib in acquisition2netcdf.items():
if hasattr(dc, attrib[1]):
value = eval("dc." + attrib[1])
if isinstance(value, np.ndarray):
acquisition_dict[attrib[0]] = value[0]
else:
acquisition_dict[attrib[0]] = value
else:
_logger.warning(
"Warning: the attribute '%s' is not defined in " "the file '%s'",
attrib[0],
filename,
)
for attrib in treatments2netcdf.items():
if hasattr(dc, attrib[1]):
treatments_dict[attrib[0]] = eval("dc." + attrib[1])
else:
_logger.warning(
"Warning: the attribute '%s' is not defined in " "the file '%s'",
attrib[0],
filename,
)
original_metadata = {
"record_by": ncfile.type,
"calibration": calibration_dict,
"acquisition": acquisition_dict,
"treatments": treatments_dict,
}
ncfile.close()
# Now we'll map some parameters
record_by = "image" if original_metadata["record_by"] == "image" else "spectrum"
if record_by == "image":
dim = len(data.shape)
names = ["Z", "Y", "X"][3 - dim :]
scaleskeys = ["zscale", "yscale", "xscale"]
originskeys = ["zorigin", "yorigin", "xorigin"]
unitskeys = ["zunits", "yunits", "xunits"]
navigate = [True, False, False]
elif record_by == "spectrum":
dim = len(data.shape)
names = ["Y", "X", "Energy"][3 - dim :]
scaleskeys = ["yscale", "xscale", "energyscale"]
originskeys = ["yorigin", "xorigin", "energyorigin"]
unitskeys = ["yunits", "xunits", "energyunits"]
navigate = [True, True, False]
# The images are recorded in the Fortran order
data = data.T.copy()
try:
scales = [calibration_dict[key] for key in scaleskeys[3 - dim :]]
except KeyError:
scales = [1, 1, 1][3 - dim :]
try:
origins = [calibration_dict[key] for key in originskeys[3 - dim :]]
except KeyError:
origins = [0, 0, 0][3 - dim :]
try:
units = [calibration_dict[key] for key in unitskeys[3 - dim :]]
except KeyError:
units = ["", "", ""]
axes = [
{
"size": int(data.shape[i]),
"index_in_array": i,
"name": names[i],
"scale": scales[i],
"offset": origins[i],
"units": units[i],
"navigate": navigate[i],
}
for i in range(dim)
]
metadata = {"General": {}, "Signal": {}}
metadata["General"]["original_filename"] = os.path.split(filename)[1]
metadata["General"]["signal_type"] = ""
dictionary = {
"data": data,
"axes": axes,
"metadata": metadata,
"original_metadata": original_metadata,
}
return dictionary
|