0

Hi I am currently trying to interpolate data. My data structure looks like this:

WMOCode,Country,Latitude,Longitude,Altitude,H2,O18,Date,Year,month,dates,a_diffH,H2a,a_diffO,O18a
7162800,CA,45.32,-75.67,114.0,-57.0,-8.85,1990-06-15,1990,6,1990-06-15,-1.71,-55.29,-0.228,-8.622

To make the interpolation better, I copy the data and paste it back to the actual data set in all sky directions (so I can interpolate data from N America with data from Japan, for example).

def extend_data(data):
    # Copy data to the "left" and "right" to allow for interpolation to the edges of the map
    return {
        "lons": np.concatenate([np.array([lon-360 for lon in data["lons"]]), data["lons"], np.array([lon+360 for lon in data["lons"]])]),
        "lats":  np.concatenate([data["lats"], data["lats"], data["lats"]]),
        "values":  np.concatenate([data["values"], data["values"], data["values"]]),
    }

To join the data together I use the concatenate command. However, if I execute it, I get the following error message: ValueError: operands could not be broadcast together with shapes (152,6) (1,2) (152,6)

I have already tried to work with the reharp command, but the error message does not change.

The big problem is, that this code workes yesterday but today (without any changes from my side) it shows this error message! So wath did Changed?

My code looks like this:

from traceback import print_tb
import numpy as np
from pykrige.ok import OrdinaryKriging
from pykrige.kriging_tools import write_asc_grid
import pykrige.kriging_tools as kt


def get_data(df):
    return {
        "lons": np.array([6.47, 4.8, 1.94]),
        "lats": np.array([46.37, 43.9, 47.83]),
        "values": np.array([-29.5, -27.6, -32.5]),
    }

def extend_data(data):
    return {
        "lons": np.concatenate([np.array([lon-360 for lon in data["lons"]]), data["lons"], np.array([lon+360 for lon in data["lons"]])]),
        "lats":  np.concatenate([data["lats"], data["lats"], data["lats"]]),
        "values":  np.concatenate([data["values"], data["values"], data["values"]]),
    }

def generate_grid(data, basemap, delta=1):
    grid = {
        'lon': np.arange(-180, 180, delta),
        'lat': np.arange(np.amin(data["lats"]), np.amax(data["lats"]), delta) 
    }
    grid["x"], grid["y"] = np.meshgrid(grid["lon"], grid["lat"])
    grid["x"], grid["y"] = basemap(grid["x"], grid["y"])
    return(grid)

def interpolate(data, grid):
    Ok = OrdinaryKriging(
        data["lons"],
        data["lats"],
        data["values"],
        variogram_model='exponential',
        # nlags=6, # TODO select plausible value
    )
    return Ok.execute("grid", grid["lon"], grid["lat"])

 base_data = get_data()
grid = generate_grid(base_data, basemap, 1)
extended_data = extend_data(base_data)
interpolation, interpolation_error = interpolate(extended_data, grid)

This error only occours at the code sniped: interpolation, interpolation_error = interpolate(extended_data, grid)

so my extended_data looks like (each List repeats 3 Times):

[-435.67       -350.8        -353.52916667 -370.25       -343.64361111
  -352.4        -350.0422     -380.56666667 -350.81       -351.635
  -347.8981     -350.0572     -351.2942     -349.5536     -348.9378
  -353.3419     -245.83333333 -423.9775     -340.15138889 -353.75
  -346.5981     -352.56067943 -351.66728038 -353.45       -351.70729317
  -348.1242     -351.21144395 -351.81321784 -346.32861111 -351.15
  -347.57       -352.40555556 -191.68       -186.72       -344.55111111
  -345.68166667 -351.2481     -327.12       -348.9892     -206.92
  -519.8        -322.13       -537.37       -419.48972222 -345.403
  -385.65       -324.7        -329.3        -253.17       -344.03
  -219.28       -348.06666667 -329.37       -357.88       -320.18
  -303.82       -432.93       -321.27       -348.41       -257.31666667
  -277.1        -255.65       -245.58333333 -354.22       -428.28
  -215.31       -272.38       -354.82       -367.55       -286.62
  -273.81944444 -267.85       -368.6        -430.833333   -326.95
  -329.7        -298.31666667 -259.57       -253.12       -454.98
  -240.71666667 -338.47       -376.9        -251.07       -242.83333333
  -255.98       -246.35       -253.28333333 -250.15       -465.11666667
  -422.33333333 -246.93333333 -474.27       -479.28333333 -241.82
  -454.06       -249.65       -441.25       -236.08333333 -250.6
  -259.2        -445.56       -280.06666667 -356.25       -354.95
  -238.6        -268.86666667 -438.83       -233.38       -387.22009444
  -366.73333333 -442.33333333 -329.45       -367.73333333 -253.01666667
  -325.91666667 -430.7        -249.92       -316.14166667 -236.08
  -244.22       -445.66666667 -444.05       -443.46       -445.345
  -444.12208333 -445.32       -443.71666667 -346.99027778 -361.08333333
  -361.1        -342.88809167 -340.417      -340.32       -338.27
  -341.52428889 -339.69353056 -342.13799444 -346.13673611 -445.11
  -443.28333333 -443.16666667 -445.53       -444.63333333 -443.85
  -445.62       -432.09198611 -433.03333333 -432.07       -432.58922222
  -466.6244     -418.56      ]

