I want to create a grid data, which I plot on a map, the lat maximum of the grid should be at 90 and the minimum at -90! however, I keep getting the following error:
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'subtract' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
I have already tried:
from traceback import print_tb
import numpy as np
import pandas as pd
def get_data():
"lats": np.array([6.47, 4.8, 1.94 ])
"lons": 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):
latmax = 90
latmin = -90
latmax = latmax.astype('float32')
latmin = latmin.astype('float32')
grid = {
'lon': np.arange(-180, 180, delta),
'lat': np.arange(latmin, latmax, delta)
}
grid["x"], grid["y"] = np.meshgrid(grid["lon"], grid["lat"])
grid["x"], grid["y"] = basemap(grid["x"], grid["y"])
return grid
base_data = get_data()
figure, axes, basemap = prepare_map_plot()
grid = generate_grid(base_data, basemap, 1)
extended_data = extend_data(base_data)
The error occours in line: extended_data = extend_data(base_data)
However when I write:
np.amin(data["lats"]), np.amax(data["lats"])
instead of:
latmax = 90
latmin = -90
latmax = latmax.astype('float32')
latmin = latmin.astype('float32')
grid = {
'lon': np.arange(-180, 180, delta),
'lat': np.arange(latmin, latmax, delta)
The error dosn´t occour, however this is not what I wanted!
But this does not work, what could be the reason?
CodePudding user response:
Unsafe casting will do the operation in the larger (rhs) precision (or the combined safe dtype) the other option will do the cast and thus the operation in the lower precision.
You can avoid it
like this example Can use "unsafe" method via numpy.subtract
arr = image.flatten()
arr = np.add(arr, image.flatten(), out=arr, casting="unsafe")
But the fundamental problem is your numpy array have "int32" type value
CodePudding user response:
for array you need the same type whenever you want to realise some operations as = or -= (additions or substracts)
CodePudding user response:
syntax errors in:
def get_data():
"lats": np.array([6.47, 4.8, 1.94 ])
"lons": np.array([46.37, 43.9, 47.83)
"values": np.array([-29.5, -27.6, -32.5 ])
}
Missing function: prepare_map_plot
After correcting get_data
:
In [146]: base_data = get_data()
In [147]: base_data
Out[147]:
{'lats': array([6.47, 4.8 , 1.94]),
'lons': array([46.37, 43.9 , 47.83]),
'values': array([-29.5, -27.6, -32.5])}
extended_data
runs:
In [149]: extended_data = extend_data(base_data)
In [150]: extended_data
Out[150]:
{'lons': array([-313.63, -316.1 , -312.17, 46.37, 43.9 , 47.83, 406.37,
403.9 , 407.83]),
'lats': array([6.47, 4.8 , 1.94, 6.47, 4.8 , 1.94, 6.47, 4.8 , 1.94]),
'values': array([-29.5, -27.6, -32.5, -29.5, -27.6, -32.5, -29.5, -27.6, -32.5])}
Rewriting generate
to skip the unavailable basemap
In [151]: def generate_grid(data, delta=1):
...: latmax = 90
...: latmin = -90
...: latmax = latmax.astype('float32')
...: latmin = latmin.astype('float32')
...: grid = {
...: 'lon': np.arange(-180, 180, delta),
...: 'lat': np.arange(latmin, latmax, delta)
...: }
...: grid["x"], grid["y"] = np.meshgrid(grid["lon"], grid["lat"])
...: # grid["x"], grid["y"] = basemap(grid["x"], grid["y"])
...: return grid
...:
Note that the basemap
line overwrites the previous meshgrid
variables.
In [152]: generate_grid(base_data)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [152], in <cell line: 1>()
----> 1 generate_grid(base_data)
Input In [151], in generate_grid(data, delta)
2 latmax = 90
3 latmin = -90
----> 4 latmax = latmax.astype('float32')
5 latmin = latmin.astype('float32')
6 grid = {
7 'lon': np.arange(-180, 180, delta),
8 'lat': np.arange(latmin, latmax, delta)
9 }
AttributeError: 'int' object has no attribute 'astype'
I get an error, but it's unrelated to the one you get. You did not post the code that actually produced your error, and you did not post the full error.
I'm going to vote to close this question because the it needs proper debugging details.