Need to read header in *.rgd file with python

Viewed 43

I need to read binary format. My file has *.grd format for Surfer. I need to reed data header.

My data are in this image

I used this code, but it's dosen't work.

import numpy as np

def read_grd(filename):
    with open(filename) as infile:
        ncols = int(infile.readline().split()[1])
        nrows = int(infile.readline().split()[1])
        xllcorner = float(infile.readline().split()[1])
        yllcorner = float(infile.readline().split()[1])
        cellsize = float(infile.readline().split()[1])
        nodata_value = int(infile.readline().split()[1])
        version = float(infile.readline().split()[1])
    longitude = xllcorner + cellsize * np.arange(ncols)
    latitude = yllcorner + cellsize * np.arange(nrows)
    value = np.loadtxt(filename, skiprows=7)

    return(longitude, latitude, value)

read_grd('filename.grd')

So, ehen I used this code I got a mistake: return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 1486: character maps to

1 Answers

All .grd file are not compatible, and I had the same issue. Try this code I've made to read the file :

import numpy as np
from osgeo import gdal

def transph_grd(file_path_grd):
    grdFile = gdal.Open(file_path_grd)
    value = grdFile.ReadAsArray()
    longitude = grdFile.GetGeoTransform()[0]
    latitude = grdFile.GetGeoTransform()[3]
    return (longitude, latitude, value)
Related