File: util.py

package info (click to toggle)
python-polsarpro 2026.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 17,024 kB
  • sloc: python: 3,830; xml: 293; sh: 91; javascript: 18; makefile: 3
file content (609 lines) | stat: -rw-r--r-- 20,731 bytes parent folder | download | duplicates (2)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
"""
This code is part of the Python PolSARpro software:

"A re-implementation of selected PolSARPro functions in Python,
following the scientific recommendations of PolInSAR 2021"

developed within an ESA funded project with SATIM.

Author: Olivier D'Hondt, 2025.
Scientific advisors: Armando Marino and Eric Pottier.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import logging
import numpy as np
from scipy.ndimage import convolve
import dask.array as da
# from bench import timeit
import xarray as xr
import xarray

log = logging.getLogger(__name__)


# @timeit
def boxcar(img: np.ndarray, dim_az: int, dim_rg: int) -> np.ndarray:
    """
    Apply a boxcar filter to an image.

    Args:
        img (complex or real array): Input image with arbitrary number of dimensions, shape (naz, nrg, ...).
        dim_az (int): Size in azimuth of the filter.
        dim_rg (int): Size in range of the filter.

    Returns:
        complex or real array: Filtered image, shape (naz, nrg, ...).

    Note:
        The filter is always applied along 2 dimensions (azimuth, range). Please ensure to provide a valid image.
    """
    if type(dim_az) != int and type(dim_rg) != int:
        raise ValueError("dimaz and dimrg must be integers")
    if (dim_az < 1) or (dim_rg < 1):
        raise ValueError("dimaz and dimrg must be strictly positive")
    if img.ndim < 2:
        raise ValueError("Input must be at least of dimension 2")

    return _boxcar_core(img, dim_az, dim_rg)


# @timeit
def boxcar_dask(img: np.ndarray, dim_az: int, dim_rg: int) -> np.ndarray:
    """
    Apply a boxcar filter to an image.

    Args:
        img (complex or real array): Input image with arbitrary number of dimensions, shape (naz, nrg, ...).
        dim_az (int): Size in azimuth of the filter.
        dim_rg (int): Size in range of the filter.

    Returns:
        complex or real array: Filtered image, shape (naz, nrg, ...).

    Note:
        The filter is always applied along 2 dimensions (azimuth, range). Please ensure to provide a valid image.
    """
    if type(dim_az) != int and type(dim_rg) != int:
        raise ValueError("dimaz and dimrg must be integers")
    if (dim_az < 1) or (dim_rg < 1):
        raise ValueError("dimaz and dimrg must be strictly positive")
    if img.ndim < 2:
        raise ValueError("Input must be at least of dimension 2")

    process_args = dict(
        dim_az=dim_az,
        dim_rg=dim_rg,
        depth=(dim_az, dim_rg),
    )
    da_in = da.map_overlap(
        _boxcar_core,
        # da.from_array(img, chunks=(500, 500, 3, 3)),
        da.from_array(img, chunks="auto"),
        **process_args,
        dtype="complex64",
    )

    return np.asarray(da_in)


def boxcar_xarray(img: xarray.Dataset, dim_az: int, dim_rg: int) -> xarray.Dataset:
    """
    Apply a boxcar filter to an image.

    Args:
        img (complex or real array): Input image with arbitrary number of dimensions, shape (naz, nrg, ...).
        dim_az (int): Size in azimuth of the filter.
        dim_rg (int): Size in range of the filter.

    Returns:
        complex or real array: Filtered image, shape (naz, nrg, ...).

    Note:
        The filter is always applied along 2 dimensions (azimuth, range). Please ensure to provide a valid image.
    """
    if "x" in img.dims and "y" in img.dims:
        # pad the data with zeros
        res = img.pad(dict(x=dim_rg, y=dim_az), mode="constant", constant_values=0)
        # compute rolling mean
        res = res.rolling(x=dim_rg, y=dim_az, center=True).mean()
        # trim the padded borders
        res = res.isel(x=slice(dim_rg, -dim_rg), y=slice(dim_az, -dim_az))
        return res
        # return img.rolling(x=dim_rg, y=dim_az, center=True).mean()
    else:
        raise ValueError("'x' and 'y' must be in the dimensions of the input data.")


def _boxcar_core(img: np.ndarray, dim_az: int, dim_rg: int) -> np.ndarray:
    n_extra_dims = img.ndim - 2

    ker_dtype = img.dtype if not np.iscomplexobj(img) else img.real.dtype

    # this convolution mode reduces error between C and python implementations
    mode = "constant"
    if (dim_az > 1) or (dim_rg > 1):
        # avoid nan propagation
        msk = np.isnan(img)
        img_ = img.copy()
        img_[msk] = 0
        ker = np.ones((dim_az, dim_rg), dtype=ker_dtype) / (dim_az * dim_rg)
        ker = np.expand_dims(ker, axis=tuple(range(2, 2 + n_extra_dims)))
        if np.iscomplexobj(img_):
            imgout = convolve(img_.real, ker, mode=mode) + 1j * convolve(
                img_.imag, ker, mode=mode
            )
            imgout[msk] = np.nan + 1j * np.nan
        else:
            imgout = convolve(img_, ker, mode=mode)
            imgout[msk] = np.nan
        return imgout
    else:
        return img


# @timeit
def multilook(img: np.ndarray, dim_az: int, dim_rg: int) -> np.ndarray:
    """
    Computes the m by n presummed image.

    Args:
        img (array-like): Input image array with shape (naz, nrg,...).
        dim_az (int): Number of lines to sum. Must be an integer >= 1.
        dim_rg (int): Number of columns to sum. Must be an integer >= 1.

    Returns:
        array: Presummed image array with shape (M, N,...), where M and N are the largest multiples of dim_az and dim_rg that are less than or equal to img.shape[0] and img.shape[1], respectively.
    Note:
        Returns the input array if dim_az==1 and dim_rg==1.
    """
    # Check if m and n are integers >= 1
    if not isinstance(dim_az, int) or not isinstance(dim_rg, int):
        raise TypeError("Parameters m and n must be integers.")
    if dim_az < 1 or dim_rg < 1:
        raise ValueError(
            "Parameters m and n must be integers greater than or equal to 1."
        )

    # Check if m and n are valid in relation to the image dimensions
    if dim_az > img.shape[0] or dim_rg > img.shape[1]:
        raise ValueError(
            "Cannot presum with these parameters; m or n is too large for the image dimensions."
        )

    # skip if m = n = 1, avoids conditionals in calls
    if (dim_az > 1) or (dim_rg > 1):
        M = (img.shape[0] // dim_az) * dim_az
        N = (img.shape[1] // dim_rg) * dim_rg

        img_trimmed = img[:M, :N]

        s = img_trimmed[::dim_az].copy()  # Make a copy once for efficiency
        for i in range(1, dim_az):
            s += img_trimmed[i::dim_az]

        t = s[:, ::dim_rg].copy()
        for j in range(1, dim_rg):
            t += s[:, j::dim_rg]

        return t / float(dim_az * dim_rg)
    else:
        return img


def S_to_C3(S: np.ndarray) -> np.ndarray:
    """Converts the Sinclair scattering matrix S to the lexicographic covariance matrix C3.

    Args:
        S (np.ndarray): input image of scattering matrices with shape (naz, nrg, 2, 2)

    Returns:
        np.ndarray: C3 covariance matrix
    """
    if np.isrealobj(S):
        raise ValueError("Inputs must be complex-valued.")
    if S.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if S.shape[-2:] != (2, 2):
        raise ValueError("S must have a shape like (naz, nrg, 2, 2)")
    return _S_to_C3_core(S)


def S_to_C3_dask(S: np.ndarray) -> np.ndarray:
    """Converts the Sinclair scattering matrix S to the lexicographic covariance matrix C3.

    Args:
        S (np.ndarray): input image of scattering matrices with shape (naz, nrg, 2, 2)

    Returns:
        np.ndarray: C3 covariance matrix
    """
    if np.isrealobj(S):
        raise ValueError("Inputs must be complex-valued.")
    if S.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if S.shape[-2:] != (2, 2):
        raise ValueError("S must have a shape like (naz, nrg, 2, 2)")

    da_in = da.map_blocks(
        _S_to_C3_core,
        da.from_array(S, chunks="auto"),
        # da.from_array(S, chunks=(500, 500, -1, -1)),
        dtype="complex64",
    )

    return np.asarray(da_in)


def S_to_C3_xarray(S: xarray.Dataset) -> xarray.Dataset:
    """Converts the Sinclair scattering matrix S to the lexicographic covariance matrix C3.

    Args:
        S (xarray.Dataset): input image of scattering matrices with shape

    Returns:
        xarray.Dataset: C3 covariance matrix
    """

    if S.poltype == "S":
        # scattering vector, enforce type as in C version
        c = np.sqrt(np.float32(2))
        k1 = S.hh.astype("complex64", copy=False)
        k2 = ((S.hv + S.vh) / c).astype("complex64", copy=False)
        k3 = S.vv.astype("complex64", copy=False)

        # compute the Hermitian matrix elements
        C3_dict = {}

        # force real diagonal to save space
        C3_dict["m11"] = (k1 * k1.conj()).real
        C3_dict["m22"] = (k2 * k2.conj()).real
        C3_dict["m33"] = (k3 * k3.conj()).real

        # upper diagonal terms
        C3_dict["m12"] = k1 * k2.conj()
        C3_dict["m13"] = k1 * k3.conj()
        C3_dict["m23"] = k2 * k3.conj()

        attrs = {"poltype": "C3", "description": "Covariance matrix (3x3)"}
        return xr.Dataset(C3_dict, attrs=attrs)
    else:
        raise ValueError("Input polarimetric type must be 'S'")


def _S_to_C3_core(S: np.ndarray) -> np.ndarray:

    k = np.dstack(
        (S[..., 0, 0], (1.0 / np.sqrt(2)) * (S[..., 0, 1] + S[..., 1, 0]), S[..., 1, 1])
    )

    return vec_to_mat(k).astype("complex64")


def S_to_T3(S: np.ndarray) -> np.ndarray:
    """Converts the Sinclair scattering matrix S to the Pauli coherency matrix T3.

    Args:
        S (np.ndarray): input image of scattering matrices with shape (naz, nrg, 2, 2)

    Returns:
        np.ndarray: T3 coherency matrix
    """
    if S.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if S.shape[-2:] != (2, 2):
        raise ValueError("S must have a shape like (naz, nrg, 2, 2)")
    return _S_to_T3_core(S)


def S_to_T3_dask(S: np.ndarray) -> np.ndarray:
    """Converts the Sinclair scattering matrix S to the Pauli coherency matrix T3.

    Args:
        S (np.ndarray): input image of scattering matrices with shape (naz, nrg, 2, 2)

    Returns:
        np.ndarray: T3 coherency matrix
    """
    if S.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if S.shape[-2:] != (2, 2):
        raise ValueError("S must have a shape like (naz, nrg, 2, 2)")

    da_in = da.map_blocks(
        _S_to_T3_core,
        da.from_array(S, chunks="auto"),
        # da.from_array(S, chunks=(500, 500, -1, -1)),
        dtype="complex64",
    )

    return np.asarray(da_in)


def S_to_T3_xarray(S: xarray.Dataset) -> xarray.Dataset:
    """Converts the Sinclair scattering matrix S to the Pauli coherency matrix T3.

    Args:
        S (xarray.Dataset): input image of scattering matrices with shape

    Returns:
        xarray.Dataset: T3 covariance matrix
    """

    if S.poltype == "S":
        # scattering vector
        c = np.sqrt(np.float32(2))
        k1 = ((S.hh + S.vv) / c).astype("complex64", copy=False)
        k2 = ((S.hh - S.vv) / c).astype("complex64", copy=False)
        k3 = (c * S.hv).astype("complex64", copy=False)

        # compute the Hermitian matrix elements
        T3_dict = {}

        # force real diagonal to save space
        T3_dict["m11"] = (k1 * k1.conj()).real
        T3_dict["m22"] = (k2 * k2.conj()).real
        T3_dict["m33"] = (k3 * k3.conj()).real

        # upper diagonal terms
        T3_dict["m12"] = k1 * k2.conj()
        T3_dict["m13"] = k1 * k3.conj()
        T3_dict["m23"] = k2 * k3.conj()

        attrs = {"poltype": "T3", "description": "Coherency matrix (3x3)"}
        return xr.Dataset(T3_dict, attrs=attrs)
    else:
        raise ValueError("Input polarimetric type must be 'S'")


def _S_to_T3_core(S: np.ndarray) -> np.ndarray:

    k = np.dstack(
        (S[..., 0, 0] + S[..., 1, 1], S[..., 0, 0] - S[..., 1, 1], 2 * S[..., 0, 1])
    ) / np.sqrt(2)

    return vec_to_mat(k).astype("complex64")


# @timeit
def T3_to_C3(T3: np.ndarray) -> np.ndarray:
    """Converts the Pauli coherency matrix T3 to the lexicographic covariance matrix C3.

    Args:
        T3 (np.ndarray): input image of coherency matrices with shape (naz, nrg, 3, 3)

    Returns:
        np.ndarray: C3 covariance matrix
    """
    if np.isrealobj(T3):
        raise ValueError("Inputs must be complex-valued.")
    if T3.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if T3.shape[-2:] != (3, 3):
        raise ValueError("T3 must have a shape like (naz, nrg, 3, 3)")

    return _T3_to_C3_core(T3=T3)


# @timeit
def T3_to_C3_dask(T3: np.ndarray) -> np.ndarray:
    """Converts the Pauli coherency matrix T3 to the lexicographic covariance matrix C3.

    Args:
        T3 (np.ndarray): input image of coherency matrices with shape (naz, nrg, 3, 3)

    Returns:
        np.ndarray: C3 covariance matrix
    """
    if np.isrealobj(T3):
        raise ValueError("Inputs must be complex-valued.")
    if T3.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if T3.shape[-2:] != (3, 3):
        raise ValueError("T3 must have a shape like (naz, nrg, 3, 3)")

    da_in = da.map_blocks(
        _T3_to_C3_core,
        # da.from_array(T3, chunks=(500, 500, -1, -1)),
        da.from_array(T3, chunks="auto"),
        dtype="complex64",
    )

    return np.asarray(da_in)


def T3_to_C3_xarray(T3: xarray.Dataset) -> xarray.Dataset:
    """Converts the Pauli coherency matrix T3 to the lexicographic covariance matrix C3.

    Args:
        T3 (xarray.Dataset): input image of coherency matrices

    Returns:
        xarray.Dataset: C3 covariance matrix
    """

    if T3.poltype == "T3":

        C3_dict = {}

        c = 1 / np.sqrt(np.float32(2))

        # force real diagonal to save space
        C3_dict["m11"] = 0.5 * (T3.m11 + T3.m22) + T3.m12.real
        C3_dict["m22"] = T3.m33
        C3_dict["m33"] = 0.5 * (T3.m11 + T3.m22) - T3.m12.real

        # upper diagonal terms
        C3_dict["m12"] = c * (T3.m13 + T3.m23)
        C3_dict["m13"] = 0.5 * (T3.m11.real - T3.m22.real) - 1j * T3.m12.imag
        C3_dict["m23"] = c * (T3.m13.conj() - T3.m23.conj())

        attrs = {"poltype": "C3", "description": "Covariance matrix (3x3)"}
        return xr.Dataset(C3_dict, attrs=attrs)

        # TODO: remove once the function has been tested
        # T3.m13.real - 1j * T3.m13.imag - T3.m23.real + 1j * T3.m23.imag
        # C3[..., 0, 0] = (T3[..., 0, 0] + 2 * T3[..., 0, 1].real + T3[..., 1, 1]) / 2
        # C3[..., 0, 1] = (T3[..., 0, 2] + T3[..., 1, 2]) / np.sqrt(2)
        # C3[..., 0, 2] = (T3[..., 0, 0].real - T3[..., 1, 1].real) / 2
        # C3[..., 0, 2] += 1j * -T3[..., 0, 1].imag
        # C3[..., 1, 1] = T3.m33
        # C3[..., 1, 2] = (T3[..., 0, 2].real - T3[..., 1, 2].real) / np.sqrt(2)
        # C3[..., 1, 2] += 1j * (-T3[..., 0, 2].imag + T3[..., 1, 2].imag) / np.sqrt(2)
        # C3[..., 2, 2] = (T3[..., 0, 0] - 2 * T3[..., 0, 1].real + T3[..., 1, 1]) / 2

    else:
        raise ValueError("Input polarimetric type must be 'T3'")


def _T3_to_C3_core(T3: np.ndarray) -> np.ndarray:

    C3 = np.zeros_like(T3, dtype="complex64")

    # Reproject T3 matrix in the lexicographic basis
    C3[..., 0, 0] = (T3[..., 0, 0] + 2 * T3[..., 0, 1].real + T3[..., 1, 1]) / 2
    C3[..., 0, 1] = (T3[..., 0, 2] + T3[..., 1, 2]) / np.sqrt(2)
    C3[..., 0, 2] = (T3[..., 0, 0].real - T3[..., 1, 1].real) / 2
    C3[..., 0, 2] += 1j * -T3[..., 0, 1].imag
    C3[..., 1, 1] = T3[..., 2, 2]
    C3[..., 1, 2] = (T3[..., 0, 2].real - T3[..., 1, 2].real) / np.sqrt(2)
    C3[..., 1, 2] += 1j * (-T3[..., 0, 2].imag + T3[..., 1, 2].imag) / np.sqrt(2)
    C3[..., 2, 2] = (T3[..., 0, 0] - 2 * T3[..., 0, 1].real + T3[..., 1, 1]) / 2

    C3[..., 1, 0] = np.conj(C3[..., 0, 1])
    C3[..., 2, 0] = np.conj(C3[..., 0, 2])
    C3[..., 2, 1] = np.conj(C3[..., 1, 2])

    return C3


def C3_to_T3(C3: np.ndarray) -> np.ndarray:
    """Converts the lexicographic covariance matrix T3 to the Pauli coherency matrix T3.

    Args:
        C3 (np.ndarray): input image of coherency matrices with shape (naz, nrg, 3, 3)

    Returns:
        np.ndarray: T3 coherency matrix
    """
    if np.isrealobj(C3):
        raise ValueError("Inputs must be complex-valued.")
    if C3.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if C3.shape[-2:] != (3, 3):
        raise ValueError("C3 must have a shape like (naz, nrg, 3, 3)")

    return _C3_to_T3_core(C3=C3)


def C3_to_T3_dask(C3: np.ndarray) -> np.ndarray:
    """Converts the lexicographic covariance matrix C3 to the Pauli coherency matrix T3.

    Args:
        C3 (np.ndarray): input image of coherency matrices with shape (naz, nrg, 3,

    Returns:
        np.ndarray: T3 coherency matrix
    """
    if np.isrealobj(C3):
        raise ValueError("Inputs must be complex-valued.")
    if C3.ndim != 4:
        raise ValueError("A matrix-valued image is expected (dimension 4)")
    if C3.shape[-2:] != (3, 3):
        raise ValueError("C3 must have a shape like (naz, nrg, 3, 3)")

    da_in = da.map_blocks(
        _C3_to_T3_core,
        da.from_array(C3, chunks="auto"),
        # da.from_array(C3, chunks=(500, 500, -1, -1)),
        dtype="complex64",
    )

    return np.asarray(da_in)


def C3_to_T3_xarray(C3: xarray.Dataset) -> xarray.Dataset:
    """Converts the lexicographic covariance matrix C3 to the Pauli coherency matrix T3.

    Args:
        C3 (xarray.Dataset): input image of covariance matrices

    Returns:
        xarray.Dataset: T3 coherency matrix
    """

    if C3.poltype == "C3":

        T3_dict = {}

        c = 1 / np.sqrt(np.float32(2))

        # force real diagonal to save space
        T3_dict["m11"] = 0.5 * (C3.m11 + C3.m33) + C3.m13.real
        T3_dict["m22"] = 0.5 * (C3.m11 + C3.m33) - C3.m13.real
        T3_dict["m33"] = C3.m22
        # upper diagonal terms
        T3_dict["m12"] = 0.5 * (C3.m11 - C3.m33) - 1j * C3.m13.imag
        T3_dict["m13"] = c * (C3.m12 + C3.m23.conj())
        T3_dict["m23"] = c * (C3.m12 - C3.m23.conj())

        attrs = {"poltype": "T3", "description": "Coherency matrix (3x3)"}
        return xr.Dataset(T3_dict, attrs=attrs)

        # TODO: remove once the function has been tested
        # T3[..., 0, 0] = 0.5 * (C3[..., 0, 0] + 2 * C3[..., 0, 2].real + C3[..., 2, 2])
        # T3[..., 0, 1] = 0.5 * (C3[..., 0, 0] - C3[..., 2, 2]) - 1j * C3[..., 0, 2].imag
        # T3[..., 0, 2] = (C3[..., 0, 1] + np.conj(C3[..., 1, 2])) / np.sqrt(2)
        # T3[..., 1, 1] = 0.5 * (C3[..., 0, 0] - 2 * C3[..., 0, 2].real + C3[..., 2, 2])
        # T3[..., 1, 2] = (C3[..., 0, 1] - np.conj(C3[..., 1, 2])) / np.sqrt(2)
        # T3[..., 2, 2] = C3[..., 1, 1]

    else:
        raise ValueError("Input polarimetric type must be 'T3'")


def _C3_to_T3_core(C3: np.ndarray) -> np.ndarray:
    T3 = np.zeros_like(C3, dtype="complex64")

    # Reproject T3 matrix in the lexicographic basis
    T3[..., 0, 0] = 0.5 * (C3[..., 0, 0] + 2 * C3[..., 0, 2].real + C3[..., 2, 2])
    T3[..., 0, 1] = 0.5 * (C3[..., 0, 0] - C3[..., 2, 2]) - 1j * C3[..., 0, 2].imag
    T3[..., 0, 2] = (C3[..., 0, 1] + np.conj(C3[..., 1, 2])) / np.sqrt(2)
    T3[..., 1, 1] = 0.5 * (C3[..., 0, 0] - 2 * C3[..., 0, 2].real + C3[..., 2, 2])
    T3[..., 1, 2] = (C3[..., 0, 1] - np.conj(C3[..., 1, 2])) / np.sqrt(2)
    T3[..., 2, 2] = C3[..., 1, 1]

    T3[..., 1, 0] = np.conj(T3[..., 0, 1])
    T3[..., 2, 0] = np.conj(T3[..., 0, 2])
    T3[..., 2, 1] = np.conj(T3[..., 1, 2])

    return T3


def vec_to_mat(vec: np.ndarray) -> np.ndarray:
    """Vector to matrix conversion. Input should have (naz, nrg, N) shape"""
    if vec.ndim != 3:
        raise ValueError("Vector valued image is expected (dimension 3)")
    return vec[:, :, None, :] * vec[:, :, :, None].conj()


# @timeit
def span(M: np.ndarray) -> np.ndarray:
    """Span computation for a image of matrices. Input should have (naz, nrg, N, N) shape"""
    if M.ndim != 4:
        raise ValueError("Matrix valued image is expected (dimension 4)")
    if M.shape[2] != M.shape[3]:
        raise ValueError("Input shape (naz, nrg, N, N) expected")

    return np.diagonal(M, axis1=2, axis2=3).real.sum(axis=-1)