# Calc

Calc is a simple calculator that helps you to do simple math based on the latest measurement values.

## Contents

## Usage

The typical way to use Calc involves the following steps:

- Create a Calc module in the sequencer and change to the corresponding tab
- Change the label of the module if desired
- Enter a name of the calculated variable and its unit
- Define whether the variable can be plotted and whether it should be saved
- In section 'Values' create as many fields you need using 'Add value field'
- Select the SweepMe! internal variables and relate them to 'x1', 'x2', and so on.
- Type in your formula in section 'Formula' using 'x1', 'x2', etc. as placeholders for

## Execution

Calc can calculate the actual value at different positions during the measurement procedure, e.g. before a new measurement point is set and measured or after all variables of a meaurement point have been taken.

**Always:**Use this default value if you do not matter and just want to have actual values all the time.**At the beginning:**Needed if you like to modify a SweepValue of a certain module that has to be handed over to another Module as a SweepValue before the next measurement point is run through.**At the end:**Can be used, if you like to post-process the latest measurement data.

## Further information

- 'x0' is the last return value of the Calc module. It might be of interest to do some iterative recalculation and refinement. Please choose the correct execution, e.g. "At the beginning" or "At the end". Otherwise, the result will be modified twice per measurement point.
- 'x0' is always initialized as float('nan') (i.e. not a number of type float). You can test whether is x0 is such a 'nan' using isnan(x0)
- Several Calc modules can be used and combined to create more complex calculations.
- Calc only provides the data of the last measurement point, which can be 0-dimensional, e.g. in case of voltage, a time etc. or 1-dimensional, e.g. in case of a spectrum or a transient of an oscilloscope.
- If you need to access all points, that have been measured so far, the module 'CustomFunction' is recommended that allows you to write your own python scrips and load additional libraries.

## Examples

- Current density in mA/cm²:
**1000 * x1 / 0.04**where x1 is the current of an SMU module in Ampere. - Normalize spectrum by max:
**x1/max(x1)**where x1 is a spectrum of a Spectrometer module. - Calibrate temperatures:
**a * x1 + b**where x1 is a measured temperature of the Temperature module and a,b are the coefficients of a linear calibration function. - Sum up a value:
**x1 if isnan(x0) else x0 + x1**where x1 is the value to be summed up and x0 is the return value. Use Execution = "At the end". In the first step, y is set to x1, so that in subsequent steps x0 (which is the last y) is initialized to a start value. - Create a message:
**"Ok" if x1 < 50 else "Warning"**where x1 is a process parameter that is checked whether it would be ok 50 or not. - Indexing:
**x1[62]**where x1 is a variable with 1-dimensional data, e.g. a spectrum. Here, the value at index 62 is returned. Please note that indices start from 0. - Peak position:
**min(x2[x1==max(x1)])**where x1 are 1-dimensional intensities and x2 are the corresponding 1-dimensional wavelengths. The function finds the first wavelength, the maximum value is achieved. - String formatting:
**"%i" % x1**where x1 is an integer. Please note that the result will be a string. - Create a message:
**"The measurement has finished"**where no variable is needed. Such messages can be used to display them during a measuerement. - Print to debug (from version: 1.5.4.):
**print("My value is" , x1)**where x1 can be any selected value. The output will be seen in the Debug window (F2).

## Supported Functions

There is a limited number of functions that can be used inside the Calc module. The following list gives the string that can be used and the corresponding function that will be loaded. If there are further functions needed, please let us know (support@sweep-me.net).

The list is related to version: 1.5.4

### python built-in functions

- 'abs' = abs
- 'len' = len
- 'max' = max
- 'min' = min
- 'int' = int
- 'float' = float
- 'round' = round
- 'str' = str
- 'repr' = repr
- 'print' = print
- 'type' = type

### datetime functions

- 'now' = datetime.datetime.now

### math functions

- 'ceil' = math.ceil
- 'degrees' = math.degrees
- 'fabs' = math.fabs
- 'floor' = math.floor
- 'fmod' = math.fmod
- 'frexp' = math.frexp
- 'hypot' = math.hypot
- 'ldexp' = math.ldexp
- 'modf' = math.modf
- 'pi' = math.pi
- 'pow' = math.pow
- 'radians' = math.radians

### random functions

- 'random' = random.random
- 'randint' = random.randint

### numpy functions

- 'sqrt' = np.sqrt

- 'absolute' = np.absolute
- 'real' = np.real
- 'imag' = np.imag
- 'angle' = np.angle
- 'conj' = np.conj

- 'sin' = np.sin
- 'sinh' = np.sinh
- 'asin' = np.arcsin
- 'arcsin' = np.arcsin

- 'cos' = np.cos
- 'cosh' = np.cosh
- 'acos' = np.arccos
- 'arccos' = np.arccos

- 'tan' = np.tan
- 'tanh' = np.tanh
- 'arctan' = np.arctan
- 'atan' = np.arctan
- 'arctan2' = np.arctan2
- 'atan2' = np.arctan2

- 'exp' = np.exp
- 'ln' = np.log
- 'log' = np.log
- 'log10' = np.log10
- 'average' = np.average
- 'mean' = np.mean
- 'arange' = np.arange
- 'linspace' = np.linspace
- 'logspace' = np.logspace
- 'divide' = np.divide
- 'sum' = np.sum
- 'trapz' = np.trapz
- 'sign' = np.sign
- 'isnan' = np.isnan # modifications: returns False if object is no float, returns bool instead of np.bool_