Skip to content

Instantly share code, notes, and snippets.

@grandadmiral-thrawn
Created August 18, 2014 16:26
Show Gist options
  • Save grandadmiral-thrawn/05169a1f8433923d5c12 to your computer and use it in GitHub Desktop.
Save grandadmiral-thrawn/05169a1f8433923d5c12 to your computer and use it in GitHub Desktop.
Campbell data logger parser for matlab
function [dvec2, dn1, crvalues] = parseCCtext(textfile)
%PARSECCTEXT(TEXTFILE)/4 takes CR logger data and breaks it down into a
%datevec and corresponding logger values.
%%% the format of the raw must be like this:
%%% '01+129.0 02+2012. 03+275.0 04+2400. 05+501.0 06+.062 '
%%% this is our formatting for the raw loggers.
%%% in the regex one could be less specific and have arguements to point to
%%% the appropriate column.
% note that these data are not "gap-filled" like the other data!
%%% coyote is the name of the variable here, but it could be anything.
coyote = importdata(textfile);
%%% pre-allocate memory for doubles only...
doy = zeros(length(coyote)-1,1);
year = zeros(length(coyote)-1,1);
hhmm = zeros(length(coyote)-1,1);
crvalue = zeros(length(coyote)-1,1);
hh = zeros(length(coyote)-1,1);
mm = zeros(length(coyote)-1,1);
for i = 1:length(coyote)-1
%%% pattern match on doy, descending through ddd, dd, and d since 002
%%% is represented as 2 and 065 as 65 etc.
try
doyt = regexp(coyote{i},'03\+?[0-9]{3}','match');
% get doy
doy(i) = str2double(doyt{1}(4:6));
catch me
try
doyt = regexp(coyote{i},'03\+?[0-9]{2}','match');
doy(i) = str2double(doyt{1}(4:5));
catch me
try
doyt = regexp(coyote{i},'03\+?[0-9]{1}','match');
doy(i) = str2double(doyt{1}(4));
catch me
end
end
end
yeart = regexp(coyote{i},'02\+?[0-9]{4}','match');
% get year
year(i) = str2num(yeart{1}(4:7));
%%% pattern match on time, descending through hhmm, hmm, and mm since
%%% 0005 etc is not listed, rather 00 or 900 etc.
try
hhmmt = regexp(coyote{i},'04\+?[0-9]{4}','match');
% get time
hhmm(i) = str2double(hhmmt{1}(4:7));
catch ME
try
hhmmt = regexp(coyote{i},'04\+?[0-9]{3}','match');
hhmm(i) = str2double(hhmmt{1}(4:6));
catch ME
try
hhmmt = regexp(coyote{i},'04\+?[0-9]{2}','match');
hhmm(i) = str2double(hhmmt{1}(4:5));
catch ME
hhmmt = regexp(coyote{i},'04\+?[0-9]{1}','match');
hhmm(i) = str2double(hhmmt{1}(4));
end
end
end
dvec = zeros(length(year),6);
%%% get the hour and minutes out of the hhmm section
hh(i) = floor(hhmm(i)/100);
mm(i) = mod(hhmm(i),100);
crt = regexp(coyote{1},'06\+\.?[0-9]{3}','match');
% get year
crvalue(i) = str2num(crt{1}(4:7));
dvec(i,1:3) = doy2date3(year(i),doy(i));
end
%%% merge the hour and minute formats
dvec2 = zeros(length(dvec),6);
dvec2(:,1:3) = dvec;
dvec2(:,4) = hh;
dvec2(:,5) = mm;
%%% generate a datenum
dn1 = datenum(dvec2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment