Skip to content

Commit 9849ef9

Browse files
Merged release/v0.2.0 into master
2 parents 1808aeb + 0e16d13 commit 9849ef9

25 files changed

+1202
-459
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.Rproj.user
22
.Rhistory
33
.RData
4+
inst/doc

DESCRIPTION

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
Package: omxr
22
Type: Package
33
Title: Read and Write Open Matrix Files
4-
Version: 0.1.0
5-
Date: 2015-07-15
6-
Author: Ben Stabler, Brian Gregor
7-
Maintainer: Greg Macfarlane <[email protected]>
8-
Description: The Open Matrix eXchange (OMX) file type is an open specification
9-
for sharing data from transportation models. The specification is built on HDF5;
10-
APIs to read and write are available for Cube, Emme, Python, and R.
11-
License: Apache
4+
Version: 0.2.0
5+
Date: 2016-01-06
6+
Authors@R: c(
7+
person("Brian", "Gregor", email = "", role = "aut"),
8+
person("Ben", "Stabler", email = "", role = "aut"),
9+
person("Greg", "Macfarlane", email = "[email protected]", role = "cre") )
10+
Description: The Open Matrix (OMX) file type is an open specification for
11+
sharing data from transportation models. The specification is built on HDF5;
12+
APIs to read and write are available for Cube, Emme, Python, Java, and R.
13+
License: file LICENSE
1214
LazyData: TRUE
15+
Suggests:
16+
knitr
17+
VignetteBuilder: knitr
18+
Imports:
19+
rhdf5
20+
RoxygenNote: 5.0.1

LICENSE.TXT renamed to LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
same "printed page" as the copyright notice for easier
188188
identification within third-party archives.
189189

190-
Copyright [yyyy] [name of copyright owner]
190+
Copyright [2015] [osPlanning]
191191

192192
Licensed under the Apache License, Version 2.0 (the "License");
193193
you may not use this file except in compliance with the License.

NAMESPACE

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
exportPattern("^[[:alpha:]]+")
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(convert_zmx)
4+
export(create_omx)
5+
export(get_omx_attr)
6+
export(list_omx)
7+
export(read_lookup)
8+
export(read_omx)
9+
export(read_selected_omx)
10+
export(read_zmx)
11+
export(write_lookup)
12+
export(write_matrix_attr)
13+
export(write_omx)
14+
export(write_zmx)
15+
import(rhdf5)

