Skip to content

Instantly share code, notes, and snippets.

@lesserwhirls
Created April 25, 2017 22:14
Show Gist options
  • Save lesserwhirls/065252e2c8ae1658da8c25e559f3f0d0 to your computer and use it in GitHub Desktop.
Save lesserwhirls/065252e2c8ae1658da8c25e559f3f0d0 to your computer and use it in GitHub Desktop.
Find the Index OPeNDAP style
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"import datetime as dt\n",
"\n",
"from netCDF4 import Dataset, date2index, num2date"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# simple function to find the index associated with a value in an array\n",
"# many assumptions in this function, like assuming there is only one index associated\n",
"# with a given value, for example.\n",
"def find_index(array, value):\n",
" norm = np.abs(array-value)\n",
" return np.where(norm == norm.min())[0]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# open the remote dataset via OPeNDAP\n",
"dataset = Dataset('http://tds.hycom.org/thredds/dodsC/GLBu0.08/reanalysis/3hrly', 'r')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# define your desired lat/lon and time range values here\n",
"wanted_lat = 26.1\n",
"wanted_lon = -86.4\n",
"start_date = dt.datetime(2011, 1, 1)\n",
"end_date = dt.datetime(2011,1, 31, 23, 59, 59)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# get all of the lat, lon, and time data\n",
"lat = dataset['lat'][:]\n",
"lon = dataset['lon'][:]\n",
"time_var = dataset['time']\n",
"#time = dataset['time'][:]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# find the index values coresponding to the desired lat/lon values\n",
"lat_index = find_index(lat, wanted_lat)\n",
"lon_index = find_index(lon, wanted_lon)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 26.07999992]\n",
"[-86.40002441]\n"
]
}
],
"source": [
"# check lat and lon index to see if correct, or close\n",
"print(lat[lat_index])\n",
"print(lon[lon_index])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# find the start and end index values that corespond to the \n",
"# start_time and end_time datetime objects\n",
"# see http://unidata.github.io/netcdf4-python/#netCDF4.date2index for info on this magic\n",
"# start_time_index will be exact match or just after the start_date\n",
"start_time_index = date2index(start_date, time_var, select='after')\n",
"# end_time_index will be exact match or just before the end_date\n",
"end_time_index = date2index(end_date, time_var, select='before')\n",
"time_index_range = list(range(start_time_index,end_time_index + 1))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2011-01-01 00:00:00\n",
"2011-01-31 21:00:00\n"
]
}
],
"source": [
"# check start and end time index to see if correct, or close\n",
"print(num2date(time_var[start_time_index], units=time_var.units, calendar=time_var.calendar))\n",
"print(num2date(time_var[end_time_index], units=time_var.units, calendar=time_var.calendar))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# get water_temp data for the given lat/lon and time range\n",
"data = dataset['water_temp'][time_index_range, 0, lat_index, lon_index]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(248, 1, 1)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# data array shape is times, single lat, single lon\n",
"data.shape"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24.8798\n"
]
}
],
"source": [
"# compute the mean of the time series at point lat/lon\n",
"print(data.mean())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment