function [DF_HEAD,SID_SPEC,totrecs,datacat,pos,SID_REC]=DSGread(FileName,numrecs, startpos) %% Program to load DSG files and extract information. File always closed at end of call. % [DF_HEAD,SID_SPEC,totrecs,datacat,pos,SID_REC]=DSGread(FileName,numrecs,startpos) % DF_HEAD % SID_SPEC % totrecs = total records in file % pos = position in bytes at end of function % datacat = concatenated data (extracted from SID_RECs) % SID_REC = data in SID_REC structure % FileName = DSG filename (note very old .vdf files also loaded) % numrecs = number of SID_RECS to read. =0 to read entire file % startpos = start position (in bytes) to start reading numrecs % To read in parts of file use output argument pos to keep track of % location in file. %% fid=fopen(FileName); if(fid<1) disp('Unable to Open File'); FileName return end [path,file,filetype]=fileparts(FileName); % Read in DF_Head DF_HEAD.Version = fread(fid,1,'uint32'); DF_HEAD.UserID = fread(fid,1,'uint32'); DF_HEAD.sec = fread(fid,1,'uint8'); DF_HEAD.min = fread(fid,1,'uint8'); DF_HEAD.hour = fread(fid,1,'uint8'); DF_HEAD.day = fread(fid,1,'uint8'); DF_HEAD.mday = fread(fid,1,'uint8'); DF_HEAD.month = fread(fid,1,'uint8'); DF_HEAD.year = fread(fid,1,'uint8'); DF_HEAD.timezone = fread(fid,1,'int8'); if strcmpi(filetype,'.VDF') == 1 DF_HEAD.NU = fread(fid,16,'uint32'); end if(DF_HEAD.Version>=1010) DF_HEAD.Lat=fread(fid,1,'float32'); DF_HEAD.Lon=fread(fid,1,'float32'); DF_HEAD.depth=fread(fid,1,'float32'); DF_HEAD.DSGcal=fread(fid,1,'float32'); DF_HEAD.hydroCal=fread(fid,1,'float32'); DF_HEAD.lpFilt=fread(fid,1,'float32'); end pos(2) = ftell(fid); % Read in SID_SPECS until get all zeroes notdone=1; SID_SPEC=[]; nSIDSPEC=1; while(notdone) SID_SPEC(nSIDSPEC).SID = fread(fid,4,'uint8=>char'); SID_SPEC(nSIDSPEC).nBytes = fread(fid,1,'uint32'); SID_SPEC(nSIDSPEC).NumChan = fread(fid,1,'uint32'); SID_SPEC(nSIDSPEC).StoreType = fread(fid,1,'uint32'); SID_SPEC(nSIDSPEC).CircType = fread(fid,1,'uint32'); SID_SPEC(nSIDSPEC).DForm = fread(fid,1,'uint32'); SID_SPEC(nSIDSPEC).SP256 = fread(fid,1,'uint32'); % Sample period (us) x 256 if strcmp(SID_SPEC(1).SID','VIPR') ==1 SID_SPEC(nSIDSPEC).RECPTS=0; SID_SPEC(nSIDSPEC).RECINT=0; else SID_SPEC(nSIDSPEC).RECPTS=fread(fid,1,'uint32'); SID_SPEC(nSIDSPEC).RECINT=fread(fid,1,'uint32'); end if(SID_SPEC(nSIDSPEC).nBytes==0) notdone=0; end nSIDSPEC=nSIDSPEC+1; end nSIDSPEC=nSIDSPEC-1; SID_SPEC(nSIDSPEC)=[]; % delete last one with all zeroes nSIDSPEC=nSIDSPEC-1; pos(3) = ftell(fid); SID_REC=[]; datacat=[]; fseek(fid, 0, 'eof'); npts=ftell(fid)/2; totrecs=floor((npts-8-(18*length(SID_SPEC)))/(SID_SPEC(1).nBytes/2)); %subtract off size of DF_HEAD and SID_SPEC if(nargout<4) % only reading in DF_HEAD, SID_SPEC, totrecs fclose(fid); return; end fseek(fid,pos(3),'bof'); % put back at end of SID_SPEC % Read in next SID_REC header, and based on ID, read in correct number of % points eofstat=0; reccounter=0; if(nargin==3) if(startpos(1)>0) fseek(fid,startpos(1),'bof'); end end while(eofstat==0) reccounter=reccounter+1; SID_REC(reccounter).nSID=fread(fid,1,'uint8'); SID_REC(reccounter).Chan=fread(fid,1,'uint8'); SID_REC(reccounter).TS256=fread(fid,1,'uint64'); % Timestamp in usecs * 256 since block record started cur_sid=(SID_REC(reccounter).nSID)+1; if(cur_sid == 1) nsamples=(SID_SPEC(cur_sid).nBytes)/2; %/2 because in bytes SID_REC(reccounter).data=fread(fid,nsamples,'int16'); datacat = vertcat(datacat,SID_REC(reccounter).data); else SID_REC(reccounter)=[]; %last one was bad so delete this entry reccounter=reccounter-1; end eofstat = feof(fid); if(numrecs>0 && (reccounter>=numrecs)) break; end end pos(1) = ftell(fid); % get position of file fclose(fid); %% ------------------------------------------------------------------------- % DSG file structure % DF_HEAD % several SID_SPECS followed by SID_SPEC with all zeros % SID_REC % DF_HEAD