@@ -155,6 +155,8 @@ Download a forecast dataset from the ECMWF.
155
155
Where the Int is the number of days to go back from today. If the Int is greater than 3, an error is raised.
156
156
If the date is a string, it must be in the form YYYYMMDD or YYYY-MM-DD.
157
157
- `dryrun`: Print the URL of the requested data and return without trying to download anything.
158
+ - `filename`: The name of the file to save the data to. If not provided, you still have two options: (1) the function
159
+ will generate a name based on the on the variables and the date. (2) Use the `prefix` option to provide a prefix for the file name.
158
160
- `format`: The format in which to save the downloaded data. It can be "grib" or "netcdf". Default is "netcdf".
159
161
- `levlist`: The pressure levels to select. It can be a string to select a unique pressure level,
160
162
or a vector of strings or Ints to select multiple pressure levels.
@@ -163,7 +165,10 @@ Download a forecast dataset from the ECMWF.
163
165
of strings or Ints to select multiple variables. When variable(s) is requested, we download only those
164
166
variables as separate files. The names of those files are the same as the variable names with the .grib2 extension.
165
167
NOTE: Not specifying a variable will download the entire forecast grib file for each forecast step selected with the `step` option.
168
+ - `prefix`: A string with the prefix to use for the file name. The file name will be the prefix followed by the variable name.
166
169
- `root`: The root URL of the CDS ERA5 dataset. Default is "https://data.ecmwf.int/forecasts".
170
+ - `region`: Specify a region of a specific geographic area. It can be provided as a string with form "N/W/S/E"
171
+ or a 4-element vector or tuple with numeric data, or all a earthregion name.
167
172
- `step`: An Int with the forecast step to select.
168
173
- `stream`: The stream to select. It can be one of: "oper", "enfo", "waef", "wave", "scda", "scwv", "mmsf". Default is "oper".
169
174
- `time`: The time in hours to select. It can be a string a ``Time`` object, or a Int. What ever it is,
@@ -172,15 +177,16 @@ Download a forecast dataset from the ECMWF.
172
177
- `type`: A string with the type of forecast to select, it must be one of: "fc", "ef", "ep", "tf". Default is "fc".
173
178
174
179
### Example
175
- Try to get the latest 10m wind and 2m temperature forecast for today. It probably will fail because
176
- the data is likely not available yet. Adding a good `date` will make it work.
177
- ```julia
180
+
181
+ Get the latest 10m wind and 2m temperature forecast for today.
182
+
183
+ ```julia
178
184
ecmwf(:forecast, vars=["10u", "2t"])
179
185
```
180
186
"""
181
187
function ecmwf (source:: Symbol = :reanalysis ; filename= " " , cb:: Bool = false , dataset= " " , params:: Union{AbstractString, Vector{String}} = " " , key:: String = " " , url:: String = " " , wait= 1.0 , levlist= " " , region= " " , format= " netcdf" , dryrun= false , verbose:: Bool = true , kw... )
182
188
183
- (source == :forecast ) && return ecmwf_fc (; levlist= levlist, dryrun= dryrun, kw... )
189
+ (source == :forecast ) && return ecmwf_fc (; filename = filename, levlist= levlist, dryrun= dryrun, kw... )
184
190
185
191
function cdsapikey ():: Tuple{String, String}
186
192
# Get the API key and URL from the ~/.cdsapirc file
@@ -306,9 +312,17 @@ function ecmwf(source::Symbol=:reanalysis; filename="", cb::Bool=false, dataset=
306
312
end
307
313
308
314
# ---------------------------------------------------------------------------------------------------------------
309
- function ecmwf_fc (; levlist= " " , kw... )
315
+ function ecmwf_fc (; filename = " " , levlist= " " , kw... )
310
316
311
317
d = KW (kw)
318
+
319
+ function which_name (d:: Dict , thisvar:: String , name:: String , filename:: AbstractString , EXT:: String ):: String
320
+ # Select which name to use for saving the file. NAME is the composed (long) name. The others are user choices.
321
+ (filename != " " ) && return filename # This one takes priority
322
+ fname_prefix:: String = ((val = find_in_dict (d, [:prefix ], false )[1 ]) != = nothing ) ? string (val):: String * " _$(thisvar)$(EXT) " : " "
323
+ return (fname_prefix != " " ) ? fname_prefix : name # 'fname_prefix' takes priority if exists.
324
+ end
325
+
312
326
root = " https://data.ecmwf.int/forecasts"
313
327
if ((val = find_in_dict (d, [:root ])[1 ]) != = nothing ) #
314
328
root = string (val):: String
@@ -399,10 +413,9 @@ function ecmwf_fc(; levlist="", kw...)
399
413
grdclip_cmd = opt_R * " -Sr1/1 -G" # Not always used. To be replaced when grdconvert GMT bug is fixed
400
414
(opt_R != " " ) && (opt_R = opt_R[4 : end ])
401
415
EXT = (((val = find_in_dict (d, [:format ])[1 ]) != = nothing ) && val == " grib" ) ? " .grib" : " .grd"
402
- fmt_info = (EXT == " .grib" ) ? " GRIB" : " netCDF" # For printing info
403
416
404
417
local mat, G:: GMTgrid{Float32,2} , x, y, range, inc
405
- tmp = tempname ()
418
+ tmp = tempname () # Temporary to store the index file
406
419
for ns = 1 : numel (step) # Loop over the steps
407
420
thisFile = root * " /$(date) /$(tim) z/$(model) /0p25/$(stream) /$(date)$(tim) 0000-$(step[ns]) h-$(stream) -$(type) ."
408
421
dryrun && (println (thisFile * " grib2" ); continue ) # Dry run
@@ -431,6 +444,7 @@ function ecmwf_fc(; levlist="", kw...)
431
444
mat[:,:,nl] .= G. z
432
445
else # Just save the single level
433
446
fname = vars[k] * " _step$(step[ns]) _level$(levels[nl]) _$(model) _$(stream) _$(type) _$(date) _$(tim)$(EXT) " # This var fname
447
+ fname = which_name (d, vars[k], fname, filename, EXT) # Decide the final name
434
448
@info " Downloading $(vars[k]) and saving to $fname "
435
449
(EXT == " .grd" ) ? gmt (" grdclip /vsisubfile/$(start) _$(len) " * " ,/vsicurl/" * url * grdclip_cmd * fname) :
436
450
run (` curl --show-error --range $(start) -$(stop) $url -o $fname ` )
@@ -439,13 +453,15 @@ function ecmwf_fc(; levlist="", kw...)
439
453
440
454
if (cubeit && n_levels > 1 ) # Save the cube (implied by 'cubeit') on a 3D file
441
455
fname = vars[k] * " _step$(step[ns]) _$(model) _$(stream) _$(type) _$(date) _$(tim) .nc" # This cube fname
456
+ fname = which_name (d, vars[k], fname, filename, " .nc" )
442
457
@info " Downloading $(vars[k]) and saving cube to $fname "
443
458
make_cube_ecmwf (G, mat, x, y, range, inc, levels, " millibars" , levels, " Pressure" , vars[1 ], fname)
444
459
end
445
460
446
461
else # A surface variable
447
462
start, len, stop = off_len[k,1 ,1 ], off_len[k,2 ,1 ], off_len[k,1 ,1 ] + off_len[k,2 ,1 ] - 1 # This var byte range
448
463
fname = vars[k] * " _step$(step[ns]) _$(model) _$(stream) _$(type) _$(date) _$(tim)$(EXT) " # This var fname
464
+ fname = which_name (d, vars[k], fname, filename, EXT)
449
465
@info " Downloading $(vars[k]) and saving to $fname "
450
466
(EXT == " .grd" ) ? gmt (" grdclip /vsisubfile/$(start) _$(len) ,/vsicurl/" * url * grdclip_cmd * fname) :
451
467
run (` curl --show-error --range $(start) -$(stop) $url -o $fname ` )
@@ -467,6 +483,7 @@ function ecmwf_fc(; levlist="", kw...)
467
483
468
484
if (cubeit && multi_steps)
469
485
fname = vars[1 ] * " _step$(step[1 ]) -$(step[end ]) _$(model) _$(stream) _$(type) _$(date) _$(tim) .nc" # This cube fname
486
+ fname = which_name (d, vars[1 ], fname, filename, " .nc" )
470
487
@info " Saving multi-step cube of $(vars[1 ]) to $fname "
471
488
make_cube_ecmwf (G, mat, x, y, range, inc, step, " hour" , step, " time" , vars[1 ], fname)
472
489
end
0 commit comments