2626from dask .utils import SerializableLock
2727from numpy import atleast_1d
2828
29- from atlite .gis import maybe_swap_spatial_dims
29+ from atlite .gis import maybe_swap_spatial_dims , rotate
3030from atlite .pv .solar_position import SolarPosition
3131from atlite .wind import calculate_windspeed_bias_correction
3232
@@ -525,28 +525,38 @@ def retrieve_data(
525525
526526
527527def retrieve_windspeed_average (
528- cutout ,
529- height ,
530- first_year = 1980 ,
531- last_year = None ,
532- data_format = "grib" ,
528+ cutout = None ,
529+ height : int = 100 ,
530+ first_year : int = 2008 ,
531+ last_year : int | None = 2017 ,
532+ data_format : Literal [ "grib" , "netcdf" ] = "grib" ,
533533 ** retrieval_params ,
534534):
535535 """
536536 Retrieve average windspeed from `first_year` to `last_year`
537537
538+ The default time-period 2008-2017 was chosen to align with the simulation
539+ period of GWA3.
540+
538541 Parameters
539542 ----------
540- cutout : atlite.Cutout
541- Cutout for which to retrieve windspeeds from CDS
543+ cutout : atlite.Cutout or None
544+ Cutout for which to retrieve windspeeds from CDS. If no cutout is
545+ specified the global means are retrieved at native resolution 0.25/0.25.
542546 height : int
543- Height of windspeeds (ERA5 typically knows about 10m, 100m, 150m? )
544- first_year : int
547+ Height of windspeeds (ERA5 typically knows about 10m, 100m)
548+ first_year : int, defaults to 2008
545549 First year to take into account
546- last_year : int, optional
550+ last_year : int, defaults to 2017
547551 Last year to take into account (if omitted takes the previous year)
552+ data_format : {"grib", "netcdf"}
553+ Data format to use for retrieving from CDS.
548554 **retrieval_params
549555
556+ References
557+ ----------
558+ https://globalwindatlas.info/
559+
550560 Returns
551561 -------
552562 DataArray
@@ -555,32 +565,56 @@ def retrieve_windspeed_average(
555565 if last_year is None :
556566 last_year = datetime .date .today ().year - 1
557567
558- ds = retrieve_data (
559- "reanalysis-era5-single-levels-monthly-means" ,
560- chunks = cutout . chunks ,
561- product_type = "monthly_averaged_reanalysis " ,
562- variable = [
563- f" { height } m_u_component_of_wind" ,
564- f" { height } m_v_component_of_wind" ,
565- ] ,
566- area = _area ( cutout .coords ) ,
567- grid = [ cutout .dx , cutout .dy ] ,
568- year = [ str ( y ) for y in range ( first_year , last_year + 1 )],
569- month = [ f" { m :02 } " for m in range ( 1 , 12 + 1 )],
570- time = [ "00:00" ],
571- data_format = data_format ,
572- ** retrieval_params ,
568+ retrieval_params = (
569+ {
570+ "dataset" : "reanalysis-era5-single-levels" ,
571+ " product_type" : "reanalysis " ,
572+ }
573+ | (
574+ {
575+ "area" : _area ( cutout . coords ) ,
576+ "chunks" : cutout .chunks ,
577+ " grid" : f" { cutout .dx } / { cutout .dy } " ,
578+ }
579+ if cutout is not None
580+ else {}
581+ )
582+ | retrieval_params
573583 )
574- ds = _rename_and_clean_coords (ds )
575584
576- return (
577- sqrt (ds [f"u{ height } " ] ** 2 + ds [f"v{ height } " ] ** 2 )
578- .mean ("time" )
579- .assign_attrs (
580- units = ds [f"u{ height } " ].attrs ["units" ],
581- long_name = f"{ height } metre wind speed as long run average" ,
585+ def retrieve_chunk (year ):
586+ ds = retrieve_data (
587+ variable = [
588+ f"{ height } m_u_component_of_wind" ,
589+ f"{ height } m_v_component_of_wind" ,
590+ ],
591+ year = [year ],
592+ month = [f"{ m :02d} " for m in range (1 , 12 + 1 )],
593+ day = [f"{ d :02d} " for d in range (1 , 31 + 1 )],
594+ time = [f"{ h :02d} " for h in range (0 , 23 + 1 )],
595+ data_format = data_format ,
596+ ** retrieval_params ,
582597 )
583- )
598+ ds = _rename_and_clean_coords (ds )
599+
600+ if cutout is None :
601+ # the default longitude range of CDS is [0, 360], while [-180, 180] is standard
602+ ds = rotate (ds )
603+
604+ return (
605+ sqrt (ds [f"u{ height } " ] ** 2 + ds [f"v{ height } " ] ** 2 )
606+ .mean ("time" )
607+ .assign_attrs (
608+ units = ds [f"u{ height } " ].attrs ["units" ],
609+ long_name = f"{ height } metre wind speed as long run average" ,
610+ )
611+ )
612+
613+ years = range (first_year , last_year + 1 )
614+ return xr .concat (
615+ compute (* (delayed (retrieve_chunk )(str (year )) for year in years )),
616+ dim = pd .Index (years , name = "year" ),
617+ ).mean ("year" )
584618
585619
586620def get_data_windspeed_bias_correction (cutout , retrieval_params , creation_parameters ):
@@ -681,7 +715,7 @@ def retrieve_once(time):
681715 elif feature == "windspeed_bias_correction" :
682716 return func (
683717 cutout ,
684- retrieval_params = dict ( tmpdir = tmpdir , lock = lock , data_format = data_format ) ,
718+ retrieval_params = retrieval_params ,
685719 creation_parameters = creation_parameters ,
686720 )
687721
0 commit comments