Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

combine_fire_points.py 2.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env python
  2. import os
  3. import click
  4. from pathlib import Path
  5. import csv
  6. import numpy as np
  7. from tqdm import tqdm
  8. from datetime import datetime as dt
  9. FIRE_EXT = '.txt'
  10. def transform_point(point):
  11. point = { k.strip(): v for k, v in point.items() }
  12. #timestr = point['Time'].strip()
  13. #if '-' in timestr:
  14. # timestr = '0000'
  15. #dtime = dt.strptime(
  16. # point['YearDay'].strip() + timestr,
  17. # '%Y%j%H%M'
  18. #)
  19. return {
  20. 'latitude': float(point['Lat'].strip()),
  21. 'longitude': float(point['Lon'].strip()),
  22. 'satellite': point['Satellite'].strip(),
  23. 'method': point['Method'].strip(),
  24. 'ecosystem': int(point['Ecosystem'].strip()),
  25. 'fire_radiative_power': float(point['FRP'].strip()),
  26. #'time': dtime,
  27. }
  28. def read_points(shapefile):
  29. basename = os.path.splitext(os.path.basename(shapefile))[0]
  30. date = dt.strptime(basename, 'hms_fire%Y%m%d')
  31. with open(shapefile, 'r') as f:
  32. reader = csv.DictReader(f)
  33. points = list(map(transform_point, reader))
  34. return date, points
  35. @click.command()
  36. @click.argument('firedir', type=click.Path(
  37. path_type=Path, exists=True
  38. ))
  39. @click.argument('outputfile', type=click.Path(
  40. path_type=Path
  41. ))
  42. def main(firedir, outputfile):
  43. fire_files = []
  44. for d, dirs, files in os.walk(firedir):
  45. for f in files:
  46. if f.endswith(FIRE_EXT):
  47. fire_files.append(os.path.join(d, f))
  48. dates = []
  49. all_points = []
  50. for sm in tqdm(sorted(fire_files), 'Reading fire points'):
  51. date, points = read_points(sm)
  52. for i in range(len(points)):
  53. dates.append(date)
  54. all_points += points
  55. get = lambda key, dtype: \
  56. np.array([p[key] for p in all_points], dtype=dtype)
  57. dates = np.array(dates, dtype='datetime64[D]')
  58. result = {
  59. 'dates': dates,
  60. 'latitudes': get('latitude', float),
  61. 'longitudes': get('longitude', float),
  62. 'satellites': get('satellite', str),
  63. 'method': get('method', str),
  64. 'ecosystem': get('ecosystem', int),
  65. 'fire_radiative_power': get('fire_radiative_power', float),
  66. }
  67. np.savez_compressed(outputfile, **result)
  68. if __name__ == '__main__':
  69. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...