summaryrefslogtreecommitdiffstats
path: root/util/bin/make-realwallpaper
blob: ba2c68535168c0c07ec17fa3c5e6053783596984 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/sh
set -euf

###### USAGE #####
#run in new directory(will be polluted with images
#just run ./make-realwallpaper

main() {
  # fetch source images in parallel
  #fetch nightmap-old-raw.jpg \
  #  http://awka.sourceforge.net/Night_le_huge.jpg &
  fetch nightmap-raw.jpg \
     http://www.zeitnews.org/sites/default/files/users/20/article_slideshow_images/nasa-noaa-satellite-reveals-new-views-earth-night-1354814354_0.jpg &
  fetch daymap-raw.png \
    http://www.nnvl.noaa.gov/images/globaldata/SnowIceCover_Daily.png &
  fetch clouds-raw.jpg \
    http://user.chol.com/~winxplanet/cloud_data/clouds_2048.jpg &
  fetch krebs.sat.tle \
     http://www.celestrak.com/NORAD/elements/stations.txt &
  wait

  #check_type nightmap-old-raw.jpg image
  check_type nightmap-raw.jpg image
  check_type daymap-raw.png image
  check_type clouds-raw.jpg image

  in_size=2048x1024
  xplanet_out_size=1466x1200
  out_geometry=1366x768+100+160

  nightsnow_color='#0c1a49'  # nightmap
  #nightsnow_color='#0a3b5c'  # nightmap-old

  # normalize *-raw.* to *.png
  #nightmap-old-raw.jpg
  for raw in \
      nightmap-raw.jpg \
      daymap-raw.png \
      clouds-raw.jpg \
      ;
  do
    normal=${raw%-raw.*}.png
    if needs_rebuild $normal $raw; then
      echo "make $normal; normalize $raw" >&2
      convert $raw -scale $in_size $normal
    fi
  done

  # create nightmap-fullsnow
  if needs_rebuild nightmap-fullsnow.png; then
    convert -size $in_size xc:$nightsnow_color nightmap-fullsnow.png
  fi

  # extract daymap-snowmask from daymap-final
  if needs_rebuild daymap-snowmask.png daymap.png; then
    convert daymap.png -threshold 95% daymap-snowmask.png
  fi

  # extract nightmap-lightmask from nightmap
  if needs_rebuild nightmap-lightmask.png nightmap.png; then
    convert nightmap.png -threshold 25% nightmap-lightmask.png
  fi

  # create layers
  make_layer nightmap-snowlayer.png nightmap-fullsnow.png daymap-snowmask.png
  make_layer nightmap-lightlayer.png nightmap.png nightmap-lightmask.png

  # apply layers
  flatten nightmap-lightsnowlayer.png \
    nightmap-lightlayer.png \
    nightmap-snowlayer.png

  flatten nightmap-final.png \
    nightmap-lightsnowlayer.png \
    nightmap.png
    # nightmap-old.png

  # make all unmodified files as final
  for normal in \
      daymap.png \
      clouds.png \
      ;
  do
    final=${normal%.png}-final.png
    needs_rebuild $final &&
      ln $normal $final
  done

  make_gcloud_cloudmask

  map=daymap-final.png
  night_map=nightmap-final.png
  cloud_map=gcloud-cloudmask.png
  satellite_file=krebs.sat

  # create xplanet output
  cat >xplanet.config <<EOF
[earth]
"Earth"
map=$map
night_map=$night_map
cloud_map=$cloud_map
cloud_threshold=10
shade=15
EOF

  # create xplanet output satellite version
  cat >xplanet-sat.config <<EOF
[earth]
"Earth"
map=$map
night_map=$night_map
cloud_map=$cloud_map
cloud_threshold=10
satellite_file=$satellite_file
shade=15
EOF

  if needs_rebuild krebs.sat; then
    cat >krebs.sat <<EOF
25544 "ISS" Image=none trail={orbit,-2,2,1} color=grey thickness=1 fontsize=10
37820 "T1" Image=none trail={orbit,-2,2,1} color=grey thickness=1 fontsize=10
39175 "ATV-4" Image=none trail={orbit,-2,2,1} color=grey thickness=1 fontsize=10
39258 "CYG" Image=none trail={orbit,-2,2,1} color=grey thickness=1 fontsize=10
EOF
  fi

  if needs_rebuild krebs.mar; then
    cat >krebs.mar <<EOF
EOF
  fi

  # rebuild every time to update shadow
  xplanet --num_times 1 --geometry $xplanet_out_size \
    --output xplanet-output.png --projection merc -config xplanet.config

  # rebuild everytime satellite version
  xplanet --num_times 1 --geometry $xplanet_out_size \
    --output xplanet-sat-output.png --projection merc -config xplanet-sat.config

  # trim xplanet output
  if needs_rebuild realwallpaper.png xplanet-output.png; then
    convert xplanet-output.png -crop $out_geometry realwallpaper.png
  fi

  # trim xplanet-sat output
  if needs_rebuild realwallpaper-sat.png xplanet-sat-output.png; then
    convert xplanet-sat-output.png -crop $out_geometry realwallpaper-sat.png
  fi
}

