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
|
- #!/usr/bin/env python
- import os
- import click
- from pathlib import Path
- import csv
- import numpy as np
- from tqdm import tqdm
- from datetime import datetime as dt
- FIRE_EXT = '.txt'
- def transform_point(point):
- point = { k.strip(): v for k, v in point.items() }
- #timestr = point['Time'].strip()
- #if '-' in timestr:
- # timestr = '0000'
- #dtime = dt.strptime(
- # point['YearDay'].strip() + timestr,
- # '%Y%j%H%M'
- #)
- return {
- 'latitude': float(point['Lat'].strip()),
- 'longitude': float(point['Lon'].strip()),
- 'satellite': point['Satellite'].strip(),
- 'method': point['Method'].strip(),
- 'ecosystem': int(point['Ecosystem'].strip()),
- 'fire_radiative_power': float(point['FRP'].strip()),
- #'time': dtime,
- }
- def read_points(shapefile):
- basename = os.path.splitext(os.path.basename(shapefile))[0]
- date = dt.strptime(basename, 'hms_fire%Y%m%d')
- with open(shapefile, 'r') as f:
- reader = csv.DictReader(f)
- points = list(map(transform_point, reader))
- return date, points
- @click.command()
- @click.argument('firedir', type=click.Path(
- path_type=Path, exists=True
- ))
- @click.argument('outputfile', type=click.Path(
- path_type=Path
- ))
- def main(firedir, outputfile):
- fire_files = []
- for d, dirs, files in os.walk(firedir):
- for f in files:
- if f.endswith(FIRE_EXT):
- fire_files.append(os.path.join(d, f))
- dates = []
- all_points = []
- for sm in tqdm(sorted(fire_files), 'Reading fire points'):
- date, points = read_points(sm)
- for i in range(len(points)):
- dates.append(date)
- all_points += points
- get = lambda key, dtype: \
- np.array([p[key] for p in all_points], dtype=dtype)
- dates = np.array(dates, dtype='datetime64[D]')
- result = {
- 'dates': dates,
- 'latitudes': get('latitude', float),
- 'longitudes': get('longitude', float),
- 'satellites': get('satellite', str),
- 'method': get('method', str),
- 'ecosystem': get('ecosystem', int),
- 'fire_radiative_power': get('fire_radiative_power', float),
- }
- np.savez_compressed(outputfile, **result)
- if __name__ == '__main__':
- main()
|