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 221 222 223 224
|
# -*- 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>.
"""Creates files replicating Renishaw's .wdf files metadata structure (PSET Blocks)."""
import numpy as np
from rsciio.renishaw._api import (
MetadataFlags,
MetadataTypeMulti,
MetadataTypeSingle,
TypeNames,
WDFReader,
)
# logging.basicConfig(level=10)
def _write_numeric(file, data, type):
if type not in TypeNames.keys():
raise ValueError(
f"Trying to write number with unknown dataformat.\n"
f"Input: {type}\n"
f"Supported types: {list(TypeNames.keys())}"
)
np.array(data).astype(TypeNames[type]).tofile(file)
def _write_string(file, input_str, encoding="utf-8"):
file.write(input_str.encode(encoding))
def _write_flag(file, type):
_write_numeric(file, MetadataFlags[type].value, "uint8")
def _write_type(file, type):
if type in MetadataTypeSingle._member_names_:
_write_string(file, MetadataTypeSingle[type].value)
elif type in MetadataTypeMulti._member_names_:
_write_string(file, MetadataTypeMulti[type].value)
else:
raise ValueError(f"Invalid type {type}")
def _write_key(file, number):
_write_numeric(file, number, "uint16")
def _write_header(file, id, uid, size):
_write_string(file, id)
_write_numeric(file, uid, "uint32")
_write_numeric(file, size, "uint64")
_write_numeric(file, int(0x54455350), "uint32") # STREAM_IS_PSET
_write_numeric(file, size - 24, "uint32") # PSET_SIZE
def _write_pset_single(file, type, key, val):
_write_type(file, type)
_write_flag(file, "normal")
_write_key(file, key)
_write_numeric(file, val, type) # value
def _write_pset_str(file, key, val):
_write_type(file, "string")
_write_flag(file, "normal")
_write_key(file, key)
_write_numeric(file, len(val), "uint32")
_write_string(file, val)
def _write_pset_key(file, key, val):
_write_type(file, "key")
_write_flag(file, "normal")
_write_key(file, key)
_write_numeric(file, len(val), "uint32")
_write_string(file, val)
def _write_pset_bin(file, key, val):
_write_type(file, "binary")
_write_flag(file, "normal")
_write_key(file, key)
_write_numeric(file, len(val), "uint32")
_write_string(file, val, encoding="ascii")
def _write_pset_nested(file, key, length):
_write_type(file, "nested")
_write_flag(file, "normal")
_write_key(file, key)
_write_numeric(file, length, "uint32")
def _write_pset_array(file, type, key, data):
_write_type(file, type)
_write_flag(file, "array")
_write_key(file, key)
_write_numeric(file, len(data), "uint32")
for entry in data:
_write_numeric(file, entry, type)
def _write_pset_compressed(file, type, key, data):
_write_type(file, type)
_write_flag(file, "compressed")
_write_key(file, key)
_write_numeric(file, len(data), "uint32")
_write_string(file, data)
class WDFFileGenerator:
def __init__(self):
pass
@staticmethod
def generate_flat_array_compressed(f):
_write_header(f, "TEST", 0, 97)
_write_pset_key(f, 1, "array")
_write_pset_key(f, 2, "compressed")
_write_pset_compressed(f, "string", 2, "abcdef")
_write_pset_array(f, "int16", 1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
@staticmethod
def generate_nested_normal_testfile(f):
_write_header(f, "TEST", 0, 367)
_write_pset_key(f, 47612, "pair_before_nested")
_write_pset_single(f, "int16", 47612, -10)
_write_pset_key(f, 234, "key_before_nested")
_write_pset_single(f, "int32", 463, 17)
## START nested (key before)
_write_pset_key(f, 9874, "nested1")
_write_pset_nested(f, 9874, 76)
_write_pset_key(f, 1, "pair_in_nested")
_write_pset_key(f, 47612, "pair_in_nested_doubled_key")
_write_pset_single(f, "int32", 1, 40891)
_write_pset_single(f, "int64", 47612, -8279)
## END nested 1
_write_pset_single(f, "uint8", 234, 87)
_write_pset_key(f, 463, "val_before_nested")
## START nested2 (key after)
_write_pset_nested(f, 99, 126)
_write_pset_key(f, 1, "pair_in_nested_doubled_key")
_write_pset_single(f, "int8", 1, 43)
## START nested3
_write_pset_key(f, 1234, "nested3")
_write_pset_nested(f, 1234, 41)
_write_pset_key(f, 1, "pair_in_double_nested")
_write_pset_single(f, "int64", 1, -123)
## END nested 3
## START nested 4
_write_pset_nested(f, 456, 0)
_write_pset_key(f, 456, "nested4")
## END nested 4
## END nested 2
_write_pset_key(f, 99, "nested2")
@staticmethod
def generate_flat_normal_testfile(f):
_write_header(f, "TEST", 0, 311)
_write_pset_single(f, "int8", 123, -12)
_write_pset_key(f, 123, "single->key")
_write_pset_key(f, 456, "key->single")
_write_pset_single(f, "uint8", 456, 46)
_write_pset_single(f, "int16", 1, 72)
_write_pset_single(f, "int32", 2, 379)
_write_pset_single(f, "int64", 3, 347)
_write_pset_key(f, 1, "order1")
_write_pset_key(f, 3, "order3")
_write_pset_key(f, 2, "order2")
_write_pset_key(f, 4, "order4")
_write_pset_key(f, 5, "order5")
_write_pset_key(f, 6, "order6")
_write_pset_single(f, "float", 4, 74.7843)
_write_pset_single(f, "double", 6, -378.36)
_write_pset_single(f, "windows_filetime", 5, 8043148)
_write_pset_str(f, 10, "test string 123")
_write_pset_key(f, 10, "key for test string")
_write_pset_key(f, 11, "key for binary")
_write_pset_bin(f, 11, "binary string 123")
class WDFFileHandler:
def __init__(self, filepath):
self.filepath = filepath
def write_file(self, func):
if self.filepath.exists():
self.filepath.unlink()
self.filepath.parent.mkdir(exist_ok=True)
with open(self.filepath, "wb+") as file:
func(file)
def read_file(self):
with open(self.filepath, "rb") as file:
wdf = WDFReader(
file,
self.filepath.name,
use_uniform_signal_axis=True,
load_unmatched_metadata=False,
)
filesize = self.filepath.stat().st_size
wdf._block_info = wdf.locate_all_blocks(filesize)
wdf._parse_metadata("TEST_0")
return wdf.original_metadata
|