# generate clouds from google maps
make_gcloud_cloudmask() {
  echo 'fetch gcloud-*.png tiles' >&2
  for y in $(seq -w 0 15); do
    for x in $(seq -w 0 15); do
      echo "curl -sS -o gcloud-$y-$x.png -z gcloud-$y-$x.png \\\"https://mts0.google.com/vt/lyrs=h@239000000,weather_nolabels,weather_0cloud&hl=en&src=app&x=$x&y=$y&z=4&s=Galil\\\""
    done
  done | xargs --max-args=1 -P 10 -I @ sh -c @
  gcloud_tiles=$(find -name 'gcloud-[0-9][0-9]-[0-9][0-9].png'|sort)
  if needs_rebuild gcloud-raw.png $gcloud_tiles; then
    echo 'make gcloud-raw.png' &&
    montage -mode Concatenate -background None \
      $gcloud_tiles -tile x16 gcloud-raw.png
  fi

  check_type gcloud-raw.png image

  gcloud_in_size=2048x2048
  gcloud_out_size=2048x1024
  gcloud_out_geometry=2048x1024+0+512
  gcloud_base_color='#ffffff'

  if needs_rebuild gcloud-normal.png gcloud-raw.png; then
    echo "make gcloud-normal.png; normalize gcloud-raw.png" >&2
    convert -flatten gcloud-raw.png \
      -scale $gcloud_in_size gcloud-normal.png
  fi

  if needs_rebuild gcloud-distmap.png; then
    convert -size 2048x2048 gradient: -rotate 180 \
      -fx "p{i, (asinh(tan((j/h+0.5)*pi))/2.6+0.5) * h }" \
      gcloud-distmap.png
  fi

  if needs_rebuild gcloud-cloudmask.png gcloud-normal.png; then
    echo 'make gcloud-cloudmask.png' &&
    convert gcloud-normal.png gcloud-distmap \
      -fx 'p{i,v*h}' \
      -crop $gcloud_out_geometry \
      gcloud-cloudmask.png
  fi

  if needs_rebuild gcloud-fullcloud.png; then
    echo 'make gcloud-fullcloud.png' &&
    convert -size $gcloud_out_size xc:$gcloud_base_color gcloud-fullcloud.png
  fi
}

# usage: getimg FILENAME URL
fetch() {
  echo "fetch $1"
  curl -sS -z "$1" -o "$1" "$2"
}

# usage: check_type FILENAME TYPE
check_type() {
  if ! file -ib "$1" | grep -q "^$2/"; then
    echo "$1 is not of type $2" >&2
    rm "$1"
    return 1
  fi
}

# usage: image_size FILENAME
image_size() {
  identify "$1" | awk '{print$3}'
}

# usage: make_mask DST SRC MASK 
make_layer() {
  if needs_rebuild "$@"; then
    echo "make $1 (apply mask)" >&2
    convert "$2" "$3" -alpha off -compose copy_opacity -composite "$1"
  fi
}

# usage: flatten DST HILAYER LOLAYER
flatten() {
  if needs_rebuild "$@"; then
    echo "make $1 (flatten)" >&2
    composite "$2" "$3" "$1"
  fi
}

# usage: needs_rebuild DST SRC...
needs_rebuild() {
  a="$1"
  shift
  if ! test -e "$a"; then
    #echo "  $a does not exist" >&2
    result=0
  else
    result=1
    for b; do
      if test "$b" -nt "$a"; then
        #echo "  $b is newer than $a" >&2
        result=0
      fi
    done
  fi
  #case $result in
  #  0) echo "$a needs rebuild" >&2;;
  #esac
  return $result
}

main "$@"