# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import numpy as np
import pandas as pd
import scipy
from datetime import datetime, timedelta

## For SDK
import getpass
from odp_sdk import ODPClient
from getpass import getpass

import warnings
warnings.filterwarnings("ignore")
# -

# # Please do the tests to the best of your ability and fill in the google form for feedback on each test
#
#

# # If you encounter any problems, please see code at the end of the notebook for help, or feel free to reach out to either:
# <br>
# <NAME>: <EMAIL> or <NAME>: <EMAIL>

# # Test 3
# Connect to ODP using SDK
# Please see Basic Usage example notebook in GitHub for help
# <br>
# **Hint**: Check the Basic Usage Example notebook for help. You should see ' connection successful' once you connect.
#

# +
## Connect to SDK here 
# -

# # Test 4
# Using the cast function, download data from:
# <br>
# longitude=[-10,10],
# <br>
# latitude=[55,70],
# <br>
# timespan=['2018-06-01','2018-08-31']
# <br>
# Let's exclude flagged data.
# <br>
# **Hint**: Please see the Basic Usage example notebook for help.

# +
## Download casts here and display first five lines of output
# -

# # Test 5
# Using the get_available_casts function, download casts and cast information for:
# <br>
# longitude=[-10,10],
# <br>
# latitude=[55,70],
# <br>
# timespan=['2018-06-01','2018-08-31']
# <br>
#
# Filter the casts for the ones which have 'NORWAY' as the associated country.
# <br>
#
# Download the data only for those specific casts
# <br>
#
# **Hint**: Please see the Basic Usage example notebook for help.

# +
## Get casts here and display first five lines of output

# +
## Filter the casts for 'NORWAY' as the associated country

# +
## Download data only for those specific casts
# -

# # Test 6
# Clone the repo in order to use the extra functionalities. Under the 'Example's' folder follow instructions for intalling Cartopy and then pip install the packages in requirements_func.txt
# <br>
# Then import UtilityFunctions
# <br>
# **Hint**: See the README under the Examples folder
# <br>
# Remember that you need to import the function files and the necessary packages

# +
#import extra packages and functions here

from UtilityFunctions import *

## If you are not running your notebook from the same folder where UtilityFunctions is, remember to point to the proper path
# -

# # Test 7
# Using the plot_casts function in UtilityFunctions.py, visualize the individual casts with associated Temperature values retrieved in Test 4 **for depths above 5 meters**. Remember that you can specify the colormap from cmocean (a color palette for plotting ocean data)
# <br>
# **Hint:** See Data Coverage Example notebook

# +
#visualize casts here
# -

# # Test 8
#
# Interpolate the data retrieved in Test 4 to get continuous Temperature map at 5m depth for the date '2018-07-14'. 
# **Hint:** Different algorithms can be used for this portion. Please see the Example Notebook Gridding Data from Ocean Data Platform. Use interpolate_casts_to_z() to interpolate all profiles to a depth of 5m and use interpolate_to_grid() for the spatial gridding

# +
##Interpolate and plot results here
# -

# # Help With Code Below

# # Test 3
# Connect to ODP using SDK
# Please see Basic Usage example notebook in GitHub for help
# <br>
# **Hint**: Check the Basic Usage Example notebook for help. You should see ' connection successful' once you connect.
#

## This code will prompt you to enter your API key to connect
client = ODPClient(api_key=getpass(prompt='Insert your personal ODP API key:'), 
                       project="odp", client_name="odp")

# # Test 4
# Using the cast function, download data from:
# <br>
# longitude=[-10,10],
# <br>
# latitude=[55,70],
# <br>
# timespan=['2018-06-01','2018-08-31']
# <br>
# Let's exclude flagged data
# <br>
# **Hint**: Please see the Basic Usage example notebook for help

# +
df=client.casts(longitude=[0,15],
                latitude=[50,65], 
                timespan=['2018-06-01','2018-08-31'],
                include_flagged_data = False,
                n_threads=35)

df.head()
# -

# # Test 5
# Using the get_available_casts function, download casts and cast information for:
# <br>
# longitude=[-10,10],
# <br>
# latitude=[55,70],
# <br>
# timespan=['2018-06-01','2018-08-31']
# <br>
#
# Filter the casts for the ones which have 'NORWAY' as the associated country.
# <br>
#
# Download the data only for those specific casts
# <br>
#
# **Hint**: Please see the Basic Usage example notebook for help.

# +
casts=client.get_available_casts(longitude=[0,15],
                                latitude=[50,65],
                                timespan=['2018-06-01','2018-08-31'],
                                meta_parameters=['extId','date','time','lon','lat','country',
                                                 'Platform','dataset_code', 'equipment'])

casts.head()
# -

casts_norway = casts[casts.country=='NORWAY']
casts_norway.head()

# +
data_norway = client.download_data_from_casts(casts_norway.extId.tolist(),n_threads=35)

data_norway.head()
# -

# # Test 6
# Clone the repo in order to use the extra functionalities. Under the 'Example's' folder follow instructions for installing Cartopy and then pip install the packages in requirements_func.txt
# <br>
# Then import UtilityFunctions
# <br>
# **Hint**: See the README under the Examples folder
# <br>
# Remember that you need to import the function files and the necessary packages

# +
## Once the repo is cloned, you need to import the following packages and functions. 
#If you get an error for the imports of Cast Functions and DataStatsFunctions, make sure they are in the same
# folder from which you are running your notebook

from UtilityFunctions import *

# -

# # Test 7
# Using the plot_casts function in UtilityFunctions.py, visualize the individual casts with associated Temperature values retrieved in Test 4 **for depths above 5 meters**. Remember that you can specify the colormap from cmocean (a color palette for plotting ocean data)
# <br>
# **Hint:** See Data Coverage Example notebook

plot_casts('Temperature',df[df.z<5],cmap=cmocean.cm.thermal)
plt.title('Temperature Measurements')

# # Test 8
#
# Interpolate the data retrieved in Test 4 to get continuous Temperature map at 5m depth for the date '2018-07-14'. 
# **Hint:** Different algorithms can be used for this portion. Please see the Example Notebook Gridding Data from Ocean Data Platform example notebook. Use interpolate_casts_to_z() to interpolate all profiles to a depth of 5m and use interpolate_to_grid() for the spatial gridding

df_int=interpolate_casts_to_z(df,'Temperature',[5]) # Interpolate all profiles to 5m depth. OBS: This one may take a long time

# +
##need to drop null values
df_int.dropna(inplace=True)

longitude=[0,15] # longitude gridding range
latitude=[50,65] # latitude gridding range

df_int['unixtime']=df_int['datetime'].apply(lambda x : x.value) 

points=df_int[['lon','lat','unixtime']].values.astype('float')
values=df_int['Temperature'].values .astype('float')

# Interpolating to a grid for the date '2018-07-14'
int_points=[np.linspace(longitude[0],longitude[1],15*4+1),
            np.linspace(latitude[0],latitude[1],15*4+1),
            [pd.Timestamp('2018-07-14').value]]
            


# +
## specify interpolation algorithm
kind='rbf'

##create grid and interpolate values
grid,g=interpolate_to_grid(points.copy(),values.copy(),int_points.copy(), interp_type=kind,
                               rbf_func='linear',rbf_smooth=1e-9,rescale=True)
## plot interpolated values
plot_grid(grid[0][:,:,0],grid[1][:,:,0],g[:,:,0],cmap=cmocean.cm.thermal,vrange=[None,None], variable_name='Temperature (degrees_C)')
plt.title('RBF Interpolation')
# -







StarcoderdataJupyterScriptsDedupFiltered