mikonvergence commited on
Commit
552b1cf
·
verified ·
1 Parent(s): 1b726b0

Update helpers/functional.py

Browse files
Files changed (1) hide show
  1. helpers/functional.py +23 -2
helpers/functional.py CHANGED
@@ -6,6 +6,7 @@ import pandas as pd
6
  from io import BytesIO
7
  import os
8
  from PIL import Image
 
9
 
10
  # GLOBAL VARIABLES
11
  if os.path.isfile('helpers/s2l2a_metadata.parquet'):
@@ -20,9 +21,26 @@ else:
20
  DATASET_NAME = 'Major-TOM/Core-S2L1C'
21
  l1c_meta_path = 'https://huggingface.co/datasets/{}/resolve/main/metadata.parquet'.format(DATASET_NAME)
22
 
 
 
 
 
 
 
23
  grid = Grid(10, latitude_range=(-90,90), longitude_range=(-180,180))
24
  l2a_df = pd.read_parquet(l2a_meta_path)
25
  l1c_df = pd.read_parquet(l1c_meta_path)
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # HELPER FUNCTIONS
28
  def gridcell2ints(grid_string):
@@ -66,7 +84,7 @@ def cell2row(grid_string, meta_df, return_row = False):
66
  else:
67
  return None
68
 
69
- def map_to_image(map, return_centre=False, return_gridcell=False, l2a=True):
70
 
71
  try:
72
  # 1. get bounds
@@ -79,7 +97,7 @@ def map_to_image(map, return_centre=False, return_gridcell=False, l2a=True):
79
  rows, cols = grid.latlon2rowcol([center[0]], [center[1]])
80
 
81
  # 3. translate major-tom cell to row in parquet
82
- df = l2a_df if l2a else l1c_df
83
  row = cell2row("{}_{}".format(rows[0],cols[0]), df, return_row = True)
84
 
85
  if row is not None:
@@ -94,6 +112,9 @@ def map_to_image(map, return_centre=False, return_gridcell=False, l2a=True):
94
  ret.append((lat,lon))
95
  if return_gridcell:
96
  ret.append(meta_row.grid_cell.item())
 
 
 
97
  return ret
98
  else:
99
  return None
 
6
  from io import BytesIO
7
  import os
8
  from PIL import Image
9
+ import datetime
10
 
11
  # GLOBAL VARIABLES
12
  if os.path.isfile('helpers/s2l2a_metadata.parquet'):
 
21
  DATASET_NAME = 'Major-TOM/Core-S2L1C'
22
  l1c_meta_path = 'https://huggingface.co/datasets/{}/resolve/main/metadata.parquet'.format(DATASET_NAME)
23
 
24
+ if os.path.isfile('helpers/s1rtc_metadata.parquet'):
25
+ rtc_meta_path = 'helpers/s1rtc_metadata.parquet'
26
+ else:
27
+ DATASET_NAME = 'Major-TOM/Core-S1RTC'
28
+ rtc_meta_path = 'https://huggingface.co/datasets/{}/resolve/main/metadata.parquet'.format(DATASET_NAME)
29
+
30
  grid = Grid(10, latitude_range=(-90,90), longitude_range=(-180,180))
31
  l2a_df = pd.read_parquet(l2a_meta_path)
32
  l1c_df = pd.read_parquet(l1c_meta_path)
33
+ rtc_df = pd.read_parquet(rtc_meta_path)
34
+
35
+ df_dict = {
36
+ 'S2-L2A' : l2a_df,
37
+ 'S2-L1C' : l1c_df,
38
+ 'S1-RTC' : rtc_df
39
+ }
40
+
41
+ def pretty_date(input):
42
+ template = '%Y%m%dT%H%M%S' if 'T' in input else '%Y%m%d%H%M%S'
43
+ return datetime.datetime.strptime(input, template).strftime('%H:%M:%S - %d %b %Y')
44
 
45
  # HELPER FUNCTIONS
46
  def gridcell2ints(grid_string):
 
84
  else:
85
  return None
86
 
87
+ def map_to_image(map, return_centre=False, return_gridcell=False, return_timestamp=False, source='S2-L2A'):
88
 
89
  try:
90
  # 1. get bounds
 
97
  rows, cols = grid.latlon2rowcol([center[0]], [center[1]])
98
 
99
  # 3. translate major-tom cell to row in parquet
100
+ df = df_dict[source]
101
  row = cell2row("{}_{}".format(rows[0],cols[0]), df, return_row = True)
102
 
103
  if row is not None:
 
112
  ret.append((lat,lon))
113
  if return_gridcell:
114
  ret.append(meta_row.grid_cell.item())
115
+ if return_timestamp:
116
+ ret.append(pretty_date(meta_row.timestamp.item()))
117
+
118
  return ret
119
  else:
120
  return None