#========== #faf3_omx.r #========== #Author: Brian Gregor, Oregon Systems Analytics LLC, gregor@or-analytics.com #February 14, 2014 # #Load rhdf5 library and omx.r api #-------------------------------- library( rhdf5 ) source( "omx.r" ) #Load FAF3 data #-------------- #Read in FAF3 data Faf3.. <- read.csv( "faf3_1_2.csv" ) #Create the OMX file #------------------- #Year for which data is included in the FAF3 data set Yr <- c( "2007", "2015", "2020", "2025", "2030", "2035", "2040" ) #Create the OMX file (one row for each row in the FAF3 data set, one column for each year createFileOMX( "FAF3.omx", Numrows=nrow(Faf3..), Numcols=length(Yr), Level=1 ) #Save weight and value matrices to file #-------------------------------------- #Write the commodity tons matrix writeMatrixOMX( OMXFileName="FAF3.omx", Matrix=as.matrix( Faf3..[,paste( "tons", Yr, sep="_" )]), MatrixSaveName="tons", RowIndex=NULL, ColIndex=NULL, NaValue=-1, Replace=FALSE, Description="Units: thousand tons (U.S. ton measure). Source: FAF3 Regional Data base for 2007 with forecasts through 2040, http://www.ops.fhwa.dot.gov/freight/freight_analysis/faf/" ) #Write the commodity value matrix writeMatrixOMX( OMXFileName="FAF3.omx", Matrix=as.matrix( Faf3..[,paste( "value", Yr, sep="_" )]), MatrixSaveName="value", RowIndex=NULL, ColIndex=NULL, NaValue=-1, Replace=FALSE, Description="Units: value in 2007 constant dollar ($million). Source: FAF3 Regional Data base for 2007 with forecasts through 2040, http://www.ops.fhwa.dot.gov/freight/freight_analysis/faf/" ) #Write the lookup vectors to the file #------------------------------------ #Write the year lookup for extracting column values writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=Yr, LookupSaveName="year", LookupDim="col", Replace=FALSE, Description="Year" ) #Write the standard commodity group number lookup SctgCode. <- as.character( Faf3..$sctg2 ) IsOneDigit. <- nchar(SctgCode.) == 1 SctgCode.[IsOneDigit.] <- paste( "0", SctgCode.[IsOneDigit.], sep="" ) writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=SctgCode., LookupSaveName="sctg2", LookupDim="row", Replace=FALSE, Description="SCTG2 number (standard classification of transported goods)" ) rm( SctgCode. ) #Write the trade type writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=Faf3..$trade_type, LookupSaveName="trade_type", LookupDim="row", Replace=FALSE, Description="U.S. Trade Type: Domestic, Import, Export" ) #Write the foreign inbound mode lookup #Note: value is NA for domestic goods, substitute -1 FrInmode. <- Faf3..$fr_inmode FrInmode.[ is.na( FrInmode. ) ] <- -1 writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=FrInmode., LookupSaveName="fr_inmode", LookupDim="row", Replace=FALSE, Description="Mode of transport from foreign origin" ) rm( FrInmode. ) #Write the foreign outbound mode lookup FrOutmode. <- Faf3..$fr_outmode FrOutmode.[ is.na( FrOutmode. ) ] <- -1 writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=FrOutmode., LookupSaveName="fr_outmode", LookupDim="row", Replace=FALSE, Description="Mode of transport to foreign destination" ) rm( FrOutmode. ) #Write the domestic mode lookup writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=Faf3..$dms_mode, LookupSaveName="dms_mode", LookupDim="row", Replace=FALSE, Description="Mode of transport to foreign destination" ) #Write the foreign origin zone lookup #Note: values are NA for domestic goods, substitute -1 FrOrig. <- Faf3..$fr_orig FrOrig.[ is.na( FrOrig. ) ] <- -1 writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=FrOrig., LookupSaveName="fr_orig", LookupDim="row", Replace=FALSE, Description="Foreign origin zone of imported goods" ) rm( FrOrig. ) #Write the domestic origin zone lookup writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=Faf3..$dms_orig, LookupSaveName="dms_orig", LookupDim="row", Replace=FALSE, Description="Domestic origin zone" ) #Write the domestic destination zone lookup writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=Faf3..$dms_dest, LookupSaveName="dms_dest", LookupDim="row", Replace=FALSE, Description="Domestic destination zone" ) #Write the foreign destination zone lookup #Note: values are NA for domestic goods, substitute -1 FrDest. <- Faf3..$fr_dest FrDest.[ is.na( FrDest. ) ] <- -1 writeLookupOMX( OMXFileName="FAF3.omx", LookupVector=FrDest., LookupSaveName="fr_dest", LookupDim="row", Replace=FALSE, Description="Foreign destination zone of exported goods" ) rm( FrDest. ) #List the contents of the FAF3.omx file #-------------------------------------- listOMX( "FAF3.omx" ) #Test extraction of data from matrices #------------------------------------- #Load correspondence table of FAF zone identifiers and names, regions, states, and positions FafZones.. <- read.csv( "FafZones.csv" ) #Identify some vectors of zones to query on WaZones. <- FafZones..$ZoneId[ FafZones..$State == "WA" ] CaZones. <- FafZones..$ZoneId[ FafZones..$State == "CA" ] #Extract tons shipped by rail from California to Washington, all commodities CaToWaRailTons_ <- readSelectedOMX( OMXFileName = "FAF3.omx", MatrixName = "tons", RowSelection = c( "dms_dest %in% WaZones.", "dms_orig %in% CaZones.", "dms_mode == 2" ), ColSelection = "year %in% c( '2007', '2015', '2020', '2025', '2030', '2035', '2040')", RowLookups = c( "sctg2", "dms_mode", "dms_orig", "dms_dest"), ColLookups = c( "year" ) ) #Pull out the matrix from the results CaToWaRailTons.XYr <- CaToWaRailTons_$Matrix #Identify the corresponding years Years <- as.numeric( unlist(CaToWaRailTons_$ColLookupValues) ) #Pull out the row lookup indexes RowIndexes.. <- CaToWaRailTons_$RowLookupValues #Calculate tons by commodity from California to Washington by year TotCaToWaRailTons.CmYr <- apply( CaToWaRailTons.XYr, 2, function(x) { tapply( x, RowIndexes..$sctg2, sum ) } ) #Plot forecast of tons by commodity matplot( Years, t( TotCaToWaRailTons.CmYr ), type="l", ylab="Tons (thousands)", main="Rail Commodity Flow from California to Washington\n2007 - 2040") #Combine the matrix and the row lookups and save CaToWaRailTons.. <- cbind( RowIndexes.., data.frame(CaToWaRailTons.XYr) ) names(CaToWaRailTons..) <- c( names(RowIndexes..), Years ) write.csv( CaToWaRailTons.., file="ca_to_wa_rail_tons.csv", row.names=FALSE )