<jupyter_start><jupyter_text>Third party commercial data The aim of this notebook is to guide you how to get Very High Resolution Satellite (VHRS) data using Sentinelhub and eo-learn. Price for commercial data is described there https://www.sentinel-hub.com/pricing/.The workflow for agriculture purposes is as follows :1) Read shapefile that represent you field (boundaries or microplots)2) Extract Sentinel-2 data and compute averaged NDVI time series to get a summary of the season vegetation dynamic.3) Get the index of time when NDVI is maximum (season's peak) and add +/- one month to this date to get the time period for VHRS data extraction.4) Extract VHRS data. If provider is Airbus, you can apply pansharpening algorithms to get the spatial resolution from the panchromatic band.5) Preprocess and save the data.6) Optional : intersects pixels with trial microplots polygons to get a value of NDVI for each microplot.  Set your working environment  Import the packages<jupyter_code>import warnings
warnings.filterwarnings("ignore")

import glob


import geopandas as gpd

import os
import numpy as np

import matplotlib.pyplot as plt


from importlib import reload

from eocrops.input import utils_sh as utils_sh
from eocrops.input import sentinel2 as sentinel2
from eocrops.input import vhrs as vhrs
from eocrops.tasks import preprocessing as preprocessing<jupyter_output><empty_output><jupyter_text>Read your vector file<jupyter_code>dir_path = os.path.dirname(os.getcwd())
print(dir_path)
#read microplot data
shapefile_input = gpd.read_file(os.path.join(dir_path, './examples/layers/POLYGON.shp'))

api =''
client_id =''
client_secret =''
config = utils_sh.config_sentinelhub_cred(api, client_id, client_secret)
#Provide here your planet API key 
config.planet_key = ''<jupyter_output>/home/johann/anaconda3/envs/eo-crops/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)<jupyter_text>Extract S2 data<jupyter_code>time_period = ('2020-02-01', '2020-10-30')
kwargs = dict(polygon=shapefile_input,
              time_stamp=time_period,
              config=config)
patch = sentinel2.workflow_instructions_S2L2A(**kwargs,
                                              coverage_predicate=0.5,
                                              interpolation={'interpolate' : True,
                                                             'period_length' : 8})  # you can add period_length in the dictionary to resample

#RGB
plt.figure(figsize=(5, 5))
plt.imshow(np.clip(patch.data['BANDS-S2-L2A'][15][..., [2, 1, 0]]*2.5, 0, 1), vmin=0, vmax=1);
plt.axis(False);
plt.show()
preprocessing = reload(preprocessing)
#Mask out pixels that are oustide the polygon for visualisation and apply a binary erosion with radius = 1 to keep only pure pixels
masking = preprocessing.MaskPixels(["NDVI"])
patch = masking.execute(patch, erosion = 1)
#NDVI
plt.figure(figsize=(5, 5))
plt.imshow(patch.data['NDVI'][15].squeeze());
plt.axis(False);
plt.show()
#LAI Time Series from masked field
variable = 'NDVI'
stats_df = preprocessing.get_time_series_profile(patch = patch, 
                                                 variable = variable, 
                                                 mask_name = 'MASK', 
                                                 function = np.nanmean)

plt.plot(patch.timestamp, stats_df[variable])
plt.xticks(rotation=90)
plt.show()
#Peak of the season is at 15/05 
time_max_index = np.nanargmax(stats_df[variable])
patch.timestamp[np.nanargmax(stats_df[variable])]
#If oyu would like to save the data in .tif format (e.g. NDVI on the first date)
from eolearn.io.local_io import ExportToTiffTask
index_time = 0
date = str(patch.timestamp[index_time]).split(' ')[0]

export = ExportToTiffTask(feature='NDVI',
                          folder=os.path.join('your_path_'+ date),
                          band_indices=[0],
                          date_indices=[index_time])
export.execute(patch)<jupyter_output><empty_output><jupyter_text>VHRS data<jupyter_code>#Define the time period
time_period_vhrs = ('2020-04-15', '2020-06-15')
#Define the workflow
download_workflow = vhrs.DownloadVHRSSentinelHub(shapefile=shapefile_input,
                                                 time_stamp = time_period_vhrs,
                                                 config = config,
                                                 maxCloudCoverage=10)<jupyter_output><empty_output><jupyter_text>Extract Planetscope data<jupyter_code>#Define the provider as follows for Planetscope
