Introduction to NRPL

2016-11-11

Description

NRPL is a stack-based language heavily inspired by Hewlett-Packard’s RPL (from which it derives its name) and Forth. It was developed to give users the ability to do complex processing on their data server-side in a concise and secure manner.

Typical uses of NRPL are for querying, filtering, and transforming data. The language is general purpose, making it suitable for a variety of uses (it has been used in the past for database and geographic applications). In this guide we describe how it works in the context of the WXT service. For a full description of the language and its various options and commands, please refer to its complete specification.

How it works

NRPL programs are sequences of commands, literals, and variables, which take arguments from, and return results to, a stack[PDF](original source). Being based on Hewlett-Packard’s RPN, it uses a postfix notation.

An example

Assume you would like to add the numbers 3 and 5 together:

Input

3 5 +

Output

1:   8

Where 1: refers to the bottommost stack level and 8 is the result of the operation.

Another example (step by step)

Let us take a slightly more involved example, and calculate the volume of a sphere of radius 7.5, using the formula 4/3 π r3:

Input

4 3 / PI * 7.5 3 ^ *

Output

1:   1767.1458676442585

The calculation happens as follows, step by step:

Description Input Stack
Enter the first number 4
   1:   4
Enter the second number 3
   2:   4
   1:   3
Divide /
   1:   1.3333333333333333
Enter the symbolic constant π PI
   2:   1.3333333333333333
   1:   3.141592653589793
Multiply *
   1:   4.1887902047863905
Enter the radius 7.5
   2:   4.1887902047863905
   1:   7.5
Enter the exponent 3
   3:   4.1887902047863905
   2:   7.5
   1:   3
Exponentiate ^
   2:   4.1887902047863905
   1:   421.875
Multiply *
   1:   1767.1458676442585

A feature of postfix notation is that parentheses are never required. As long as enough stack levels are available (NRPL has a conceptually infinite stack) calculations can be of any arbitrary complexity.

Getting data in and out

Data is entered into the program via two mechanisms:

  1. Direct input into the stack, as we have seen above.
  2. Via variables, as we will see in a moment.

In the WXT service results are always retrieved from the stack.

Variables

Variables are used in WXT to:

  1. Control certain parameters. These variables are assignable.
  2. Retrieve data. These variables are read-only.

Specifying location, altitude, epoch

At endpoints where the user has direct control over the location and epoch parameters, these variables may be used.

These variables are case-sensitive

Assignment Purpose Example
!lat Set the latitude 3.5 !lon
!lon Set the longitude 45.52 !lat
!ele Set the elevation "A2000" !ele (usually defaults to "GND")
!epoch Set the epoch @2016-01-01T03:30Z@ !epoch (usually defaults to current time)

Recalling those variables (e.g., $lat, $lon) will put their current value on the stack.

Accessing weather data

If any external variable except for those listed above is recalled, it will be assumed to be a weather forecast element, and its value will reflect the value of that element at the location and time indicated by the aforementioned variables.

Example

The following program will retrieve the current temperature in Celsius, relative humidity, and wind force in knots in the centre of Wrocław, Poland (51.1079° N, 17.0385° E):

Input

51.1079 !lat 17.0385 !lon     § Set the location
$TMP § Get temperature
273.15 - § Convert from Kelvin to degrees Celsius
$RH § Get relative humidity
$UGRD $VGRD HYPOT § Get the wind vector (in m/s)
3.6 * 1.852 / § Convert to knots

Stack (example)

3:   0.13400000000001455
2: 88.482
1: 3.6297260958716424

If your subscription supports it, you may run the above program by visiting this link: https//wxt.navlost.eu/api/v0/nrpl/51.1079!lat;17.0385!lon;$TMP;273.15-;$RH;$UGRD;$VGRD;HYPOT;3.6×;1.852÷.

Or alternatively, uploading the program file via cURL:

curl -u username:password -s -H "Content-Type: text/plain" --data-binary @wrocław.nrpl "https://wxt.navlost.eu/api/v1/nrpl/"

Which should return a JSON array with the contents of the stack, for example:

[0.13400000000001455,88.482,3.6297260958716424]

More information

For a full description of NRPL, please refer to the specification.

For its use in WXT, please refer to the relevant endpoints.