Conversion of 3d array to 2d array in text file
이전 댓글 표시
I have temperature data in text file. This text file consist on 3D array. Its complete description is as fellow
- The first row, longitude, contains 20 values
- The second row, latitude, contains 18 values
- The third row, StdPressureLev, contains 24 values
- From 4th row to onward its description is
Temperature_TqJ_A[x][y],value1, value2, …, valueN
- X ranges from 0 to 23 —— this is the StdPressureLev index(pressure level index) which are ranging from 1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1
- Y ranges from 0 to 17 —— this is the Latitude index( ranging from 37.5, 36.5, 35.5, 34.5, 33.5, 32.5, 31.5, 30.5, 29.5, 28.5, 27.5, 26.5, 25.5, 24.5, 23.5, 22.5, 21.5)
- The values for each row range from value1 to value 19 ——— these are the temperature values ordered by Longitude(ranging from 60.5, 61.5, 62.5, 63.5, 64.5, 65.5, 66.5, 67.5, 68.5, 69.5, 70.5, 71.5, 72.5, 73.5, 74.5, 75.5, 76.5, 77.5, 78.5) for a particular pressure and Latitude.Such form of 3D array repeated in a single text file!
My requirements: I want to form 24 different text files from this single text file, each ,based on pressure level(1-24 pressure level). Each text file in have 3 columns(first column consist on latitude, second consist on longitude and third column consist on temperature value at this lat, lon).
My Code With assistance of @Cedric Wannaz,once i prepared
pressures = [1000, 925, 850, 700, 600, 500, 400, 300, 250, 200, 150, 100, 70, 50, 30, 20, 15, 10, 7, 5, 3, 2, 1.5, 1] ;
% - Read source file.
fSpec = ['Temperature_TqJ_A[%f][%f]', repmat( ', %f', 1, 20 )] ;
data = textscan( fileread( 'Data1.txt' ), fSpec ) ;
% - Extract/gather all x and gp values.
X = data{1} ;
GP = horzcat( data{3:end} ) ;
% - Build arrays of lon/lat which correspond to GP.
[lon, lat] = meshgrid( 60:79, 22:39 ) ;
% - Iterate through pressure IDs and process.
for x = 0 : 23
% Get relevant block of GP (the one thta corresponds to current p).
gp = GP(X==x,:) ;
% Build 3 columns data array.
data = [reshape(lat.',[],1), reshape(lon.',[],1), reshape(gp.',[],1)].';
% Verbose.
fprintf( 'Export for pressure ID %d -> p=%.1fhpa.\n', x, pressures(x+1) ) ;
% Export.
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, 'latitude\tlongitude\tGP_value\r\n' ) ;
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
But now i am getting error, although its converting single text file(name data1)into 24 text files based on pressure level. But its first two column consist on randomly values of Lat, Lon while third column i do not sure either giving lat, or lon. I want to correct this in way, i describe above.
Text file have attached with this query. And a lot of thanks always for this assistance
Regards;
댓글 수: 4
Muhammad Usman Saleem
2016년 1월 16일
Walter Roberson
2016년 1월 17일
We thought you were going to show us the error message.
Muhammad Usman Saleem
2016년 1월 17일
Muhammad Usman Saleem
2016년 1월 18일
편집: Muhammad Usman Saleem
2016년 1월 18일
채택된 답변
추가 답변 (1개)
%%define some constants
filename = 'data1.txt';
NLongitude = 19;
NLatitude = 17;
NStdPressureLev = 24;
%%Process file
fid = fopen(filename);
line = fgets(fid); % first line is empty
% first read in values of Longitude, Latitude and StdPressureLev
line = fgets(fid); % should be Longitude
checktoken = 'Longitude';
assert(strcmp(line(1:length(checktoken)), checktoken), ...
[checktoken ' expected, but not found.'])
Longitude = sscanf(strrep(line(length(checktoken)+1:end), ',', ''), '%f');
assert(numel(Longitude)==NLongitude,...
['Wrong number of values for ' checktoken '.'])
line = fgets(fid); % should be Latitude
checktoken = 'Latitude';
assert(strcmp(line(1:length(checktoken)), checktoken), ...
[checktoken ' expected, but not found.'])
Latitude = sscanf(strrep(line(length(checktoken)+1:end), ',', ''), '%f');
assert(numel(Latitude) == NLatitude,...
['Wrong number of values for ' checktoken '.'])
line = fgets(fid); % should be StdPressureLev
checktoken = 'StdPressureLev';
assert(strcmp(line(1:length(checktoken)), checktoken), ...
[checktoken ' expected, but not found.'])
StdPressureLev = sscanf(strrep(line(length(checktoken)+1:end), ',', ''), '%f');
assert(numel(StdPressureLev) == NStdPressureLev,...
['Wrong number of values for ' checktoken '.'])
% read all numbers into a single 2D matrix of size
% (NStdPressureLev*NLatitude) x (2 + NLongitude)
format = ['Temperature_TqJ_A[%f][%f]', repmat(', %f', 1, NLongitude)];
T = cell2mat(textscan(fid, format, 'CollectOutput', true));
fclose(fid);
%%Reshape into 3D matrix NLatitude x NstdPressureLev x NLongitude
% of Temperature values
T = reshape(T(:, 3:end), [NLatitude, NStdPressureLev, NLongitude]);
%%Write files for each std pressure levels
% create columns for table of (Latitude Longitude Temperature)
[lat, lon] = meshgrid(Latitude, Longitude);
% cycle through all std pressure levels
for i = 1:3% NStdPressureLev
tem = squeeze(T(:,i,:))';
Tab_i = [lat(:) lon(:) tem(:)];
fid = fopen(sprintf('StdPressureLev_%d.txt', i), 'w');
fprintf(fid, '%%Std Pressure Level %f\n', StdPressureLev(i));
fprintf(fid, '%%Latitude Longitude Temperature\r\n');
fprintf(fid, '%.1f %.1f %.3f\n', Tab_i');
fclose(fid);
end
댓글 수: 4
Muhammad Usman Saleem
2016년 1월 18일
Muhammad Usman Saleem
2016년 4월 5일
Thorsten
2016년 4월 5일
Hi Muhammad! Have you run my code on temp.txt? If you have further questions please start a new question. People do not look at answered questions. Best, T
Muhammad Usman Saleem
2016년 4월 5일
카테고리
도움말 센터 및 File Exchange에서 Language Support에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