provider_planet = ('PLANET', 'PSScene4Band')
#You can retrieve order id and collection ID on your SH account (https://apps.sentinel-hub.com/dashboard/#/tpdi) instead of doing a request again
#order_id, collection_id = '2f900fad-1f5e-4a46-b6da-327012d400e1', '79a1a2a9-ccf8-4e1f-8f42-9d854bf1dca9'
#Define the workflow to get your order_id and collection_id. It can be also be retrieved in your SH account in https://apps.sentinel-hub.com/dashboard/#/tpdi
order_id, results = download_workflow.execute_query(provider_planet, name_query='Example Planet')
#You can access to all the meta data in results.
len(results['features'])
#Check the status of the order. Image will not be available 
collection_id = download_workflow.confirm_order(order_id)
#While the statut is "RUNNING", you should wait that you data is ingested in your SH account. The output of this cell must be "DONE" before your get access to the data (~ 2-5 minutes)
download_workflow.check_status(order_id)
eopatch_planet = download_workflow.get_data(order_id = order_id,
                                            collection_id = collection_id,
                                            provider = provider_planet,
                                            resolution = 3)
#Reset the download_workflow if you would like to launch new orders. It is a way to prevent to download multiple times the same image and consume credits.
download_workflow.reset_workflow()
plt.figure(figsize=(5, 5))
plt.imshow(np.clip(eopatch_planet['data']['BANDS'][..., [2, 1, 0]][7,], 0, 1));
plt.axis(False);
plt.show()
#LAI Time Series from masked field
variable = 'NDVI'
stats_df = preprocessing.get_time_series_profile(patch = eopatch_planet, 
                                                 variable = variable, 
                                                 mask_name = 'MASK', 
                                                 function = np.nanmean)

plt.plot(eopatch_planet.timestamp, stats_df[variable])
plt.xticks(rotation=90)
plt.show()
time_peak = np.nanargmax(stats_df[variable])
plt.figure(figsize=(5, 5))
plt.imshow(eopatch_planet.data['NDVI'][time_peak,].astype(float))
plt.colorbar()
plt.axis(False);
plt.show()<jupyter_output>/home/johann/anaconda3/envs/eo-crops/lib/python3.7/site-packages/ipykernel/ipkernel.py:287: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.
  and should_run_async(code)<jupyter_text>If you would like to save the EOPatch, you can - call the method .save (e.g. eopatch_spot.save(your_path)) from your EOPatch to save the corresponding npys- save .tif images using ExportToTiffTask from eo-learn https://eo-learn.readthedocs.io/en/latest/eolearn.io.local_io.htmleolearn.io.local_io.ExportToTiffTask  Extract AIRBUS SPOT data<jupyter_code>#Define the provider as follows for Planetscope
provider_spot = ('AIRBUS', 'SPOT')
#can be also be retrieved in your SH account in https://apps.sentinel-hub.com/dashboard/#/tpdi
#order_id_spot, collection_id_spot = '222331e9-d8d3-4e2f-959c-e292a665b214', '6458a2f4-70fb-4986-bb90-1b3aeaeaca25'
#Define the workflow to get your order_id and meta data.  
order_id_spot, results_spot = download_workflow.execute_query(provider_spot, name_query='example spot')
collection_id_spot = download_workflow.confirm_order(order_id_spot)
#While the statut is "RUNNING", you should wait that you data is ingested in your SH account. The output of this cell must be "DONE" before your get access to the data (~ 2-5 minutes)
download_workflow.check_status(order_id_spot)
#Apply pansharpenning method to project native bands (VIS + NIR) at 6m into the panchromatic resolution (1.5). You need to have OrfeoToolbox installed in your computer, available for free on https://www.orfeo-toolbox.org/CookBook/Installation.html.
eopatch_spot = download_workflow.get_data(order_id = order_id_spot,
                                          collection_id = collection_id_spot,
                                          provider = provider_spot,
                                          resolution = 1.5,
                                          pansharpen = True,
                                          otb_path='/home/s999379/git-repo/OTB-7.4.0-Linux64/bin')
