13

I am using Python Numpy arrays (rasters converted to 2D arrays, specifically) and what I want to do is take one array that has arbitrary dummy values of -999 representing "no data" and I want to replace those values with the corresponding "real" values from a different array of the same size and shape in the correct location. I couldn't find a very similar question to this but note that I am a novice with Python and Numpy.

But what I want to do is this:

array_a = 
([[0.564,-999,-999],
 [0.234,-999,0.898],
 [-999,0.124,0.687], 
 [0.478,0.786,-999]])

array_b = 
([[0.324,0.254,0.204],
 [0.469,0.381,0.292],
 [0.550,0.453,0.349], 
 [0.605,0.582,0.551]])

use the values of array_b to fill in the -999 values in array_a and create a new array:

new_array_a = 
([[0.564,0.254,0.204],
 [0.234,0.381,0.898],
 [0.550,0.124,0.687], 
 [0.478,0.786,0.551]])

I don't really want to change the shape or dimensions of the array because I am going to convert back out into a raster afterwards so I need the correct values in the correct locations. What is the best way to do this?

2
  • 4
    Look into masking. Commented Apr 24, 2017 at 14:30
  • Yeah, I looked into that and I was able to mask out the -999 values in array_a using this: mask_a = ma.masked_where(array_a < -1,array_a) but then what? I just have removed the -999 values and I don't know how to get the values from array_b into the same locations? Commented Apr 24, 2017 at 14:34

2 Answers 2

25

Just do boolean masking:

mask = (array_a == -999)
new_array = np.copy(array_a)
new_array[mask] = array_b[mask]
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, nice! I think that works! Thank you! >>> array_a = np.array([[0.564,-999,-999], ... [0.234,-999,0.898], ... [-999,0.124,0.687], ... [0.478,0.786,-999]]) >>> array_b = np.array([[0.324,0.254,0.204], ... [0.469,0.381,0.292], ... [0.550,0.453,0.349], ... [0.605,0.582,0.551]]) >>> mask = (array_a == -999) >>> new_array = np.copy(array_a) >>> new_array[mask] = array_b[mask] >>> print new_array [[ 0.564 0.254 0.204] [ 0.234 0.381 0.898] [ 0.55 0.124 0.687] [ 0.478 0.786 0.551]] >>>
4

all you need to do is

array_a[array_a==-999]=array_b[array_a==-999]

we are putting boolean condition on array elements to update should have value -999

import numpy as np
array_a =np.array([[0.564,-999,-999],
 [0.234,-999,0.898],
 [-999,0.124,0.687], 
 [0.478,0.786,-999]])

array_b =np.array([[0.324,0.254,0.204],
 [0.469,0.381,0.292],
 [0.550,0.453,0.349], 
 [0.605,0.582,0.551]])
array_a[array_a==-999]=array_b[array_a==-999]

run this snippet enter image description here

[1]: https://i.sstatic.net/oRdsE.png

5 Comments

I doubt that this helps - or even works at all. To convince me otherwise please explain how this works and why it is supposed to solve the problem.
it's simple you are only taking those parts which are True for the condition for both.
Please explain that (how and why) inside your answer.
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
@DimaKozhevin I have updated it. please see it again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.