Friday, February 13, 2015
Reading binary data NSIDC Sea Ice Concentrations into GeoTIFF raster with Python
The blog entry, which helped med finding out how to do this is this one but it had to be modified for my files.
Part One uses the Python struct command to unpack he binary file into a tuple
import struct, numpy, gdalThe "unpack_from" considers the offset of the header bytes and the tuple "z" contains now all the values in one long row of values -- this one-dimensional string of values is converted into an array and the reshaped according to the dimensions of the raster. Now I have my values in a two-dimensional array, the next part of the code writes it into a GeoTIFF file:
#Dimensions from https://nsidc.org/data/docs/daac/nsidc0051_gsfc_seaice.gd.html
height = 448
width = 304
#for this code, inspiration found at https://stevendkay.wordpress.com/category/python/
icefile = open(r"U:SSMI
t_20120101_f17_v01_n.bin", "rb")
contents = icefile.read()
icefile.close()
# unpack binary data into a flat tuple z
s="%dB" % (int(width*height),)
z=struct.unpack_from(s, contents, offset = 300)
nsidc = numpy.array(z).reshape((448,304))
nsidc = numpy.rot90(nsidc, 1)
#write the data to a GeotiffThis then is the resulting and imported image:
driver = gdal.GetDriverByName("GTiff")
outraster = driver.Create(C:UsersmaxDesktop\test4.tif, height, width, 1, gdal.GDT_Int16 )
raster = numpy.zeros((width, height), numpy.float)
outraster.GetRasterBand(1).WriteArray( raster )
outband = outraster.GetRasterBand(1)
#Write to file
outband.WriteArray(nsidc)
outband.FlushCache()
#Clear arrays and close files
outband = None
iceraster = None
outraster = None
outarray = None
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.