You are assuming here that ```y_indices```, ```x_indices``` and ```mask``` are all aligned. In other words you've assumed by flattening the arrays that coord_dims are all (0, 1), but the x_coord points array might be (0, 1) ordered and the y_coord points might be (1, 0) ordered.