#Reset the download_workflow method if you would like to execute new queries. 
# t is a way to prevent to download multiple times the same image and consume credits for nothing.
download_workflow.reset_workflow()
plt.figure(figsize=(5, 5))
plt.imshow(np.clip(eopatch_spot['data']['BANDS'][..., [2, 1, 0]][1,], 0, 1));
plt.axis(False);
plt.show()

plt.figure(figsize=(5, 5))
plt.imshow(eopatch_spot.data['NDVI'][1,].astype(float), vmax = 0.88)
plt.colorbar()
plt.axis(False);
plt.show()<jupyter_output><empty_output><jupyter_text>If you would like to save the EOPatch, you can - call the method .save (e.g. eopatch_spot.save(your_path)) from your EOPatch to save the corresponding npys - save .tif images using ExportToTiffTask from eo-learn https://eo-learn.readthedocs.io/en/latest/eolearn.io.local_io.htmleolearn.io.local_io.ExportToTiffTask  Extract AIRBUS Pleaides data Airbus Pleiades have a Panchromatic band at 0.5m and VIS-NIR at 1.5m. On SH, the price is roughly 10€ for 1km2 of order, with a minimal order of 0.25km2 (2€50).<jupyter_code>#Define the provider as follows for Planetscope
provider_pleaides = ('AIRBUS', 'PHR')
#In case, you can use as follows to run the example instead of ordering again data to avoid to run the order and consume credits for nothing
#order_id_pleiades, collection_id_pleaides = 'e7f5c398-0d23-4f9d-a6a8-03b2b0632554', '9eb3a890-7984-46bb-970d-db30ad5a4209'
#Define the workflow to get your order_id and collection_id. It can be also be retrieved in your SH account in https://apps.sentinel-hub.com/dashboard/#/tpdi
order_id_pleiades, results_pleiades = download_workflow.execute_query(provider_pleaides, name_query='example pleaides')
collection_id_pleaides = download_workflow.confirm_order(order_id_pleiades)
#While the statut is "RUNNING", you should wait that you data is ingested in your SH account. The output of this cell must be "DONE" before your get access to the data (~ 2-5 minutes)
download_workflow.check_status( order_id_pleiades)

#Apply pansharpenning method to project native bands (VIS + NIR) at 6m into the panchromatic resolution (1.5). You need to have OrfeoToolbox installed in your computer, available for free on https://www.orfeo-toolbox.org/CookBook/Installation.html.
eopatch_pleaides = download_workflow.get_data(order_id = order_id_pleiades,
                                          collection_id = collection_id_pleaides,
                                          provider = provider_pleaides,
                                          resolution = 0.5,
                                          pansharpen = False, #Apply pansharpenning method to project native bands (VIS + NIR) at 1.5m into the panchromatic resolution (0.5). You need to have OrfeoToolbox installed in your computer, available for free on https://www.orfeo-toolbox.org/CookBook/Installation.html.
                                          otb_path='/home/s999379/git-repo/OTB-7.4.0-Linux64/bin')
eopatch_pleaides.timestamp
plt.figure(figsize=(5, 5))
plt.imshow(np.clip(eopatch_pleaides['data']['BANDS'][..., [2, 1, 0]][0,], 0, 1));
plt.axis(False);
plt.show()

plt.figure(figsize=(5, 5))
plt.imshow(eopatch_pleaides.data['BANDS-PAN'][0,].astype(float),vmin = 0.2, vmax = 0.9)
plt.colorbar()
plt.axis(False);
plt.show()
plt.figure(figsize=(5, 5))
plt.imshow(eopatch_pleaides.data['NDVI'][0,].astype(float),vmin = 0.2, vmax = 0.9)
plt.colorbar()
plt.axis(False);
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.02582996592979954, 'mean_word_length': 3.414830360265827, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 10, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 70, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '462609', 'license': 'MIT', 'n_tokens_mistral': 4167, 'n_tokens_neox': 3865, 'n_words': 1105, 'path': 'eo-crops/examples/VHRS data.ipynb', 'repo_name': 'j-desloires/eo-learn-examples', 'size': '12621'}