The Output of Lats:

[ 45.32        48.8281      46.37222222  51.93        48.24861111
   50.3381      49.7703     -75.58333333  47.6772      49.0392
   49.0422      50.4972      53.8713      52.2914      47.4828
   49.7478      22.31666667 -65.07944444  50.06166667  51.83
   52.4672      46.95222563  46.57248471  53.23        46.65772518
   50.3119      46.17378583  46.72738634  46.60305556  44.42
   51.35        47.59583333 -46.42       -35.07        47.07777778
   46.64833333  52.1042      39.95        47.8008     -27.43
  -21.2        -46.88        28.22        13.07176111  46.095
   37.77        36.98        36.88        -6.18        45.80666667
   -2.53        78.91666667  46.48        41.38        47.25
   58.01       -41.47         9.          48.22        25.01666667
   55.03        52.27        38.03333333  50.92       -54.78
  -40.68        43.78        52.1         40.41666667  55.01
   69.4075      58.45        41.13333333 -53.          68.96666667
   59.96666667  69.76666667  38.93        27.7         74.72
   26.08333333  68.68333333  32.63333333  34.3         39.1
   30.67        34.72        26.58333333  40.67        69.1
   82.5         28.2         62.28        76.23333333  32.18
   58.75        20.03333333  68.78666667  47.38333333  24.35
   13.17        80.          37.13333333  51.3         52.8
   37.53        29.7        -33.62        45.68        38.66038889
   41.8         23.16666667  37.75        41.31666667  47.93333333
   36.79444444 -33.45        25.07        39.66666667  13.03
  -31.95         9.96666667   9.8          8.95        10.31
   10.10808333  10.76666667   9.78333333  46.52083333  52.88333333
   51.6         48.16905278  48.933       49.36        48.65833333
   48.28460556  49.13615278  47.96013056  45.641775     9.69
   10.06666667   8.64        10.5          9.66666667   9.98333333
   10.59       -36.6395     -36.79744444 -45.35       -38.73263889
   52.141      -62.12      ]

The Output for Values:

