Skip to content

Commit 2e1fba6

Browse files
authored
Merge pull request #6 from emilyselwood/test-branch
updates to make stats functions work
2 parents 4780d10 + f26afee commit 2e1fba6

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

datacube_utilities/dc_mosaic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def convert_to_dtype(data, dtype):
6161
Compositing Functions
6262
"""
6363

64-
def create_mosaic(dataset_in, clean_mask=None, no_data=-9999, dtype=None, intermediate_product=None, **kwargs):
64+
def create_mosaic(dataset_in, clean_mask=None, no_data=float('nan'), dtype=None, intermediate_product=None, **kwargs):
6565
"""
6666
Creates a most-recent-to-oldest mosaic of the input dataset.
6767
@@ -125,7 +125,7 @@ def create_mosaic(dataset_in, clean_mask=None, no_data=-9999, dtype=None, interm
125125
dataset_out = restore_or_convert_dtypes(dtype, band_list, dataset_in_dtypes, dataset_out, no_data)
126126
return dataset_out
127127

128-
def create_mean_mosaic(dataset_in, clean_mask=None, no_data=-9999, dtype=None, **kwargs):
128+
def create_mean_mosaic(dataset_in, clean_mask=None, no_data=float('nan'), dtype=None, **kwargs):
129129
"""
130130
Method for calculating the mean pixel value for a given dataset.
131131
@@ -215,7 +215,7 @@ def create_median_mosaic(dataset_in, clean_mask=None, no_data=float('nan'), dtyp
215215
return dataset_out
216216

217217

218-
def create_max_ndvi_mosaic(dataset_in, clean_mask=None, no_data=-9999, dtype=None, intermediate_product=None, **kwargs):
218+
def create_max_ndvi_mosaic(dataset_in, clean_mask=None, no_data=float('nan'), dtype=None, intermediate_product=None, **kwargs):
219219
"""
220220
Method for calculating the pixel value for the max ndvi value.
221221
@@ -279,7 +279,7 @@ def create_max_ndvi_mosaic(dataset_in, clean_mask=None, no_data=-9999, dtype=Non
279279
return dataset_out
280280

281281

282-
def create_min_ndvi_mosaic(dataset_in, clean_mask=None, no_data=[-9999, 0, np.float('nan')], dtype=None, intermediate_product=None, **kwargs):
282+
def create_min_ndvi_mosaic(dataset_in, clean_mask=None, no_data=float('nan'), dtype=None, intermediate_product=None, **kwargs):
283283
"""
284284
Method for calculating the pixel value for the min ndvi value.
285285
@@ -525,12 +525,12 @@ def restore_or_convert_dtypes(dtype_for_all, band_list, dataset_in_dtypes, datas
525525
if dtype_for_all is not None:
526526
# Integer types can't represent nan.
527527
if np.issubdtype(dtype_for_all, np.integer): # This also works for Python int type.
528-
utilities.nan_to_num(dataset_out, no_data)
528+
dataset_out.fillna(no_data)
529529
convert_to_dtype(dataset_out, dtype_for_all)
530530
else: # Restore dtypes to state before masking.
531531
for band in band_list:
532532
band_dtype = dataset_in_dtypes[band]
533533
if np.issubdtype(band_dtype, np.integer):
534-
utilities.nan_to_num(dataset_out[band], no_data)
534+
dataset_out[band].fillna(no_data)
535535
dataset_out[band] = dataset_out[band].astype(band_dtype)
536536
return dataset_out

datacube_utilities/dc_utilities.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,7 @@ def nan_to_num(data, number):
182182
----------
183183
data: xarray.Dataset or xarray.DataArray
184184
"""
185-
if isinstance(data, xr.Dataset):
186-
for key in list(data.data_vars):
187-
data[key].values[np.isnan(data[key].values)] = number
188-
elif isinstance(data, xr.DataArray):
189-
data.values[np.isnan(data.values)] = number
185+
data.fillna(number)
190186

191187

192188
def clear_attrs(dataset):

datacube_utilities/query.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,32 @@
44
from datacube_utilities.createAOI import create_lat_lon
55

66

7-
def create_base_query(aoi, res, output_projection, aoi_crs, dask_chunks):
7+
def create_base_query(aoi, res, output_crs, aoi_crs, dask_chunks, cube_crs="EPSG:3460"):
8+
"""
9+
create_base_query sets up the basic data cube query and makes sure the AOI is in the right CRS
10+
11+
Parameters
12+
----------
13+
aoi: WKT formatted string.
14+
res: Int Resolution of the output images
15+
output_crs: Resulting projection string EPSG code.
16+
aoi_crs: Projection string EPSG code of the AOI . Does not have to be the same as the output.
17+
dask_chunks: Number of dask chunks to split the resulting data into.
18+
cube_crs: The Projection string EPSG code used by the data cube.
19+
20+
Returns
21+
-------
22+
query: A query object ready to be fed into dc.load
23+
"""
824
lat_extents, lon_extents = create_lat_lon(aoi)
9-
inProj = Proj("+init=EPSG:4326")
10-
outProj = Proj("+init=EPSG:3460")
25+
in_proj = Proj("+init=" + aoi_crs)
26+
out_proj = Proj("+init=" + cube_crs)
1127

1228
min_lat, max_lat = lat_extents
1329
min_lon, max_lon = lon_extents
14-
15-
x_A, y_A = transform(inProj, outProj, min_lon, min_lat)
16-
x_B, y_B = transform(inProj, outProj, max_lon, max_lat)
30+
31+
x_A, y_A = transform(in_proj, out_proj, min_lon, min_lat)
32+
x_B, y_B = transform(in_proj, out_proj, max_lon, max_lat)
1733

1834
lat_range = (y_A, y_B)
1935
lon_range = (x_A, x_B)
@@ -23,10 +39,10 @@ def create_base_query(aoi, res, output_projection, aoi_crs, dask_chunks):
2339
query = {
2440
"y": lat_range,
2541
"x": lon_range,
26-
"output_crs": output_projection,
42+
"output_crs": output_crs,
2743
"resolution": resolution,
2844
"dask_chunks": dask_chunks,
29-
"crs": aoi_crs,
45+
"crs": cube_crs,
3046
}
3147
return query
3248

0 commit comments

Comments
 (0)