# Interpolation of non-gridded data in 2 and 3 dimensions

**URL:** <https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932>\
**Category:** Announcements\
**Created:** [July 5, 2022, 7:09pm UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932 "2022-07-05T19:09:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [July 5, 2022, 7:09pm UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/1 "2022-07-05T19:09:17Z")

</div>

I have published a very preliminary project on Github - [GitHub - arjenmarkus/interpolation2d3d: Interpolation in 2D and 3D, object-oriented interfaces](https://github.com/arjenmarkus/interpolation2d3d) - that modernises several classical packages available on Netlib for interpolating spatial data. As @ivanpribec pointed out, I need to clarify the actual licensing conditions, as it uses code from ACM TOMS. I am working on that 🙂 Meanwhile, have a look at the interface I propose - it hides much of the complexity of the original code, thanks to the object-oriented features. (It does lack more elaborate examples and tests, so that is to be considered “work-in-progress”.)

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [July 5, 2022, 8:30pm UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/2 "2022-07-05T20:30:32Z")

</div>

@Arjen, you might also have a look at Alexander Barth’s “optimal” interpolation code (based on Kriging) in the NASA CFDtools github repository.

> **[cfdtools/lib/optinterp at develop · nasa/cfdtools](https://github.com/nasa/cfdtools/tree/develop/lib/optinterp)**
>
> develop/lib/optinterp

From the README

‘‘This Optimal Interpolation module provides a powerful means of treating  
unstructured datasets in n-space, whether they be clouds of points from  
laser scans of some objects, irregularly spaced function values that need  
interpolating to new target independent variables, or whatever. It belongs  
in the family of methods best known as Kriging, which determine the linear  
combination of neighboring data points that minimizes each interpolation  
error in some sense under certain statistical assumptions (hence the use  
of the term optimal).’’

The license is BSD and NASA’s open source license

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [July 6, 2022, 6:10am UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/3 "2022-07-06T06:10:16Z")

</div>

Thanks for this reference. I did not know it. It does seem of a completely different class of algorithms, but it should be useful as part of an interpolation toolset.

---

<div class="post-metadata">

**Author:** ![rwmsu](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@rwmsu](https://fortran-lang.discourse.group/u/rwmsu)\
**Post date:** [July 6, 2022, 12:59pm UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/4 "2022-07-06T12:59:52Z")

</div>

My understanding of Kriging (and I could be wrong) based interpolation methods is that they are in the same family as Radial Basis Functions (RBFs). If you have time you might also take a look around for some RBF packages and associated literature.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [July 6, 2022, 8:30pm UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/5 "2022-07-06T20:30:48Z")

</div>

Had only a brief look at the tests, but would it make sense for the “initializer” to be more of a functional style? I.e. instead of

```fortran
call shepard%create_mesh( x, y ,f )

```

do

```fortran
shepard = qshep_2d(x, y, f)

```

Also, and I haven’t run it so I’m not sure what it does now, but what is returned when an error occurs? I.e.

```auto
    px = 1.5
    py = 1.5

    write(*,*) px, py, shepard%interpolate_point( px, py )
    call shepard%error_status( error_code, error_message )
    write(*,*) 'Error: ', error_code, ' -- ', trim(error_message)

```

seems to suggest that an error is expected, but what would prompt a new user to look for and find the `error_status` procedure? What happens if I perform multiple interpolations in an expression? Like

```auto
foo = shepard%interpolate_point(1.25, 1.25) + shepard%interpolate_point(0.75, 0.75)

```

The standard says nothing about which is evaluated first, and so am I guaranteed to receive an error if one of them fails? What error do I get if both fail?

I commend the effort. This is the kind of library that I’m sure will prove to be quite useful in many applications.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [July 7, 2022, 6:23am UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/6 "2022-07-07T06:23:35Z")

</div>

Thanks for your comments - I have thought of an initialisation function, but in the end chose this style. With a function you would have to copy the data on assignment, so the style I chose might be more efficient. Alternatively, you can define the appropriate assignment procedure …

I will look into this after my holidays 🙂

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [July 7, 2022, 9:31pm UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/7 "2022-07-07T21:31:08Z")

</div>

everythingfunctional has given us one good reason for using a subroutine instead of a function: if a statement like `foo = f(x1) + f(x2)` fails because of something done inside the function `f`, it might be hard to say whether it was `f(x1)` or `f(x2)` that caused the trouble. Another good reason that hit me yesterday was that if `print *,f(x)` did not fail but there was another `print` statement inside `f`, the output looked somewhat garbled. So I turned my function `f` into a subroutine with one `intent(out)` argument.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [July 8, 2022, 1:32am UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/8 "2022-07-08T01:32:15Z")

</div>

In the last example, where f(x) is in an i/o list and itself prints output, that case is specifically not allowed by fortran. Up until recently, no i/o of any kind at all could be done in this manner. More recently, some exceptions are allowed, such as doing i/o to separate output units.

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [July 8, 2022, 10:48pm UTC](https://fortran-lang.discourse.group/t/interpolation-of-non-gridded-data-in-2-and-3-dimensions/3932/9 "2022-07-08T22:48:14Z")

</div>

I know that if a function is in an output list to unit u and itself writes to the same unit u then that is forbidden. However both gfortran and ifort let me do it with the program below. That is not a bug in either compiler because it is one of the many errors that the standard does not require to be diagnosed. It was revealed (as often in such cases) by the different compilers giving different outputs.

```auto
program badwrite ! Forbidden by the Fortran standard
  implicit none
  write(*,"(A,I0,A) ") 'f()="',f(),'"'
contains
  integer function f()
    f = 666
    write(*,"(A)") 'Hello world!'
  end function f
end program badwrite

```
