import xarray as xr
import numpy as np
from IPython.display import display, JSON
from datetime import datetime, timedelta, time
import os
# highlight the harmony-py library
from harmony import BBox, Client, Collection, Request, Environment, LinkType
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
%matplotlib inline
# Additional libraries compared to the first tutorial.
from urllib import request
import json
import requests
import sys
import shutil
from urllib.parse import urlencode
03. Build a Time Series of SWOT Simulated Data
Import libraries
In additional to libraries from the first tutorial, also import libraries needed to submit CMR requests.
Let’s start up the client from the harmony-py library and define the CMR url.
= Client(env=Environment.PROD)
harmony_client = 'cmr.earthdata.nasa.gov' cmr_root
KaRIn CalVal
Search by cycle and pass using CMR
CMR Search: Number of item returned is limited to 10,000 (or 1 million if targeting collections) https://cmr.earthdata.nasa.gov/search/site/docs/search/api.html#paging-details
= 'POCLOUD'
provider = 'SWOT_SIMULATED_L2_KARIN_SSH_GLORYS_CALVAL_V1'
short_name
= list(range(1,101))
target_cycle = [2, 17]
target_pass = 2000
page_size
= []
granuleIDs = [
params_without_cycle 'page_size', page_size),
('sort_key', "-start_date"),
('provider', provider),
('ShortName', short_name),
(#('collection_concept_id', ccid),
#('token', token),
#('cycle', targetcycle), # can only specify one cycle at a time when specifying passes
'passes[0][pass]', target_pass[0]),
('passes[1][pass]', target_pass[1]),
(
]
for v in target_cycle:
= [("cycle[]", v)]
params
params.extend(params_without_cycle)# print(params)
= urlencode(params)
query = f"https://{cmr_root}/search/granules.umm_json?{query}"
cmr_url # print(cmr_url)
= requests.get(cmr_url)
response = response.json()
response_body
for itm in response_body['items']:
'meta']['concept-id'])
granuleIDs.append(itm[
len(granuleIDs) # Note the 200-granule limit
Then perform a spatial subset and download using Harmony
On the back end, the subsetting part of Harmony is powered by L2SS-py
# collection = Collection(id=ccid)
= Collection(id=short_name)
collection
# start_day = datetime(2015,4,15,0,0,0)
# end_day = datetime(2015,4,17,0,0,0)
= Request(
request =collection,
collection=BBox(-140, 20, -100, 50), # [20-50N], [-140W, -100W] CA Current
spatial#variables=[],
# temporal={
# 'start': start_day,
# 'stop': end_day # goal: try up to 21 days at least,
#},
=granuleIDs,
granule_id
)
request.is_valid()
print(harmony_client.request_as_curl(request))
= harmony_client.submit(request)
job_id print(f'Job ID: {job_id}')
harmony_client.status(job_id)
# results = harmony_client.result_urls(job_id, link_type=LinkType.s3)
# urls = list(results)
# print(urls)
# create a new folder to put the subsetted data in
"swot_ocean",exist_ok = True) os.makedirs(
= harmony_client.download_all(job_id, directory='./swot_ocean', overwrite=True)
futures = [f.result() for f in futures]
file_names sorted(file_names)
= xr.open_mfdataset(sorted(file_names),combine='nested',concat_dim='num_lines')
ds ds
# Plot only a pair of passes at a time
= np.arange(0,1725*2)+1725*90
i_time
= plt.figure(figsize=[11,7])
fig = plt.axes(projection=ccrs.PlateCarree())
ax
ax.coastlines()-150, -90, 10, 60])
ax.set_extent([=1, c=ds.ssha_karin[i_time,:])
plt.scatter(ds.longitude[i_time,:], ds.latitude[i_time,:], lw='SSHA (m)')
plt.colorbar(label-1,1)
plt.clim( plt.show()