Skip to content

Instantly share code, notes, and snippets.

@j08lue
Last active April 25, 2016 11:06
Show Gist options
  • Save j08lue/d87c3fe5482f8e63627c76e891e9d752 to your computer and use it in GitHub Desktop.
Save j08lue/d87c3fe5482f8e63627c76e891e9d752 to your computer and use it in GitHub Desktop.
scipy io netcdf issue with NaN written to integer variable
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"from scipy.io import netcdf\n",
"import os.path\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'0.17.0'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import scipy\n",
"scipy.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"rootdir = os.path.join(os.path.expanduser('~'), 'Downloads')\n",
"fname = os.path.join(rootdir, 'scipy_nan_test.nc')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make some sample data in a masked array"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"masked_array(data = [0.0 1.0 2.0 3.0 4.0 5.0 -- -- -- --],\n",
" mask = [False False False False False False True True True True],\n",
" fill_value = 1e+20)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = np.ma.array(np.arange(10), float)\n",
"data[data > 5] = np.ma.masked\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 1., 2., 3., 4., 5., nan, nan, nan, nan])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.filled(np.nan)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write sample data to netCDF file as floats and integers, filled with `np.nan` or `_FillValue`"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"float filled with np.nan: \n",
" [ 0. 1. 2. 3. 4. 5. nan nan nan nan]\n",
"\n",
"int filled with np.nan: \n",
" [ 0 1 2 3 4 5\n",
" -2147483648 -2147483648 -2147483648 -2147483648]\n",
"\n",
"int written as masked array: \n",
" [0 1 2 3 4 5 6 7 8 9]\n",
"\n",
"int filled with _FillValue: \n",
" [ 0 1 2 3 4 5\n",
" -2147483648 -2147483648 -2147483648 -2147483648]\n"
]
}
],
"source": [
"with netcdf.netcdf_file(fname, 'w') as ds:\n",
" ds.createDimension('time', 10)\n",
" time = ds.createVariable('time', 'i', ('time',))\n",
" time[:] = np.arange(10)\n",
" time.units = 'days since 2008-01-01'\n",
" \n",
" floatvar = ds.createVariable('data_float', 'f', ('time',))\n",
" floatvar[:] = data.filled(np.nan)\n",
" print('\\nfloat filled with np.nan: \\n', floatvar[:])\n",
" \n",
" intvar_masked = ds.createVariable('data_int_masked', 'i', ('time',))\n",
" intvar_masked[:] = data\n",
" print('\\nint written as masked array: \\n', intvar_masked[:])\n",
" \n",
" intvar = ds.createVariable('data_int', 'i', ('time',))\n",
" intvar[:] = data.filled(np.nan)\n",
" print('\\nint filled with np.nan: \\n', intvar[:])\n",
" \n",
" intvar_fill = ds.createVariable('data_int_fill', 'i', ('time',))\n",
" intvar_fill._FillValue = 1e30\n",
" intvar_fill[:] = data.filled(intvar_fill._FillValue)\n",
" print('\\nint filled with _FillValue: \\n', intvar_fill[:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: Both when filled with `np.nan` and `_FillValue`, missing values are written to the netCDF file as the integer value of `np.nan`."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment