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