<jupyter_start><jupyter_text>Quick StartIn this notebook, we demonstrate the capabilitis of Azure Farmbeats python SDK  Before running this notebook, please create conda environment and select 'farmbeats-sdk-env'  as notebook kernel Refer `Getting Started` section of [README.md](../README.md) for instructions to create environment and activate kernel<jupyter_code>import sys
print(sys.executable)
print (sys.version)<jupyter_output><empty_output><jupyter_text>Import Libraries<jupyter_code># Standard library imports
import json
import os
import sys
import uuid
from datetime import datetime

# Disable unnecessary logs 
import logging
logging.disable(sys.maxsize)
import warnings
warnings.filterwarnings("ignore")

# Local imports
from config import farmbeats_config

# Azure imports
from azure.core.exceptions import HttpResponseError, ResourceNotFoundError
from azure.identity import ClientSecretCredential

# Azure FarmBeats SDK imports
from azure.agrifood.farming import FarmBeatsClient
from azure.agrifood.farming.models import (Farmer, Boundary, Polygon,
                                    SatelliteDataIngestionJob,
                                    WeatherDataIngestionJob, 
                                    SatelliteData)<jupyter_output><empty_output><jupyter_text>Farmbeats Configuration<jupyter_code># FarmBeats Client definition
credential = ClientSecretCredential(
    tenant_id=farmbeats_config['tenant_id'],
    client_id=farmbeats_config['client_id'],
    client_secret=farmbeats_config['client_secret'],
    authority=farmbeats_config['authority']
)

credential_scopes = [farmbeats_config['default_scope']]

fb_client = FarmBeatsClient(
    endpoint=farmbeats_config['instance_url'],
    credential=credential,
    credential_scopes=credential_scopes,
    logging_enable=True
)<jupyter_output><empty_output><jupyter_text>Create Farmer<jupyter_code>farmer_id = "contoso_farmer"
try:
    farmer = fb_client.farmers.get(farmer_id=farmer_id)
    print(f"Farmer with id '{farmer_id}' exists.")
except ResourceNotFoundError:        
    print(f"Farmer with id '{farmer_id}' doesn't exist. Creating ... ", end="", flush=True)
    farmer = fb_client.farmers.create_or_update(
        farmer_id=farmer_id,
        farmer=Farmer()
    )
    print(f"Farmer with id '{farmer_id}' created.")<jupyter_output><empty_output><jupyter_text>Create Boundary<jupyter_code>boundary_id = "contoso_boundary"
try:
    boundary_obj = fb_client.boundaries.get(
                farmer_id=farmer_id,
                boundary_id=boundary_id
            )

    print(f"Boundary with id '{boundary_id}' exists", end="\n")
            
except ResourceNotFoundError: 
    print(f"Creating boundary with id '{boundary_id}'... ", end="")
    boundary_obj = fb_client.boundaries.create_or_update(
        farmer_id=farmer_id,
        boundary_id=boundary_id,
        boundary=Boundary(
            description="Created by SDK",
            geometry=Polygon(
                 coordinates=[
                    [
                        [79.27057921886444, 18.042507660177698],
                        [79.26899135112762, 18.040135849620704],
                        [79.27113711833954, 18.03927382882835],
                        [79.27248358726501, 18.041069275656195],
                        [79.27057921886444, 18.042507660177698]
                    ]
                ]
            )
        )
    )

    print(f"Boundary with id '{boundary_id}' created")

except Exception as e:
    print(e)<jupyter_output><empty_output><jupyter_text>Satellite and Weather Jobs<jupyter_code>RUN_ID = uuid.uuid1()
# Start and End data for Satellite and Weather data to be pulled
start_dt = datetime.strptime("01-01-2021", "%d-%m-%Y")
end_dt = datetime.strptime("30-04-2021", "%d-%m-%Y")
job_id = "s-job" + str(RUN_ID)

# Submit Satellite Job
try:
    print(f"Queuing satellite job for boudary '{boundary_obj.id}'. ", end="", flush=True)
    satellite_job = fb_client.scenes.begin_create_satellite_data_ingestion_job(
        job_id=job_id,
        job=SatelliteDataIngestionJob(
            farmer_id=boundary_obj.farmer_id,
            boundary_id=boundary_obj.id,
            start_date_time=start_dt,
            end_date_time=end_dt,
            data=SatelliteData(
                image_names=[
                    # "B01",
                    # "B02",
                    # "B03",
                    # "B04",
                    "NDVI"
                ]
            )
        ),
        polling=True
    )
    print(f"Submitted satellite job '{job_id}'.")

except HttpResponseError as e:
    print(e.response.body())
    raise
# Weather API inputs
extension_id = farmbeats_config["weather_provider_extension_id"]
extension_data_provider_api_key = farmbeats_config["weather_provider_key"]
extension_data_provider_app_id = farmbeats_config["weather_provider_id"]
extension_api_name = "dailyhistorical"
job_id = "w-hist" + str(RUN_ID)
st_unix = int(start_dt.timestamp())
ed_unix = int(end_dt.timestamp())
try:
    print(f"Queuing weather job for boudary '{boundary_obj.id}'. ", end="", flush=True)
    weather_job = fb_client.weather.begin_create_data_ingestion_job(
        job_id=job_id,
        job=WeatherDataIngestionJob(
            farmer_id=boundary_obj.farmer_id,
            boundary_id=boundary_obj.id,
            extension_id=extension_id, 
            extension_api_name=extension_api_name, 
            extension_api_input={"start": st_unix, "end": ed_unix},
            extension_data_provider_api_key=extension_data_provider_api_key,
            extension_data_provider_app_id=extension_data_provider_app_id
        ),
        polling=True
    )
    print(f"Submitted weather job '{job_id}'.")
except HttpResponseError as e:
    print(e.response.body())
    raise<jupyter_output><empty_output><jupyter_text>Check status of satellite and weather jobs<jupyter_code>print("Waiting for jobs to complete")

# Print job id and status after succeeded

print(f"Waiting for job '{satellite_job.result().as_dict()['id']}'.")
print(f"Satellite job '{satellite_job.result().as_dict()['id']}' {satellite_job.status()}.")


print(f"Waiting for job '{weather_job.result().as_dict()['id']}'.")
print(f"Weather job '{weather_job.result().as_dict()['id']}' {weather_job.status()}.")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10235219326128417, 'fraction_numerical': 0.03003814367450731, 'mean_word_length': 3.313228238519534, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '211164', 'license': 'MIT', 'n_tokens_mistral': 2174, 'n_tokens_neox': 1976, 'n_words': 352, 'path': 'quick_start/quick_start.ipynb', 'repo_name': 'bkuriach/azure-farmbeats-samples', 'size': '6292'}