1818import skimage
1919import skimage .io
2020import skimage .transform
21+ import skimage .color
2122import io
2223import datetime as dt
2324import numpy as np
24- from matplotlib import pyplot as plt
25- from matplotlib .font_manager import FontProperties
2625import smart_fact_crawler as sfc
2726import requests
2827import logging
29- import warnings
28+ from PIL import Image , ImageDraw , ImageFont
3029
3130from multiprocessing .pool import ThreadPool
3231
@@ -45,93 +44,83 @@ def empty_image(rows, cols):
4544
4645def clock2img (rows , cols ):
4746
48- dpi = 96
49- fig = plt .figure (figsize = (cols / dpi , rows / dpi ), dpi = dpi )
50- ax = plt .gca ()
51- ax .yaxis .set_visible (False )
52-
53- ax .text (
54- 0.5 , 0.7 , dt .datetime .utcnow ().strftime ('%H:%M:%S' ),
55- horizontalalignment = 'center' ,
56- verticalalignment = 'center' ,
57- fontsize = 100 , color = 'red' ,
58- transform = ax .transAxes ,
59- )
47+ img = Image .new ('RGB' , (cols , rows ))
6048
61- ax .text (
62- 0.5 , 0.3 , dt .datetime .utcnow ().strftime ('%Y.%m.%d' ),
63- horizontalalignment = 'center' ,
64- verticalalignment = 'center' ,
65- fontsize = 80 , color = 'red' ,
66- transform = ax .transAxes ,
67- )
49+ font_time = ImageFont .truetype ('DejaVuSansMono.ttf' , size = 120 )
50+ font_date = ImageFont .truetype ('DejaVuSansMono.ttf' , size = 100 )
51+ font_run = ImageFont .truetype ('DejaVuSansMono.ttf' , size = 120 )
6852
69- try :
70- run_id = sfc .main_page ().run_id
71- if run_id is not None :
72- ax .text (
73- 0.5 , 0.0 , 'Run {0: 3d}' .format (run_id ),
74- horizontalalignment = 'center' ,
75- verticalalignment = 'center' ,
76- fontsize = 60 , color = 'red' ,
77- transform = ax .transAxes ,
78- )
79- except :
80- log .exception ("Could't get run_id." )
53+ d = ImageDraw .Draw (img )
54+
55+ now = dt .datetime .utcnow ()
8156
82- ax .spines ['top' ].set_color ('none' )
83- ax .spines ['right' ].set_color ('none' )
84- ax .spines ['left' ].set_color ('none' )
85- ax .xaxis .set_ticks_position ('bottom' )
86- ax .patch .set_facecolor ('black' )
87-
88- buf = io .BytesIO ()
89- plt .savefig (
90- buf ,
91- format = 'png' ,
92- dpi = dpi ,
93- transparent = False ,
94- frameon = False ,
95- facecolor = 'black' ,
96- edgecolor = 'none' ,
57+ time = now .strftime ('%H:%M:%S' )
58+ date = now .strftime ('%Y-%m-%d' )
59+
60+ w , h = d .textsize (time , font = font_time )
61+ d .text (
62+ ((cols - w ) / 2 , 0 ),
63+ time ,
64+ font = font_time ,
65+ fill = 'red'
9766 )
98- buf .seek (0 )
99- plt .close (fig )
10067
101- return (skimage .io .imread (buf )[:, :, 0 :3 ]).astype ('uint8' )
68+ w , h = d .textsize (date , font = font_date )
69+ d .text (
70+ ((cols - w ) / 2 , (rows - h ) / 2 ),
71+ date ,
72+ font = font_date ,
73+ anchor = 'center' ,
74+ align = 'center' ,
75+ fill = 'red'
76+ )
77+ try :
78+ runs = sfc .observations (fallback = True ).runs
79+
80+ if runs :
81+ last_run = runs [- 1 ]
82+ if (dt .datetime .utcnow () - last_run .start ) <= dt .timedelta (minutes = 15 ):
83+ run_str = 'Run {0: 3d}' .format (last_run .id )
84+ w , h = d .textsize (run_str , font = font_run )
85+ d .text (
86+ ((cols - w ) / 2 , rows - h - 10 ),
87+ run_str ,
88+ font = font_run ,
89+ anchor = 'top' ,
90+ fill = 'red'
91+ )
10292
93+ except Exception as e :
94+ log .exception ("Could't get run_id." )
10395
104- def smart_fact2img ( rows , cols ):
96+ return np . array ( img , dtype = 'uint8' )
10597
106- dpi = 96
107- fig = plt .figure (figsize = (cols / dpi , rows / dpi ), dpi = dpi )
108- ax = plt .gca ()
109- ax .yaxis .set_visible (False )
11098
99+ def smart_fact2img (rows , cols ):
111100 currents = sfc .sipm_currents ()
112101 drive_pointing = sfc .drive_pointing ()
113102 weather = sfc .weather ()
114103 rel_temp = sfc .camera_climate ().relative_temperature_mean .value
115104 cam_temp = rel_temp + weather .temperature .value
116105
117- out = (
106+ status_text = (
118107 'SQM\n '
119- ' Magnitude.... {Magnitude:.1f}\n '
108+ ' Magnitude..{Magnitude: > 6 .1f}\n '
120109 'SIPM\n '
121- ' power........ {power:.1f} {power_unit:s} \n '
122- ' min med max.. {min_cur:.1f}, {med_cur:.1f}, {max_cur:.1f} {cur_unit:s}\n '
110+ ' min........{min_cur: > 6.1f} {cur_unit}\n '
111+ ' med........{med_cur: > 6.1f} {cur_unit}\n '
112+ ' max........{max_cur: > 6.1f} {cur_unit}\n '
123113 'Temp\n '
124- ' outside...... {out_temp:.1f} C\n '
125- ' container.... {cont_temp:.1f} C\n '
126- ' camera....... {cam_temp:.1f} C\n '
127- 'Source\n '
128- ' name......... {source_name}\n '
129- ' Azimuth...... {source_az:.1f} {source_az_unit:s}\n '
130- ' Zenith....... {source_zd:.1f} {source_zd_unit:s}\n '
114+ ' outside....{out_temp: > 6.1f} C\n '
115+ ' container..{cont_temp: > 6.1f} C\n '
116+ ' camera.....{cam_temp: > 6.1f} C\n '
117+ 'Pointing: {source_name}\n '
118+ ' Azimuth....{source_az: > 6.1f} {source_az_unit}\n '
119+ ' Zenith.....{source_zd: > 6.1f} {source_zd_unit}\n '
131120 ).format (
132121 Magnitude = sfc .sqm ().magnitude .value ,
133- power = currents .power .value ,
134- power_unit = currents .power .unit ,
122+ power = currents .power_camera .value ,
123+ power_unit = currents .power_camera .unit ,
135124 min_cur = currents .min_per_sipm .value ,
136125 med_cur = currents .median_per_sipm .value ,
137126 max_cur = currents .max_per_sipm .value ,
@@ -146,37 +135,14 @@ def smart_fact2img(rows, cols):
146135 source_zd_unit = drive_pointing .zenith_distance .unit ,
147136 )
148137
149- font = FontProperties ()
150- font .set_family ('monospace' )
151- ax .text (
152- 0 , 0.5 , out ,
153- verticalalignment = 'center' ,
154- fontsize = 20 , color = 'red' ,
155- horizontalalignment = 'left' ,
156- fontproperties = font ,
157- transform = ax .transAxes ,
158- )
138+ img = Image .new ('RGB' , (cols , rows ))
139+ font = ImageFont .truetype ('DejaVuSansMono.ttf' , size = 32 )
159140
160- ax .spines ['top' ].set_color ('none' )
161- ax .spines ['right' ].set_color ('none' )
162- ax .spines ['left' ].set_color ('none' )
163- ax .xaxis .set_ticks_position ('bottom' )
164- ax .patch .set_facecolor ('black' )
165-
166- buf = io .BytesIO ()
167- plt .savefig (
168- buf ,
169- format = 'png' ,
170- dpi = dpi ,
171- transparent = False ,
172- frameon = False ,
173- facecolor = 'black' ,
174- edgecolor = 'none' ,
175- )
176- buf .seek (0 )
177- plt .close (fig )
141+ d = ImageDraw .Draw (img )
142+
143+ d .text ((10 , 10 ), status_text , font = font , anchor = 'top' , fill = 'red' )
178144
179- return ( skimage . io . imread ( buf )[:, :, 0 : 3 ]). astype ( 'uint8' )
145+ return np . array ( img , dtype = 'uint8' )
180146
181147
182148def stack_image_list_into_rows_and_cols (imgs , big_rows , big_cols ):
@@ -214,7 +180,11 @@ def download_and_resize_image(url, rows, cols, fmt='jpg', fallback=True):
214180 req = requests .get (url , verify = False , timeout = 15 )
215181
216182 img = skimage .io .imread (io .BytesIO (req .content ), format = fmt )
217- img = skimage .transform .resize (img , (rows , cols , 3 ))
183+
184+ if img .ndim == 2 :
185+ img = skimage .color .gray2rgb (img )
186+
187+ img = skimage .transform .resize (img , (rows , cols ))
218188 img = (img * 255 ).astype ('uint8' )
219189
220190 log .debug ('Downloaded image from url {}' .format (url ))
0 commit comments