Skip to content

Instantly share code, notes, and snippets.

@rutj3
Last active May 10, 2017 09:53
Show Gist options
  • Save rutj3/4faa43873ee6389873dd4de85de7c450 to your computer and use it in GitHub Desktop.
Save rutj3/4faa43873ee6389873dd4de85de7c450 to your computer and use it in GitHub Desktop.
GDAL blocksize
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import gdal\n",
"from tqdm import tqdm_notebook as tqdm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# NOAA GFS gribfile\n",
"ingrb = r'D:\\Data\\GFS_grib\\gfs.t00z.pgrb2.0p25.f015'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"ds = gdal.Open(ingrb)\n",
"b1 = ds.GetRasterBand(1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1440, 721)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Raster dimensions\n",
"xs, ys = b1.XSize, b1.YSize\n",
"xs, ys"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1440, 1)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Blocksize of the file\n",
"xbs, ybs = b1.GetBlockSize() # striped file layout, \"row-wise\"\n",
"xbs, ybs"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"gdal.SetCacheMax(0) # Dont cache in order to force disk I/O (not a realistic scenario)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loop, best of 3: 1.39 s per loop\n"
]
}
],
"source": [
"%%timeit\n",
"# read column by column\n",
"for colnum in range(xs): \n",
" data = b1.ReadAsArray(xoff=colnum, yoff=0, win_xsize=1, win_ysize=ys)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 3: 10.8 ms per loop\n"
]
}
],
"source": [
"%%timeit\n",
"# read row by row\n",
"for rownum in range(ys): \n",
" data = b1.ReadAsArray(xoff=0, yoff=rownum, win_xsize=xs, win_ysize=1)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ds = None # close the file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment