Module fordead.writing_data
Created on Fri Nov 6 17:32:26 2020
@author: Raphael Dutrieux
Functions
convert_dateindex_to_datenumber
def convert_dateindex_to_datenumber(
date_array,
mask_array,
dates
)
Converts array containing dates as an index to an array containing dates as the number of days since "2015-01-01" or to a no data value if masked
Parameters
----------
date_array : xarray DataArray
array containing date indices
mask_array : xarray DataArray
mask array, pixels containing False are given no data value of 99999999
dates : array
Array of dates in the format "YYYY-MM-DD", index of the date in this array corresponds to the indices in date_array.
Returns
-------
results_date_number : xarray DataArray
DataArray with dates as the number of days since "2015-01-01", or no data value of 99999999
export_csv
def export_csv(
data_directory,
ground_obs_path,
ground_obs_buffer=None,
name_column='id'
)
Produce the anomaly detection from the model, along with exporting as two .csv files data relating to pixels within the ground_obs_path shapefile.
Parameters
----------
data_directory: str
Path of the output directory
ground_obs_path: str
Path to the shapefile containing ground observation polygons
ground_obs_buffer: int
Buffer applied to vectors, positive for dilation, negative for erosion.
name_column: str
Name of the column containing the ID of the ground observation polygon
get_bins
def get_bins(
start_date,
end_date,
frequency,
dates
)
Creates bins from the start_date (or first used SENTINEL date if it is later than the start date) to the end_date (or last used SENTINEL date if it is earlier than the end_date) with specified frequency
Parameters
----------
start_date : str
Date in the format 'YYYY-MM-DD'
end_date : str
Date in the format 'YYYY-MM-DD'
frequency : str
Frequency as used in pandas.date_range (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.date_range.html), e.g. 'M' (monthly), '3M' (three months), '15D' (fifteen days). It can also be 'sentinel', then bins correspond to the list given as parameter 'dates'
dates : list
List of dates used for detection
Returns
-------
bins_as_date : numpy array
Bins as an array of dates in the format 'YYYY-MM-DD'
bins_as_datenumber : numpy array
Bins as an array of integers corresponding to the number of days since "2015-01-01"
get_periodic_results_as_shapefile
def get_periodic_results_as_shapefile(
first_date_number,
bins_as_date,
bins_as_datenumber,
relevant_area,
attrs
)
Aggregates pixels in array containing dates, based on the period they fall into, then vectorizes results masking dates oustide the bins.
Parameters
----------
first_date_number : array
Array containing dates as the number of days since "2015-01-01"
bins_as_date : array
Array containing dates used as bins in the format "YYYY-MM-JJ"
bins_as_datenumber : array
Array containing dates used as bins, as the number of days since "2015-01-01"
relevant_area : array
Mask where pixels with value False will be ignored.
attrs : dict
Dictionnary containing 'tranform' and 'crs' to create the vector.
Returns
-------
period_end_results : geopandas dataframe
Polygons containing the period during which dates fall.
get_state_at_date
def get_state_at_date(
state_code,
relevant_area,
attrs
)
Vectorizes array 'state_code' using 'relevant_area' as mask
Parameters
----------
state_code : array
Array to vectorize, in which value 0 is ignored, 1 is 'Anomaly', 2 is "Bare ground" and 3 is 'Bare ground after anomaly'
relevant_area : array
Mask where pixels with value False will be ignored.
attrs : dict
Dictionnary containing 'tranform' and 'crs' to create the vector.
Returns
-------
period_end_results : geopandas dataframe
Polygons from vectorization of state_code, aggregated according to value 'Anomaly', "Bare ground" and 'Bare ground after anomaly'
union_confidence_class
def union_confidence_class(
periodic_results,
confidence_class
)
Computes union between periodic_results containing the dates of detection, and confidence_class containing the confidence classes
Polygons not intersecting correspond to areas detected as bare ground and their class column is filled with "Bare ground"
Parameters
----------
period_end_results : geopandas dataframe
Polygons where dieback is detected, containing the period of the first anomaly detected
confidence_class : geopandas dataframe
Polygons containing the confidence class
Returns
-------
union : geopandas dataframe
Union of periodic_results and confidence_class
vectorizing_confidence_class
def vectorizing_confidence_class(
confidence_index,
nb_dates,
relevant_area,
bins_classes,
classes,
attrs
)
Classifies pixels in the relevant area into dieback classes based on the confidence index and the number of unmasked dates since the first anomaly.
Parameters
----------
confidence_index : xarray DataArray (x,y) (float)
Confidence index.
nb_dates : xarray DataArray (x,y) (int)
number of unmasked dates since the first anomaly.
relevant_area : xarray DataArray (x,y) (bool)
Array with True where pixels will be vectorized, and False where ignored.
bins_classes : list of float
List of bins to classify pixels based on confidence_index
classes : list of str
List with names of classes (length of classes must be length of bins_classes + 1)
attrs : dict
Dictionnary containing 'tranform' and 'crs' to create the vector.
Returns
-------
gp_results : geopandas geodataframe
Polygons from pixels in the relevant areacontaining the dieback class.
write_tif
def write_tif(
data_array,
attributes,
path,
nodata=None
)
Writes raster to the disk
Parameters
----------
data_array : xarray DataArray
Object to be written
attributes : dict
Dictionnary containing attributes used to write the data_array ("crs","nodata","scales","offsets")
path : str
Path of the file to which data will be written
nodata : int or float, optional
Number used as nodata. If None, the nodata attribute of the object will be kept. The default is None.
Returns
-------
None.