## Create empty HDF5 file in the writer process library(rhdf5) hf <- "x.hdf5" h5createFile(hf) # [1] TRUE h5dump(hf)
## Then, in the reader process open a handle and dump the file library(rhdf5) hf <- "x.hdf5" fid <- H5Fopen(hf) h5dump(fid)
## Now leave the rhdf5 handle open in the reader and go back to the ## writer process and write a data set h5write(1, hf, "foo") h5dump(hf) # $foo # [1] 1
## Now go back to the reader and try to read it: h5dump(fid)
## That is not right---it doesn't reflect the change. ## Maybe the handle is bad. Try to read it using the filename instead h5dump(hf)
## still can't see it. Try a new rhdf5 handle fid2 <- H5Fopen(hf) h5dump(fid2)
## Still can't see it. Turns out if I close all the open rhdf5 handles ## I can see it. H5Fclose(fid) H5Fclose(fid2) h5dump(hf)
source("Rutils.R") # get well parameters: input and calculated params <- well.params() well.data <- params$out.data well.calc <- params$out.calc # get the well name from the parameters well.name <- well.data$well.name # wellFile <- paste(paste("./data", well.name, sep = "/"), "hdf5", sep = ".") # save(well.data, file = well.file) # build the full filename wells.dir <- "./" wellFile <- file.path(wells.dir, "WOODLIB.01.hdf5") # add FIELD group and WELL group field <- "HAGBR" # field name hg <- add.wellRecord(wellFile, field, well.name) hg ds.well.data <- paste(hg, "well.data", sep = "/") ds.well.calc <- paste(hg, "well.calc", sep = "/") h5write(well.data, wellFile, ds.well.data) h5write(well.calc, wellFile, ds.well.calc) H5close()
suppressPackageStartupMessages(library(rhdf5)) # if the HDF file does not exist it will create one if (file.exists(wellFile)) { file.remove(wellFile) # option 1: remove the file } else { fid <- H5Fcreate(wellFile) } # h5createFile(well.file) h5save(well.data, well.calc, file = wellFile) #H5close() #h5wH <- h5write(well.data, file = wellFile, "welldata") # H5Dclose(did) # H5Sclose(sid) H5Fclose(fid) H5close()
# write a scalar library(rhdf5) # if the HDF file does not exist it will create one if (file.exists(wellFile)) file.remove(wellFile) if (!file.exists(wellFile)) fid <- H5Fcreate(wellFile) sid <- H5Screate_simple(c(1,1)) did <- H5Dcreate(fid, "THT", "H5T_STD_I32LE", sid) H5Dwrite(did, well.data$tht, h5spaceMem = sid, h5spaceFile = sid) H5Dclose(did) H5Sclose(sid) H5Fclose(fid)
well.params <- function(well.name = "ExampleGuo", depth.wh = 0, # depth at wellhead depth.bh = 9700, # depth at bottomhole diam.in = 1.995, GLR = 75, liq.rt = 758, wcut = 0.10, thp = 200, # tubing head pressure tht = 80, # tubing head temperature bht = 180, # bottomhole temperature API = 40, gas.sg = 0.7, wat.sg = 1.05, oil.visc = 5, ed = 0.0006, # relative roughness if.tens = 30, segments = 30, # number of depth segments model = "hagbr.mod", # VLP model selection salinity = 0 ) { # calculate segments and depths # depth counts have to be greater than segments to allocate the zero # or initial depth value # consider that in length.out parameter depths <- seq.int(from = depth.wh, to = depth.bh, length.out = segments+1) n <- length(depths) # which is the same as # rows # calculate temperature gradient temp.grad <- (bht - tht) / depth.bh # convert tubing diameter to ft diam <- diam.in /12 # calculate area in ft^2 area <- pi / 4 * diam^2 # calculate specific gravity of oil from API oil.sg <- 141.5 / (131.5 + API) # oil and water fractions wat.fraction <- wcut oil.fraction <- 1 - wat.fraction WOR <- wat.fraction / oil.fraction # calculate oil, gas and water rate at standard conditions # oil.srt, gas.srt, wat.srt # OIL.RT, GAS.RT, WAT.RT # oil.Srt, gas.Srt, wat.Srt oil.rt <- liq.rt * oil.fraction gas.rt <- liq.rt * GLR wat.rt <- liq.rt * wat.fraction # GOR GOR = (oil.rt + wat.rt) / oil.rt * GLR # total mass per STB = mass oil + mass water + mass gas. C42.3 mass.total <- oil.sg * 350 * (1 / (1+WOR)) + wat.sg * 350 * (WOR / (1+WOR)) + 0.0764 * GLR * gas.sg # calculate fluid properties at P, T conditions # input parameters out.data <- named.list( well.name, depth.wh, tht, depth.bh, bht, diam.in, ed, thp, liq.rt, wcut, API, oil.visc, gas.sg, GLR, wat.sg, salinity, if.tens ) # calculated parameters out.calc <- named.list( depths, n, temp.grad, diam, area, oil.sg, oil.fraction, wat.fraction, WOR, oil.rt, gas.rt, wat.rt, mass.total, GOR ) # put together input and calculated lists out.surf <- named.list(out.data, out.calc) return(out.surf) }
#' @param depth.wh depth at the wellhead double feet #' @param depth.bh depth of the well double feet #' @param diam.in inner diameter of tubing double inches #' @param liq.rt liquid rate double stb/day #' @param wcut watercut double percent #' @param GLR Gas Liquid Ratio double scf/stb #' @param thp tubing head pressure double psia #' @param tht tubing head temperature double deg Farenheit #' @param bht bottomhole temperature double deg Farenheit #' @param API oil gravity double API #' @param gas.sg specific gravity of gas double adim #' @param wat.sg specific gravity of water double adim #' @param oil.visc oil viscosity double cp #' @param ed relative roughness of pipe double adim #' @param if.tens interfacial tension double dyn/cm #' @param segments number of tubing segments int adim #' @param tol tolerance for iterations double adim #' @param model name of the model char #' @param salinity salinity of water double ppm
# converting items in a list in global variables source("VLP.R") well <- well.params() # list of well parameters for (item in names(well)) { cat(item, well[[item]], "\n") assign(item, well[[item]]) # make list member globals }
# save an R object in a RDATA file and retrieve it in a new environment # to avoid collision with global objects load("./data/well02.rda") # load any data rdata.file <- "./data/mymodel.Rdata" save(well.data, file = rdata.file) # save the data to a RDA file load(rdata.file, envir = e <- new.env()) # load the RDA file in environment `e` identical(well.data, # test if both variable are the same e$well.data, ignore.environment = TRUE)
Source: http://www.fromthebottomoftheheap.net/2012/04/01/saving-and-loading-r-objects/
# Retrieve objects in RData files and asign them to variables equal # to the name of the file setwd("./data") # set working directory to ./data filelist = list.files(pattern = ".rda") # extract .RData files from working directory # determine unique names to assign to the objects # (this solution is based on the file name) names <- make.names(sapply(strsplit(filelist, split="_"),"[[",1)) # read in objects, giving each a unique name list2env(lapply(setNames(filelist, names), function(x) get(load(x))), envir = .GlobalEnv)
a <- seq(1, 10) b <- 5 c <- 10 aa <- list(a) df <- data.frame(b = b, c = c, aa = aa)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.