File: update_elements.py

package info (click to toggle)
pyepr 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,144 kB
  • sloc: python: 3,028; makefile: 124
file content (82 lines) | stat: -rwxr-xr-x 2,334 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
#!/usr/bin/env python3

import numpy as np
from matplotlib import pyplot as plt

import epr

FILENAME = "MER_LRC_2PTGMV20000620_104318_00000104X000_00000_00000_0001.N1"

# load original data
with epr.open(FILENAME) as product:
    band = product.get_band("water_vapour")
    wv_orig_histogram, orig_bins = np.histogram(band.read_as_array().flat, 50)

# plot water vapour histogram
plt.figure()
plt.bar(orig_bins[:-1], wv_orig_histogram, 0.02, label="original")
plt.grid()
plt.title("Water Vapour Histogram")
plt.savefig("water_vapour_histogram_01.png")

# modily scaling facotrs
with epr.open(FILENAME, "rb+") as product:
    dataset = product.get_dataset("Scaling_Factor_GADS")
    record = dataset.read_record(0)

    field = record.get_field("sf_wvapour")
    scaling = field.get_elem()
    scaling *= 1.1
    field.set_elem(scaling)

# re-open the product and load modified data
with epr.open(FILENAME) as product:
    band = product.get_band("water_vapour")
    unit = band.unit
    new_data = band.read_as_array()
    wv_new_histogram, new_bins = np.histogram(new_data.flat, 50)

# plot histogram of modified data
plt.figure()
plt.bar(orig_bins[:-1], wv_orig_histogram, 0.02, label="original")
plt.grid()
plt.title("Water Vapour Histogram")
plt.hold()
plt.bar(new_bins[:-1], wv_new_histogram, 0.02, color="red", label="new")
plt.legend()
plt.savefig("water_vapour_histogram_02.png")

# plot the water vapour map
plt.figure(figsize=(8, 4))
plt.imshow(new_data)
plt.grid()
plt.title("Water Vapour")
cb = plt.colorbar()
cb.set_label(f"[{unit}]")
plt.savefig("modified_water_vapour.png")

# modify the "Vapour_Content" dataset
with epr.open(FILENAME, "rb+") as product:
    dataset = product.get_dataset("Vapour_Content")
    for line in range(70, 100):
        record = dataset.read_record(line)
        field = record.get_field_at(2)
        elems = field.get_elems()
        elems[50:100] = 0
        field.set_elems(elems)

# re-open the product and load modified data
with epr.open(FILENAME) as product:
    band = product.get_band("water_vapour")
    unit = band.unit
    data = band.read_as_array()

# plot the water vapour map
plt.figure(figsize=(8, 4))
plt.imshow(data)
plt.grid()
plt.title("Water Vapour with box")
cb = plt.colorbar()
cb.set_label(f"[{unit}]")
plt.savefig("modified_water_vapour_with_box.png")
plt.show()