[[ -8.85  -5.26  -6.4   -5.43  -6.37  -4.67  -5.48 -18.73  -6.45  -4.19
   -8.23  -7.36  -7.12  -5.05  -6.61  -3.08  -5.98 -13.37  -5.52  -6.55
   -8.16  -6.45 -11.47  -5.9   -9.59  -7.47  -5.83  -8.37  -8.08  -2.33
   -8.8   -4.45  -6.63  -4.13  -5.43  -8.33  -5.13  -2.9   -9.83  -4.32
   -1.74  -5.85  -2.23  -2.06  -5.87  -0.19  -2.66  -5.15  -4.28  -4.76
   -8.77 -11.01  -6.9   -2.84  -5.1  -12.6   -5.8    2.57 -10.77 -11.85
  -11.1  -14.5   -6.47  -5.4  -11.4   -6.77  -9.33  -4.83  -3.46  -6.7
  -11.4   -8.5   -1.77 -13.9   -9.6   -9.    -9.2   -3.83  -6.95 -25.25
   -5.66 -12.55  -2.64  -8.69  -4.61  -5.27 -12.04  -8.45  -5.94 -23.17
  -21.48  -9.8  -14.33 -23.49 -10.78 -15.73 -11.37 -22.81 -10.25  -6.16
   -3.49 -25.94   2.23  -5.14  -5.98  -9.64  -3.33  -2.6  -12.31  -0.12
   -3.84  -3.54  -6.07  -2.63  -9.17  -3.98  -6.1   -5.58  -6.95  -9.77
   -3.53  -7.48  -8.96  -8.55  -9.32 -11.76  -5.51  -8.63  -7.16  -7.5
   -5.6   -5.9   -8.55  -6.03  -4.23  -6.    -5.18  -7.15  -5.54  -7.3
   -4.69  -9.54  -7.06  -9.54 -11.04  -8.15  -6.2   -2.8  -15.    -4.8
   -9.6  -17.3 ]

and my grid looks like:

 print(grid)
{'lon': array([-180, -179, -178, -177, -176, -175, -174, -173, -172, -171, -170,
       -169, -168, -167, -166, -165, -164, -163, -162, -161, -160, -159,
       -158, -157, -156, -155, -154, -153, -152, -151, -150, -149, -148,
       -147, -146, -145, -144, -143, -142, -141, -140, -139, -138, -137,
       -136, -135, -134, -133, -132, -131, -130, -129, -128, -127, -126,
       -125, -124, -123, -122, -121, -120, -119, -118, -117, -116, -115,
...
        -37,  -36,  -35,  -34,  -33,  -32,  -31,  -30,  -29,  -28,  -27,
        -26,  -25,  -24,  -23,  -22,  -21,  -20,  -19,  -18,  -17,  -16,
        -15,  -14,  -13,  -12,  -11,  -10,   -9,   -8,   -7,   -6,   -5,
...


        172,  173,  174,  175,  176,  177,  178,  179]), 'lat': array([-75.58333333, -74.58333333, -73.58333333, -72.58333333,
       -71.58333333, -70.58333333, -69.58333333, -68.58333333,
...])
Hexenbrei
  • 192
  • 8
  • Does [this question](https://stackoverflow.com/q/24560298/15432738) help you? – Felix Jun 09 '22 at 11:52
  • I allready hat a look at it. As u may see my coding skills are very low :e – Hexenbrei Jun 09 '22 at 11:54
  • Thats fine, everyone has to start somewhere. The most important thing is to try and keep learning ;). At first, which line throws the error? Execute them one by one to narrow down the problem. – Felix Jun 09 '22 at 12:02
  • it begins, when I activate: interpolation, interpolation_error = interpolate(extended_data, grid) – Hexenbrei Jun 09 '22 at 12:03
  • The problem is: unitl yesterday this all worked fine, and i didn´t change anything (not even at the even) – Hexenbrei Jun 09 '22 at 12:05
  • Ok. does that error also happens with the sample data provided in `get_data` from the code example posted here? If so, the values from the input of `interpolate` (`extended_data` and `grid`) would be helpfull to debug. The whole problem could even be narrowed down to "I have this input and that `interpolate` function and I get this error.". Probably the question could be reduced to just that. (unless the actual issue lies in the input data.) – Felix Jun 09 '22 at 12:07
  • Ok i did It and it shows the same error massage. But it focous on the interpolate definition – Hexenbrei Jun 09 '22 at 12:10
  • Also, the information that it worked yesterday but does not today with nothing having changed is crutial and sould be stated in the question. this would completete change the problem from "What is wrong with this code?" to "What DID actualy change?" – Felix Jun 09 '22 at 12:11
  • Ok, if it shows the same error messge, please post the values of `extended_data` and `grid`. – Felix Jun 09 '22 at 12:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245449/discussion-between-hexenbrei-and-felix). – Hexenbrei Jun 09 '22 at 12:21
  • If the code worked, did the data change? Showing a lot of values doesn't help. Focus on the `shape` (and `dtype`) of the arrays. – hpaulj Jun 09 '22 at 15:01
  • I tried to reporduce your error but the code just executed fine. (I had to change `def get_data(df)` to `def get_data()` and remove `basemap` from the code.) Possibly the error is somewhere inside `basemap` (you did not provide any code about it). – Felix Jun 10 '22 at 08:53

0 Answers0