
Chris W
By: a guest on
Nov 28th, 2009 | syntax:
None | size: 3.65 KB | hits: 23 | expires: Never
function [rwm_data] = rwm_read(rwm_filename, plot_debug)
error("\nUsage:
function [rwm_data
] = yamal_read
(rwm_filename, <plot_debug|true:false>
)\n"
);
endif
plot_debug = false;
endif
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Retrieves and loads a .rwm tree file
% Returns a result of struct type rwm_data with:
% rwm_data.type - "rwm_data"
% rwm_data.length - the number of core smaples included
% rwm_data.earliest_year - first year in record, of all the cores
% rwm_data.latest_year - last year in record, of all the cores
% rwm_data.cores - a struct array of all the core samples, from 1 to rwm_data.length
% rwm_data.cores(i).id - the id of the core sample
% rwm_data.cores(i).sequence_number - the sequence number for the core from the .rwm file
% rwm_data.cores(i).start_year - the start year of a core sample
% rwm_data.cores(i).end_year - the end year of a core sample
% rwm_data.cores(i).ring_widths - a vector ofring widths from start_year to end_year, .01 mm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Variables
rwm_fid = -1;
in_record = false;
data_string = "";
width_vec = [];
rwm_data.
type = "rwm_data";
rwm_data.earliest_year = 99999999;
rwm_data.latest_year = -99999999;
% Open the file
disp(sprintf("\nReading and loading \"
%s\".", rwm_filename));
rwm_fid =
fopen(rwm_filename
);
if rwm_fid == -1
endif
% Read first line
while feof(rwm_fid
) == false
% Read next line
line_in =
fgetl(rwm_fid
);
if feof(rwm_fid
) == true || isempty
(strfind
(line_in, "~"
)) == false
if in_record == true
% Remove trailing white space (if any), and store previous
while (length(data_string
) > 0
) &&
(isspace
(data_string
(end)) == true
)
data_string = data_string(1:end-1);
else
data_string = "";
endif
endwhile
error(sprintf("Data field not divisible by 3
for id
%s", rwm_data.cores(j).id));
endif
if rwm_data.
cores(j.html">j).
end_year > rwm_data.
latest_year
rwm_data.
latest_year = rwm_data.
cores(j.html">j).
end_year;
endif
% This ensures the field names for type rwm_data are in order...
rwm_data.
cores(j.html">j).
ring_widths = width_vec;
endif
if feof(rwm_fid
) == false
% Get ready for the next
in_record = true;
rwm_data.
cores(j.html">j).
id = strtrim
(line_in
(21:40
));
rwm_data.
cores(j.html">j).
sequence_number =
sscanf(strtrim
(line_in
(1:
10)), "
%d=N");
rwm_data.
cores(j.html">j).
start_year =
sscanf(strtrim
(line_in
(11:
20)), "
%d=I");
if rwm_data.
cores(j.html">j).
start_year < rwm_data.
earliest_year
rwm_data.
earliest_year = rwm_data.
cores(j.html">j).
start_year;
endif
data_string = "";
endif
else
% Add rings
data_string =
strcat(data_string, line_in
);
endif
endwhile
% Clean up
% Report
disp(sprintf("File
%s read: %d cores loaded.\n", rwm_filename, rwm_data.length));
if plot_debug == true
rwm_data.
cores(i.html">i).
ring_widths, "1."
);
endwhile
endif
% Return [rwm_data]
endfunction