aboutsummaryrefslogtreecommitdiff
path: root/icon_download.sh
blob: 24d1b5ac4205c7925c4afeed45240a7359a21d83 (plain)
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
#!/bin/bash

# http://opendata.dwd.de/weather/nwp/icon-d2/grib/00/t/icon-d2_germany_regular-lat-lon_pressure-level_2023080200_000_1000_t.grib2.bz2

# <BASE>/<RUN>/<PARAMETER>/icon-d2_regular-lat-lon_pressure-level_<INIT>_<OFFSET>_<LEVEL>_<PARAMETER>

# Detect latest with
# curl https://opendata.dwd.de/weather/nwp/content.log.bz2 | bzip2 -d

function get_latest_run() {
	# Allow up to 3hours for DWD to upload the new model run. We also want UTC
	export TZ=UTC
	local corrected_date=$(date -d "$(date '+%Y-%m-%d %H:%M:%S') 3 hours ago"  '+%Y-%m-%d %H:%M:%S')
	local current_hour=$(date -d "$corrected_date" +%H)
	local run_hour=$(( (current_hour/6) * 6 ))

	RUN=$(printf "%02d" $run_hour)
	DATE=$(date -d "$corrected_date" +%Y%m%d)

	echo Selecting run $RUN - $DATE
}


NPROC=$(nproc)

#OUTDIR=dwd_icon-d2
OUTDIR=dwd_icon-eu
#MODEL=icon-d2
#MODEL_LONG=icon-d2_germany
MODEL=icon-eu
MODEL_LONG=icon-eu_europe
BASE="https://opendata.dwd.de/weather/nwp"

get_latest_run
#RUN="00"
#DATE=$(date +%Y%m%d)

# In ICON-EU, the parameter name in the filename is in caps.
# This is a stupid fix for a stupid problem.
PARAMETER_FILENAME_CAPS=yes

PARAMETERS=( "t" "relhum" "u" "v" "fi" "clc" )
# tot_prec and cape_ml/cin_ml is in 15min intervals and screws with xygrib
PARAMETERS_SINGLE_LEVEL=( "pmsl" "t_2m" "relhum_2m" )
# DE
#PRESSURE_LEVELS=( "1000" "975" "950" "850" "700" "600" "500" "400" "300" "250" "200" )
PRESSURE_LEVELS=( "1000" "950" "925" "900" "875" "850" "825" "800" "775" "700" "600" "500" "400" "300" "250" "200" "150" "100" )
OFFSETS=( "000" "003" "006" "009" "012" "015" "018" "021" "024" "027" "030" "033" "036"  "039" "042" "045" "048" )

mkdir -p $OUTDIR

for OFFSET in "${OFFSETS[@]}"; do
	for PARAMETER in "${PARAMETERS[@]}"; do
		for LEVEL in "${PRESSURE_LEVELS[@]}"; do
			while [ $(pgrep -c -P$$) -gt $NPROC ]; do
				sleep 1
			done

			if [ "$PARAMETER_FILENAME_CAPS" = "yes" ]; then
				PARAMETER2=${PARAMETER^^}
			else
				PARAMETER2=${PARAMETER}
			fi

			URL="$BASE/$MODEL/grib/$RUN/$PARAMETER/${MODEL_LONG}_regular-lat-lon_pressure-level_${DATE}${RUN}_${OFFSET}_${LEVEL}_${PARAMETER2}.grib2.bz2"
			BNAME=$(basename "$URL")
			echo Getting "$BNAME"
			( wget -q --directory-prefix=$OUTDIR  "$URL" || echo FAILED: "$BNAME" ) &

		done
	done

	for PARAMETER in "${PARAMETERS_SINGLE_LEVEL[@]}"; do
			while [ $(pgrep -c -P$$) -gt $NPROC ]; do
				sleep 1
			done

			if [ "$PARAMETER_FILENAME_CAPS" = "yes" ]; then
				PARAMETER2=${PARAMETER^^}
			else
				PARAMETER2=${PARAMETER}
			fi

			# !! Icon-D2 needs _2d_ between OFFSET and PARAMETER2
			URL="$BASE/$MODEL/grib/$RUN/$PARAMETER/${MODEL_LONG}_regular-lat-lon_single-level_${DATE}${RUN}_${OFFSET}_${PARAMETER2}.grib2.bz2"
			BNAME=$(basename "$URL")
			echo Getting "$BNAME"
			( wget -q --directory-prefix=$OUTDIR  "$URL" || echo FAILED: "$BNAME" ) &
	done
done

while [ $(pgrep -c -P$$) -gt 0 ]; do
	sleep 1
done

echo Done downloading. Decompressing...

for F in $OUTDIR/*.grib2.bz2; do
	while [ $(pgrep -c -P$$) -gt $NPROC ]; do
		sleep 1
	done

	bzip2 -df "$F" &
done

rm -f $OUTDIR/combined.grib2*

grib_copy $OUTDIR/*.grib2 $OUTDIR/combined.grib2 || exit 1
rm -f $OUTDIR/icon*.grib2

echo Done.