| 1 |
""" |
| 2 |
.. module:: skrf.io.citi |
| 3 |
|
| 4 |
======================================== |
| 5 |
citi (:mod:`skrf.io.citi`) |
| 6 |
======================================== |
| 7 |
|
| 8 |
Citi class and utilities |
| 9 |
|
| 10 |
.. autosummary:: |
| 11 |
:toctree: generated/ |
| 12 |
|
| 13 |
Citi |
| 14 |
|
| 15 |
""" |
| 16 |
from __future__ import annotations |
| 17 |
|
| 18 |
import typing |
| 19 |
from pathlib import Path |
| 20 |
|
| 21 |
import numpy as np |
| 22 |
|
| 23 |
from ..frequency import Frequency |
| 24 |
from ..mathFunctions import magdeg_2_reim |
| 25 |
from ..network import Network, z2s |
| 26 |
from ..networkSet import NetworkSet |
| 27 |
from ..util import get_fid |
| 28 |
|
| 29 |
|
| 30 |
class Citi: |
| 31 |
""" |
| 32 |
Class to read CITI N-port files. |
| 33 |
|
| 34 |
CITI file (.cti) is a standardized data format, used for exchanging data |
| 35 |
between different computers and instruments. CITI file is an abbreviation |
| 36 |
for "Common Instrumentation Transfer and Interchange file" [#]_ . |
| 37 |
|
| 38 |
|
| 39 |
Parameters |
| 40 |
---------- |
| 41 |
file : str or file-object |
| 42 |
mdif file to load |
| 43 |
|
| 44 |
References |
| 45 |
---------- |
| 46 |
.. [#] https://na.support.keysight.com/plts/help/WebHelp/FilePrint/CITIfile_Format.htm |
| 47 |
.. [#] Handbook of Microwave Component Measurements: with Advanced VNA Techniques, Joel P. Dunsmore, 2020, |
| 48 |
Section 6.1.6.1 |
| 49 |
|
| 50 |
Examples |
| 51 |
-------- |
| 52 |
From filename |
| 53 |
|
| 54 |
>>> m = rf.Citi('network.cti') |
| 55 |
|
| 56 |
From file-object |
| 57 |
|
| 58 |
>>> file = open('network.cti') |
| 59 |
>>> m = rf.Citi(file) |
| 60 |
""" |
| 61 |
def __init__(self, file: str | Path | typing.TextIO): |
| 62 |
""" |
| 63 |
Constructor |
| 64 |
|
| 65 |
Parameters |
| 66 |
---------- |
| 67 |
file : str, Path, or file-object |
| 68 |
mdif file to load |
| 69 |
""" |
| 70 |
self._comments = [] |
| 71 |
self._name = '' |
| 72 |
self._params = dict() |
| 73 |
self._data = dict() |
| 74 |
|
| 75 |
with get_fid(file) as fid: |
| 76 |
self.filename = fid.name |
| 77 |
self._parse_citi(fid.readlines()) |
| 78 |
|
| 79 |
@property |
| 80 |
def comments(self) -> list: |
| 81 |
""" |
| 82 |
Comments defined in the CITI file. |
| 83 |
|
| 84 |
Returns |
| 85 |
------- |
| 86 |
list : list of strings |
| 87 |
|
| 88 |
""" |
| 89 |
return self._comments |
| 90 |
|
| 91 |
@property |
| 92 |
def name(self) -> str: |
| 93 |
""" |
| 94 |
Name of the CITI package. |
| 95 |
|
| 96 |
Returns |
| 97 |
------- |
| 98 |
name : str |
| 99 |
Name of the CITI package |
| 100 |
|
| 101 |
""" |
| 102 |
return self._name |
| 103 |
|
| 104 |
@property |
| 105 |
def params(self) -> list: |
| 106 |
""" |
| 107 |
Named parameters list. |
| 108 |
|
| 109 |
This list excludes the "FREQ" parameter, which is always defined. |
| 110 |
|
| 111 |
Returns |
| 112 |
------- |
| 113 |
list : list of string |
| 114 |
List of the named parameters defined in the CITI file |
| 115 |
|
| 116 |
""" |
| 117 |
|
| 118 |
params = self._params.keys() |
| 119 |
return [param for param in params if param.lower() != 'freq'] |
| 120 |
|
| 121 |
def _parse_citi(self, lines): |
| 122 |
""" |
| 123 |
Parse the CITI file. |
| 124 |
|
| 125 |
Parameters |
| 126 |
---------- |
| 127 |
lines : list of str |
| 128 |
Lines of the file |
| 129 |
|
| 130 |
Raises |
| 131 |
------ |
| 132 |
NotImplementedError |
| 133 |
If the number formatting is not recognized |
| 134 |
|
| 135 |
""" |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
params_list = [] |
| 140 |
data_list = [] |
| 141 |
order = 0 |
| 142 |
|
| 143 |
while lines: |
| 144 |
line = lines.pop(0) |
| 145 |
|
| 146 |
if line.strip().startswith(('#','!')): |
| 147 |
self._comments.append(line) |
| 148 |
|
| 149 |
if line.strip().upper().startswith('NAME'): |
| 150 |
self._name = line.strip()[5:] |
| 151 |
|
| 152 |
if line.strip().upper().startswith('VAR '): |
| 153 |
|
| 154 |
|
| 155 |
_, name, formt, occ = line.strip().split(' ') |
| 156 |
|
| 157 |
if name.lower() == 'freq': |
| 158 |
name = 'freq' |
| 159 |
self._params[name] = {'format': formt, 'occurences': int(occ), |
| 160 |
'order': order} |
| 161 |
order += 1 |
| 162 |
params_list.append(name) |
| 163 |
|
| 164 |
if line.strip().upper().startswith('DATA'): |
| 165 |
|
| 166 |
|
| 167 |
_, name, formt = line.strip().split(' ') |
| 168 |
self._data[name] = {'format': formt} |
| 169 |
data_list.append(name) |
| 170 |
|
| 171 |
if line.strip().upper().startswith('VAR_LIST_BEGIN'): |
| 172 |
|
| 173 |
_param_values = [] |
| 174 |
cur_name = params_list.pop(0) |
| 175 |
for _idx in range(self._params[cur_name]['occurences']): |
| 176 |
line = lines.pop(0) |
| 177 |
|
| 178 |
_param_values.append(line.strip()) |
| 179 |
|
| 180 |
self._params[cur_name]['values'] = np.array(_param_values, dtype=float) |
| 181 |
|
| 182 |
if line.upper().startswith('BEGIN'): |
| 183 |
_data_values = [] |
| 184 |
cur_name = data_list.pop(0) |
| 185 |
|
| 186 |
|
| 187 |
nb_lines = np.prod([self._params[name]['occurences'] for name in self._params.keys()]) |
| 188 |
for _idx in range(nb_lines): |
| 189 |
line = lines.pop(0) |
| 190 |
|
| 191 |
|
| 192 |
_data_values.append([el.strip() for el in line.split(',')]) |
| 193 |
|
| 194 |
|
| 195 |
_data = np.array(_data_values, dtype=float) |
| 196 |
|
| 197 |
if self._data[cur_name]['format'].upper() == 'RI': |
| 198 |
values = _data[:,0] + 1j*_data[:,1] |
| 199 |
elif self._data[cur_name]['format'].upper() == 'MAGANGLE': |
| 200 |
values = magdeg_2_reim(_data[:,0], _data[:,1]) |
| 201 |
elif self._data[cur_name]['format'].upper() == 'DBANGLE': |
| 202 |
values = ((10**(_data[:,0]/20.0)) * np.exp(1j*np.pi/180 * _data[:,1])) |
| 203 |
else: |
| 204 |
raise NotImplementedError('Not implemented format case') |
| 205 |
|
| 206 |
self._data[cur_name]['values'] = values |
| 207 |
|
| 208 |
@property |
| 209 |
def networks(self) -> list: |
| 210 |
""" |
| 211 |
Return the list of Networks read from the CITI file. |
| 212 |
|
| 213 |
Returns |
| 214 |
------- |
| 215 |
list : list of `skrf.network.Network` |
| 216 |
List of Networks described in the CITI file |
| 217 |
|
| 218 |
""" |
| 219 |
networks = [] |
| 220 |
|
| 221 |
|
| 222 |
if 'freq' not in [it.lower() for it in self._params.keys()]: |
| 223 |
raise ValueError('Frequency points not found') |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
if len(self._params.keys()) == 1 and 'freq' in self._params.keys(): |
| 228 |
self._params["dummy"] = {} |
| 229 |
self._params["dummy"]['values'] = np.array([0]) |
| 230 |
self._params["dummy"]['occurences'] = 1 |
| 231 |
|
| 232 |
freq = Frequency.from_f(self._params['freq']['values'], unit='Hz') |
| 233 |
|
| 234 |
|
| 235 |
if any([it.startswith('S') for it in self._data.keys()]): |
| 236 |
ntwkprm = 'S' |
| 237 |
elif any([it.startswith('Z') for it in self._data.keys()]): |
| 238 |
ntwkprm = 'Z' |
| 239 |
else: |
| 240 |
raise NotImplementedError('No network parameter found in this file') |
| 241 |
|
| 242 |
|
| 243 |
rank = int(np.sqrt(len([it for it in self._data.keys() if it.startswith(ntwkprm)]))) |
| 244 |
|
| 245 |
|
| 246 |
occurences = [self._params[name]['occurences'] for name in self.params] |
| 247 |
occ = np.prod(occurences) |
| 248 |
|
| 249 |
|
| 250 |
p = np.zeros((occ, len(freq), rank, rank), dtype=complex) |
| 251 |
|
| 252 |
|
| 253 |
if self.params: |
| 254 |
params_sets = np.array(np.meshgrid(*[self._params[name]['values'] |
| 255 |
for name in self.params])).reshape(-1,len(self.params)) |
| 256 |
else: |
| 257 |
params_sets = [] |
| 258 |
|
| 259 |
|
| 260 |
z0s = np.full((occ, len(freq), rank), 50, dtype=complex) |
| 261 |
if any(k.upper().startswith('PORTZ') for k in self._data.keys()): |
| 262 |
|
| 263 |
if any(k.startswith('PortZ') for k in self._data.keys()): |
| 264 |
zname = 'PortZ' |
| 265 |
else: |
| 266 |
zname = 'PORTZ' |
| 267 |
|
| 268 |
for m in range(rank): |
| 269 |
for idx_set in range(len(params_sets)): |
| 270 |
z0s[idx_set,:,m] = self._data[f'{zname}[{m+1}]']['values'].reshape((int(occ), len(freq)))[idx_set,:] |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
for m in range(rank): |
| 281 |
for n in range(rank): |
| 282 |
|
| 283 |
if f'{ntwkprm}[{m+1},{n+1}]' in self._data.keys(): |
| 284 |
pp = self._data[f'{ntwkprm}[{m+1},{n+1}]']['values'].reshape((int(occ), len(freq))) |
| 285 |
else: |
| 286 |
|
| 287 |
pp = self._data['S']['values'].reshape((int(occ), len(freq))) |
| 288 |
ntwkprm = 'S' |
| 289 |
|
| 290 |
|
| 291 |
for idx_set in range(len(params_sets)): |
| 292 |
p[idx_set,:,m,n] = pp[idx_set,:] |
| 293 |
|
| 294 |
|
| 295 |
for (idx_set, params_set) in enumerate(params_sets): |
| 296 |
|
| 297 |
params = dict(zip(self.params, params_set)) |
| 298 |
|
| 299 |
if ntwkprm == 'S': |
| 300 |
ntwk = Network(frequency=freq, s=p[idx_set], params=params, z0=z0s[idx_set]) |
| 301 |
elif ntwkprm == 'Z': |
| 302 |
ntwk = Network(frequency=freq, s=z2s(p[idx_set], z0s[idx_set]), params=params, z0=z0s[idx_set]) |
| 303 |
else: |
| 304 |
raise NotImplementedError('Unknown Network Parameter') |
| 305 |
networks.append(ntwk) |
| 306 |
|
| 307 |
return networks |
| 308 |
|
| 309 |
|
| 310 |
def to_networkset(self) -> NetworkSet: |
| 311 |
""" |
| 312 |
Convert the CITI file data into a NetworkSet. |
| 313 |
|
| 314 |
Returns |
| 315 |
------- |
| 316 |
ns : `skrf.networkset.NetworkSet` |
| 317 |
|
| 318 |
""" |
| 319 |
return NetworkSet(self.networks) |