---
title: "Standing-Katz chart data"
date: '`r Sys.Date()`'
output: rmarkdown::html_vignette

vignette: >
  %\VignetteIndexEntry{Standing-Katz chart data}
  %\VignetteEngine{knitr::rmarkdown} 
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=F, error=T, message=F, warning=F}
knitr::opts_chunk$set(echo=T, comment=NA, error=T, warning=F, message = F, fig.align = 'center')
```


## How the data was collected
The Standing-Katz (SK) chart data is read directly from a scanned figure of the original plot drawn by Standing and Katz in 1951. 

The software used to digitize the data is `graphClick` for the operating system `osX` by Apple. This software has been tested and qualified by scholars working on a similar task of digitizing data. 

Each one of the SK chart curves has been digitized. There are some curves where more points were taken due to the curvature. Other curves are almost linear and few points were necessary to define the curve.


## Saving the Standing-Katz curves
If you feel you can add a Standing-Katz chart with higher resolution and improve the work of this package, you can do so. Just keep in mind certain convention to name the text files. Let's see how the curves were saved for this package.

We invoke the function `listStandingKatzCurves()` to list all the Standing-Katz curve data that has been digitized. See the `R` code chunk below:

```{r}
library(zFactor)
listStandingKatzCurves(pprRange = 'all')
```

Note that the files follow this convention:

    sk_: Sanding-Katz
    hp:  high pressure range
    lp:  low pressure range
    tpr: pseudo-reduced tempereture
    nnn: a three digit number representing the curve by the Tpr
    
Now that we know the meaning of the name of digitized curve. we could only list then the files that contain data only in the 'high pressure' (hp) range:

```{r}
library(zFactor)
listStandingKatzCurves(pprRange = 'hp')
```

For the low-pressure `Ppr` range:

```{r}
library(zFactor)
listStandingKatzCurves(pprRange = 'lp')
```


## Reading and plotting the Standing-Katz chart data
The SK chart data once is read from the curves and saved on text files, it can read from `R` and viewed, plotted or saved y using the function `getStandingKatzCurve()`. The header of function looks like this:

    getStandingKatzCurve <- function(tpr = 1.3, pprRange = "lp", 
                                    tolerance = 0.01,
                                     toView = TRUE, toSave = TRUE, toPlot = TRUE) 
                                     { more code }

The user should supply at least the pseudo-reduced temperatureor $T_{pr}$ to retrieve the points from the SK chart for that particular temperature. What the user will obtain is a dataset of $P_{pr}$ vs $z$.

Here we are calling for the curve at $T_{pr}$ = 1.30, We will only plot it at this time:

```{r fig.width=5, fig.height=5}
library(zFactor)
getStandingKatzCurve(tpr = 1.3, toView = FALSE, toSave = FALSE)
```

In a similar manner we could plot the SK curve at $T_{pr}$ = 1.3 but at the `high pressure` range or `hp`. This is the lower part of the chart with the scale from 7.0 to 15.

We have to specify that we want the high pressure range with `hp` as shown below:

```{r fig.width=5, fig.height=5}
library(zFactor)

getStandingKatzCurve(tpr = 1.3, pprRange = 'hp', toView = FALSE, toSave = FALSE)
```


We could change the `Y` scale of the plot to accomadate the high `z`` values:

```{r}
getStandingKatzCurve(tpr = 1.3, pprRange = 'hp', ylim = c(0.75, 1.75))
```


## At `Tpr = 1.05`

```{r fig.width=5, fig.height=5}
library(zFactor)
getStandingKatzCurve(tpr = 1.05, pprRange = "lp", toView = FALSE, toSave = FALSE)
```



## More Examples

```{r, echo=FALSE, results='asis'}
library(zFactor)

ds <- getStandingKatzCurve(tpr = 1.3, pprRange = 'hp', 
                           toView = FALSE, toSave = FALSE, toPlot = FALSE)
knitr::kable(head(ds, 10))
```

The table show us few rows of the dataset at $T_{pr}$ = 1.3 but also some columns -or variables. $P_{pr}$ and $z$ are self-explanatory. The three other columns deserve an explanation:

    isNear:   how close is the digitized point to the grid (1/10th)
    Ppr_near: what is the value of the nearest grid value to the point
    diff:     the difference between the Ppr of the point read and the grid
    
    
## Getting tables for multiple pseudo-reduced pressures (`Tpr`)    
We will choose not to plot, save ot view any of the results at this time. We limit to obtain the z vs. Ppr data at given $T_{pr}$ values in a vector.


```{r}
library(zFactor)
library(tibble)

tpr <- c(1.05, 1.3, 1.5)
tpr_li <- lapply(tpr, getStandingKatzData, pprRange = 'lp')
names(tpr_li) <- tpr
tibble(tpr_li)
```

```{r}
as_tibble(tpr_li[["1.05"]])
as_tibble(tpr_li[["1.3"]])
as_tibble(tpr_li[["1.5"]])
```


> Note that we have just retrieved the z vs. Ppr data for three Tpr curves with one command. That the power of the `apply` functions family.


