google / Xee

An Xarray extension for Google Earth Engine
https://xee.rtfd.io
Apache License 2.0
251 stars 29 forks source link

Better handling of setting EE nodata masked value to NaNs values. #103

Closed dabhicusp closed 11 months ago

dabhicusp commented 11 months ago

Fixed #86.

In the existing code, we're comparing data from Earth Engine, which arrives as floating-point numbers with 7-digit precision, to a masked value that's accurate to 9 digits. This discrepancy prevents a direct match. To address this, I introduced np.isclose(), which can match masked value with earth-engine data despite this precision difference.

Let's take an example:

import numpy as np
data = [(9999.,), (9999.01,), (9999.1,)]
Previous_Result = np.where( data == 9999, np.nan, data)
Current_Result = np.where( np.isclose(data, 9999), np.nan, data)
print(Previous_Result, Current_Result)

Output:

Previous_Result : [[9999.  ] [9999.01] [9999.1 ]] 
Current_Result : [[nan] [nan] [9999.1]]

So np.isclose is match the value when their difference is less than 0.1. Official documentation link: https://numpy.org/doc/stable/reference/generated/numpy.isclose.html#numpy-isclose.

naschmitz commented 11 months ago

I added a comment change to the PR. LGTM!