annotate implementations/meredith/import->db.lisp @ 253:b5ffec94ae6d

some very sketchy Charm constituent code
author Jamie Forth <j.forth@gold.ac.uk>
date Thu, 24 Feb 2011 11:23:18 +0000
parents c9573d61b1b9
children
rev   line source
j@215 1 (cl:in-package #:amuse-meredith)
j@215 2
j@215 3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
j@215 4 ;; Importing
j@215 5
j@215 6 (defun make-path (base-path file-prefix file-id file-suffix)
j@215 7 (pathname (concatenate 'string
j@215 8 base-path
j@215 9 file-prefix
j@215 10 (princ-to-string file-id)
j@215 11 file-suffix)))
j@215 12
j@229 13 (defun raph-c-file->list-of-lists (path)
j@215 14 (with-open-file (stream path)
j@215 15 (loop
j@215 16 for row-strings = (split-sequence:split-sequence
j@215 17 #\space (read-line stream nil)
j@215 18 :remove-empty-subseqs t)
j@215 19 while row-strings
j@215 20 collect (loop
j@215 21 for value in row-strings
j@215 22 collect (read-from-string value)
j@215 23 into row-objects
j@215 24 finally (return row-objects))
j@215 25 into results
j@215 26 finally (return (cdr results)))))
j@215 27
j@215 28 (defvar *import-file* (pathname "/tmp/raph-c.txt"))
j@215 29
j@215 30 (defun write-to-db-file (composition-id datapoints)
j@215 31 (with-open-file (stream *import-file*
j@215 32 :direction :output
j@215 33 :if-exists :supersede
j@215 34 :external-format :latin1)
j@215 35 (sequence:dosequence (datapoint datapoints)
j@215 36 (format stream
j@215 37 "~S \\N ~{~S ~}~%"
j@215 38 composition-id datapoint)))
j@215 39 t)
j@215 40
j@215 41 (defun import-meredith-composition (event-lists &key description
j@215 42 database)
j@215 43 "Import a composition into the database. A composition is a list of
j@215 44 lists (dataset in the SIA sense), where each internal list (datapoint)
j@215 45 represents an event. All datapoints are the same cardinality. The
j@215 46 description is a string of up to 255 characters."
j@215 47 #.(clsql:locally-enable-sql-reader-syntax)
j@215 48 (clsql:with-transaction
j@215 49 (:database database)
j@215 50 (let (composition-id)
j@215 51 (clsql:insert-records :into "meredith_compositions"
j@215 52 :attributes '([description])
j@215 53 :values (list description)
j@215 54 :database database)
j@215 55 #.(clsql:locally-disable-sql-reader-syntax)
j@215 56 (setf composition-id (clsql-mysql::mysql-insert-id
j@215 57 (clsql-mysql::database-mysql-ptr
j@215 58 database)))
j@215 59 (write-to-db-file composition-id event-lists)
j@215 60 (clsql:execute-command
j@215 61 (concatenate 'string
j@215 62 "LOAD DATA LOCAL INFILE '"
j@215 63 (namestring *import-file*)
j@215 64 "' INTO TABLE "
j@215 65 "meredith_events")
j@215 66 :database database)
j@215 67 (delete-file *import-file*)
j@215 68 (format t "composition added to db, id: ~A~%" composition-id)
j@215 69 (make-meredith-composition-identifier composition-id))))
j@215 70
j@215 71 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
j@215 72 ;;; Import functions for specific datasets.
j@215 73
j@215 74 (defun import-bach-inv+sin (&optional (database *amuse-database*))
j@215 75 "Imports all of Dave's encodings of Bach's Inventions and Sinfonias,
j@229 76 and assigns them all to a unique dataset."
j@215 77 (clsql:with-transaction
j@215 78 (:database database)
j@229 79 (let ((amuse-dataset-identifier (make-new-dataset
j@215 80 "Bach Inventions and Sinfonias"
j@215 81 database)))
j@229 82 (import-bach-inventions database amuse-dataset-identifier)
j@229 83 (import-bach-sinfonias database amuse-dataset-identifier)
j@229 84 amuse-dataset-identifier)))
j@215 85
j@229 86 (defun import-bach-inventions (database &optional amuse-dataset-identifier)
j@215 87 "Imports all of Dave's encodings of Bach's Inventions."
j@215 88 (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/")
j@215 89 (file-prefix "bachrasmussinventio0")
j@215 90 (file-suffix "01.raph-c"))
j@215 91 (loop for file-id from 772 to 786
j@215 92 for composition-order from 1
j@215 93 for composition-identifier =
j@215 94 (import-meredith-composition
j@229 95 (raph-c-file->list-of-lists
j@215 96 (make-path base-path file-prefix file-id file-suffix))
j@215 97 :description (concatenate 'string
j@215 98 "Bach Invention No."
j@215 99 (princ-to-string composition-order)
j@215 100 " BWV "
j@215 101 (princ-to-string file-id))
j@215 102 :database database)
j@229 103 do (when amuse-dataset-identifier
j@215 104 (assign-composition-to-dataset
j@229 105 composition-identifier amuse-dataset-identifier database)))))
j@215 106
j@229 107 (defun import-bach-sinfonias (database &optional amuse-dataset-identifier)
j@215 108 "Imports all of Dave's encodings of Bach's Sinfonias."
j@215 109 (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/")
j@215 110 (file-prefix "bachrasmusssinfonie0")
j@215 111 (file-suffix "01.raph-c"))
j@215 112 (loop for file-id from 787 to 801
j@215 113 for composition-order from 1
j@215 114 for composition-identifier =
j@215 115 (import-meredith-composition
j@229 116 (raph-c-file->list-of-lists
j@215 117 (make-path base-path file-prefix file-id file-suffix))
j@215 118 :description (concatenate 'string
j@215 119 "Bach Sinfonia No."
j@215 120 (princ-to-string composition-order)
j@215 121 " BWV "
j@215 122 (princ-to-string file-id))
j@215 123 :database database)
j@229 124 do (when amuse-dataset-identifier
j@215 125 (assign-composition-to-dataset
j@229 126 composition-identifier amuse-dataset-identifier database)))))