entet-examples


#|
 | #########################################################################################
 | 
 | Program which reads the raw data in a file <tensiondata> eatch line having the shape:
 | X nb1 nb2 nb3 nb4 nb5 nb6 nb7 
 |
 | X is a character indicating that the measurement is taken in the morning or evening.
 | For example L for the morning C for the evening.
 | nb1 is the systolic pressure.
 | nb2 is the diastolic pressure.
 | nb3 is the pulsation.
 | nb4 nb5 are hours and minutes of the measure.
 | nb6 nb7 are day and month of the measurement.
 | The first measure is on the bottom of the file (stacked rows) and therefore 
 | the last measurement must be at the top of the file.
 | Blank lines are ignored and the values X nb1 nb2 nb3 ... nb7 must be separated by 
 | at least one space. Non blank lines must be ordinates by the dates, we can start 
 | from the evening or morning indifferently and measures morning or evening can 
 | subsequently succeeded in case you forget to make a measurement.
 | Example of use:
 |* (transfdata)
 |  
 | specify the path of the data file
 |  
 | specify the path of the formatted file
 |  
 | Example of file tensiondata :
 |
 | L 121 72 68 06 55 13 07
 | C 141 82 72 23 00 12 07
 | L 127 79 65 07 30 11 07
 | C 145 84 70 22 45 18 07
 |
 |
 |formatted file obtained :
 |====================================================================
 |                       Robert CLINT  25/07/2015
 |====================================================================
 |          |   syst | diast |  puls |   H   |   mn  |  jour |  mois |
 |====================================================================
 |       C  |   145  |   84  |   70  |   22  |   45  |   18  |   07  |
 |--------------------------------------------------------------------
 |       L  |   127  |   79  |   65  |   07  |   30  |   11  |   07  |
 |--------------------------------------------------------------------
 |       C  |   141  |   82  |   72  |   23  |   00  |   12  |   07  |
 |--------------------------------------------------------------------
 |       L  |   121  |   72  |   68  |   06  |   55  |   13  |   07  |
 |--------------------------------------------------------------------
 |======================================
 |moy morning | 124.0  |  75.5 |  66.5 |
 |--------------------------------------
 |moy evening | 143.0  |  83.0 |  71.0 |
 |--------------------------------------
 |moy m+s     | 133.5  |  79.3 |  68.8 |
 |======================================
 |
 | ##########################################################################################
 |#

(require :cl-ppcre)
(use-package :cl-ppcre)

(defun recupfile (file)
  "Reads the file line by line;ignores blank lines and puts the results in the data variable"
  (let ((data nil) (i 0))
    (with-open-file (stream file)
      (do ((line (read-line stream nil) (read-line stream nil)))
	((null line))
	(incf i)
	(cond ((not (equal "" line))
		 (push (cl-ppcre:split "\\s+" line) data))
	      (t nil))))
	data))

(defun repchar (flux c nb)
  "repetition of character c nb times"
  (format flux "~v@{~A~:*~}" nb c))

(defun dupli (lst nb &optional (ini nb))
  "Repeat nb times the character which is in the list lst in a list"
    (cond ((eql lst nil) nil)
          ((<= nb 0) (dupli (cdr lst) ini ini))
          (t (cons (car lst) (dupli lst (1- nb) ini )))))

