# INSERT YOUR PROJECT HERE
PROJECT = 'your-project'

!gcloud auth login --project {PROJECT}

"""
Explanation: <table class="ee-notebook-buttons" align="left"><td>
<a target="_blank"  href="http://colab.research.google.com/github/google/earthengine-api/blob/master/python/examples/ipynb/Earth_Engine_REST_API_compute_table.ipynb">
    <img src="https://www.tensorflow.org/images/colab_logo_32px.png" /> Run in Google Colab</a>
</td><td>
<a target="_blank"  href="https://github.com/google/earthengine-api/blob/master/python/examples/ipynb/Earth_Engine_REST_API_compute_table.ipynb"><img width=32px src="https://www.tensorflow.org/images/GitHub-Mark-32px.png" /> View source on GitHub</a></td></table>

Table computations with the Earth Engine REST API
Note: The REST API contains new and advanced features that may not be suitable for all users.  If you are new to Earth Engine, please get started with the JavaScript guide.
The Earth Engine REST API quickstart shows how to access blocks of pixels from an Earth Engine asset.  The compute pixels example demonstrates how to apply a computation to the pixels before obtaining the result.  This example demonstrates getting the mean of pixels in each image of an ImageCollection in each feature of a FeatureCollection.  Specifically, this is a POST request to the computeFeatures endpoint.
Before you begin
Follow these instructions to:

Apply for Earth Engine
Create a Google Cloud project
Enable the Earth Engine API on the project
Create a service account
Give the service account project level permission to perform Earth Engine computations

Note: To complete this tutorial, you will need a service account that is registered for Earth Engine access.  See these instructions to register a service account before proceeding.
Authenticate to Google Cloud
The first thing to do is login so that you can make authenticated requests to Google Cloud.  You will set the project at the same time.  Follow the instructions in the output to complete the sign in.
End of explanation
"""


# INSERT YOUR SERVICE ACCOUNT HERE
SERVICE_ACCOUNT='your-service-account@your-project.iam.gserviceaccount.com'
KEY = 'key.json'

!gcloud iam service-accounts keys create {KEY} --iam-account {SERVICE_ACCOUNT}

"""
Explanation: Obtain a private key file for your service account
You should already have a service account registered to use Earth Engine.  If you don't, follow these instructions to get one.  Copy the email address of your service account into the following cell.  (The service account must already be registered to use Earth Engine).  In the following cell, the gsutil command line is used to generate a key file for the service account.  The key file will be created on the notebook VM.
End of explanation
"""


from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(KEY)
scoped_credentials = credentials.with_scopes(
    ['https://www.googleapis.com/auth/cloud-platform'])

session = AuthorizedSession(scoped_credentials)

url = 'https://earthengine.googleapis.com/v1beta/projects/earthengine-public/assets/LANDSAT'

response = session.get(url)

from pprint import pprint
import json
pprint(json.loads(response.content))

"""
Explanation: Start an AuthorizedSession and test your credentials
Test the private key by using it to get credentials.  Use the credentials to create an authorized session to make HTTP requests.  Make a GET request through the session to check that the credentials work.
End of explanation
"""


import ee

# Get some new credentials since the other ones are cloud scope.
ee_creds = ee.ServiceAccountCredentials(SERVICE_ACCOUNT, KEY)
ee.Initialize(ee_creds)

"""
Explanation: Serialize a computation
Before you can send a request to compute something, the computation needs to be put into the Earth Engine expression graph format.  The following demonstrates how to obtain the expression graph.
Authenticate to Earth Engine
Get Earth Engine scoped credentials from the service account.  Use them to initialize Earth Engine.
End of explanation
"""


# A collection of polygons.
states = ee.FeatureCollection('TIGER/2018/States')
maine = states.filter(ee.Filter.eq('NAME', 'Maine'))

# Imagery: NDVI vegetation index from MODIS.
band = 'NDVI'
images = ee.ImageCollection('MODIS/006/MOD13Q1').select(band)
image = images.first()

computation = image.reduceRegions(
  collection=maine, 
  reducer=ee.Reducer.mean().setOutputs([band]), 
  scale=image.projection().nominalScale()
)

# Print the value to test.
print(computation.first().get(band).getInfo())

"""
Explanation: Define a computation
Prototype a simple computation with the client API.  Note that the result of the computation is a FeatureCollection.
To check that the computation can succeed without errors, get a value from the first Feature (the mean NDVI in the polygon).
End of explanation
"""


# Serialize the computation.
serialized = ee.serializer.encode(computation)

"""
Explanation: Serialize the expression graph
This will create an object that represents the Earth Engine expression graph (specifically, an Expression).  In general, you should build these with one of the client APIs.
End of explanation
"""


import json

url = 'https://earthengine.googleapis.com/v1beta/projects/{}/table:computeFeatures'

response = session.post(
  url = url.format(PROJECT),
  data = json.dumps({'expression': serialized})
)

import json
pprint(json.loads(response.content))

"""
Explanation: Send the request
Make a POST request to the computeFeatures endpoint.  Note that the request contains the Expression, which is the serialized computation.
End of explanation
"""


