Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

""" Tracker to submit and download GEE pre-ARD tasks 

""" 

from collections import defaultdict 

import datetime as dt 

import itertools 

import logging 

import os 

from pathlib import Path 

import string 

 

import ee 

import pandas as pd 

 

from stems.gis.grids import TileGrid, Tile 

 

from . import defaults, ordering, utils 

from .exceptions import EmptyCollectionError 

from .metadata import TrackingMetadata, get_submission_info 

 

logger = logging.getLogger(__name__) 

 

_STR_FORMATTER = string.Formatter() 

 

 

class Tracker(object): 

""" CEDAR "pre-ARD" order tracker 

 

Parameters 

---------- 

tile_grid : stems.gis.grids.TileGrid 

Tile Grid to use for ARD 

store : cedar.stores.Store 

A Store that can be used to store images & metadata 

name_template : str, optional 

Template for "pre-ARD" image and metadata name 

prefix_template : str, optional 

Template for "pre-ARD" image and metadata prefix 

tracking_template : str, optional 

Template for order tracking file name 

tracking_prefix : str, optional 

Order tracking file prefix folder 

filters : Dict[str, Sequence[dict or ee.Filter]] 

Earth Engine filters to apply, organized by image collection name. 

Values should either be ``ee.Filter`` objects or dictionaries 

that describe the filter (see :py:func:`cedar.utils.serialize_filter`) 

""" 

def __init__(self, tile_grid, store, 

name_template=defaults.PREARD_NAME, 

prefix_template=defaults.PREARD_PREFIX, 

tracking_template=defaults.PREARD_TRACKING, 

tracking_prefix=defaults.PREARD_TRACKING_PREFIX, 

filters=None, 

export_image_kwds=None): 

assert isinstance(tile_grid, TileGrid) 

self.tile_grid = tile_grid 

self.store = store 

self.name_template = name_template 

self.prefix_template = prefix_template 

self.tracking_template = tracking_template 

self.tracking_prefix = tracking_prefix 

self._filters = filters or defaultdict(list) 

self.export_image_kwds = export_image_kwds or {} 

 

@property 

def filters(self): 

""" list[ee.Filter]: Earth Engine filters to apply 

""" 

# Convert from dict as needed 

from .utils import create_filters 

return { 

image_collection: create_filters(filters) 

for image_collection, filters in self._filters.items() 

} 

 

def submit(self, collections, tile_indices, 

period_start, period_end, period_freq=None, 

save_empty_metadata=True, error_if_empty=False): 

""" Submit and track GEE pre-ARD tasks 

 

Parameters 

---------- 

collections: str or Sequence[str] 

GEE image collection name(s) 

tile_indices : Sequence[(int, int)] 

Tuple(s) of rows/columns in TileGrid to process 

period_start : dt.datetime 

Starting period date 

period_end : dt.datetime 

Ending period date 

period_freq : str, optional 

If provided, ``period_start``, ``period_end``, and ``period_freq`` 

are interpeted as the range for :py:func:`pandas.date_range` and 

one or more Tasks will be submitted 

save_empty_metadata : bool, optional 

If True, Pre-ARD image requests that have 0 results (e.g., because 

of spotty historical record) will store metadata, but will not start 

the task. If False, will not store this metadata 

error_if_empty : bool, optional 

If True, raise an EmptyCollectionError if the image collection 

result has no images. The default behavior is to log and skip 

empty search results 

 

Returns 

------- 

str 

Task tracking information name 

str 

Task tracking information identifier (an ID, path, etc) 

""" 

# TODO: add callback (e.g., for progressbar) 

# TODO: eventually allow start/end to be None (use limits of data) 

if isinstance(collections, str): 

collections = (collections, ) 

assert len(tile_indices) >= 1 

if isinstance(tile_indices[0], int): 

tile_indices = [tile_indices] 

 

# Get tiles 

tiles = [self.tile_grid[index] for index in tile_indices] 

 

# Split up period into 1 or more sub-periods if freq is given 

periods = _parse_date_freq(period_start, period_end, period_freq) 

logger.debug(f'Creating {len(periods)} ARD slice(s) for date range') 

 

# Create tracking name 

s_tile_indices = [f'h{h:03d}v{v:03d}' for v, h in tile_indices] 

