@@ -377,6 +377,99 @@ def warm_cache(logger, date=None, max_counter=30, sleep_duration=600):
377
377
if counter > max_counter :
378
378
raise TimeoutError ("Max counter reached" )
379
379
380
+ def download_historical_forecasts (s3_key ,
381
+ s3_secret ,
382
+ s3_entrypoint ,
383
+ s3_bucket ,
384
+ prefix = "./" ,
385
+ variables = "all" ,
386
+ forecast_type = "all" ,
387
+ dryrun = False
388
+ ):
389
+ """Download the historical forecasts from the S3 bucket.
390
+
391
+ Parameters
392
+ ----------
393
+ s3_key : str
394
+ the key to access the S3 bucket.
395
+ s3_secret : str
396
+ the secret to access the S3 bucket.
397
+ s3_entrypoint : str
398
+ the entrypoint of the S3 bucket.
399
+ s3_bucket : str
400
+ the name of the S3 bucket.
401
+ prefix : str
402
+ The prefix where the files are downloaded.
403
+ Should be similar to ``"./data/silver"``.
404
+ variables : str or list[str], optional
405
+ the variables to download.
406
+ Can be ``"wind_speed_hourly"``, ``"sun_flux_downward_hourly"``, or ``"temperature_hourly"``
407
+ or a list of these values.
408
+ Default is ``"all"``, which downloads all the variables.
409
+ forecast_type : str or list[str], optional
410
+ the forecast type to download.
411
+ Can be ``"d0"``, ``"d1"``, ``"d2"``, or ``"d3"``,
412
+ or a list of these values.
413
+ Default is ``"all"``, which downloads all the forecast types.
414
+ dryrun : bool, optional
415
+ if True, do not download the files.
416
+ Default is False.
417
+
418
+ Returns
419
+ -------
420
+ list[Path]
421
+ the list of the files downloaded.
422
+ """
423
+ import boto3
424
+
425
+ session = boto3 .Session (
426
+ aws_access_key_id = s3_key ,
427
+ aws_secret_access_key = s3_secret ,
428
+ )
429
+ s3 = session .resource ("s3" , endpoint_url = s3_entrypoint )
430
+ bucket = s3 .Bucket (s3_bucket )
431
+ list_files = []
432
+ key_prefix = "weather_forecasts"
433
+ if variables == "all" :
434
+ variables = ["wind_speed_hourly" ,
435
+ "sun_flux_downward_hourly" ,
436
+ "temperature_hourly" ]
437
+ if isinstance (variables , str ):
438
+ variables = [variables ]
439
+ for var in variables :
440
+ if var not in ["wind_speed_hourly" ,
441
+ "sun_flux_downward_hourly" ,
442
+ "temperature_hourly" ]:
443
+ raise ValueError (f"Unknown variable { var } : must be in ['wind_speed_hourly', 'sun_flux_downward_hourly', 'temperature_hourly']" )
444
+ if forecast_type == "all" :
445
+ forecast_type = ["d0" , "d1" , "d2" , "d3" ]
446
+ if isinstance (forecast_type , str ):
447
+ forecast_type = [forecast_type ]
448
+ for forecast in forecast_type :
449
+ if forecast not in ["d0" , "d1" , "d2" , "d3" ]:
450
+ raise ValueError (f"Unknown forecast type { forecast } : must be in ['d0', 'd1', 'd2', 'd3']" )
451
+
452
+ for var in variables :
453
+ for forecast in forecast_type :
454
+ key = f"{ key_prefix } /{ var } _{ forecast } .nc"
455
+ # test if the key exists
456
+ filename = Path (prefix + "/" + key )
457
+ if filename .exists ():
458
+ print (f"{ filename } already downloaded, skipping" )
459
+ continue
460
+ filename .parent .mkdir (parents = True , exist_ok = True )
461
+ if dryrun :
462
+ print (f"DRY RUN : would Download { key } to { filename } " )
463
+ # test if the key exists without downloading it
464
+ try :
465
+ s3 .Object (s3_bucket , key ).load ()
466
+ except Exception as e :
467
+ print (e )
468
+
469
+ else :
470
+ bucket .download_file (key , filename )
471
+ list_files .append (filename )
472
+ return list_files
380
473
381
474
382
475
if __name__ == "__main__" :
0 commit comments