A Fortran 90 module for creating 1D and 2D lookup tables. These tables can be used to efficiently interpolate one or more values.
Author: Jannis Teunissen
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | private, | parameter | :: | dp | = | kind(1.0d0) |
Type to indicate a location in a 2D lookup table
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| real(kind=dp), | private | :: | low_frac(2) |
The distance from low_ix (up to low_ix+1), given as a real number between 0 and 1. |
|||
| integer, | private | :: | low_ix(2) |
The x-value lies between low_ix and low_ix+1 |
The 2D lookup table type
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| real(kind=dp), | public | :: | dx(2) |
The x-spacing in the lookup coordinate |
|||
| real(kind=dp), | public | :: | inv_dx(2) |
The inverse x-spacing |
|||
| integer, | public | :: | n_cols |
The number of columns/variables |
|||
| integer, | public | :: | n_points(2) |
The size of the table |
|||
| real(kind=dp), | public, | allocatable | :: | rows_cols(:,:,:) | |||
| real(kind=dp), | public | :: | x_min(2) |
The minimum lookup coordinate |
Type to indicate a location in a 3D lookup table
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| real(kind=dp), | private | :: | low_frac(3) |
The distance from low_ix (up to low_ix+1), given as a real number between 0 and 1. |
|||
| integer, | private | :: | low_ix(3) |
The x-value lies between low_ix and low_ix+1 |
The 3D lookup table type
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| real(kind=dp), | public | :: | dx(3) |
The x-spacing in the lookup coordinate |
|||
| real(kind=dp), | public | :: | inv_dx(3) |
The inverse x-spacing |
|||
| integer, | public | :: | n_cols |
The number of columns/variables |
|||
| integer, | public | :: | n_points(3) |
The size of the table |
|||
| real(kind=dp), | public, | allocatable | :: | rows_cols(:,:,:,:) | |||
| real(kind=dp), | public | :: | x_min(3) |
The minimum lookup coordinate |
Type to indicate a location in the lookup table, which can be used to speed up multiple lookups of different columns.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| real(kind=dp), | private | :: | low_frac |
The distance from low_ix (up to low_ix+1), given as a real number between 0 and 1. |
|||
| integer, | private | :: | low_ix |
The x-value lies between low_ix and low_ix+1 |
The lookup table type. There can be one or more columns, for which values can be looked up for a given 'x-coordinate'.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| real(kind=dp), | public, | allocatable | :: | cols_rows(:,:) |
The table in column-major order |
||
| real(kind=dp), | public | :: | dx |
The x-spacing in the lookup coordinate |
|||
| real(kind=dp), | public | :: | inv_dx |
The inverse x-spacing |
|||
| integer, | public | :: | n_cols |
The number of columns |
|||
| integer, | public | :: | n_points |
The number of points |
|||
| real(kind=dp), | public, | allocatable | :: | rows_cols(:,:) |
The table in row-major order |
||
| real(kind=dp), | public | :: | x_min |
The minimum lookup coordinate |
This function returns a new lookup table
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_min(2) |
Minimum coordinate |
||
| real(kind=dp), | intent(in) | :: | x_max(2) |
Maximum coordinate |
||
| integer, | intent(in) | :: | n_points(2) |
How many values to store |
||
| integer, | intent(in) | :: | n_cols |
Number of variables that will be looked up |
This function returns a new lookup table from regularly spaced data
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_min(2) |
Minimum coordinate |
||
| real(kind=dp), | intent(in) | :: | x_max(2) |
Maximum coordinate |
||
| real(kind=dp), | intent(in) | :: | spaced_data(:,:,:) |
Input data of shape (nx, ny, n_cols) |
Get the value of a single column at x
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT2_t), | intent(in) | :: | my_lt | |||
| integer, | intent(in) | :: | col_ix | |||
| real(kind=dp), | intent(in) | :: | x1 | |||
| real(kind=dp), | intent(in) | :: | x2 |
This function returns a new lookup table
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_min(3) |
Minimum coordinate |
||
| real(kind=dp), | intent(in) | :: | x_max(3) |
Maximum coordinate |
||
| integer, | intent(in) | :: | n_points(3) |
How many values to store |
||
| integer, | intent(in) | :: | n_cols |
Number of variables that will be looked up |
This function returns a new lookup table from regularly spaced data
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_min(3) |
Minimum coordinate |
||
| real(kind=dp), | intent(in) | :: | x_max(3) |
Maximum coordinate |
||
| real(kind=dp), | intent(in) | :: | spaced_data(:,:,:,:) |
Input data of shape (nx, ny, nz, n_cols) |
Get the value of a single column at x
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT3_t), | intent(in) | :: | my_lt | |||
| integer, | intent(in) | :: | col_ix | |||
| real(kind=dp), | intent(in) | :: | x1 | |||
| real(kind=dp), | intent(in) | :: | x2 | |||
| real(kind=dp), | intent(in) | :: | x3 |
This function returns a new lookup table
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_min |
Minimum x-coordinate |
||
| real(kind=dp), | intent(in) | :: | x_max |
Maximum x-coordinate |
||
| integer, | intent(in) | :: | n_points |
How many x-values to store |
||
| integer, | intent(in) | :: | n_cols |
Number of variables that will be looked up |
This function returns a new lookup table from regularly spaced data
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_min |
Minimum coordinate |
||
| real(kind=dp), | intent(in) | :: | x_max |
Maximum coordinate |
||
| real(kind=dp), | intent(in) | :: | spaced_data(:,:) |
Input data of shape (n_points, n_cols) |
Get the value of a single column at x
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT_t), | intent(in) | :: | my_lt | |||
| integer, | intent(in) | :: | col_ix | |||
| real(kind=dp), | intent(in) | :: | x |
Get the values of all columns at x
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT_t), | intent(in) | :: | my_lt | |||
| real(kind=dp), | intent(in) | :: | x |
Linearly interpolate the (x, y) input data to the new_x coordinates
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | in_x(:) | |||
| real(kind=dp), | intent(in) | :: | in_y(:) | |||
| real(kind=dp), | intent(in) | :: | new_x(:) |
Returns the x-coordinates of the lookup table
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_min | |||
| real(kind=dp), | intent(in) | :: | dx | |||
| integer, | intent(in) | :: | n_points |
Adaptive search (combination of linear and binary search) of sorted list for the smallest ix such that list(ix) >= val. On failure, returns size(list)+1
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | list(:) |
Sorted list |
||
| real(kind=dp), | intent(in) | :: | val |
Value to search for |
Binary search of sorted list for the smallest ix such that list(ix) >= val. On failure, returns size(list)+1
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | list(:) |
Sorted list |
||
| real(kind=dp), | intent(in) | :: | val |
Value to search for |
Linear search of sorted list for the smallest ix such that list(ix) >= val. On failure, returns size(list)+1
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | list(:) |
Sorted list |
||
| real(kind=dp), | intent(in) | :: | val |
Value to search for |
Fill the column with index col_ix using linearly interpolated data
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT2_t), | intent(inout) | :: | my_lt | |||
| integer, | intent(in) | :: | col_ix | |||
| real(kind=dp), | intent(in) | :: | x1(:) | |||
| real(kind=dp), | intent(in) | :: | x2(:) | |||
| real(kind=dp), | intent(in) | :: | y(:,:) |
Add a new column by linearly interpolating the (x, y) data
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT_t), | intent(inout) | :: | my_lt | |||
| real(kind=dp), | intent(in) | :: | x(:) | |||
| real(kind=dp), | intent(in) | :: | y(:) |
Read the lookup table from file (in binary, potentially unportable)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT_t), | intent(inout) | :: | my_lt | |||
| character(len=*), | intent(in) | :: | filename |
Get the x-coordinates and the columns of the lookup table
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT_t), | intent(in) | :: | my_lt | |||
| real(kind=dp), | intent(out) | :: | x_data(:) | |||
| real(kind=dp), | intent(out) | :: | cols_rows(:,:) |
Compute by use of linear interpolation the value in the middle
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=dp), | intent(in) | :: | x_list(:) | |||
| real(kind=dp), | intent(in) | :: | y_list(:) | |||
| real(kind=dp), | intent(in) | :: | x_value | |||
| real(kind=dp), | intent(out) | :: | y_value |
Fill the column with index col_ix using the linearly interpolated (x, y) data
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT_t), | intent(inout) | :: | my_lt | |||
| integer, | intent(in) | :: | col_ix | |||
| real(kind=dp), | intent(in) | :: | x(:) | |||
| real(kind=dp), | intent(in) | :: | y(:) |
Write the lookup table to file (in binary, potentially unportable)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(LT_t), | intent(in) | :: | my_lt | |||
| character(len=*), | intent(in) | :: | filename |