What is this script for?
My tutor gave me a .grd file and ask me to deal with that with MATLAB, so I have to rewrite it into another form that I could make use of that afterward.
So here is the code.
filename: readte.m
clear,clc;
%This script is to read grd files and convert it into (lon,lat,value) form
%in .txt file, filename is output.txt.
%.grd files should be from surfer.
%Keith Jin, ZJU, 21/08/02, ver. 1.0
[matrix xmin xmax ymin ymax]=grd_read_v2('out.grd');
file = fopen('output.txt','w');
countx = 0;
fprintf(file,"lon lat te\n");
for x = xmin:0.10034188034188:xmax
countx = countx+1;
county = 0;
for y = ymin:0.1:ymax
county = county+1;
fprintf(file,"%.4f %.4f %f\n",x,y,matrix(county,countx));
end %end y
end %end x
fclose('all');
and here’s another one grd read function, from
https://www.mathworks.com/matlabcentral/fileexchange/20880-surfer-grid-import-export?s_tid=FX_rc1_behav
function [matrix xmin xmax ymin ymax]=grd_read_v2(namefile)
% Function to read a GRD file
% (from Golden Software Surfer, ASCII format)
%
% [matrix xmin xmax ymin ymax]=grd_read_v2(name of file)
%
% Input:
% nomarch = name of the file to be read, including ".grd" extension
% Output:
% matrix = matrix of the read data
% xmin xmax ymin ymax = grid limits
%
% Coded by Alberto Avila Armella.
% UPDATED & IMPROVED BY Jose Maria Garcia-Valdecasas
grdfile=fopen(namefile,'r'); % Open file
code=fgetl(grdfile); % Reads surfer code 'DSAA'
% Grid dimensions (number of nodes)
aux=str2num(fgetl(grdfile)); nx=aux(1); ny=aux(2);
% X limits
aux=str2num(fgetl(grdfile)); xmin=aux(1); xmax=aux(2);
% Y limits
aux=str2num(fgetl(grdfile)); ymin=aux(1); ymax=aux(2);
% Z limits
aux=str2num(fgetl(grdfile)); zmin=aux(1); aux(2);
% Read matrix
[matrix,count] = fscanf(grdfile, '%f', [nx,ny]);
matrix=matrix'; % Trasposes matrix
fclose(grdfile);