Skip to content

Commit bb1b56a

Browse files
committed
Removed commented function.
1 parent ca3f66b commit bb1b56a

2 files changed

Lines changed: 67 additions & 47 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import datacube
2+
from utils.data_cube_utilities.dc_mosaic import ls8_unpack_qa, ls7_unpack_qa
3+
import numpy as np
4+
from functools import partial
5+
import pandas as pd
6+
7+
def build_cloud_coverage_table_landsat(product=None,
8+
platform=None,
9+
latitude=None,
10+
longitude=None,
11+
time=None,
12+
dc=None,
13+
extra_band='green'):
14+
15+
if product is None: raise Exception("product argument is required")
16+
if platform is None: raise Exception("platform argument is required")
17+
if latitude is None: raise Exception("latitude argument is required")
18+
if longitude is None: raise Exception("longitude argument is required")
19+
20+
def clean_mask(ds, unpacking_func, bands):
21+
masks = [unpacking_func(ds, band) for band in bands]
22+
return np.logical_or(*masks).values
23+
24+
unpack_function = {"LANDSAT_7": ls7_unpack_qa,
25+
"LANDSAT_8": ls8_unpack_qa}
26+
27+
dc = dc if dc else datacube.Datacube(app="")
28+
29+
load_params = dict(product=product,
30+
platform=platform,
31+
latitude=latitude,
32+
longitude=longitude,
33+
measurements=[extra_band, 'pixel_qa'])
34+
35+
if time is not None:
36+
load_params["time"] = time
37+
38+
geo_data = dc.load(**load_params)
39+
40+
times = list(geo_data.time.values)
41+
scene_slice_list = list(map(lambda t: geo_data.sel(time=str(t)), times))
42+
43+
def create_clean_mask_list(ds):
44+
return clean_mask(ds, unpacking_func=unpack_function[platform], bands=["clear", "water"])
45+
clean_mask_list = list(map(lambda ds: create_clean_mask_list(ds.pixel_qa), scene_slice_list))
46+
no_data_mask_list = list(map(lambda ds: (ds[extra_band] == -9999).values, scene_slice_list))
47+
# This method of creating `percentage_list` gives the percentage of pixels with data (i.e. not no_data (-9999))
48+
# which are also not cloud.
49+
# def create_clean_percentage_with_data_list(masks_tup):
50+
# """Merges two masks - passed as a tuple - and calculates the percentage
51+
# of pixels that are clean and have data."""
52+
# clean_mask, no_data_mask = masks_tup
53+
# merged_mask = clean_mask & ~no_data_mask
54+
# return merged_mask.sum() / (~no_data_mask).sum() * 100
55+
# percentage_list = list(map(create_clean_percentage_with_data_list,
56+
# zip(clean_mask_list, no_data_mask_list)))
57+
# This method of creating `percentage_list` gieves the percentage of all pixels
58+
# which are also not cloud.
59+
percentage_list = [clean_mask.mean()*100 for clean_mask in clean_mask_list]
60+
clean_pixel_count_list = list(map(np.sum, clean_mask_list))
61+
62+
data = {"times": times,
63+
# "clean_percentage" is the percent of pixels that are not no_data which are clear.
64+
"clean_percentage": percentage_list,
65+
"clean_count": clean_pixel_count_list }
66+
67+
return geo_data, pd.DataFrame(data=data, columns=["times", "clean_percentage", "clean_count"])

data_cube_utilities/dc_utilities.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -70,53 +70,6 @@ def get_range(platform, collection, level):
7070
return range_dict
7171

7272

73-
# def convert_range(dataset, from_platform, from_collection, from_level,
74-
# to_platform, to_collection, to_level):
75-
# """
76-
# Converts an xarray.Dataset's range from its product's range
77-
# to that of another product's range.
78-
79-
# Parameters
80-
# ----------
81-
# dataset: xarray.Dataset
82-
# The dataset to convert to another range.
83-
# from_platform, from_collection, from_level: string
84-
# The dataset's product's platform, collection, and level.
85-
# For example, ('LANDSAT_8', 'c2', 'l2').
86-
# to_platform, to_collection, to_level: string
87-
# The platform, collection, and level to convert the
88-
# dataset's range to.
89-
# For example, ('LANDSAT_7', 'c1', 'l2').
90-
# """
91-
# # Get the original and destination ranges.
92-
# from_rng = get_range(from_platform, from_collection, from_level)
93-
# if from_rng is None:
94-
# raise ValueError(
95-
# f'The original range is not recorded '\
96-
# f'(platform: {from_platform}, collection: {from_collection}, level: {from_level}).')
97-
# to_rng = get_range(to_platform, to_collection, to_level)
98-
# if to_rng is None:
99-
# raise ValueError(
100-
# f'The destination range is not recorded '\
101-
# f'(platform: {to_platform}, collection: {to_collection}, level: {to_level}).')
102-
103-
# # Determine the data variables with ranges in both
104-
# # the original and destination range information.
105-
# data_vars_both = list(set(from_rng.keys()) & set(to_rng.keys()))
106-
# out_dataset = dataset.copy(deep=True)
107-
# for data_var_name in data_vars_both:
108-
# from_rng_cur = from_rng[data_var_name]
109-
# to_rng_cur = to_rng[data_var_name]
110-
# out_dataset[data_var_name].data = np.interp(out_dataset[data_var_name], from_rng_cur, to_rng_cur)
111-
112-
# # Temporary approximate corrections - range scaling is often very inaccurate.
113-
# if (from_platform, from_collection, from_level) == ('LANDSAT_8', 'c2', 'l2') and \
114-
# to_platform in ['LANDSAT_7', 'LANDSAT_8'] and \
115-
# (to_collection, to_level) == ('c1', 'l2'):
116-
# out_dataset[data_var_name] = out_dataset[data_var_name] * 0.1
117-
118-
# return out_dataset
119-
12073
def convert_range(dataset, from_platform, from_collection, from_level,
12174
to_platform, to_collection, to_level):
12275
"""

0 commit comments

Comments
 (0)