## Select a table for a given `Tpr` curve
Since the function `getStandingKatzData` returns a list of dataframes if we supply a vector of `Tpr` curves, we could select one of those tables by specifying the `Tpr`.
In this example, we will retrieve the table for `Tpr = `1.3` and show the first 10 rows:

```{r}
# show the first 10 rows
head(tpr_li[["1.3"]], 10)
```



## Get a table/matrix of `z` values for medium range of Tpr curves
Let's say we want to take a look at the `z` values Standing-Katz chart at specific sets of pseudo-reduced pressure $P_{pr}$ and pseudo-reduced temperatures $T_{pr}$.
We use the function `getStandingKatzMatrix()` and provide the Ppr and Tpr vectors.

## `z` values for the medium range of `Tpr` curves

```{r}
library(zFactor)

tpr_vec <- c(1.4, 1.5, 1.6, 1.7, 1.8, 1.9)
ppr_vec <- c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5)

getStandingKatzMatrix(ppr_vector = ppr_vec, tpr_vector = tpr_vec)
```


> Note that these values are not interpolated; they are raw values read from the Standing-Katz chart.

**NOTE** \
> In this case we could form a pretty nice matrix of `z` values because we know beforehand that the values of Ppr are common to the given Tpr curves. What does this mean?

This means that we digitized points at exactly these Ppr values `0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5` for these Tpr curves `1.3, 1.5, 1.7, 2.0`. We may not be so lucky in creating such a pretty nice looking matrix unless we take care of digitizing the `Tpr` curves at common `Ppr` values. It will always be better to digitized points that are over the 1/10th division of the Standing-Katz grid.

## What about the low range `Tpr`?

```{r}
library(zFactor)

tpr_vec <- c(1.05, 1.1, 1.2, 1.3)
ppr_vec <- c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5)
getStandingKatzMatrix(ppr_vector = ppr_vec, tpr_vector = tpr_vec)
```

## Or the high range region of `Tpr`

```{r}
library(zFactor)

tpr_vec <- c(2.0, 2.2, 2.4, 2.6, 2.8, 3.0)
ppr_vec <- c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.0)
getStandingKatzMatrix(ppr_vector = ppr_vec, tpr_vector = tpr_vec)
```


## Smaller sample of `z`
If a smaller sample is required, change the vectors:

```{r}
library(zFactor)

tpr_vec <- c(1.1, 1.2)
ppr_vec <- c(1, 2, 3, 4, 5, 6)
getStandingKatzMatrix(ppr_vector = ppr_vec, tpr_vector = tpr_vec, 
                      pprRange = "lp")
```

> Again. We knew that there were two `Ppr` points at the `Tpr` supplied.

Now, what happens if you guess the `Ppr` values and they are not found in the digitized file? R will throw an error:

    Ppr values may not be digitized at this Tpr

```{r}
library(zFactor)

tpr_vec <- c(1.2)
ppr_vec <- c(8.3, 10)
getStandingKatzMatrix(ppr_vector = ppr_vec, tpr_vector = tpr_vec, 
                      pprRange = "hp")
```

## Getting all `Ppr` digitized values for a `Tpr`
If we don't know what `Ppr` values have been exactly digitized for a `Tpr` curve, then we just pull all the `Ppr` points. In this example, we will work with the `hp` or high pressure `Ppr`:

```{r}
library(zFactor)

tpr_vec <- c(1.2)
getStandingKatzMatrix(tpr_vector = tpr_vec, 
                      pprRange = "hp")
```

> You can see why were getting the error above; there is no digitized point at `Ppr = 8.3` or `Ppr = 10`. Now, let's try at 8.6 and 15.

```{r}
library(zFactor)

tpr_vec <- c(1.2)
ppr_vec <- c(8.6, 10.3, 15)
getStandingKatzMatrix(ppr_vector = ppr_vec, tpr_vector = tpr_vec, 
                      pprRange = "hp")
```


## Get one `z` point for a `Ppr` at a curve `Tpr`

```{r}
library(zFactor)

asked_tpr <- 1.5
asked_ppr <- 2.5

tpr_vec <- c(asked_tpr)
res_li <- lapply(tpr_vec, getStandingKatzData, pprRange = 'lp')
names(res_li) <- tpr_vec

asked_tpr_str <- as.character(asked_tpr)
df <- res_li[[asked_tpr_str]]
df[which(res_li[[asked_tpr_str]]["Ppr_near"] == asked_ppr), "z"]
```


## Concatenating `Tpr` tables

```{r}
library(zFactor)
library(data.table)

tpr <- c(1.05, 1.1, 1.2)

tpr_li <- lapply(tpr, getStandingKatzData, pprRange = 'lp')
names(tpr_li) <- tpr

common <- rbindlist(tpr_li, idcol = TRUE)
common
```

## Finding common values of `Ppr`

```{r}
library(zFactor)
getStandingKatzTpr(pprRange = "all")
```


```{r}
library(zFactor)
getStandingKatzTpr(pprRange = "lp")
```

```{r}
library(zFactor)
getStandingKatzTpr(pprRange = "hp")
```