(defun dataform (fl data)
  "Shaping the raw data"
  (loop for i from 0 to (1- (length data)) do
	(format fl "       ~{~a  |   ~} ~%~{~a~}~%" (nth i data) (dupli '(-) 68))))

(defun entete (flux)
  (let((nomdate)(cote))
    (format t "Enter first name, last name and date in the form dd-mm-yy")
    (setf nomdate(read-line))
    (terpri)
    (setf cote (/ (- 68 (length nomdate)) 2))
    (repchar flux "=" 68)
    (terpri flux)
    (format flux "~v@{~A~:*~}" cote " ")
    (format flux "~a~%" nomdate)
    (repchar flux "=" 68)
    (terpri flux)
    (repchar flux " " 10)
    (format flux "|   syst | diast |  puls |   H   |   mn  |  day  | month | ~%")
    (repchar flux "=" 68)
    (terpri flux)))

(defun calcmL (k lst)
  "Average of the values recorded at the awakening"
  (if (= k 0) (format t "k should not be null")
    (progn
      (let ((sum 0) (lg (length lst)) (cpt 0))
	(do ((i 0 (1+ i)))
	  ((= i lg))
	  (cond ((equal (nth 0 (nth i lst)) "L")
		 (incf cpt)
		 (setf sum (+ sum (parse-integer (nth k (nth i lst))))))
		(t nil)))
	(setf  sum  (/ sum cpt))
	sum))))

(defun calcmC (k lst)
  "Average of the values recorded at the bedtime"
  (if (= k 0) (format t "k should not be null")
    (progn
      (let ((sum 0) (lg (length lst)) (cpt 0))
	(do ((i 0 (1+ i )))
	  ((= i lg))
	  (cond ((equal (nth 0 (nth i lst)) "C")
		 (incf cpt)
		 (setf sum (+ sum (parse-integer (nth k (nth i lst))))))
		(t nil )))
	(setf  sum (/ sum  cpt))
  sum))))

(defun calcm (k lst)
  "Overall average"
  (let ((sum 0) (lg (length lst)))

    (do ((i 0 (1+ i)))
      ((= i lg))
      (setf sum (+ sum (parse-integer (nth k (nth i lst))))))
      (setf  sum  (/ sum lg))
      sum))

(defun displaymoy (flux mot &optional cmoy)
  "Shaping the display of the averages"
  (repchar flux " " 1)
  (cond ((equal cmoy "C")
	 (format flux "~{| ~,1f  |  ~,1f |  ~,1f | ~}" `(,(calcmC 1 mot) ,(calcmC 2 mot) ,(calcmC 3 mot))))
	((equal cmoy "L")
	 (format flux "~{| ~,1f  |  ~,1f |  ~,1f | ~}" `(,(calcmL 1 mot) ,(calcmL 2 mot) ,(calcmL 3 mot))))
	(t (format flux "~{| ~,1f  |  ~,1f |  ~,1f | ~}" `(,(calcm 1 mot) ,(calcm 2 mot) ,(calcm 3 mot))))))

(defun formatage (inputfile outputfile)
  "Function that reads the raw data file (inputfile) and 
  writes in the file outputfile the  formatted data"

  (with-open-file (fl outputfile :direction :output :if-exists :supersede)
    (let ((data (recupfile inputfile)))
      (entete fl)
      (dataform fl data)
      (repchar fl "=" 36)
      (terpri fl)
      (format fl "moy matin")
      (displaymoy fl data "L")
      (terpri fl)
      (repchar fl "-" 36)
      (terpri fl)
      (format fl "moy soir ")
      (displaymoy fl data "C")
      (terpri fl)
      (repchar fl "-" 36)
      (terpri fl)
      (format fl "moy m+s  ")
      (displaymoy fl data)
      (terpri fl)
      (repchar fl "=" 36)
      (terpri fl))))

(defun transfdata ()

  (let ((data) (form) (rep) (old))
    (setf old (write-to-string (get-universal-time)))
    (format t "specify the path of the data file~%")
    (setf data (read-line))
    (format t "specify the path of the formatted file~%")
    (setf form (read-line))


    (cond ((not (probe-file form))
	   (formatage data form))
	  (t (format t "the ~a file exists ~%if you want to save it type save and then enter~%otherwise type overwrite then enter" form)
	     (terpri) 
	     (setf rep (read))
	     (if (equal rep 'overwrite)
	       (formatage data form)
	       (progn
		 (rename-file form (concatenate 'string form old ".txt"))
		 (formatage data form)))))))