# How to estimate temperature based on known points in a map?

Imagine I have a map with known current temperatures measured at certain map points provided by meteorologic stations:

Source here

What's the most robust/accurate model to deduce the temperature to any given point in the map? A polynomial regression or interpolation f:R^2->R would be ok? If there is anything for Javascript/NodeJS or Python would be amazing. I'm developing an API.

• NB: that would be interpolation, not extrapolation.
– gerrit
Commented Jul 26 at 6:44
• @gerrit amended to "deduce" Commented Jul 26 at 11:02

## 6 Answers

That basic regression will give you a rough guide, sometimes rougher than others. To get something accurate you'd have to model the topography, the dominant air currents, and the moderating effects of water, both in the form of the ocean and nearby rivers and lakes.

• Regression analysis needs a model or fitting function, I don't see how one would actually do a "basic regression" - could you add some details? How would you compare that to interpolation which seems a heck of a lot easier. Both are estimations, neither is correct, but one is a lot more straightforward to implement than the other.
– uhoh
Commented Jul 25 at 12:33
• @uhoh Regression creates a fitting function. Interpolation is also a function of regression models, you use regressions to interpolate data points that you don't have that fall within the space of known variables. Regression and interpolation are both gradient creation techniques used to create best fit lines/surfaces across the empty space between known data points. You may well be right that interpolation is both better and easier, not a programmer, but the OP has already suggested a model they feel comfortable using for the regression modelling.
– Ash
Commented Jul 25 at 12:38
• Thank you, I meant also interpolation, it's also an option. I also have the model of topography, and these stations also give wind intensity and wind directions (ipma.pt/pt/otempo/obs.superficie). Commented Jul 25 at 12:52
• I think that for at least the simplest 2D interpolations (weighted averages of local points) one doesn't need gradients or iterations. However for irregularly spaced data you do have the challenge of finding the n closest points for each interpolation point, and have to take care with the weighting so that the result is at least not discontinuous. But I don't think my comments are particularly profound so I'll cut it out and go to sleep - it's past my bedtime here. :-)
– uhoh
Commented Jul 25 at 15:26
• Your caveats are all true but that is already step two whereas OP asked for step one. The question is about how to get an interpolation function to the given points in the first place, not how to choose a very good one among a lot of options that use a lot of extra data. Commented Jul 27 at 1:49

Have you looked into K Nearest Neighbors regression? You can take the nearest k (say, 4) known points and average their temperatures. Has its drawbacks, but it's pretty good for interpolation. Readily implemented in Scikit-learn for Python.

I think Kriging would be great for this. Its an interpolation method based on Gaussian processes that is used in geostatistics.

Here is a paper about it, that also takes noise and bias in your data into account:

https://journals.plos.org/climate/article?id=10.1371/journal.pclm.0000216#sec005

• interesting, there's even a JS library github.com/oeo4b/kriging.js/tree/master Commented Jul 26 at 21:27
• I already implemented kriging as you suggested, but I'm struggling a lot to find the correct sigma2 and alpha, as I get totally non-sense results. Do you have any idea on how to proceed on chosing these parameters? Commented Jul 27 at 19:43
• @JoãoPimentelFerreira Some implementations will automatically optimize covariance parameters, like fields (in R) and apparently pykrige (Python). If you want to do it explicitly, a common approach is to fit the variogram. You can also use maximum likelihood estimation or cross-validation, more rigorously. Commented Jul 29 at 17:35

I suggest that you take a look at some of the spatial interpolation functions in QGIS. It is OpenSource GIS software that can handle both raster and vector data. Here's documentation about using Inverse Distance Weighted (IDW) and Triangular Irregular Network (TIN), as well as mention of others available. The example on that page is for unevenly distributed temperature data (just like you have). And, here's a tutorial on Interpolating Point Data.

The spatial variability of temperature anomalies (departures from a suitable average for a given time of year) is typically much smaller than the spatial variability of raw temperatures. Consequently, a good first step is to first acquire or create a map of average (normal) temperatures, including topographic and coastal effects. This is the "first guess". Then you can use any of several techniques to analyze the differences between the daily or instantaneous temperatures and the corresponding normal temperatures. Add that analysis to the map of normal temperatures, and you have your complete analysis.

Beforehand, thanks a lot to everyone that replied, I got really good input. For the sake of transparency and open information, I share how did I finally implement it:

• for every X minutes (ex.: 15 minutes) the algorithm fetches the temperature data from the meteo stations via its API,
• then it creates for the temperature a Triangulated Irregular Network (TIN), sorting each triangle of the TIN by area/surface,
• Then, for every incoming request with the coordinates of the point to analyse, it finds which triangles of the TIN contain the point, and gets the first 5 of those triangles (i.e., finds the 5 smallest triangles of the TIN which contain the point),
• Then for every of these 5 triangle it makes a simple linear interpolation to deduce the temperature on that requested point,
• Then it computes the average of those 5 temperatures to get the final computed temperature.
• If the point does not fit within any triangle of the Triangulated Irregular Network, simply finds the nearest point/station instead.

I used the library TurfJS since my project is developed in NodeJS

• It's always encouraged to add your own answer post to your question if you've found a solution that works best for you. Welcome to Earth Science!
– uhoh
Commented Aug 2 at 15:36