Skip to content

Instantly share code, notes, and snippets.

@tkych
Last active December 22, 2015 17:58

Revisions

  1. tkych revised this gist Sep 18, 2013. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ;;;; Last modified: 2013-09-16 10:02:06 tkych
    ;;;; Last modified: 2013-09-18 20:24:02 tkych

    ;; This code is in the public domain.
    ;; This script is in the public domain.
    ;; Latest version is available at https://gist.github.com/tkych/6509285


    @@ -157,30 +157,30 @@
    (drakma:http-request *monitoring-data-url* :want-stream t)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file fetching [~S]" c)))))
    "FAIL: zip-file fetching [~S]" (type-of c))))))
    ;; Generate zip file
    (handler-case
    (>> in tmp-zip-file :element-type '(unsigned-byte 8)
    :if-exists :supersede)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file generating [~S]" c)))))
    "FAIL: zip-file generating [~S]" (type-of c))))))
    ;; Parse zip file
    (multiple-value-bind
    (csv-string csv-file-name)
    (handler-case
    (parse-zip tmp-zip-file)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file parsing [~S]" c))))
    "FAIL: zip-file parsing [~S]" (type-of c)))))
    ;; Parse csv string
    (multiple-value-bind
    (title data times)
    (handler-case
    (extract-cvs-content csv-string)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: cvs-file parsing [~S]" c))))
    "FAIL: cvs-file parsing [~S]" (type-of c)))))
    ;; Print result
    (format t "~& ~A~% ~A~%~A"
    (extract-date csv-file-name)
  2. tkych revised this gist Sep 17, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ;;;; Last modified: 2013-09-16 10:02:06 tkych

    ;; This code is in Public Domain.
    ;; This code is in the public domain.
    ;; Latest version is available at https://gist.github.com/tkych/6509285


  3. tkych revised this gist Sep 16, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ;;;; Last modified: 2013-09-12 19:03:28 tkych
    ;;;; Last modified: 2013-09-16 10:02:06 tkych

    ;; This code is in Public Domain.
    ;; Latest version is available at https://gist.github.com/tkych/6509285
    @@ -147,7 +147,7 @@
    date-time)
    "\\1-\\2-\\3"))

    (defun watch (&key (detail? nil) (inf 0) (sup 500))
    (defun watch (&key (detail? nil) (min 0) (max 500))
    (let ((tmp-zip-file (generate-tmp-file-name)))
    (unwind-protect
    (progn
    @@ -192,9 +192,9 @@
    :for d :in (reverse data)
    :for tm :in (reverse times)
    :collect (format nil "~A: ~5,,,@A" d tm))
    :inf inf :sup sup)
    :min min :max max)
    (cl-spark:spark
    (nreverse data) :inf inf :sup sup))))))
    (nreverse data) :min min :max max))))))
    ;; Cleanup
    (when (probe-file tmp-zip-file)
    (delete-file tmp-zip-file))))
  4. tkych revised this gist Sep 12, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -101,7 +101,7 @@
    (if-exists :append)
    (external-format :default)
    (buff-size 4096))
    "Redirect from `input-stream' to `file-spec' like output redirector \">>\" in shell."
    "Redirect from `input-stream' to `output-filespec' like output redirector \">>\" in shell."
    (with-open-file (out output-filespec
    :direction :output
    :element-type element-type
  5. tkych revised this gist Sep 12, 2013. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ;;;; Last modified: 2013-09-12 12:04:08 tkych
    ;;;; Last modified: 2013-09-12 19:03:28 tkych

    ;; This software is in Public Domain.
    ;; This code is in Public Domain.
    ;; Latest version is available at https://gist.github.com/tkych/6509285


    @@ -55,8 +55,7 @@
    ;; http://www.city.fukushima.fukushima.jp/soshiki/29/20130222.html
    ;; http://www.mext.go.jp/english/incident/1305092.htm
    ;;
    ;; * An average person over one normal day received 10 microSv
    ;; as background dose.
    ;; * An average person over one normal day received 10 microSv as background dose.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    ;;
    @@ -77,7 +76,7 @@
    (in-package :cl-user)

    (eval-when (:compile-toplevel :load-toplevel :execute)
    (ql:quickload '(:cl-spark :drakma :zip :cl-ppcre)))
    (ql:quickload '(:cl-spark :drakma :flexi-streams :zip :cl-ppcre)))

    (defpackage #:fukushima-monitor
    (:use :cl)
    @@ -95,21 +94,22 @@
    (defun generate-tmp-file-name ()
    (format nil "./TMP-fukushima-monitor-~D.zip" (get-universal-time)))

    (defun >> (input-stream filespec
    (defun >> (input-stream output-filespec
    &key
    (element-type (stream-element-type input-stream))
    (if-does-not-exist :create)
    (if-exists :append)
    (external-format :default))
    (external-format :default)
    (buff-size 4096))
    "Redirect from `input-stream' to `file-spec' like output redirector \">>\" in shell."
    (with-open-file (out filespec
    (with-open-file (out output-filespec
    :direction :output
    :element-type element-type
    :if-does-not-exist if-does-not-exist
    :if-exists if-exists
    :external-format external-format)
    (loop
    :with buf := (make-array 4096 :element-type element-type)
    :with buf := (make-array buff-size :element-type element-type)
    :for pos := (read-sequence buf input-stream)
    :while (plusp pos)
    :do (write-sequence buf out :end pos))))
  6. tkych revised this gist Sep 12, 2013. 1 changed file with 53 additions and 44 deletions.
    97 changes: 53 additions & 44 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    ;;;; Last modified: 2013-09-11 21:11:13 tkych
    ;;;; Last modified: 2013-09-12 12:04:08 tkych

    ;; This software is in Public Domain.
    ;; Latest version is available at https://gist.github.com/tkych/6509285


    ;;====================================================================
    @@ -10,28 +11,31 @@
    ;; Usage:
    ;; ------
    ;;
    ;; * (load "fukushima-monitor.lisp")
    ;; => T
    ;;
    ;; * (fukushima-monitor:watch)
    ;; =>
    ;; 2013-09-09
    ;; 2013-09-11
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
    ;;
    ;; T
    ;;
    ;; * (fukushima-monitor:watch :detail? t)
    ;; =>
    ;; 2013-09-09
    ;; 2013-09-11
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;;
    ;; 0 500 1000
    ;; 0 250 500
    ;; ˫------------------+-------------------˧
    ;; 15: 0:00
    ;; 15: 0:30
    ;; 15: 1:00
    ;; ~~~~~~~~~~~~~~~~ omitting from 1:30 to 22:00 ~~~~~~~~~~
    ;; 16: 22:30
    ;; 15: 23:00
    ;; 15: 23:30

    ;; 16: 0:00 █▎
    ;; 16: 0:30 █▎
    ;; 16: 1:00 █▎
    ;; ~~~~~~~~~~~~~~~~ omitting from 1:30 to 22:00 ~~~~~~~~~~
    ;; 16: 22:30 █▎
    ;; 16: 23:00 █▎
    ;; 16: 23:30 █▎
    ;; T

    ;; Note:
    ;; -----
    @@ -46,12 +50,12 @@
    ;; Currently, that area is off-limits to public.
    ;;
    ;; * The air dose rate at the city hall of fukushima city (prefectural capital)
    ;; is 0.42 [microSv/h] (2013-09-11).
    ;; is 0.42 [microSv] (2013-09-11).
    ;; c.f. http://fukushima-radioactivity.jp/index.php
    ;; http://www.city.fukushima.fukushima.jp/soshiki/29/20130222.html
    ;; http://www.mext.go.jp/english/incident/1305092.htm
    ;;
    ;; * An average person over one normal day received 10 [microSv]
    ;; * An average person over one normal day received 10 microSv
    ;; as background dose.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    @@ -91,19 +95,30 @@
    (defun generate-tmp-file-name ()
    (format nil "./TMP-fukushima-monitor-~D.zip" (get-universal-time)))

    (defun redirect (input-stream output-stream
    &optional (element-type (stream-external-format input-stream)))
    (let ((buf (make-array 4096 :element-type element-type)))
    (defun >> (input-stream filespec
    &key
    (element-type (stream-element-type input-stream))
    (if-does-not-exist :create)
    (if-exists :append)
    (external-format :default))
    "Redirect from `input-stream' to `file-spec' like output redirector \">>\" in shell."
    (with-open-file (out filespec
    :direction :output
    :element-type element-type
    :if-does-not-exist if-does-not-exist
    :if-exists if-exists
    :external-format external-format)
    (loop
    :with buf := (make-array 4096 :element-type element-type)
    :for pos := (read-sequence buf input-stream)
    :while (plusp pos)
    :do (write-sequence buf output-stream :end pos))))
    :do (write-sequence buf out :end pos))))

    (defun parse-zip (tmp-zip-file)
    (zip:with-zipfile (z tmp-zip-file)
    (zip:do-zipfile-entries (cvs-file-name entry z)
    (when (search "tmp" cvs-file-name) ;"tmp" means Temporary Monitoring Post
    (return-from parse-zip
    (when (search "tmp" cvs-file-name) ;Accoding to TEPCO, "tmp" means Temporary Monitoring Post!
    (RETURN-FROM parse-zip
    (values (flexi-streams:octets-to-string
    (zip:zipfile-entry-contents entry))
    cvs-file-name))))))
    @@ -132,29 +147,24 @@
    date-time)
    "\\1-\\2-\\3"))

    (defun watch (&key (detail? nil) (inf 0) (sup 1000))
    (defun watch (&key (detail? nil) (inf 0) (sup 500))
    (let ((tmp-zip-file (generate-tmp-file-name)))
    (unwind-protect
    (progn
    (with-open-file (out tmp-zip-file
    :direction :output
    :if-does-not-exist :create
    :if-exists :supersede
    :element-type '(unsigned-byte 8))
    ;; Fatch zip data
    (with-open-stream
    (in (handler-case
    (drakma:http-request *monitoring-data-url* :want-stream t)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file fetching [~S]" c)))))
    ;; Generate zip file
    (handler-case
    (redirect in out '(unsigned-byte 8))
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file generating [~S]" c))))))

    ;; Fatch zip data
    (with-open-stream
    (in (handler-case
    (drakma:http-request *monitoring-data-url* :want-stream t)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file fetching [~S]" c)))))
    ;; Generate zip file
    (handler-case
    (>> in tmp-zip-file :element-type '(unsigned-byte 8)
    :if-exists :supersede)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file generating [~S]" c)))))
    ;; Parse zip file
    (multiple-value-bind
    (csv-string csv-file-name)
    @@ -163,7 +173,6 @@
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file parsing [~S]" c))))

    ;; Parse csv string
    (multiple-value-bind
    (title data times)
    @@ -172,7 +181,6 @@
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: cvs-file parsing [~S]" c))))

    ;; Print result
    (format t "~& ~A~% ~A~%~A"
    (extract-date csv-file-name)
    @@ -189,7 +197,8 @@
    (nreverse data) :inf inf :sup sup))))))
    ;; Cleanup
    (when (probe-file tmp-zip-file)
    (delete-file tmp-zip-file)))))
    (delete-file tmp-zip-file))))
    T)


    ;;====================================================================
  7. tkych revised this gist Sep 11, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    ;;;; Last modified: 2013-09-11 21:11:13 tkych

    ;; Author: Takaya OCHIAI <https://github.com/tkych>
    ;; This software is in Public Domain.


  8. tkych revised this gist Sep 11, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    ;;;; Last modified: 2013-09-11 21:11:13 tkych

    ;; Author: Takaya OCHIAI <https://github.com/tkych>
    ;; This software is released under Public License.
    ;; This software is in Public Domain.


    ;;====================================================================
  9. tkych revised this gist Sep 11, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -92,7 +92,7 @@
    (defun generate-tmp-file-name ()
    (format nil "./TMP-fukushima-monitor-~D.zip" (get-universal-time)))

    (defun pipeline (input-stream output-stream
    (defun redirect (input-stream output-stream
    &optional (element-type (stream-external-format input-stream)))
    (let ((buf (make-array 4096 :element-type element-type)))
    (loop
    @@ -151,7 +151,7 @@
    "FAIL: zip-file fetching [~S]" c)))))
    ;; Generate zip file
    (handler-case
    (pipeline in out '(unsigned-byte 8))
    (redirect in out '(unsigned-byte 8))
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file generating [~S]" c))))))
  10. tkych revised this gist Sep 11, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -47,12 +47,12 @@
    ;; Currently, that area is off-limits to public.
    ;;
    ;; * The air dose rate at the city hall of fukushima city (prefectural capital)
    ;; is 0.42 [microSv] (2013-09-11).
    ;; is 0.42 [microSv/h] (2013-09-11).
    ;; c.f. http://fukushima-radioactivity.jp/index.php
    ;; http://www.city.fukushima.fukushima.jp/soshiki/29/20130222.html
    ;; http://www.mext.go.jp/english/incident/1305092.htm
    ;;
    ;; * An average person over one normal day received 10 microSv
    ;; * An average person over one normal day received 10 [microSv]
    ;; as background dose.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
  11. tkych revised this gist Sep 11, 2013. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ;;;; Last modified: 2013-09-11 tkych
    ;;;; Last modified: 2013-09-11 21:11:13 tkych

    ;; Author: Takaya OCHIAI <https://github.com/tkych>
    ;; This software is released under Public License.
    @@ -40,13 +40,22 @@
    ;; * If the rate is too high, DON'T PANIC!
    ;;
    ;; 1. You should check the scale. try (fukushima-monitor:watch :detail? t).
    ;;
    ;; * 1 [Sv] = 1,000 [mSv] = 1,000,000 [microSv]
    ;; * An average person over one normal day received 10 microSv
    ;; as background dose.
    ;;
    ;; * These rate are at the Main Gate of Fukushima Daiichi Nuclear Power Station.
    ;; Currently, that area is off-limits to public.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    ;;
    ;; * The air dose rate at the city hall of fukushima city (prefectural capital)
    ;; is 0.42 [microSv] (2013-09-11).
    ;; c.f. http://fukushima-radioactivity.jp/index.php
    ;; http://www.city.fukushima.fukushima.jp/soshiki/29/20130222.html
    ;; http://www.mext.go.jp/english/incident/1305092.htm
    ;;
    ;; * An average person over one normal day received 10 microSv
    ;; as background dose.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    ;;
    ;; 2. Probably it is a bug, check the web page for real rate,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/index-e.html
  12. tkych revised this gist Sep 11, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ;;;; Last modified: 2013-09-11 tkych

    ;; Copyright (c) 2013 Takaya OCHIAI <tkych.repl@gmail.com>
    ;; Author: Takaya OCHIAI <https://github.com/tkych>
    ;; This software is released under Public License.


  13. tkych revised this gist Sep 11, 2013. 1 changed file with 33 additions and 31 deletions.
    64 changes: 33 additions & 31 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -13,15 +13,15 @@
    ;;
    ;; * (fukushima-monitor:watch)
    ;; =>
    ;; 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
    ;;
    ;;
    ;; * (fukushima-monitor:watch :detail? t)
    ;; =>
    ;; 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;;
    ;; 0 500 1000
    ;; ˫------------------+-------------------˧
    @@ -43,20 +43,20 @@
    ;; * 1 [Sv] = 1,000 [mSv] = 1,000,000 [microSv]
    ;; * An average person over one normal day received 10 microSv
    ;; as background dose.
    ;; * This rate is at the Main Gate of Fukushima Daiichi Nuclear Power Station.
    ;; Currently, that area is off limit to public.
    ;; * These rate are at the Main Gate of Fukushima Daiichi Nuclear Power Station.
    ;; Currently, that area is off-limits to public.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    ;;
    ;; 2. Probably it is a bug, so you should check the web page,
    ;; 2. Probably it is a bug, check the web page for real rate,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/index-e.html
    ;;
    ;; * If nothing is printing, DON'T PANIC!
    ;;
    ;; 1. Probably it is a bug, so you should check the web page,
    ;; 1. Probably it is a bug, check the web page for real rate,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/index-e.html
    ;;
    ;; 2. Probably monitoring data url is changed. Check the url,
    ;; 2. Probably monitoring data url had been changed, check the url,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/images/2013monitoring/f1-mp-tcnew-e.zip


    @@ -83,13 +83,13 @@
    (defun generate-tmp-file-name ()
    (format nil "./TMP-fukushima-monitor-~D.zip" (get-universal-time)))

    (defun pipeline-stream (input output
    &optional (element-type (stream-element-type input)))
    (defun pipeline (input-stream output-stream
    &optional (element-type (stream-external-format input-stream)))
    (let ((buf (make-array 4096 :element-type element-type)))
    (loop
    :for pos := (read-sequence buf input)
    :for pos := (read-sequence buf input-stream)
    :while (plusp pos)
    :do (write-sequence buf output :end pos))))
    :do (write-sequence buf output-stream :end pos))))

    (defun parse-zip (tmp-zip-file)
    (zip:with-zipfile (z tmp-zip-file)
    @@ -119,9 +119,9 @@
    (defun extract-date (csv-file-name)
    (cl-ppcre:regex-replace-all
    "(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)\\d\\d"
    (cl-ppcre:register-groups-bind (date-string)
    (cl-ppcre:register-groups-bind (date-time)
    ("f1-tmp-(.*?)-e.csv" csv-file-name)
    date-string)
    date-time)
    "\\1-\\2-\\3"))

    (defun watch (&key (detail? nil) (inf 0) (sup 1000))
    @@ -134,37 +134,39 @@
    :if-exists :supersede
    :element-type '(unsigned-byte 8))
    ;; Fatch zip data
    (let ((input
    (handler-case
    (drakma:http-request *monitoring-data-url* :want-stream t)
    (error (c) (RETURN-FROM watch
    (format t "FAIL: zip-file fetching [~S]" c))))))
    (with-open-stream
    (in (handler-case
    (drakma:http-request *monitoring-data-url* :want-stream t)
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file fetching [~S]" c)))))
    ;; Generate zip file
    (unwind-protect
    (handler-case
    (pipeline-stream input out '(unsigned-byte 8))
    (error (c) (RETURN-FROM watch
    (format t "FAIL: zip-file generating [~S]" c))))
    (close input))))
    (handler-case
    (pipeline in out '(unsigned-byte 8))
    (error (c) (RETURN-FROM watch
    (format *error-output*
    "FAIL: zip-file generating [~S]" c))))))

    ;; Zip file to csv-string
    ;; Parse zip file
    (multiple-value-bind
    (csv-string csv-file-name)
    (handler-case
    (parse-zip tmp-zip-file)
    (error (c) (RETURN-FROM watch
    (format t "FAIL: zip-file parsing [~S]" c))))
    (format *error-output*
    "FAIL: zip-file parsing [~S]" c))))

    ;; Parse csv-string
    ;; Parse csv string
    (multiple-value-bind
    (title data times)
    (handler-case
    (extract-cvs-content csv-string)
    (error (c) (RETURN-FROM watch
    (format t "FAIL: cvs-file parsing [~S]" c))))
    (format *error-output*
    "FAIL: cvs-file parsing [~S]" c))))

    ;; Print result
    (format t "~&~A~%~A~%~A"
    (format t "~& ~A~% ~A~%~A"
    (extract-date csv-file-name)
    title
    (if detail?
  14. tkych revised this gist Sep 11, 2013. 1 changed file with 95 additions and 63 deletions.
    158 changes: 95 additions & 63 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ;;;; Last modified: 2013-09-10 22:13:59 tkych
    ;;;; Last modified: 2013-09-11 tkych

    ;; Author: Takaya OCHIAI <https://github.com/tkych>
    ;; Copyright (c) 2013 Takaya OCHIAI <tkych.repl@gmail.com>
    ;; This software is released under Public License.


    @@ -10,23 +10,25 @@
    ;;
    ;; Usage:
    ;; ------
    ;;
    ;; * (fukushima-monitor:watch)
    ;; =>
    ;; Date: 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
    ;;
    ;;
    ;; * (fukushima-monitor:watch :detail? t)
    ;; =>
    ;; Date: 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;;
    ;; 0 500 1000
    ;; ˫------------------+-------------------˧
    ;; 15: 0:00 ▋
    ;; 15: 0:30 ▋
    ;; 15: 1:00 ▋
    ;; ~~~~~~~~~~~~~~~~~~omitting from 1:30-22:30~~~~~~~~~~~~~~
    ;; 15: 22:00 ▋
    ;; ~~~~~~~~~~~~~~~~ omitting from 1:30 to 22:00 ~~~~~~~~~~
    ;; 16: 22:30 ▋
    ;; 15: 23:00 ▋
    ;; 15: 23:30 ▋
    @@ -45,13 +47,15 @@
    ;; Currently, that area is off limit to public.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    ;;
    ;; 2. Probably it is a bug, so you should check the web page,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/index-e.html
    ;;
    ;; * If nothing is printing, DON'T PANIC!
    ;;
    ;; 1. Probably it is a bug, so you should check the web page,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/index-e.html
    ;;
    ;; 2. Probably monitoring data url is changed. Check the url,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/images/2013monitoring/f1-mp-tcnew-e.zip

    @@ -79,73 +83,101 @@
    (defun generate-tmp-file-name ()
    (format nil "./TMP-fukushima-monitor-~D.zip" (get-universal-time)))

    (defun pipeline-stream (input output
    &optional (element-type (stream-element-type input)))
    (let ((buf (make-array 4096 :element-type element-type)))
    (loop
    :for pos := (read-sequence buf input)
    :while (plusp pos)
    :do (write-sequence buf output :end pos))))

    (defun parse-zip (tmp-zip-file)
    (zip:with-zipfile (z tmp-zip-file)
    (zip:do-zipfile-entries (cvs-file-name entry z)
    (when (search "tmp" cvs-file-name) ;"tmp" means Temporary Monitoring Post
    (return-from parse-zip
    (values (flexi-streams:octets-to-string
    (zip:zipfile-entry-contents entry))
    cvs-file-name))))))

    (defun extract-cvs-content (csv-string)
    (let ((data nil)
    (times nil)
    (title nil))
    (cl-ppcre:do-register-groups (time rate)
    (".*?,(.*?),.*?,(.*?),.*?
    " csv-string)
    (when (stringp rate)
    (let ((num (parse-integer rate :junk-allowed t)))
    (if num
    (progn
    (push num data)
    (push time times))
    (setf title rate)))))
    (values title data times)))

    (defun extract-date (csv-file-name)
    (cl-ppcre:regex-replace-all
    "(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)\\d\\d"
    (cl-ppcre:register-groups-bind (date-string)
    ("f1-tmp-(.*?)-e.csv" csv-file-name)
    date-string)
    "\\1-\\2-\\3"))

    (defun watch (&key (detail? nil) (inf 0) (sup 1000))
    (let ((tmp-zip-file (generate-tmp-file-name)))
    (unwind-protect
    (progn
    ;; Generate zip file
    (with-open-file (out tmp-zip-file
    :direction :output
    :if-does-not-exist :create
    :if-exists :supersede
    :element-type '(unsigned-byte 8))
    ;; Fatch zip file
    ;; Fatch zip data
    (let ((input
    (handler-case
    (drakma:http-request *monitoring-data-url* :want-stream t)
    (error (c)
    (RETURN-FROM watch (format t "Fetch Fail: [~S]" c))))))
    ;; Stream to zip file
    (let ((buf (make-array 4096 :element-type '(unsigned-byte 8))))
    (loop
    :for pos := (read-sequence buf input)
    :while (plusp pos)
    :do (write-sequence buf out :end pos)))))
    ;; Zip file to data-string
    (let ((data-string nil)
    (csv-file-name nil))
    (zip:with-zipfile (z tmp-zip-file)
    (zip:do-zipfile-entries (name entry z)
    (when (search "tmp" name) ;"tmp" means Temporary Monitoring Post
    (setf data-string
    (flexi-streams:octets-to-string
    (zip:zipfile-entry-contents entry)))
    (setf csv-file-name name))))
    ;; parse data-string
    (if data-string
    (let ((data nil)
    (times nil)
    (title nil))
    (cl-ppcre:do-register-groups (time rate)
    (".*?,(.*?),.*?,(.*?),.*?
    " data-string)
    (when (stringp rate)
    (let ((num (parse-integer rate :junk-allowed t)))
    (if num
    (progn
    (push num data)
    (push time times))
    (setf title rate)))))
    (format t "~& Date: ~A~A"
    (cl-ppcre:regex-replace-all
    "(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)\\d\\d"
    (cl-ppcre:register-groups-bind (date)
    ("f1-tmp-(.*?)-e.csv" csv-file-name)
    date)
    "\\1-\\2-\\3")
    (if detail?
    (cl-spark:vspark (reverse data)
    :labels (loop
    :for d :in (reverse data)
    :for tm :in (reverse times)
    :collect (format nil "~A: ~5,,,@A" d tm))
    :inf inf :sup sup
    :title title)
    (format nil "~% ~A~%~A"
    title
    (cl-spark:spark (nreverse data)
    :inf inf :sup sup)))))
    (RETURN-FROM watch (format t "Zip-file parse Fail.")))))
    (error (c) (RETURN-FROM watch
    (format t "FAIL: zip-file fetching [~S]" c))))))
    ;; Generate zip file
    (unwind-protect
    (handler-case
    (pipeline-stream input out '(unsigned-byte 8))
    (error (c) (RETURN-FROM watch
    (format t "FAIL: zip-file generating [~S]" c))))
    (close input))))

    ;; Zip file to csv-string
    (multiple-value-bind
    (csv-string csv-file-name)
    (handler-case
    (parse-zip tmp-zip-file)
    (error (c) (RETURN-FROM watch
    (format t "FAIL: zip-file parsing [~S]" c))))

    ;; Parse csv-string
    (multiple-value-bind
    (title data times)
    (handler-case
    (extract-cvs-content csv-string)
    (error (c) (RETURN-FROM watch
    (format t "FAIL: cvs-file parsing [~S]" c))))

    ;; Print result
    (format t "~&~A~%~A~%~A"
    (extract-date csv-file-name)
    title
    (if detail?
    (cl-spark:vspark
    (reverse data)
    :labels (loop
    :for d :in (reverse data)
    :for tm :in (reverse times)
    :collect (format nil "~A: ~5,,,@A" d tm))
    :inf inf :sup sup)
    (cl-spark:spark
    (nreverse data) :inf inf :sup sup))))))
    ;; Cleanup
    (when (probe-file tmp-zip-file)
    (delete-file tmp-zip-file)))))

  15. tkych revised this gist Sep 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,7 @@
    ;; * An average person over one normal day received 10 microSv
    ;; as background dose.
    ;; * This rate is at the Main Gate of Fukushima Daiichi Nuclear Power Station.
    ;; Currently, there is off limit for an ordinary person.
    ;; Currently, that area is off limit to public.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    ;; 2. Probably it is a bug, so you should check the web page,
  16. tkych revised this gist Sep 10, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -12,13 +12,13 @@
    ;; ------
    ;; * (fukushima-monitor:watch)
    ;; =>
    ;; Posted: 2013-09-09T23
    ;; Date: 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
    ;;
    ;; * (fukushima-monitor:watch :detail? t)
    ;; =>
    ;; Posted: 2013-09-09T23
    ;; Date: 2013-09-09
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; 0 500 1000
    ;; ˫------------------+-------------------˧
  17. tkych revised this gist Sep 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ;;;; Last modified: 2013-09-10 22:13:59 tkych

    ;; Copyright (c) 2013 Takaya OCHIAI <tkych.repl@gmail.com>
    ;; Author: Takaya OCHIAI <https://github.com/tkych>
    ;; This software is released under Public License.


  18. tkych created this gist Sep 10, 2013.
    153 changes: 153 additions & 0 deletions fukushima-monitor.lisp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,153 @@
    ;;;; Last modified: 2013-09-10 22:13:59 tkych

    ;; Copyright (c) 2013 Takaya OCHIAI <[email protected]>
    ;; This software is released under Public License.


    ;;====================================================================
    ;; Radiation Monitor for Fukushima Daiichi Nuclear Power Station
    ;;====================================================================
    ;;
    ;; Usage:
    ;; ------
    ;; * (fukushima-monitor:watch)
    ;; =>
    ;; Posted: 2013-09-09T23
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
    ;;
    ;; * (fukushima-monitor:watch :detail? t)
    ;; =>
    ;; Posted: 2013-09-09T23
    ;; Radiation Dose Rate(microSv/h) At the Main Gate
    ;; 0 500 1000
    ;; ˫------------------+-------------------˧
    ;; 15: 0:00 ▋
    ;; 15: 0:30 ▋
    ;; 15: 1:00 ▋
    ;; ~~~~~~~~~~~~~~~~~~omitting from 1:30-22:30~~~~~~~~~~~~~~
    ;; 15: 22:00 ▋
    ;; 16: 22:30 ▋
    ;; 15: 23:00 ▋
    ;; 15: 23:30 ▋


    ;; Note:
    ;; -----
    ;;
    ;; * If the rate is too high, DON'T PANIC!
    ;;
    ;; 1. You should check the scale. try (fukushima-monitor:watch :detail? t).
    ;; * 1 [Sv] = 1,000 [mSv] = 1,000,000 [microSv]
    ;; * An average person over one normal day received 10 microSv
    ;; as background dose.
    ;; * This rate is at the Main Gate of Fukushima Daiichi Nuclear Power Station.
    ;; Currently, there is off limit for an ordinary person.
    ;; c.f. http://en.wikipedia.org/wiki/Sievert
    ;; http://en.wikipedia.org/wiki/Background_radiation
    ;; 2. Probably it is a bug, so you should check the web page,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/index-e.html
    ;;
    ;; * If nothing is printing, DON'T PANIC!
    ;;
    ;; 1. Probably it is a bug, so you should check the web page,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/index-e.html
    ;; 2. Probably monitoring data url is changed. Check the url,
    ;; http://www.tepco.co.jp/en/nu/fukushima-np/f1/images/2013monitoring/f1-mp-tcnew-e.zip


    ;;--------------------------------------------------------------------

    (in-package :cl-user)

    (eval-when (:compile-toplevel :load-toplevel :execute)
    (ql:quickload '(:cl-spark :drakma :zip :cl-ppcre)))

    (defpackage #:fukushima-monitor
    (:use :cl)
    (:export #:*monitoring-data-url*
    #:watch))

    (in-package #:fukushima-monitor)


    ;;--------------------------------------------------------------------

    (defparameter *monitoring-data-url*
    "http://www.tepco.co.jp/en/nu/fukushima-np/f1/images/2013monitoring/f1-mp-tcnew-e.zip")

    (defun generate-tmp-file-name ()
    (format nil "./TMP-fukushima-monitor-~D.zip" (get-universal-time)))

    (defun watch (&key (detail? nil) (inf 0) (sup 1000))
    (let ((tmp-zip-file (generate-tmp-file-name)))
    (unwind-protect
    (progn
    ;; Generate zip file
    (with-open-file (out tmp-zip-file
    :direction :output
    :if-does-not-exist :create
    :if-exists :supersede
    :element-type '(unsigned-byte 8))
    ;; Fatch zip file
    (let ((input
    (handler-case
    (drakma:http-request *monitoring-data-url* :want-stream t)
    (error (c)
    (RETURN-FROM watch (format t "Fetch Fail: [~S]" c))))))
    ;; Stream to zip file
    (let ((buf (make-array 4096 :element-type '(unsigned-byte 8))))
    (loop
    :for pos := (read-sequence buf input)
    :while (plusp pos)
    :do (write-sequence buf out :end pos)))))
    ;; Zip file to data-string
    (let ((data-string nil)
    (csv-file-name nil))
    (zip:with-zipfile (z tmp-zip-file)
    (zip:do-zipfile-entries (name entry z)
    (when (search "tmp" name) ;"tmp" means Temporary Monitoring Post
    (setf data-string
    (flexi-streams:octets-to-string
    (zip:zipfile-entry-contents entry)))
    (setf csv-file-name name))))
    ;; parse data-string
    (if data-string
    (let ((data nil)
    (times nil)
    (title nil))
    (cl-ppcre:do-register-groups (time rate)
    (".*?,(.*?),.*?,(.*?),.*?
    " data-string)
    (when (stringp rate)
    (let ((num (parse-integer rate :junk-allowed t)))
    (if num
    (progn
    (push num data)
    (push time times))
    (setf title rate)))))
    (format t "~& Date: ~A~A"
    (cl-ppcre:regex-replace-all
    "(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)\\d\\d"
    (cl-ppcre:register-groups-bind (date)
    ("f1-tmp-(.*?)-e.csv" csv-file-name)
    date)
    "\\1-\\2-\\3")
    (if detail?
    (cl-spark:vspark (reverse data)
    :labels (loop
    :for d :in (reverse data)
    :for tm :in (reverse times)
    :collect (format nil "~A: ~5,,,@A" d tm))
    :inf inf :sup sup
    :title title)
    (format nil "~% ~A~%~A"
    title
    (cl-spark:spark (nreverse data)
    :inf inf :sup sup)))))
    (RETURN-FROM watch (format t "Zip-file parse Fail.")))))
    (when (probe-file tmp-zip-file)
    (delete-file tmp-zip-file)))))


    ;;====================================================================