Want more features on Pastebin? Sign Up, it's FREE!
Guest

Chris W

By: a guest on Nov 28th, 2009  |  syntax: None  |  size: 3.65 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function [rwm_data] = rwm_read(rwm_filename, plot_debug)
  2.          
  3.         if nargin > 2 || nargin < 1
  4.                 error("\nUsage: function [rwm_data] = yamal_read(rwm_filename, <plot_debug|true:false>)\n");
  5.         endif
  6.        
  7.         if nargin == 1
  8.                 plot_debug = false;
  9.         endif
  10.        
  11.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12.         % Retrieves and loads a .rwm tree file
  13.         % Returns a result of struct type rwm_data with:
  14.         % rwm_data.type - "rwm_data"
  15.         % rwm_data.length - the number of core smaples included
  16.         % rwm_data.earliest_year - first year in record, of all the cores
  17.         % rwm_data.latest_year - last year in record, of all the cores
  18.         % rwm_data.cores - a struct array of all the core samples, from 1 to rwm_data.length
  19.         % rwm_data.cores(i).id - the id of the core sample
  20.         % rwm_data.cores(i).sequence_number - the sequence number for the core from the .rwm file
  21.         % rwm_data.cores(i).start_year - the start year of a core sample
  22.         % rwm_data.cores(i).end_year - the end year of a core sample
  23.         % rwm_data.cores(i).ring_widths - a vector ofring widths from start_year to end_year, .01 mm
  24.         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  25.  
  26.         % Variables
  27.        
  28.         i.html">i = 0;
  29.         j.html">j = 0;
  30.         rwm_fid = -1;
  31.         in_record = false;
  32.         data_string = "";
  33.         width_vec = [];
  34.         rwm_data.type = "rwm_data";
  35.         rwm_data.length = 0;
  36.         rwm_data.earliest_year = 99999999;
  37.         rwm_data.latest_year = -99999999;
  38.        
  39.         % Open the file
  40.  
  41.         disp(sprintf("\nReading and loading \"%s\".", rwm_filename));  
  42.        
  43.         rwm_fid = fopen(rwm_filename);
  44.        
  45.         if rwm_fid == -1
  46.                 error(sprintf("File %s not found", rwm_filename));
  47.         endif
  48.        
  49.         % Read first line
  50.        
  51.         while feof(rwm_fid) == false
  52.                 % Read next line
  53.                 line_in = fgetl(rwm_fid);
  54.                 if feof(rwm_fid) == true || isempty(strfind(line_in, "~")) == false
  55.                         if in_record == true                   
  56.                                 % Remove trailing white space (if any), and store previous
  57.                                 while (length(data_string) > 0) && (isspace(data_string(end)) == true)
  58.                                         if length(data_string) > 1
  59.                                                 data_string = data_string(1:end-1);
  60.                                         else
  61.                                                 data_string = "";
  62.                                         endif
  63.                                 endwhile
  64.                                 if mod(length(data_string), 3) != 0
  65.                                         error(sprintf("Data field not divisible by 3 for id %s", rwm_data.cores(j).id));
  66.                                 endif                          
  67.                                 width_vec = str2num(reshape(data_string, 3, length(data_string) / 3)');
  68.                                 rwm_data.cores(j.html">j).end_year = (length(width_vec) - 1) + rwm_data.cores(j.html">j).start_year;
  69.                                 if rwm_data.cores(j.html">j).end_year > rwm_data.latest_year
  70.                                         rwm_data.latest_year = rwm_data.cores(j.html">j).end_year;
  71.                                 endif
  72.                                 % This ensures the field names for type rwm_data are in order...
  73.                                 rwm_data.cores(j.html">j).ring_widths = width_vec;
  74.                         endif
  75.                         if feof(rwm_fid) == false
  76.                                 % Get ready for the next
  77.                                 in_record = true;
  78.                                 rwm_data.length++;
  79.                                 j.html">j = rwm_data.length;
  80.                                 rwm_data.cores(j.html">j).id = strtrim(line_in(21:40));
  81.                                 rwm_data.cores(j.html">j).sequence_number = sscanf(strtrim(line_in(1:10)), "%d=N");
  82.                                 rwm_data.cores(j.html">j).start_year = sscanf(strtrim(line_in(11:20)), "%d=I");
  83.                                 if rwm_data.cores(j.html">j).start_year < rwm_data.earliest_year
  84.                                         rwm_data.earliest_year = rwm_data.cores(j.html">j).start_year;
  85.                                 endif
  86.                                 data_string = "";
  87.                         endif
  88.                 else
  89.                         % Add rings
  90.                         data_string = strcat(data_string, line_in);
  91.                 endif
  92.         endwhile
  93.        
  94.         % Clean up
  95.         fclose(rwm_fid);
  96.  
  97.         % Report
  98.         disp(sprintf("File %s read: %d cores loaded.\n", rwm_filename,  rwm_data.length));
  99.        
  100.         if plot_debug == true
  101.                 clf;
  102.                 hold on;               
  103.                 i.html">i = 1;
  104.                 while i.html">i <= rwm_data.length                     
  105.                         plot([rwm_data.cores(i.html">i).start_year:rwm_data.cores(i.html">i).end_year], \
  106.                                 rwm_data.cores(i.html">i).ring_widths, "1.");
  107.                         i.html">i++;
  108.                 endwhile
  109.                 hold off;
  110.         endif
  111.  
  112.         % Return [rwm_data]
  113.        
  114. endfunction
clone this paste RAW Paste Data