This repository has no description
12 kB
374 lines
1# Copied from stockholm krebs/5pkgs/simple/realwallpaper (cc283503).
2# nomads-cloud and grib2json are its stockholm dependencies, used only here.
3{ pkgs, callPackage }:
4let
5 nomads-cloud = callPackage ./nomads-cloud.nix { };
6in
7(pkgs.writers.writeDashBin "generate-wallpaper" ''
8 set -euf
9
10 export PATH=${
11 pkgs.lib.makeBinPath (
12 [
13 nomads-cloud
14 ]
15 ++ (with pkgs; [
16 coreutils
17 curl
18 gnugrep
19 gnused
20 file
21 findutils
22 imagemagick
23 inkscape
24 jq
25 xplanet
26 ])
27 )
28 }
29
30 # usage: getimg FILENAME URL
31 fetch() {
32 echo "fetch $1"
33 curl -LsS -z "$1" -o "$1" "$2"
34 }
35
36 # usage: check_type FILENAME TYPE
37 check_type() {
38 if ! file -ib "$1" | grep -q "^$2/"; then
39 echo "$1 is not of type $2" >&2
40 rm "$1"
41 return 1
42 fi
43 }
44
45 # check if file exists and fetch only if missing
46 fetch_once() {
47 name=$1
48 url=$2
49 test -e "$name" || fetch "$name" "$url"
50 }
51
52 fetch_older_min() {
53 min=$1
54 name=$2
55 url=$3
56 if ! test "$(find $name -mmin -$min)"; then
57 fetch "$name" "$url"
58 fi
59 }
60
61 fetch_older_days() {
62 days=$1
63 name=$2
64 url=$3
65 if ! test "$(find $name -mtime -$days)"; then
66 fetch "$name" "$url"
67 fi
68 }
69
70 # usage: needs_rebuild DST SRC...
71 needs_rebuild() {
72 a="$1"
73 shift
74 if ! test -e "$a"; then
75 #echo " $a does not exist" >&2
76 result=0
77 else
78 result=1
79 for b; do
80 if check_type "$b" image; then
81 if test "$b" -nt "$a"; then
82 #echo " $b is newer than $a" >&2
83 result=0
84 fi
85 fi
86 done
87 fi
88 #case $result in
89 # 0) echo "$a needs rebuild" >&2;;
90 #esac
91 return $result
92 }
93
94 get_neo_url() {
95 url=$1
96 curl -Ss "$url" | grep '3600 x 1800' | sed 's/.*href="\([^"]*\)".*/\1/'
97 }
98
99 main() {
100 cd "''${working_dir:-$PWD}"
101
102 # fetch source images in parallel
103 fetch_once nightmap-raw.jpg \
104 'https://eoimages.gsfc.nasa.gov/images/imagerecords/144000/144898/BlackMarble_2016_3km.jpg' &
105 fetch_once daymap-raw.tif \
106 'https://eoimages.gsfc.nasa.gov/images/imagerecords/57000/57752/land_shallow_topo_8192.tif' &
107
108 fetch_once mercury-raw.svg \
109 'https://upload.wikimedia.org/wikipedia/commons/2/2e/Mercury_symbol.svg' &
110 fetch_once venus-raw.svg \
111 'https://upload.wikimedia.org/wikipedia/commons/6/66/Venus_symbol.svg' &
112 fetch_once mars-raw.svg \
113 'https://upload.wikimedia.org/wikipedia/commons/b/b7/Mars_symbol.svg' &
114 fetch_once jupiter-raw.svg \
115 'https://upload.wikimedia.org/wikipedia/commons/2/26/Jupiter_symbol.svg' &
116 fetch_once saturn-raw.svg \
117 'https://upload.wikimedia.org/wikipedia/commons/7/74/Saturn_symbol.svg' &
118 fetch_once uranus-raw.svg \
119 'https://upload.wikimedia.org/wikipedia/commons/f/f1/Uranus_symbol.svg' &
120 fetch_once neptune-raw.svg \
121 'https://upload.wikimedia.org/wikipedia/commons/4/47/Neptune_symbol.svg' &
122
123 fetch_once krebs-raw.svg \
124 'https://raw.githubusercontent.com/krebs/painload/master/cholerab/bling/krebs_aquarium.svg' &
125
126 fetch_older_min 720 ice-raw.jpg $(get_neo_url \
127 'https://neo.gsfc.nasa.gov/view.php?datasetId=NISE_D') &
128 fetch_older_days 1 snow-raw.jpg $(get_neo_url \
129 'https://neo.gsfc.nasa.gov/view.php?datasetId=MOD10C1_E_SNOW') &
130 fetch_older_days 1 chlora-raw.jpg $(get_neo_url \
131 'https://neo.gsfc.nasa.gov/view.php?datasetId=MY1DMM_CHLORA') &
132 fetch_older_days 1 fire-raw.jpg $(get_neo_url \
133 'https://neo.gsfc.nasa.gov/view.php?datasetId=MOD14A1_E_FIRE') &
134
135 # regular fetches
136 fetch marker.json.tmp "''${marker_url:-}" || :
137 if [ -s marker.json.tmp ]; then
138 mv marker.json.tmp marker.json
139 fi
140 fetch sun-raw.jpg 'https://sdo.gsfc.nasa.gov/assets/img/latest/latest_512_0171.jpg' &
141
142 wait
143
144 # fetch clouds if they are older than 3h
145 if ! test "$(find clouds-raw.png -mmin -180)"; then
146 nomads-cloud clouds-raw.png
147 fi
148
149 in_size=3600x1800
150 xplanet_out_size=3200x2500
151 out_geometry=3200x1800+0+350
152
153 for raw in \
154 nightmap-raw.jpg \
155 daymap-raw.tif \
156 chlora-raw.jpg \
157 clouds-raw.png \
158 ;
159 do
160 normal=''${raw%-raw.*}.png
161 if needs_rebuild $normal $raw; then
162 echo "make $normal; normalize $raw" >&2
163 convert $raw -scale $in_size $normal
164 fi
165 done
166
167 # remove snow from ice map
168 if needs_rebuild ice.png ice-raw.jpg; then
169 convert ice-raw.jpg -fuzz 20% -fill black -opaque white -colorspace gray -blur 0x6 -scale "$in_size" ice.png
170 fi
171
172 if needs_rebuild snow.png snow-raw.jpg; then
173 convert snow-raw.jpg -fuzz 20% -fill '#DEDEDE' -opaque white -scale "$in_size" snow.png
174 fi
175
176 # make fire more red
177 if needs_rebuild fire.png fire-raw.jpg; then
178 convert fire-raw.jpg -fuzz 20% -fill '#ef840c' -opaque white -scale "$in_size" fire.png
179 fi
180
181 # cut out sun with alpha transparency
182 if needs_rebuild sun.png sun-raw.jpg; then
183 convert sun-raw.jpg \
184 \( +clone -colorspace HSB -fill white -draw "circle 256,256 256,54" -separate -delete 0,1 \) \
185 -compose copyopacity -composite -crop 512x472+0+20 -scale "100x100" sun.png
186 fi
187
188 if needs_rebuild krebs.png krebs-raw.svg; then
189 inkscape --export-type="png" --export-width=16 --export-height=16 --export-filename=krebs.png krebs-raw.svg
190 fi
191
192 # -- Planets --
193 for planet in mercury venus mars jupiter saturn uranus neptune; do
194 if needs_rebuild "$planet".png "$planet"-raw.svg; then
195 sed -i 's/#000/#FE8019/g' "$planet"-raw.svg
196 inkscape --export-type="png" --export-width=40 --export-height=40 --export-filename="$planet.png" "$planet-raw.svg"
197 fi
198 done
199
200 # -- Daymap --
201
202 # merge with water chlora layer
203 if needs_rebuild daymap-final.png daymap.png fire.png snow.png ice.png chlora.png; then
204 convert daymap.png fire.png -compose lighten -composite daymap-1.png
205 convert daymap-1.png ice.png -compose lighten -composite daymap-2.png
206 convert daymap-2.png snow.png -compose lighten -composite daymap-3.png
207 convert daymap-3.png chlora.png -compose lighten -composite daymap-final.png
208 fi
209
210 # -- Nightmap --
211
212 if needs_rebuild nightmap-final.png nightmap.png fire.png snow.png ice.png chlora.png; then
213 convert nightmap.png fire.png -compose lighten -composite nightmap-1.png
214 convert nightmap-1.png \( -fill black -colorize 70% ice.png \) -compose lighten -composite nightmap-2.png
215 convert nightmap-2.png \( -fill black -colorize 70% snow.png \) -compose lighten -composite nightmap-3.png
216 convert nightmap-3.png \( -fill black -colorize 70% chlora.png \) -compose lighten -composite nightmap-final.png
217 fi
218
219 # create marker file from json
220 echo 'position=sun image=sun.png' > marker_file
221 echo 'position=moon image=moon.png' >> marker_file
222 echo 'position=mercury image=mercury.png' >> marker_file
223 echo 'position=venus image=venus.png' >> marker_file
224 echo 'position=mars image=mars.png' >> marker_file
225 echo 'position=jupiter image=jupiter.png' >> marker_file
226 echo 'position=saturn image=saturn.png' >> marker_file
227 echo 'position=uranus image=uranus.png' >> marker_file
228 echo 'position=neptune image=neptune.png' >> marker_file
229
230 # generate moon
231 xplanet -body moon --num_times 1 -origin earth \
232 --transpng moon.png --geometry 50x50 \
233 -config ${pkgs.writeText "moon.config" ''
234 [moon]
235 shade=10
236 ''}
237
238 # rebuild every time to update shadow
239 xplanet --num_times 1 --geometry $xplanet_out_size \
240 --output xplanet-output.png --projection merc \
241 -config ${pkgs.writeText "xplanet.config" ''
242 [earth]
243 "Earth"
244 map=daymap-final.png
245 night_map=nightmap-final.png
246 cloud_map=clouds.png
247 cloud_threshold=1
248 cloud_gamma=10
249 shade=15
250 ''}
251
252 xplanet --num_times 1 --geometry $xplanet_out_size \
253 --output xplanet-marker-output.png --projection merc \
254 -config ${pkgs.writeText "xplanet-marker.config" ''
255 [earth]
256 "Earth"
257 map=daymap-final.png
258 night_map=nightmap-final.png
259 cloud_map=clouds.png
260 cloud_threshold=1
261 cloud_gamma=10
262 marker_file=marker_file
263 shade=15
264 ''}
265
266 if [ -s marker.json ]; then
267 jq -r 'to_entries[] | select(.value.latitude != null) | @json "\(.value.latitude) \(.value.longitude) image=krebs.png"' marker.json >> marker_file
268 fi
269
270 xplanet --num_times 1 --geometry $xplanet_out_size \
271 --output xplanet-krebs-output.png --projection merc \
272 -config ${pkgs.writeText "xplanet-krebs.config" ''
273 [earth]
274 "Earth"
275 map=daymap-final.png
276 night_map=nightmap-final.png
277 cloud_map=clouds.png
278 cloud_threshold=1
279 cloud_gamma=10
280 marker_file=marker_file
281 shade=15
282 ''}
283
284 ${
285 pkgs.writers.writePython3 "get_constellations" {
286 libraries = [ pkgs.python3Packages.astropy ];
287 } ./get_constellations.py
288 } ${
289 pkgs.fetchurl {
290 url = "https://raw.githubusercontent.com/ofrohn/d3-celestial/7e720a3de062059d4c5400a379146a601d9010e0/data/constellations.lines.json";
291 hash = "sha256-KU9mvvXVz1Cx4X8W0u+h2XoVExYSxo3ZNa3vbnNz4Tw=";
292 }
293 } > constellations.arcs || : # seems like astropy doesn't want to convert from icrs to itrs anymore
294
295 xplanet --num_times 1 --geometry $xplanet_out_size \
296 --output xplanet-krebs-stars-output.png --projection merc \
297 -config ${pkgs.writeText "xplanet-krebs-stars.config" ''
298 [default]
299
300 arc_thickness=1
301 arc_file=constellations.arcs
302
303 [earth]
304 "Earth"
305 map=daymap-final.png
306 night_map=nightmap-final.png
307 cloud_map=clouds.png
308 cloud_threshold=1
309 cloud_gamma=10
310 marker_file=marker_file
311 shade=15
312 ''}
313
314 xplanet --num_times 1 --geometry $xplanet_out_size \
315 --latitude 52.520008 --longitude 13.404954 \
316 --output xplanet-krebs-stars-berlin-output.png --projection merc \
317 -config ${pkgs.writeText "xplanet-krebs-stars.config" ''
318 [default]
319
320 arc_thickness=1
321 arc_file=constellations.arcs
322
323 [earth]
324 "Earth"
325 map=daymap-final.png
326 night_map=nightmap-final.png
327 cloud_map=clouds.png
328 cloud_threshold=1
329 cloud_gamma=10
330 marker_file=marker_file
331 shade=15
332 ''}
333
334 # trim xplanet output
335 if needs_rebuild realwallpaper.png xplanet-output.png; then
336 convert xplanet-output.png -crop $out_geometry \
337 realwallpaper-tmp.png
338 mv realwallpaper-tmp.png realwallpaper.png
339 fi
340
341 if needs_rebuild realwallpaper-marker.png xplanet-marker-output.png; then
342 convert xplanet-marker-output.png -crop $out_geometry \
343 realwallpaper-marker-tmp.png
344 mv realwallpaper-marker-tmp.png realwallpaper-marker.png
345 fi
346
347 if needs_rebuild realwallpaper-krebs.png xplanet-krebs-output.png; then
348 convert xplanet-krebs-output.png -crop $out_geometry \
349 realwallpaper-krebs-tmp.png
350 mv realwallpaper-krebs-tmp.png realwallpaper-krebs.png
351 mkdir -p archive
352 convert realwallpaper-krebs.png archive/"$(date -Is)".jpg
353 fi
354
355 if needs_rebuild realwallpaper-krebs-stars.png xplanet-krebs-stars-output.png; then
356 convert xplanet-krebs-stars-output.png -crop $out_geometry \
357 realwallpaper-krebs-stars-tmp.png
358 mv realwallpaper-krebs-stars-tmp.png realwallpaper-krebs-stars.png
359 fi
360
361 if needs_rebuild realwallpaper-krebs-stars-berlin.png xplanet-krebs-stars-berlin-output.png; then
362 convert xplanet-krebs-stars-berlin-output.png -crop $out_geometry \
363 realwallpaper-krebs-stars-berlin-tmp.png
364 mv realwallpaper-krebs-stars-berlin-tmp.png realwallpaper-krebs-stars-berlin.png
365 fi
366 }
367
368 main "$@"
369'').overrideAttrs
370 (old: {
371 passthru = (old.passthru or { }) // {
372 updateScript = ./update.sh;
373 };
374 })