knitr::opts_chunk$set(echo=T, comment=NA, error=T, warning=F, message = F, fig.align = 'center')

Challenge

Given this MD, TVD data, build a new table with angles in radians and degrees.

0       0
600     600
1005    1000
4075    4000
7700    7500
9275    9000

Routine in Python that calculates angle and displacement

def get_full_deviation_survey(self):
    """
    dataframe with calculations for angle and cumulative displacement
    :return: a dataframe
    """
    df = self.get_deviation_survey()
    df['tvd2'] = df.tvd - df.tvd.shift(1)
    df['md2']  = df.md  - df.md.shift(1)

    df['angle_radians'] = np.arccos(df.tvd2/df.md2)
    df['angle_degrees'] = df.angle_radians * 180 / math.pi
    df['disp'] = df.md2 * np.sin(df.angle_radians )
    df['cum_disp'] = df.disp.cumsum()
    df.fillna(0, inplace=True)
    return df

Example of table after calculations

          MD    TVD  CumDisp     Angle
     1   0       0    0        0    
     2   600     600      0        0    
     3   1005    1000    63.4429     9.01245    
     4   4075    4000    715.286     12.2587    
     5   7700    7500    1659.02     15.0902    
     6   9275    9000    2139.25     17.7528

\
\
\

Solution

Read the MD, TVD table into a dataframe

well <- "
MD  TVD
0       0
600     600
1005    1000
4075    4000
7700    7500
9275    9000
"

# read string text to dataframe
well_table <- read.table(header = TRUE, text = well)
well_table
# a dataframe column is a vector
md2 <- well_table[, "MD"]
md2

Building functions to subtract $TVD_{i+1}$ from $TVD_i$

# function required to move a row up or down

shift <- function(x, n, invert=FALSE, default=NA){
  stopifnot(length(x)>=n)
  if(n==0){
    return(x)
  }
  n <- ifelse(invert, n*(-1), n)
  if(n<0){
    n <- abs(n)
    forward=FALSE
  }else{
    forward=TRUE
  }
  if(forward){
    return(c(rep(default, n), x[seq_len(length(x)-n)]))
  }
  if(!forward){
    return(c(x[seq_len(length(x)-n)+n], rep(default, n)))
  }
}

# function to fill NAs with zeroes
na.zero <- function (x) {
    x[is.na(x)] <- 0
    return(x)
}
# Example
shift(1:10, 5)
# Example
shift(1:10, -5, default = 999)

Finally, find the angle

    df['angle_radians'] = np.arccos(df.tvd2/df.md2)
    df['angle_degrees'] = df.angle_radians * 180 / math.pi
    df['disp'] = df.md2 * np.sin(df.angle_radians )
# create table with delta MD, TVD, radian, degrees

# calculate delta MD
sh <- 1
.md2 <- well_table[, "MD"]
md2 <- shift(.md2, sh, default = 0)
well_table[, "delta.md"] <- well_table[, "MD"] - md2

# calculate delta TVD
.tvd2 <- well_table[, "TVD"]
tvd2 <- shift(.tvd2, sh, default = 0)
well_table[, "delta.tvd"] <- well_table[, "TVD"] - tvd2

# calculate the angle for each of the segments
well_table[, "radians"] <- acos(well_table[, "delta.tvd"] / well_table[, "delta.md"])
well_table[, "degrees"] <- well_table[, "radians"] * 180 / pi

# fill NAs with zeroes
well_table[, "radians"] <- na.zero(well_table[, "radians"])
well_table[, "degrees"] <- na.zero(well_table[, "degrees"])

well_table


f0nzie/rNodal documentation built on May 6, 2019, 10:14 a.m.