-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathmagphase.py
More file actions
63 lines (50 loc) · 1.66 KB
/
Copy pathmagphase.py
File metadata and controls
63 lines (50 loc) · 1.66 KB
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
#!/usr/bin/env python3
# ******************************************************************************
#
# Project: GDAL
# Purpose: Example computing the magnitude and phase from a complex image.
# Author: Frank Warmerdam, warmerdam@pobox.com
#
# ******************************************************************************
# Copyright (c) 2008, Frank Warmerdam <warmerdam@pobox.com>
#
# SPDX-License-Identifier: MIT
# ******************************************************************************
import sys
import numpy as np
from osgeo import gdal, gdal_array
def Usage():
print(
f"Usage: {sys.argv[0]} -- This is a sample. Read source to know how to use. --"
)
return 2
def doit(src_filename, dst_magnitude, dst_phase):
src_ds = gdal.Open(src_filename)
xsize = src_ds.RasterXSize
ysize = src_ds.RasterYSize
print("{} x {}".format(xsize, ysize))
src_image = src_ds.GetRasterBand(1).ReadAsArray()
mag_image = pow(
np.real(src_image) * np.real(src_image)
+ np.imag(src_image) * np.imag(src_image),
0.5,
)
gdal_array.SaveArray(mag_image, dst_magnitude)
phase_image = np.angle(src_image)
gdal_array.SaveArray(phase_image, dst_phase)
return 0
def main(argv=sys.argv):
if len(sys.argv) <= 4:
return Usage()
# src_filename = 'complex.tif'
# dst_magnitude = 'magnitude.tif'
# dst_phase = 'phase.tif'
if len(argv) > 1:
src_filename = argv[1]
if len(argv) > 2:
dst_magnitude = argv[2]
if len(argv) > 3:
dst_phase = argv[3]
return doit(src_filename, dst_magnitude, dst_phase)
if __name__ == "__main__":
sys.exit(main(sys.argv))