Merge branch 'fixf'
[wrf-fire-matlab.git] / detection / geotiff2mat.py
bloba4b9bd02eef3d01e5f483bdbd3abb8481a5f45bc
1 # convert geotiff file to matlab file & print basic info on screen too
2 # usage: python geotiff2mat.py filename.tif
3 # limitation: for now, one band only
5 # tested on Python 3.4
7 # arguments
8 import sys
10 file=sys.argv[1]
11 infile=file
12 outfile = file + '.mat'
14 # read the geotiff file
15 # see http://geoinformaticstutorial.blogspot.com/2012/09/reading-raster-data-with-python-and-gdal.html
17 import gdal
18 from gdalconst import *
19 print('Reading GeoTiff file',infile)
20 dataset = gdal.Open(infile, GA_ReadOnly)
21 cols = dataset.RasterXSize
22 rows = dataset.RasterYSize
23 bands = dataset.RasterCount
24 print('rows ',rows)
25 print('columns ',cols)
26 print('bands ',bands)
27 geotransform = dataset.GetGeoTransform()
28 print('top left X ',geotransform[0])
29 print('W-E pixel resolution',geotransform[1])
30 print('rotation, 0=North up',geotransform[2])
31 print('top left Y ',geotransform[3])
32 print('rotation, 0=North up',geotransform[4])
33 print('N-S pixel resolution',geotransform[5])
34 band = dataset.GetRasterBand(1)
35 data = band.ReadAsArray(0, 0, cols, rows)
37 # write matlab file
38 # using http://docs.scipy.org/doc/scipy-0.14.0/reference/tutorial/io.html
39 import scipy.io as sio
40 print('writing Matlab file ',outfile)
41 sio.savemat(outfile,{'data' : data,'geotransform' : geotransform})
43 exit()