@BeginModule
@NewPage
@Name      = Setmiss
@Title     = Set missing value
@Section   = Modification
@Arguments = ifile ofile
@Operators = setmissval setctomiss setmisstoc setrtomiss setvrange setmisstonn setmisstodis

@BeginDescription
This module sets part of a field to missing value or missing values
to a constant value. Which part of the field is set depends on the 
chosen operator.
@EndDescription
@EndModule


@BeginOperator_setmissval
@Title     = Set a new missing value
@Parameter = newmiss

@BeginDescription
@IfMan
         / newmiss   if i(t,x) EQ miss
o(t,x) = 
         \ i(t,x)    if i(t,x) NE miss
@EndifMan
@IfDoc
@BeginMath
o(t,x) = \left\{
\begin{array}{cll}
 \mbox{newmiss} & \mbox{if} \;\; i(t,x) = \mbox{\sl miss}      \\
 i(t,x)         & \mbox{if} \;\; i(t,x) \neq \mbox{\sl miss}   \\
\end{array}   \right.
@EndMath
@EndifDoc
@EndDescription
@EndOperator


@BeginOperator_setctomiss
@Title     = Set constant to missing value
@Parameter = c

@BeginDescription
@IfMan
         / miss   if i(t,x) EQ c
o(t,x) = 
         \ i(t,x) if i(t,x) NE c
@EndifMan
@IfDoc
@BeginMath
o(t,x) = \left\{
\begin{array}{cll}
 \mbox{miss} & \mbox{if} \;\; i(t,x) = \mbox{\sl c}      \\
 i(t,x)      & \mbox{if} \;\; i(t,x) \neq \mbox{\sl c}   \\
\end{array}   \right.
@EndMath
@EndifDoc
@EndDescription
@EndOperator


@BeginOperator_setmisstoc
@Title     = Set missing value to constant
@Parameter = c

@BeginDescription
@IfMan
         / c      if i(t,x) EQ miss
o(t,x) = 
         \ i(t,x) if i(t,x) NE miss
@EndifMan
@IfDoc
@BeginMath
o(t,x) = \left\{
\begin{array}{cll}
 \mbox{\sl c} & \mbox{if} \;\; i(t,x) = \mbox{miss}     \\
 i(t,x)       & \mbox{if} \;\; i(t,x) \neq \mbox{miss}  \\
\end{array}   \right.
@EndMath
@EndifDoc
@EndDescription
@EndOperator


@BeginOperator_setrtomiss
@Title     = Set range to missing value
@Parameter = rmin rmax

@BeginDescription
@IfMan
         / miss   if i(t,x) GE rmin AND i(t,x) LE rmax
o(t,x) = 
         \ i(t,x) if i(t,x) LT rmin OR  i(t,x) GT rmax
@EndifMan
@IfDoc
@BeginMath
o(t,x) = \left\{
\begin{array}{cll}
 \mbox{miss}   & \mbox{if} \;\; i(t,x) \geq \mbox{\sl rmin} \wedge i(t,x) \leq \mbox{\sl rmax}  \\
 i(t,x) & \mbox{if} \;\; i(t,x) < \mbox{\sl rmin} \vee i(t,x) > \mbox{\sl rmax}  \\
\end{array}   \right.
@EndMath
@EndifDoc
@EndDescription
@EndOperator


@BeginOperator_setvrange
@Title     = Set valid range
@Parameter = rmin rmax

@BeginDescription
@IfMan
         / miss   if i(t,x) LT rmin OR  i(t,x) GT rmax
o(t,x) = 
         \ i(t,x) if i(t,x) GE rmin AND i(t,x) LE rmax
@EndifMan
@IfDoc
@BeginMath
o(t,x) = \left\{
\begin{array}{cll}
 \mbox{miss}   & \mbox{if} \;\; i(t,x) < \mbox{\sl rmin} \vee i(t,x) > \mbox{\sl rmax}  \\
 i(t,x) & \mbox{if} \;\; i(t,x) \geq \mbox{\sl rmin} \wedge i(t,x) \leq \mbox{\sl rmax}  \\
\end{array}   \right.
@EndMath
@EndifDoc
@EndDescription
@EndOperator


@BeginOperator_setmisstonn
@Title     = Set missing value to nearest neighbor

@BeginDescription
Set all missing values to the nearest non missing value.
@IfMan
         / i(t,y) if i(t,x) EQ miss AND i(t,y) NE miss
o(t,x) = 
         \ i(t,x) if i(t,x) NE miss
@EndifMan
@IfDoc

@BeginMath
o(t,x) = \left\{
\begin{array}{cll}
 i(t,y) & \mbox{if} \;\; i(t,x) = \mbox{miss} \wedge i(t,y) \neq \mbox{miss}  \\
 i(t,x) & \mbox{if} \;\; i(t,x) \neq \mbox{miss}  \\
\end{array}   \right.
@EndMath
@EndifDoc
@EndDescription
@EndOperator


@BeginOperator_setmisstodis
@Title     = Set missing value to distance-weighted average
@Parameter = [neighbors]

@BeginDescription
Set all missing values to the distance-weighted average of the nearest non missing values.
The default number of nearest neighbors is 4.
@EndifDoc
@EndDescription
@EndOperator


@BeginParameter rmax
@Item = neighbors
INTEGER  Number of nearest neighbors
@Item = newmiss
FLOAT    New missing value
@Item = c
FLOAT    Constant
@Item = rmin
FLOAT    Lower bound
@Item = rmax
FLOAT    Upper bound
@EndParameter


@BeginExample
@bold{setrtomiss}

Assume an input dataset has one field with temperatures in the range from 246 to 304 Kelvin.
To set all values below 273.15 Kelvin to missing value use:
@BeginVerbatim
   cdo setrtomiss,0,273.15 ifile ofile
@EndVerbatim
Result of '{\tt cdo info ifile}':
@BeginListing
   -1 :       Date  Time    Code Level  Size  Miss :  Minimum     Mean  Maximum
    1 : 1987-12-31 12:00:00 139      0  2048     0 :   246.27   276.75   303.71
@EndListing
Result of '{\tt cdo info ofile}':
@BeginListing
   -1 :       Date  Time    Code Level  Size  Miss :  Minimum     Mean  Maximum
    1 : 1987-12-31 12:00:00 139      0  2048   871 :   273.16   287.08   303.71
@EndListing

@bold{setmisstonn}

Set all missing values to the nearest non missing value:
@BeginVerbatim
   cdo setmisstonn ifile ofile
@EndVerbatim
@IfDoc
Below is a schematic illustration of this example:

\vspace{1mm}

\includegraphics[width=\textwidth]{setmisstonn.pdf}

\vspace{1mm}

On the left side is input data with missing values in grey and on the right side the result with the filled missing values.
@EndifDoc
@EndExample
