-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathglm-tracking.py
124 lines (88 loc) · 2.84 KB
/
glm-tracking.py
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
#
# This file is part of TATHU - Tracking and Analysis of Thunderstorms.
# Copyright (C) 2022 INPE.
#
# TATHU - Tracking and Analysis of Thunderstorms is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
#
import glob
from tathu.tracking.utils import area2degrees
from tathu.io import spatialite
from tathu.utils import file2timestamp
from tathu.satellite.glm import NowcastingGLMDensity
from tathu.tracking import descriptors
from tathu.tracking import detectors
from tathu.tracking import trackers
from tathu.visualizer import MapView
### Setup Parameters ###
# Define extent
extent = [-53.0, -26.0, -44.0, -19.0]
# Define stats
stats = ['max', 'mean', 'std', 'count']
# Threshold value
threshold = 1 # Number of lightnings (>=)
# Define minimum area
minarea = 16 # km^2
# Convert to degrees^2
minarea = area2degrees(minarea)
def view(systems, grid):
# Visualize result
m = MapView(extent)
m.plotImage(grid)
m.plotSystems(systems, facecolor='none', edgecolor='red', centroids=True)
m.show()
def detect(path, var, visualize=False):
# Extract timestamp
timestamp = file2timestamp(path)
print('*** Processing', timestamp, path, '***')
# Get data
glm = NowcastingGLMDensity(path) # from DIPTC/INPE
grid = glm.getData(var, extent)
# Create detector
detector = detectors.GreaterThan(threshold, minarea)
# Searching for systems
systems = detector.detect(grid)
# Adjust timestamp
for s in systems:
s.timestamp = timestamp
# Create statistical descriptor
descriptor = descriptors.StatisticalDescriptor(stats=stats, rasterOut=True)
# Describe systems (stats)
descriptor.describe(grid, systems)
if visualize:
view(systems, grid)
grid = None
return systems
def main():
# Base directory
images = '*.nc'
# Get files
files = sorted(glob.glob(images))
# Define variable that will be tracked
var = 'group' # or flash
# Visualize results at run-time?
visualize = False
# Detect first systems
current = detect(files[0], var, visualize)
# Create database connection
db = spatialite.Outputter('glm-tracking-' + var + '.sqlite', 'systems', stats)
# Save to database
db.output(current)
# Prepare tracking...
previous = current
# Create overlap area strategy
strategy = trackers.IntersectsStrategy()
# for each image file
for i in range(1, len(files)):
# Detect current systems
current = detect(files[i], var, visualize)
# Let's track!
t = trackers.OverlapAreaTracker(previous, strategy=strategy)
t.track(current)
# Save to database
db.output(current)
# Prepare next iteration
previous = current
print('Done!')
if __name__ == "__main__":
main()