R/attributes.R

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#' Read OMX attributes
2+
#'
3+
#' This function reads the SHAPE and VERSION attributes of an OMX file. This is
4+
#' called by several other functions.
5+
#'
6+
#' @param file full path name of the OMX file being read
7+
#' @return A list containing \code{SHAPE} and \code{VERSION} attributes.
8+
#'
9+
#' @export
10+
#' @import rhdf5
11+
get_omx_attr <- function( file ) {
12+
H5File <- rhdf5::H5Fopen( file )
13+
H5Attr <- rhdf5::H5Aopen( H5File, "SHAPE" )
14+
RootAttr <- list()
15+
RootAttr$SHAPE <- rhdf5::H5Aread( H5Attr )
16+
rhdf5::H5Aclose( H5Attr )
17+
H5Attr <- rhdf5::H5Aopen( H5File, "OMX_VERSION" )
18+
RootAttr$VERSION <- rhdf5::H5Aread( H5Attr )
19+
rhdf5::H5Aclose( H5Attr )
20+
rhdf5::H5Fclose( H5File )
21+
22+
RootAttr
23+
}
24+
25+
#' Function to write matrix attribute
26+
#'
27+
#' @param file Full path name of the OMX file to store the matrix in. If this is
28+
#' a new matrix file, see \link{create_omx}.
29+
#' @param name Name of the matrix in the OMX object.
30+
#' @param attr_name Name of the attribute.
31+
#' @param value Attribute value
32+
#'
33+
#' @import rhdf5
34+
#'
35+
#' @export
36+
write_matrix_attr <- function( file, name, attr_name, value) {
37+
H5File <- rhdf5::H5Fopen(file)
38+
H5Group <- rhdf5::rH5Gopen( H5File, "data" )
39+
H5Data <- rhdf5::H5Dopen( H5Group, name )
40+
rhdf5::h5writeAttribute(value, H5Data, attr_name)
41+
42+
#Close everything up before exiting
43+
rhdf5::H5Dclose( H5Data )
44+
rhdf5::H5Gclose( H5Group )
45+
rhdf5::H5Fclose( H5File )
46+
}
47+
48+
49+
#' List the contents of an OMX file
50+
#'
51+
#' @param file The path to the OMX file.
52+
#'
53+
#' @return A list with 5 elements:
54+
#' \describe{
55+
#' \item{Version}{the OMX version number}
56+
#' \item{Rows}{The number of rows in the matrix.}
57+
#' \item{Columns}{The number of columns in the matrix.}
58+
#' \item{Matrices}{A \code{data.frame} identifying the matrices and their
59+
#' attributes}
60+
#' \item{Lookups}{A \code{data.frame} identifying the lookups and their
61+
#' attributes.}
62+
#' }
63+
#'
64+
#' @export
65+
#' @import rhdf5
66+
list_omx <- function( file ) {
67+
#Get the version and shape information
68+
RootAttr <- get_omx_attr( file )
69+
Version <- RootAttr$VERSION
70+
Shape <- RootAttr$SHAPE
71+
#Use the h5ls function to read the contents of the file
72+
Contents <- rhdf5::h5ls( file )
73+
MatrixContents <- Contents[ Contents$group == "/data", ]
74+
LookupContents <- Contents[ Contents$group == "/lookup", ]
75+
#Read the matrix attribute information
76+
Names <- MatrixContents$name
77+
Types <- MatrixContents$dclass
78+
H5File <- rhdf5::H5Fopen( file )
79+
H5Group <- rhdf5::H5Gopen( H5File, "data" )
80+
MatAttr <- list()
81+
82+
for( i in 1:length(Names) ) {
83+
Attr <- list(type="matrix")
84+
H5Data <- H5Dopen( H5Group, Names[i] )
85+
if(H5Aexists(H5Data, "NA")) {
86+
H5Attr <- rhdf5::H5Aopen( H5Data, "NA" )
87+
Attr$navalue <- rhdf5::H5Aread( H5Attr )
88+
rhdf5::H5Aclose( H5Attr )
89+
}
90+
if(H5Aexists(H5Data, "Description")) {
91+
H5Attr <- rhdf5::H5Aopen( H5Data, "Description" )
92+
Attr$description <- rhdf5::H5Aread( H5Attr )
93+
rhdf5::H5Aclose( H5Attr )
94+
}
95+
MatAttr[[Names[i]]] <- Attr
96+
rhdf5::H5Dclose( H5Data )
97+
rm( Attr )
98+
}
99+
100+
rhdf5::H5Gclose( H5Group )
101+
rhdf5::H5Fclose( H5File )
102+
103+
MatAttr <- do.call( rbind, lapply( MatAttr, function(x) data.frame(x) ) )
104+
rm( Names, Types )
105+
106+
#Read the lookup attribute information
107+
H5File <- rhdf5::H5Fopen( file )
108+
H5Group <- rhdf5::H5Gopen( H5File, "lookup" )
109+
Names <- LookupContents$name
110+
Types <- LookupContents$dclass
111+
LookupAttr <- list()
112+
113+
if(length(Names)>0){
114+
for( i in 1:length(Names) ) {
115+
Attr <- list()
116+
H5Data <- rhdf5::H5Dopen( H5Group, Names[i] )
117+
118+
if( rhdf5::H5Aexists( H5Data, "DIM" ) ) {
119+
H5Attr <- rhdf5::H5Aopen( H5Data, "DIM" )
120+
Attr$lookupdim <- rhdf5::H5Aread( H5Attr )
121+
rhdf5::H5Aclose( H5Attr )
122+
} else {
123+
Attr$lookupdim <- ""
124+
}
125+
126+
if( rhdf5::H5Aexists( H5Data, "Description" ) ) {
127+
H5Attr <- rhdf5::H5Aopen( H5Data, "Description" )
128+
Attr$description <- rhdf5::H5Aread( H5Attr )
129+
rhdf5::H5Aclose( H5Attr )
130+
} else {
131+
Attr$description <- ""
132+
}
133+
134+
LookupAttr[[Names[i]]] <- Attr
135+
rhdf5::H5Dclose( H5Data )
136+
rm( Attr )
137+
}
138+
139+
rhdf5::H5Gclose( H5Group )
140+
rhdf5::H5Fclose( H5File )
141+
142+
LookupAttr <- do.call( rbind, lapply( LookupAttr, function(x) data.frame(x) ) )
143+
rm( Names, Types )
144+
}
145+
146+
#Combine the results into a list
147+
if(length(MatAttr)>0) {
148+
MatInfo <- cbind( MatrixContents[,c("name","dclass","dim")], MatAttr )
149+
} else {
150+
MatInfo <- MatrixContents[,c("name","dclass","dim")]
151+
}
152+
153+
if(length(LookupAttr)>0) {
154+
LookupInfo <- cbind( LookupContents[,c("name","dclass","dim")], LookupAttr )
155+
} else {
156+
LookupInfo <- LookupContents[,c("name","dclass","dim")]
157+
}
158+
159+
list(
160+
OMXVersion=Version,
161+
Rows=Shape[1], Columns=Shape[2],
162+
Matrices=MatInfo, Lookups=LookupInfo
163+
)
164+
}

0 commit comments

Comments
 (0)