namespace = { 

'collections': collections, 

'tiles': tiles, 

'tile_indices': '_'.join(s_tile_indices), 

'period_start': period_start.isoformat(), 

'period_end': period_end.isoformat(), 

'period_freq': period_freq, 

'now': dt.datetime.now().isoformat() 

} 

tracking_name = self.tracking_template.format(**namespace) 

 

# Create submission info 

submission_info = get_submission_info(self.tile_grid, collections, 

tile_indices, 

period_start, period_end, 

period_freq) 

 

# Determine parameters for each submission 

iter_submit = list(itertools.product(collections, tiles, periods)) 

 

logger.debug(f'Creating order named "{tracking_name}"') 

order = ordering.Order( 

tracking_name, 

self.tracking_prefix, 

name_template=self.name_template, 

prefix_template=self.prefix_template 

) 

 

# Loop over product of collections, tiles, and dates 

for collection, tile, (date_start, date_end) in iter_submit: 

logger.debug( 

f'Adding "{collection}" - ' 

f'"h{tile.horizontal:03d}v{tile.vertical:03d} - ' 

f'{date_start} to {date_end}' 

) 

order.add( 

collection, tile, date_start, date_end, 

filters=self.filters.get(collection, []), 

error_if_empty=error_if_empty 

) 

 

logger.debug('Submitting order') 

tracking_id = order.submit( 

self.store, 

submission_info=submission_info, 

save_empty_metadata=save_empty_metadata, 

export_image_kwds=self.export_image_kwds 

) 

logger.debug(f'Submitted order with name="{tracking_name}" ' 

f'stored at ID="{tracking_id}"') 

 

return tracking_name, tracking_id 

 

def list(self, pattern=None): 

""" Return a list of all tracking metadata 

 

Parameters 

---------- 

pattern : str, optional 

Search pattern for tracking info. Specify to subset to specific 

tracking info (e.g., from some date). If ``None`` provided, 

looks for tracking information matching 

:py:attr:`~Tracker.tracking_template` 

 

Returns 

------- 

list[str] 

Name of stored tracking information 

""" 

if pattern is None: 

d = defaultdict(lambda: '*') 

pattern = self.tracking_template.format_map(d).split('*')[0] 

return self.store.list(path=self.tracking_prefix, pattern=pattern) 

 

def read(self, name): 

""" Returns stored tracking information as dict 

 

Parameters 

---------- 

name : str 

Name of tracking metadata (e.g., taken from running 

:func:`~Tracker.list_tracking`) 

 

Returns 

------- 

dict 

JSON tracking info data as a dict 

""" 

data = self.store.read_metadata(name, path=self.tracking_prefix) 

return TrackingMetadata(data) 

 

def update(self, name): 

""" Refresh and reupload tracking information by checking with the GEE 

 

Parameters 

---------- 

name : str 

Name of tracking metadata (e.g., taken from running 

:func:`~Tracker.list_tracking`) 

 

Returns 

------- 

dict 

JSON tracking info data as a dict 

""" 

tracking_info = self.read(name) 

updated = tracking_info.update() 

name_ = self.store.store_metadata(dict(updated), name) 

return updated 

 

def download(self, tracking_info, dest, overwrite=True, callback=None): 

""" Download "pre-ARD" and metadata to a directory 

 

Parameters 

---------- 

tracking_info : dict 

JSON tracking info data as a dict 

dest : str or pathlib.Path 

Destination download directory 

overwrite : bool, optional 

Overwrite existing downloaded data 

callback : callable 

Callback function to execute after each file is downloaded. 

Should take arguments "item" and "n_steps". Use this for 

progress bars or other download status reporting 

 

Returns 

------- 

tuple[str, list[str]] 

Key value pairs mapping GEE task IDs to the filenames of 

downloaded data. Wrap it in a ``dict`` to make it not lazy 

""" 

logger.debug(f'Downloading for {len(tracking_info["orders"])} tasks') 

iter_download = download_tracked(tracking_info, self.store, dest, 

overwrite=overwrite) 

 

downloaded = defaultdict(list) 

for task_id, n_images, meta, images in iter_download: 

# Download, report (if callback), and store filenames 

logger.debug(f'Downloading output for task "{task_id}" ' 

f'({n_images or "unknown"} images)') 

 

for meta_ in meta: 

if callback: 

# Metadata doesn't count as a "step" 

callback(item=task_id, n_steps=0) 

