Home > constants > getRfactor.m

getRfactor

PURPOSE ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SYNOPSIS ^

function [r_factor] = getRfactor(date, filename)

DESCRIPTION ^

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   function [r_factor] = getRfactor(date, filename)

   function that reads an ASCII text file, given by filename, and finds the
   rfactor correction values that should be applied to the data for the
   time given by date

   sjcm

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [r_factor] = getRfactor(date, filename)
0002 
0003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0004 %
0005 %   function [r_factor] = getRfactor(date, filename)
0006 %
0007 %   function that reads an ASCII text file, given by filename, and finds the
0008 %   rfactor correction values that should be applied to the data for the
0009 %   time given by date
0010 %
0011 %   sjcm
0012 %
0013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0014 
0015 if(nargin<2)
0016   filename = 'constants/r_factors.txt';
0017 end
0018 
0019 rfactors = load(filename);
0020 
0021 order  = rfactors(:,1);
0022 rDate  = rfactors(:,2);
0023 rfactors(:,1:2) = [];
0024 
0025 
0026 % first put things in chronological order
0027 [rDateChron, sortOrder] = sort(rDate);
0028 rChron     = rfactors(sortOrder,:);
0029 orderChron = order(sortOrder);
0030 
0031 % redefine the variables
0032 rDate     = rDateChron;
0033 rfactors  = rChron;
0034 order     = orderChron;
0035 
0036 % let us get rid of the repeats - always trust the lastest one
0037 reps   = find(diff(rDate)==0);
0038 rDate(reps,:) = [];
0039 rfactors(reps,:) = [];
0040 order(reps,:)  = [];
0041 
0042 % find the closest value to our desired date
0043 dateDif = rDate - repmat(date, size(rDate));
0044 f = find(dateDif > 0);
0045 if(isempty(f))
0046   f = length(dateDif);
0047 else
0048   f = f-1;
0049   f = f(1);
0050 end
0051 if(f==0)
0052   f=1;
0053 end
0054 
0055 r_f = rfactors(f,:);
0056 finOrder = f;
0057 
0058 % check for nans
0059 if(any(isnan(r_f)))
0060     display(['getRfactor:: Found some NaN values.']);
0061     display('getRfactor:: Will find previous good data');
0062     fNan = find(isnan(r_f));
0063 
0064     for m=1:length(fNan)
0065         thisR = rfactors(:,fNan(m));
0066         if(all(isnan(thisR)))
0067             display('getRfactor:: CAN''T DO SHIT!!!');
0068             error(['getRfactor:: ALL your R-factor corrections for this channel are bad']);
0069         end
0070         % since we have already cut the repeats, we can just use f as
0071         % our index to remove..
0072         newOrder = f;
0073         rVal = thisR(newOrder);
0074         while(isnan(rVal))
0075             newOrder = newOrder - 1;
0076             rVal = thisR(newOrder);
0077         end
0078         r_f(fNan(m)) = rVal;
0079     end
0080 end
0081 
0082 % that's it
0083 r_factor = r_f;
0084 
0085 
0086 return;
0087

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005