j@215: (cl:in-package #:amuse-meredith) j@215: j@215: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; j@215: ;; Importing j@215: j@215: (defun make-path (base-path file-prefix file-id file-suffix) j@215: (pathname (concatenate 'string j@215: base-path j@215: file-prefix j@215: (princ-to-string file-id) j@215: file-suffix))) j@215: j@229: (defun raph-c-file->list-of-lists (path) j@215: (with-open-file (stream path) j@215: (loop j@215: for row-strings = (split-sequence:split-sequence j@215: #\space (read-line stream nil) j@215: :remove-empty-subseqs t) j@215: while row-strings j@215: collect (loop j@215: for value in row-strings j@215: collect (read-from-string value) j@215: into row-objects j@215: finally (return row-objects)) j@215: into results j@215: finally (return (cdr results))))) j@215: j@215: (defvar *import-file* (pathname "/tmp/raph-c.txt")) j@215: j@215: (defun write-to-db-file (composition-id datapoints) j@215: (with-open-file (stream *import-file* j@215: :direction :output j@215: :if-exists :supersede j@215: :external-format :latin1) j@215: (sequence:dosequence (datapoint datapoints) j@215: (format stream j@215: "~S \\N ~{~S ~}~%" j@215: composition-id datapoint))) j@215: t) j@215: j@215: (defun import-meredith-composition (event-lists &key description j@215: database) j@215: "Import a composition into the database. A composition is a list of j@215: lists (dataset in the SIA sense), where each internal list (datapoint) j@215: represents an event. All datapoints are the same cardinality. The j@215: description is a string of up to 255 characters." j@215: #.(clsql:locally-enable-sql-reader-syntax) j@215: (clsql:with-transaction j@215: (:database database) j@215: (let (composition-id) j@215: (clsql:insert-records :into "meredith_compositions" j@215: :attributes '([description]) j@215: :values (list description) j@215: :database database) j@215: #.(clsql:locally-disable-sql-reader-syntax) j@215: (setf composition-id (clsql-mysql::mysql-insert-id j@215: (clsql-mysql::database-mysql-ptr j@215: database))) j@215: (write-to-db-file composition-id event-lists) j@215: (clsql:execute-command j@215: (concatenate 'string j@215: "LOAD DATA LOCAL INFILE '" j@215: (namestring *import-file*) j@215: "' INTO TABLE " j@215: "meredith_events") j@215: :database database) j@215: (delete-file *import-file*) j@215: (format t "composition added to db, id: ~A~%" composition-id) j@215: (make-meredith-composition-identifier composition-id)))) j@215: j@215: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; j@215: ;;; Import functions for specific datasets. j@215: j@215: (defun import-bach-inv+sin (&optional (database *amuse-database*)) j@215: "Imports all of Dave's encodings of Bach's Inventions and Sinfonias, j@229: and assigns them all to a unique dataset." j@215: (clsql:with-transaction j@215: (:database database) j@229: (let ((amuse-dataset-identifier (make-new-dataset j@215: "Bach Inventions and Sinfonias" j@215: database))) j@229: (import-bach-inventions database amuse-dataset-identifier) j@229: (import-bach-sinfonias database amuse-dataset-identifier) j@229: amuse-dataset-identifier))) j@215: j@229: (defun import-bach-inventions (database &optional amuse-dataset-identifier) j@215: "Imports all of Dave's encodings of Bach's Inventions." j@215: (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/") j@215: (file-prefix "bachrasmussinventio0") j@215: (file-suffix "01.raph-c")) j@215: (loop for file-id from 772 to 786 j@215: for composition-order from 1 j@215: for composition-identifier = j@215: (import-meredith-composition j@229: (raph-c-file->list-of-lists j@215: (make-path base-path file-prefix file-id file-suffix)) j@215: :description (concatenate 'string j@215: "Bach Invention No." j@215: (princ-to-string composition-order) j@215: " BWV " j@215: (princ-to-string file-id)) j@215: :database database) j@229: do (when amuse-dataset-identifier j@215: (assign-composition-to-dataset j@229: composition-identifier amuse-dataset-identifier database))))) j@215: j@229: (defun import-bach-sinfonias (database &optional amuse-dataset-identifier) j@215: "Imports all of Dave's encodings of Bach's Sinfonias." j@215: (let ((base-path "/home/jamie/Music/meredith-data/20060301raph-c/") j@215: (file-prefix "bachrasmusssinfonie0") j@215: (file-suffix "01.raph-c")) j@215: (loop for file-id from 787 to 801 j@215: for composition-order from 1 j@215: for composition-identifier = j@215: (import-meredith-composition j@229: (raph-c-file->list-of-lists j@215: (make-path base-path file-prefix file-id file-suffix)) j@215: :description (concatenate 'string j@215: "Bach Sinfonia No." j@215: (princ-to-string composition-order) j@215: " BWV " j@215: (princ-to-string file-id)) j@215: :database database) j@229: do (when amuse-dataset-identifier j@215: (assign-composition-to-dataset j@229: composition-identifier amuse-dataset-identifier database)))))