How to convert grid file to text file?

Hi there.
Can you help me on how to convert this type of grid file into text file (lat, lon, amp, phase) for each column.
The grid file is attached

 Réponse acceptée

Walter Roberson
Walter Roberson le 28 Juin 2020
fid = fopen('Example.txt');
data = cell2mat(textscan(fid, '', 'HeaderLines', 5));
fclose(fid);
amps = data(1:2:end,:);
phases = data(2:2:end,:);
[nr, nc] = size(amps);
longs = linspace(0, 360, nc+1);
longs(end) = [];
lats = linspace(-90, 90, nr+1);
lats(end) = [];
subplot(2,1,1);
surf(longs, lats, amps);
xlabel('long')
ylabel('lat')
title('amplitudes')
subplot(2,1,2)
surf(longs, lats, phases)
xlabel('long')
ylabel('lat')
title('phases')

9 commentaires

hanif hamden
hanif hamden le 28 Juin 2020
Thank you Sir. one more question, how i would like to extract the lat long from the surf? I mean i want to save it in text file lat(column1), lon(column2),amp(column3),phase(column4).
hanif hamden
hanif hamden le 28 Juin 2020
Hello sir, i just realized that something not right with the linspace code. From the file attached.
header 1 = 0.0 360.0 (latitude)
header 2 = -90.000 90.000 (longitude)
header 3 = 0.125 0.125 (gridsize)
header 4 = 2881 1441 <- represent [row column]
header 5 = 999.9 999.9 <- represent NaN
Therefore, I am not good in converting this type of grid files into text file format as below:
lat(col1) lon(col2) amp(col3) phase(col4)
I was hoping that you could help me to solve this matter. Thank you.
Walter Roberson
Walter Roberson le 28 Juin 2020
Latitude is never measured outside +/- 90. I think you are misreading the header.
The data you posted decidedly does not have anywhere near 1441 rows or columns.
1440 spaced 0.125 apart would give a span of 180 which would match the -90 to 90, and the 360 implied by the header would be 2880. However those have no relation to the 20 lines by 30 columns that were posted.
1441 and 2881 imply that the last point wraps around to be the same as the first. An examination of the data shows that is not the case.
If you are not going to post data that is representative of your file then you need to learn how to adapt the code that handled what you did provide.
hanif hamden
hanif hamden le 28 Juin 2020
I am sorry Sir, it should be header 1 <- longitude while header 2 <-latitude. However, the file is too large to attach here sir. The format for this file could be referred at this link:
Walter Roberson
Walter Roberson le 29 Juin 2020
Modifié(e) : Walter Roberson le 29 Juin 2020
https://github.com/USNavalResearchLaboratory/TrackerComponentLibrary looks like it might have tools for fes2004 format
hanif hamden
hanif hamden le 29 Juin 2020
Thanks for sharing the link Sir. However, the FES2004 coefficient in that link is different from format I that I've been provided previously.
filename = 'O1DTU10.asc';
fid = fopen(filename);
L = cell(5,1);
for K = 1 : 5; L{K} = fgetl(fid); end
longrange = sscanf(L{1}, '%f');
latrange = sscanf(L{2}, '%f');
%spacing = sscanf(L{3}, '%f');
widths = sscanf(L{4}, '%f');
indicates_missing = sscanf(L{5}, '%f');
per_line = 30;
partwidth = widths(1);
nparts = widths(2);
parts = cell(nparts,1);
colgroups = floor(partwidth / per_line);
leftover = partwidth - colgroups * per_line;
failed = false;
for P = 1 : nparts
data1 = fscanf(fid, '%f', [per_line 2*colgroups]);
if isempty(data1); failed = true; break; end
data1 = reshape(data1, [per_line 2 colgroups]);
data2 = fscanf(fid, '%f', [leftover 2]);
data = [reshape(permute(data1, [1 3 2]), [], 2); data2];
parts{P} = data;
end
if failed
fprintf('Only got %d of %d parts successfully\n', P, npars);
end
if ~failed
L = fgetl(fid);
if ischar(L) && ~isempty(L)
fprintf('Not empty at apparent EOF, content is:\n%s\n', L);
failed = true;
end
end
if ~failed
lats = linspace(latrange(1), latrange(2), widths(2));
longs = linspace(longrange(1), longrange(2), widths(1));
data = permute(cat(3, parts{:}),[1 3 2]);
amps = data(:,:,1) .';
amps(amps == indicates_missing(1)) = nan;
phases = data(:,:,2) .';
phases(phases == indicates_missing(2)) = nan;
subplot(2,1,1);
surf(longs, lats, amps, 'edgecolor', 'none');
xlim(longrange);
ylim(latrange);
xlabel('long'); ylabel('lat'); zlabel('amp');
title('amplitudes');
subplot(2,1,2);
surf(longs, lats, phases, 'edgecolor', 'none');
xlim(longrange);
ylim(latrange);
xlabel('long'); ylabel('lat'); zlabel('phase');
title('phases');
fprintf('Data available; see lats, longs, amps, phases\n');
end
fclose(fid);
hanif hamden
hanif hamden le 29 Juin 2020
Thank you so much sir!! :)
hanif hamden
hanif hamden le 5 Juil 2020
Modifié(e) : Walter Roberson le 5 Juil 2020

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation of 2-D Selections in 3-D Grids dans Centre d'aide et File Exchange

Produits

Version

R2019a

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by