From e9c0db5ae2254b78473f726a37359a59bf5093d0 Mon Sep 17 00:00:00 2001 From: rebeccaringuette <49281118+rebeccaringuette@users.noreply.github.com> Date: Thu, 30 Mar 2023 19:51:21 -0400 Subject: [PATCH 1/3] Adding a sample script for kamodo-ccmc --- scripts/python-kamodo-ccmc.py | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 scripts/python-kamodo-ccmc.py diff --git a/scripts/python-kamodo-ccmc.py b/scripts/python-kamodo-ccmc.py new file mode 100644 index 0000000..9b8bbf2 --- /dev/null +++ b/scripts/python-kamodo-ccmc.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +""" +Created on Thu Mar 30 19:22:55 2023 + +@author: rringuet +""" + +# Python 2.7 and 3 compatible +# Report software bugs/issues/feature requests at +# https://github.com/hapi-server/client-python/issues +# Report data server issues to CONTACT + +# Install latest hapiclient package from https://pypi.org/project/hapiclient/ +# Only needs to be executed once. +import os; print(os.popen('pip install hapiclient --upgrade').read()) + +from hapiclient import hapi + +server = 'SERVER' +dataset = 'DATASET' +# Notes: +# 1. Use parameters='' to request all parameters from DATASET. +# 2. Multiple parameters can be requested using a comma-separated +# listCSV_EXAMPLE +parameters = 'PARAMETERS' +start = 'START' # min STARTMIN +stop = 'STOP' # max STOPMAX + +data, meta = hapi(server, dataset, parameters, start, stop) + +# See note below to install kamodo-ccmc. kamodo-ccmc requires Python 3. +from kamodo_ccmc.tools.functionalize_hapi import functionalize_hapi +kamodo_object = functionalize_hapi(data, meta) +kamodo_object # prints the LaTeX representation of all variables in data + +# To plot a variable, retrieve the list of variables names, then plot the first +# variable in the list. You can replace the var_list[0] with any variable name. +# The colors and scales of the plot can easily be changed. See link below. +from kamodo_ccmc.tools.functionalize_hapi import varlist +var_list = varlist(meta) # retrieve the variable list +kamodo_object.plot(var_list[0]) # plot the first variable + +# To interpolate between time values, create one or more utc timestamps +# and imitate the syntax below. sample_datetime can one timestamp or an array +# of timestamps. The times must be between start and stop from above. +from datetime import datetime, timezone +sample_datetime = datetime(2011, 3, 27, 23, tzinfo=timezone.utc).timestamp() +kamodo_object[var_list[0]](UTC_time=sample_datetime) +# Users can also define their own custom interpolation methods. + +# For more details on what is possible with this function in kamodo-ccmc, +# please visit https://nasa.github.io/Kamodo/notebooks/FunctionalizeHAPI.html + +# To install kamodo-ccmc, follow the instructions at +# https://nasa.github.io/Kamodo/notebooks/QuickStart.html +# If you encounter problems, please contact either Darren De Zeeuw +# https://ccmc.gsfc.nasa.gov/staff/darren-de-zeeuw/ +# or Lutz Rastaetter +# https://ccmc.gsfc.nasa.gov/staff/lutz-rastaetter/ From 6c8efe975fce04f4f9f5647fac88b01ecac17ad2 Mon Sep 17 00:00:00 2001 From: rebeccaringuette <49281118+rebeccaringuette@users.noreply.github.com> Date: Fri, 31 Mar 2023 11:48:30 -0400 Subject: [PATCH 2/3] using hapitime instead --- scripts/python-kamodo-ccmc.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/scripts/python-kamodo-ccmc.py b/scripts/python-kamodo-ccmc.py index 9b8bbf2..0e20857 100644 --- a/scripts/python-kamodo-ccmc.py +++ b/scripts/python-kamodo-ccmc.py @@ -14,7 +14,7 @@ # Only needs to be executed once. import os; print(os.popen('pip install hapiclient --upgrade').read()) -from hapiclient import hapi +from hapiclient import hapi, hapitime2datetime server = 'SERVER' dataset = 'DATASET' @@ -29,23 +29,30 @@ data, meta = hapi(server, dataset, parameters, start, stop) # See note below to install kamodo-ccmc. kamodo-ccmc requires Python 3. +# The commands below assume the user is in a notebook. Interpolation with +# Kamodo works fine in an interactive Python environment, but the kamodo_object +# and the plots generated below may not display properly unless in a notebook. from kamodo_ccmc.tools.functionalize_hapi import functionalize_hapi kamodo_object = functionalize_hapi(data, meta) kamodo_object # prints the LaTeX representation of all variables in data # To plot a variable, retrieve the list of variables names, then plot the first -# variable in the list. You can replace the var_list[0] with any variable name. -# The colors and scales of the plot can easily be changed. See link below. +# variable in the list. You can replace the var_list[0] with any variable name +# from parameters. The colors and scales of the plot can easily be changed. +# See link below. from kamodo_ccmc.tools.functionalize_hapi import varlist var_list = varlist(meta) # retrieve the variable list kamodo_object.plot(var_list[0]) # plot the first variable # To interpolate between time values, create one or more utc timestamps -# and imitate the syntax below. sample_datetime can one timestamp or an array -# of timestamps. The times must be between start and stop from above. -from datetime import datetime, timezone -sample_datetime = datetime(2011, 3, 27, 23, tzinfo=timezone.utc).timestamp() -kamodo_object[var_list[0]](UTC_time=sample_datetime) +# and imitate the syntax on the last line below. The variable sample_datetime +# can be one timestamp or an array of timestamps, but the times must be between +# the start and stop times from above or NaNs will be returned. +from numpy import mean +start_ts = hapitime2datetime(start)[0].timestamp() # convert to UTC timestamp +stop_ts = hapitime2datetime(stop)[0].timestamp() +sample_datetime = mean([start_ts, stop_ts]) # pick the mean as an example +kamodo_object[var_list[0]](UTC_time=sample_datetime) # interpolate # Users can also define their own custom interpolation methods. # For more details on what is possible with this function in kamodo-ccmc, From 7f568dabb34d96d77be887b12944fe57e22260b3 Mon Sep 17 00:00:00 2001 From: rebeccaringuette <49281118+rebeccaringuette@users.noreply.github.com> Date: Fri, 31 Mar 2023 11:49:37 -0400 Subject: [PATCH 3/3] Update python-kamodo-ccmc.py --- scripts/python-kamodo-ccmc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/python-kamodo-ccmc.py b/scripts/python-kamodo-ccmc.py index 0e20857..0927053 100644 --- a/scripts/python-kamodo-ccmc.py +++ b/scripts/python-kamodo-ccmc.py @@ -45,14 +45,14 @@ kamodo_object.plot(var_list[0]) # plot the first variable # To interpolate between time values, create one or more utc timestamps -# and imitate the syntax on the last line below. The variable sample_datetime +# and imitate the syntax on the last line below. The variable sample_timestamp # can be one timestamp or an array of timestamps, but the times must be between # the start and stop times from above or NaNs will be returned. from numpy import mean start_ts = hapitime2datetime(start)[0].timestamp() # convert to UTC timestamp stop_ts = hapitime2datetime(stop)[0].timestamp() -sample_datetime = mean([start_ts, stop_ts]) # pick the mean as an example -kamodo_object[var_list[0]](UTC_time=sample_datetime) # interpolate +sample_timestamp = mean([start_ts, stop_ts]) # pick the mean as an example +kamodo_object[var_list[0]](UTC_time=sample_timestamp) # interpolate # Users can also define their own custom interpolation methods. # For more details on what is possible with this function in kamodo-ccmc,