downloaded[task_id].append(meta_) 

 

# We might not know how many images will be downloaded 

steps_image = 1 / n_images if n_images else 0 

for image in images: 

downloaded[task_id].append(image) 

if callback: 

callback(item=image.stem, n_steps=steps_image) 

 

# Update for all downloaded images at once if we didn't know 

# a priori 

if steps_image == 0 and callback: 

item = os.path.commonprefix( 

[p.name for p in downloaded[task_id]]) 

callback(item=item + '...', n_steps=1) 

 

return downloaded 

 

def clean(self, tracking_info, tracking_name=None, callback=None): 

""" Clean "pre-ARD" imagery, metadata, and tracking metadata off GCS 

 

Parameters 

---------- 

tracking_info : dict 

JSON tracking info data as a dict 

tracking_name : str 

Name of tracking info file (will be deleted if provided) 

callback : callable 

Callback function to execute after each file is deleted. 

Should take arguments "item" and "n_steps". Use this for 

progress bars or other download status reporting 

 

Returns 

------- 

dict[str, list[str]] 

Mapping of GEE Task ID to filename(s) cleaned 

""" 

iter_clean = clean_tracked(tracking_info, self.store) 

 

cleaned = defaultdict(list) 

for task_id, n_images, names in iter_clean: 

for name in names: 

if callback: 

callback(item=task_id, n_steps=1 / n_images) 

cleaned[task_id].append(name) 

 

if tracking_name: 

self.store.remove(tracking_name, self.tracking_prefix) 

 

return cleaned 

 

 

def download_tracked(tracking_info, store, dest, overwrite=False): 

""" Download stored "pre-ARD" and metadata described by tracking info 

 

Parameters 

---------- 

tracking_info : dict 

Tracking information 

store : cedar.stores.Store 

cedar store class 

dest : str or pathlib.Path 

Destination download directory 

overwrite : bool, optional 

Overwrite previously downoaded data, or not 

 

Yields 

------ 

id : str 

Task ID 

n_images : int 

Number of images to download if known, otherwise ``None`` 

metadata : generator 

Generator that downloads metadata and yields filenames 

images : generator 

Generator that downloads imagery and yields filenames 

""" 

dest_ = Path(str(dest)) 

if not dest_.exists(): 

dest_.mkdir(exist_ok=True, parents=True) 

else: 

assert dest_.is_dir() 

 

orders = tracking_info['orders'] 

for order in orders: 

# Get info about order 

id_ = order['status'].get('id', None) 

name, prefix = order['name'], order['prefix'] 

n_images = len(order['status'].get('output_url', [])) or None 

 

# Retrieve image and metadata 

dst_meta = store.retrieve_metadata(dest, name, prefix, 

overwrite=overwrite) 

dst_imgs = store.retrieve_image(dest, name, prefix, 

overwrite=overwrite) 

 

yield (id_, n_images, dst_meta, dst_imgs, ) 

 

 

def clean_tracked(tracking_info, store): 

""" Delete stored "pre-ARD" and metadata described by tracking info 

 

Parameters 

---------- 

tracking_info : dict 

Tracking information 

store : cedar.stores.gcs.GCSStore or cedar.stores.gdrive.GDriveStore 

cedar store class 

 

Yields 

------ 

id : str 

Order GEE Task ID 

names : generator 

Generator that deletes files and returns their names 

""" 

orders = tracking_info['orders'] 

for order in orders: 

id_ = order['status'].get('id', None) 

name, prefix = order['name'], order['prefix'] 

logger.debug(f'Deleting image and metadata for id={id_}, ' 

f'name="{name}", prefix="{prefix}"') 

 

# Retrieve image and metadata 

names = store.list(path=prefix, pattern=name) 

yield (id_, len(names), (store.remove(name) for name in names)) 

 

 

def _parse_date_freq(start, end, freq=None): 

import pandas as pd # hiding because it can be expensive to import 

start_ = pd.to_datetime(start).to_pydatetime() 

end_ = pd.to_datetime(end).to_pydatetime() 

if freq is None: 

return list(zip([start], [end])) 

else: 

# Add offset to make inclusive of end date 

from pandas.tseries.frequencies import to_offset 

offset = to_offset(freq) 

times = pd.date_range(start, end + offset, freq=freq).to_pydatetime() 

return list(zip(times[:-1], times[1:]))