diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/src/GNUmakefile.am b/src/GNUmakefile.am index ff27ea2..f4d2195 100644 --- a/src/GNUmakefile.am +++ b/src/GNUmakefile.am @@ -4,6 +4,8 @@ AM_CFLAGS += -D _FILE_OFFSET_BITS=64 AM_LDFLAGS = +AUTOMAKE_OPTIONS = subdir-objects + bin_PROGRAMS = picoquant include_HEADERS = picoquant.h \ @@ -12,7 +14,8 @@ include_HEADERS = picoquant.h \ picoharp.h picoharp/ph_v20.h\ hydraharp.h hydraharp/hh_v10.h hydraharp/hh_v20.h \ timeharp.h timeharp/th_v20.h timeharp/th_v30.h \ - timeharp/th_v50.h timeharp/th_v60.h + timeharp/th_v50.h timeharp/th_v60.h \ + ptu_dispatch.h picoquant_SOURCES = picoquant_main.c picoquant.c \ error.c types.c options.c files.c \ header.c continuous.c interactive.c tttr.c t2.c t3.c \ @@ -31,4 +34,5 @@ picoquant_SOURCES = picoquant_main.c picoquant.c \ timeharp/th_v50.c timeharp/th_v50_tttr.c \ timeharp/th_v50_interactive.c timeharp/th_v50_continuous.c \ timeharp/th_v60.c timeharp/th_v60_tttr.c \ - timeharp/th_v60_interactive.c timeharp/th_v60_continuous.c + timeharp/th_v60_interactive.c timeharp/th_v60_continuous.c \ + ptu_dispatch.c diff --git a/src/header.c b/src/header.c index 9e267a7..7ed7497 100644 --- a/src/header.c +++ b/src/header.c @@ -29,9 +29,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "header.h" #include "error.h" +#include "picoquant.h" int pq_header_read(FILE *stream_in, pq_header_t *pq_header) { size_t n_read; @@ -44,13 +44,521 @@ int pq_header_read(FILE *stream_in, pq_header_t *pq_header) { } } +int ptu_header_parse(FILE *in_stream, ptu_header_t *ptu_header){ + char Magic[8]; + char Version[8]; + char Buffer[40]; + char* AnsiBuffer; + //WCHAR* WideBuffer; + int Result; + + //long long NumRecords = -1; + //long long RecordType = 0; + + TagHead_t TagHead; + fseek(in_stream, sizeof(Magic)+sizeof(Version), SEEK_SET);//make sure we start at the right place + // read tagged header + do{ + Result = fread( &TagHead, 1, sizeof(TagHead) ,in_stream); + if (Result!= sizeof(TagHead)) + { + error("Incomplete file in header of ptu"); + } + + strcpy(Buffer, TagHead.Ident); + if (TagHead.Idx > -1) + { + sprintf(Buffer, "%s(%d)", TagHead.Ident,TagHead.Idx); + } + //fprintf(fpout, "\n%-40s", Buffer); + switch (TagHead.Typ) + { + case tyEmpty8: + if (strcmp(TagHead.Ident, name_Fast_Load_End)==0){ + ptu_header->Fast_Load_End = TagHead.TagValue ;//just kept everything + } + //fprintf(fpout, ""); + break; + case tyBool8: + if (strcmp(TagHead.Ident, name_MeasDesc_StopOnOvfl)==0){ + ptu_header->MeasDesc_StopOnOvfl = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_MeasDesc_Restart)==0){ + ptu_header->MeasDesc_Restart = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispLog)==0){ + ptu_header->CurSWSetting_DispLog = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show0)==0){ + ptu_header->CurSWSetting_DispCurve_Show0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show1)==0){ + ptu_header->CurSWSetting_DispCurve_Show1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show2)==0){ + ptu_header->CurSWSetting_DispCurve_Show2 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show3)==0){ + ptu_header->CurSWSetting_DispCurve_Show3 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show4)==0){ + ptu_header->CurSWSetting_DispCurve_Show4 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show5)==0){ + ptu_header->CurSWSetting_DispCurve_Show5 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show6)==0){ + ptu_header->CurSWSetting_DispCurve_Show6 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_Show7)==0){ + ptu_header->CurSWSetting_DispCurve_Show7 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HW_ExternalRefClock)==0){ + ptu_header->HW_ExternalRefClock = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWInpChan_Enabled0)==0){ + ptu_header->HWInpChan_Enabled0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_Enabled1)==0){ + ptu_header->HWInpChan_Enabled1 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWMarkers_RisingEdge0)==0){ + ptu_header->HWMarkers_RisingEdge0 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWMarkers_RisingEdge1)==0){ + ptu_header->HWMarkers_RisingEdge1 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWMarkers_RisingEdge2)==0){ + ptu_header->HWMarkers_RisingEdge2 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWMarkers_RisingEdge3)==0){ + ptu_header->HWMarkers_RisingEdge3 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWMarkers_Enabled0)==0){ + ptu_header->HWMarkers_Enabled0 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWMarkers_Enabled1)==0){ + ptu_header->HWMarkers_Enabled1 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWMarkers_Enabled2)==0){ + ptu_header->HWMarkers_Enabled2 = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_HWMarkers_Enabled3)==0){ + ptu_header->HWMarkers_Enabled3 = TagHead.TagValue; + } + //fprintf(fpout, "%s", bool(TagHead.TagValue)?"True":"False"); + //fprintf(fpout, " tyBool8"); + break; + case tyInt8: + if (strcmp(TagHead.Ident, name_Measurement_Mode)==0){ + ptu_header->Measurement_Mode = TagHead.TagValue ; + } else if (strcmp(TagHead.Ident, name_Measurement_SubMode)==0){ + ptu_header->Measurement_SubMode = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResult_StopReason)==0){ + ptu_header->TTResult_StopReason = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResultFormat_TTTRRecType)==0){ + ptu_header->TTResultFormat_TTTRRecType = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResultFormat_BitsPerRecord)==0){ + ptu_header->TTResultFormat_BitsPerRecord = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_MeasDesc_BinningFactor)==0){ + ptu_header->MeasDesc_BinningFactor = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_MeasDesc_Offset)==0){ + ptu_header->MeasDesc_Offset = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_MeasDesc_AcquisitionTime)==0){ + ptu_header->MeasDesc_AcquisitionTime = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_MeasDesc_StopAt)==0){ + ptu_header->MeasDesc_StopAt = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispAxisTimeFrom)==0){ + ptu_header->CurSWSetting_DispAxisTimeFrom = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispAxisTimeTo)==0){ + ptu_header->CurSWSetting_DispAxisTimeTo = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispAxisCountFrom)==0){ + ptu_header->CurSWSetting_DispAxisCountFrom = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispAxisCountTo)==0){ + ptu_header->CurSWSetting_DispAxisCountTo = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurves)==0){ + ptu_header->CurSWSetting_DispCurves = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo0)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo1)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo2)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo2 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo3)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo3 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo4)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo4 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo5)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo5 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo6)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo6 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_CurSWSetting_DispCurve_MapTo7)==0){ + ptu_header->CurSWSetting_DispCurve_MapTo7 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HW_Modules )==0){ + ptu_header->HW_Modules = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWModule_TypeCode0)==0){ + ptu_header->HWModule_TypeCode0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWModule_TypeCode1)==0){ + ptu_header->HWModule_TypeCode1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWModule_TypeCode2)==0){ + ptu_header->HWModule_TypeCode2 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWModule_VersCode0)==0){ + ptu_header->HWModule_VersCode0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWModule_VersCode1)==0){ + ptu_header->HWModule_VersCode1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWModule_VersCode2)==0){ + ptu_header->HWModule_VersCode2 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HW_InpChannels)==0){ + ptu_header->HW_InpChannels = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HW_ExternalDevices)==0){ + ptu_header->HW_ExternalDevices = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWSync_Divider)==0){ + ptu_header->HWSync_Divider = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWSync_CFDLevel)==0){ + ptu_header->HWSync_CFDLevel = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWSync_CFDZeroCross)==0){ + ptu_header->HWSync_CFDZeroCross = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWSync_Offset)==0){ + ptu_header->HWSync_Offset = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_ModuleIdx0)==0){ + ptu_header->HWInpChan_ModuleIdx0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_ModuleIdx1)==0){ + ptu_header->HWInpChan_ModuleIdx1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_CFDLevel0)==0){ + ptu_header->HWInpChan_CFDLevel0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_CFDLevel1)==0){ + ptu_header->HWInpChan_CFDLevel1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_CFDZeroCross0)==0){ + ptu_header->HWInpChan_CFDZeroCross0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_CFDZeroCross1)==0){ + ptu_header->HWInpChan_CFDZeroCross1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_Offset0)==0){ + ptu_header->HWInpChan_Offset0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWInpChan_Offset1)==0){ + ptu_header->HWInpChan_Offset1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HW_Markers)==0){ + ptu_header->HW_Markers = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_HWMarkers_HoldOff )==0){ + ptu_header->HWMarkers_HoldOff = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResult_SyncRate)==0){ + ptu_header->TTResult_SyncRate = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResult_InputRate0)==0){ + ptu_header->TTResult_InputRate0 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResult_InputRate1)==0){ + ptu_header->TTResult_InputRate1 = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResult_StopAfter)==0){ + ptu_header->TTResult_StopAfter = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_TTResult_NumberOfRecords)==0){ + ptu_header->TTResult_NumberOfRecords = TagHead.TagValue; + } + /*fprintf(fpout, "%lld", TagHead.TagValue); + fprintf(fpout, " tyInt8"); + // get some Values we need to analyse records + if (strcmp(TagHead.Ident, TTTRTagNumRecords)==0) // Number of records + NumRecords = TagHead.TagValue; + if (strcmp(TagHead.Ident, TTTRTagTTTRRecType)==0) // TTTR RecordType + RecordType = TagHead.TagValue;*/ + break; + /*case tyBitSet64: //not sure when this is used + //fprintf(fpout, "0x%16.16X", TagHead.TagValue); + //fprintf(fpout, " tyBitSet64"); + break;*/ + /*case tyColor8: + fprintf(fpout, "0x%16.16X", TagHead.TagValue); + fprintf(fpout, " tyColor8"); + break;*/ + case tyFloat8: + if (strcmp(TagHead.Ident, name_HW_BaseResolution)==0){ //this one probs not needed + ptu_header->HW_BaseResolution = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_MeasDesc_Resolution)==0){ + ptu_header->MeasDesc_Resolution = TagHead.TagValue; + } else if (strcmp(TagHead.Ident, name_MeasDesc_GlobalResolution)==0){ + ptu_header->MeasDesc_GlobalResolution = TagHead.TagValue;//in ns + }/* + fprintf(fpout, "%E", *(double*)&(TagHead.TagValue)); + fprintf(fpout, " tyFloat8"); + if (strcmp(TagHead.Ident, TTTRTagRes)==0) // Resolution for TCSPC-Decay + Resolution = *(double*)&(TagHead.TagValue); + if (strcmp(TagHead.Ident, TTTRTagGlobRes)==0) // Global resolution for timetag + GlobRes = *(double*)&(TagHead.TagValue); // in ns*/ + break; + /*case tyFloat8Array: //is this used when there are multiple records in one file? + fprintf(fpout, "", TagHead.TagValue / sizeof(double)); + fprintf(fpout, " tyFloat8Array"); + // only seek the Data, if one needs the data, it can be loaded here + fseek(in_stream, (long)TagHead.TagValue, SEEK_CUR); + break;*/ + case tyTDateTime: + ;//need empty statement before declaration after a : + time_t ftime; + ftime = TDateTime_TimeT(*((double*)&(TagHead.TagValue))); + strftime(ptu_header->FileTime, 20, "%Y-%m-%d %H:%M:%S", localtime(&ftime)); + //fprintf(fpout, "%s", asctime(gmtime(&CreateTime)), "\0"); + //fprintf(fpout, " tyTDateTime"); + break; + case tyAnsiString: + AnsiBuffer = (char*)calloc((size_t)TagHead.TagValue,1); + Result = fread(AnsiBuffer, 1, (size_t)TagHead.TagValue, in_stream); + if (Result!= TagHead.TagValue){ + printf("\nIncomplete File at AnsiBuffer."); + } else if (strcmp(TagHead.Ident, name_File_GUID)==0){ + sprintf(ptu_header->File_GUID, "%lld", TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_File_AssuredContent)==0){ + sprintf(ptu_header->File_AssuredContent,"%lld",TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_CreatorSW_ContentVersion)==0){ + sprintf(ptu_header->CreatorSW_ContentVersion ,"%lld",TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_CreatorSW_Name)==0){ + sprintf(ptu_header->CreatorName ,"%lld",TagHead.TagValue);//creatorname,version is consistent with other files + } else if (strcmp(TagHead.Ident, name_CreatorSW_Version)==0){ + sprintf(ptu_header->CreatorVersion ,"%lld", TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_File_Comment)==0){//T2 Mode!!!!!!!!! + sprintf(ptu_header->Comment ,"%lld", TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_HW_Type)==0){//HydraHarp400!!!!!!!! + sprintf(ptu_header->HW_Type ,"%lld", TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_HW_PartNo)==0){ + sprintf(ptu_header->HW_PartNo ,"%lld", TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_HW_Version)==0){//Version 2.0!!!!!!! + sprintf(ptu_header->HW_Version ,"%lld", TagHead.TagValue); + } else if (strcmp(TagHead.Ident, name_HW_SerialNo)==0){ + sprintf(ptu_header->HW_SerialNo ,"%lld", TagHead.TagValue); + } + //fprintf(fpout, "%s", AnsiBuffer); + //fprintf(fpout, " tyAnsiString"); + free(AnsiBuffer); + break; + /*case tyWideString: + WideBuffer = (WCHAR*)calloc((size_t)TagHead.TagValue,1); + Result = fread(WideBuffer, 1, (size_t)TagHead.TagValue, in_stream); + if (Result!= TagHead.TagValue){ + printf("\nIncomplete File at WideBuffer"); + } + //fwprintf(fpout, L"%s", WideBuffer); + fprintf(fpout, " tyWideString"); + free(WideBuffer); + break;*/ + /*case tyBinaryBlob: + fprintf(fpout, "", TagHead.TagValue); + fprintf(fpout, " tyBinaryBlob"); + // only seek the Data, if one needs the data, it can be loaded here + fseek(in_stream, (long)TagHead.TagValue, SEEK_CUR); + break;*/ + default: + //error ("Illegal Type identifier found! Broken file?"); + return(PQ_ERROR_IO); + break; + } + } + while((strncmp(TagHead.Ident, name_Header_End, sizeof(name_Header_End)))); + return (PQ_SUCCESS); +// End Header loading +} + +time_t TDateTime_TimeT(double Convertee){ + const int EpochDiff = 25569; // days between 30/12/1899 and 01/01/1970 + const int SecsInDay = 86400; // number of seconds in a day + time_t Result; + Result = ((long)(((Convertee) - EpochDiff) * SecsInDay)); + return Result; +} + void pq_header_printf(FILE *stream_out, pq_header_t *pq_header) { fprintf(stream_out, "Ident = %.*s\n", (int)sizeof(pq_header->Ident), pq_header->Ident); - fprintf(stream_out, "FormatVersion = %.*s\n", + fprintf(stream_out, "FormatVersion xxx = %.*s\n", (int)sizeof(pq_header->Ident), pq_header->FormatVersion); } + void pq_header_fwrite(FILE *stream_out, pq_header_t *pq_header) { fwrite(pq_header, sizeof(pq_header_t), 1, stream_out); } + +void ptu_header_fwrite(FILE *stream_out, ptu_header_t *ptu_header){ + fwrite(ptu_header, sizeof(ptu_header_t), 1, stream_out); +} + +void ptu_header_printf(FILE *stream_out, ptu_header_t *ptu_header) { + fprintf(stream_out, "CreatorName = %.*s\n", + (int)sizeof(ptu_header->CreatorName), ptu_header->CreatorName); + fprintf(stream_out, "CreatorVersion = %.*s\n", + (int)sizeof(ptu_header->CreatorVersion), ptu_header->CreatorVersion); + fprintf(stream_out, "FileTime = %.*s\n", + (int)sizeof(ptu_header->FileTime), ptu_header->FileTime); + + fprintf(stream_out, "Comment = %.*s\n", + (int)sizeof(ptu_header->Comment), ptu_header->Comment); + fprintf(stream_out, "NumberOfCurves = %.*d\n", + (int)sizeof(ptu_header->NumberOfCurves), ptu_header->NumberOfCurves); + + fprintf(stream_out, "MeasDesc_StopOnOvfl = %.*u\n", + (int)sizeof(ptu_header->MeasDesc_StopOnOvfl), ptu_header->MeasDesc_StopOnOvfl); + fprintf(stream_out, "MeasDesc_Restart = %.*u\n", + (int)sizeof(ptu_header->MeasDesc_Restart), ptu_header->MeasDesc_Restart); + fprintf(stream_out, "CurSWSetting_DispLog = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispLog), ptu_header->CurSWSetting_DispLog); + fprintf(stream_out, "CurSWSetting_DispCurve_Show0 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show0), ptu_header->CurSWSetting_DispCurve_Show0); + fprintf(stream_out, "CurSWSetting_DispCurve_Show1 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show1), ptu_header->CurSWSetting_DispCurve_Show1); + fprintf(stream_out, "CurSWSetting_DispCurve_Show2 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show2), ptu_header->CurSWSetting_DispCurve_Show2); + fprintf(stream_out, "CurSWSetting_DispCurve_Show3 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show3), ptu_header->CurSWSetting_DispCurve_Show3); + fprintf(stream_out, "CurSWSetting_DispCurve_Show4 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show4), ptu_header->CurSWSetting_DispCurve_Show4); + fprintf(stream_out, "CurSWSetting_DispCurve_Show5 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show5), ptu_header->CurSWSetting_DispCurve_Show5); + fprintf(stream_out, "CurSWSetting_DispCurve_Show6 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show6), ptu_header->CurSWSetting_DispCurve_Show6); + fprintf(stream_out, "CurSWSetting_DispCurve_Show7 = %.*u\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_Show7), ptu_header->CurSWSetting_DispCurve_Show7); + fprintf(stream_out, "HW_ExternalRefClock = %.*u\n", + (int)sizeof(ptu_header->HW_ExternalRefClock), ptu_header->HW_ExternalRefClock); + fprintf(stream_out, "HWInpChan_Enabled0 = %.*u\n", + (int)sizeof(ptu_header->HWInpChan_Enabled0), ptu_header->HWInpChan_Enabled0); + fprintf(stream_out, "HWInpChan_Enabled1 = %.*u\n", + (int)sizeof(ptu_header->HWInpChan_Enabled1), ptu_header->HWInpChan_Enabled1); + fprintf(stream_out, "HWMarkers_RisingEdge0 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_RisingEdge0), ptu_header->HWMarkers_RisingEdge0); + fprintf(stream_out, "HWMarkers_RisingEdge1 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_RisingEdge1), ptu_header->HWMarkers_RisingEdge1); + fprintf(stream_out, "HWMarkers_RisingEdge2 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_RisingEdge2), ptu_header->HWMarkers_RisingEdge2); + fprintf(stream_out, "HWMarkers_RisingEdge3 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_RisingEdge3), ptu_header->HWMarkers_RisingEdge3); + fprintf(stream_out, "HWMarkers_Enabled0 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_Enabled0), ptu_header->HWMarkers_Enabled0); + fprintf(stream_out, "HWMarkers_Enabled1 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_Enabled1), ptu_header->HWMarkers_Enabled1); + fprintf(stream_out, "HWMarkers_Enabled2 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_Enabled2), ptu_header->HWMarkers_Enabled2); + fprintf(stream_out, "HWMarkers_Enabled3 = %.*u\n", + (int)sizeof(ptu_header->HWMarkers_Enabled3), ptu_header->HWMarkers_Enabled3); + + fprintf(stream_out, "Fast_Load_End = %.*d\n", + (int)sizeof(ptu_header->Fast_Load_End), ptu_header->Fast_Load_End); + fprintf(stream_out, "Measurement_Mode = %.*d\n", + (int)sizeof(ptu_header->Measurement_Mode), ptu_header->Measurement_Mode); + fprintf(stream_out, "Measurement_SubMode = %.*d\n", + (int)sizeof(ptu_header->Measurement_SubMode), ptu_header->Measurement_SubMode); + fprintf(stream_out, "TTResult_StopReason = %.*d\n", + (int)sizeof(ptu_header->TTResult_StopReason), ptu_header->TTResult_StopReason); + fprintf(stream_out, "TTResultFormat_TTTRRecType = %.*d\n", + (int)sizeof(ptu_header->TTResultFormat_TTTRRecType), ptu_header->TTResultFormat_TTTRRecType); + fprintf(stream_out, "TTResultFormat_BitsPerRecord = %.*d\n", + (int)sizeof(ptu_header->TTResultFormat_BitsPerRecord), ptu_header->TTResultFormat_BitsPerRecord); + fprintf(stream_out, "MeasDesc_BinningFactor = %.*d\n", + (int)sizeof(ptu_header->MeasDesc_BinningFactor), ptu_header->MeasDesc_BinningFactor); + fprintf(stream_out, "MeasDesc_Offset = %.*d\n", + (int)sizeof(ptu_header->MeasDesc_Offset), ptu_header->MeasDesc_Offset); + fprintf(stream_out, "MeasDesc_AcquisitionTime = %.*d\n", + (int)sizeof(ptu_header->MeasDesc_AcquisitionTime), ptu_header->MeasDesc_AcquisitionTime); + fprintf(stream_out, "MeasDesc_StopAt = %.*d\n", + (int)sizeof(ptu_header->MeasDesc_StopAt), ptu_header->MeasDesc_StopAt); + fprintf(stream_out, "CurSWSetting_DispAxisTimeFrom = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispAxisTimeFrom), ptu_header->CurSWSetting_DispAxisTimeFrom); + fprintf(stream_out, "CurSWSetting_DispAxisTimeTo = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispAxisTimeTo), ptu_header->CurSWSetting_DispAxisTimeTo); + fprintf(stream_out, "CurSWSetting_DispAxisCountFrom = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispAxisCountFrom), ptu_header->CurSWSetting_DispAxisCountFrom); + fprintf(stream_out, "CurSWSetting_DispAxisCountTo = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispAxisCountTo), ptu_header->CurSWSetting_DispAxisCountTo); + fprintf(stream_out, "CurSWSetting_DispCurves = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurves), ptu_header->CurSWSetting_DispCurves); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo0 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo0), ptu_header->CurSWSetting_DispCurve_MapTo0); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo1 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo1), ptu_header->CurSWSetting_DispCurve_MapTo1); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo2 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo2), ptu_header->CurSWSetting_DispCurve_MapTo2); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo3 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo3), ptu_header->CurSWSetting_DispCurve_MapTo3); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo4 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo4), ptu_header->CurSWSetting_DispCurve_MapTo4); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo5 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo5), ptu_header->CurSWSetting_DispCurve_MapTo5); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo6 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo6), ptu_header->CurSWSetting_DispCurve_MapTo6); + fprintf(stream_out, "CurSWSetting_DispCurve_MapTo7 = %.*d\n", + (int)sizeof(ptu_header->CurSWSetting_DispCurve_MapTo7), ptu_header->CurSWSetting_DispCurve_MapTo7); + fprintf(stream_out, "HW_Modules = %.*d\n", + (int)sizeof(ptu_header->HW_Modules), ptu_header->HW_Modules); + fprintf(stream_out, "HWModule_TypeCode0 = %.*d\n", + (int)sizeof(ptu_header->HWModule_TypeCode0), ptu_header->HWModule_TypeCode0); + fprintf(stream_out, "HWModule_TypeCode1 = %.*d\n", + (int)sizeof(ptu_header->HWModule_TypeCode1), ptu_header->HWModule_TypeCode1); + fprintf(stream_out, "HWModule_TypeCode2 = %.*d\n", + (int)sizeof(ptu_header->HWModule_TypeCode2), ptu_header->HWModule_TypeCode2); + fprintf(stream_out, "HWModule_VersCode0 = %.*d\n", + (int)sizeof(ptu_header->HWModule_VersCode0), ptu_header->HWModule_VersCode0); + fprintf(stream_out, "HWModule_VersCode1 = %.*d\n", + (int)sizeof(ptu_header->HWModule_VersCode1), ptu_header->HWModule_VersCode1); + fprintf(stream_out, "HWModule_VersCode2 = %.*d\n", + (int)sizeof(ptu_header->HWModule_VersCode2), ptu_header->HWModule_VersCode2); + fprintf(stream_out, "HW_InpChannels = %.*d\n", + (int)sizeof(ptu_header->HW_InpChannels), ptu_header->HW_InpChannels); + fprintf(stream_out, "HW_ExternalDevices = %.*d\n", + (int)sizeof(ptu_header->HW_ExternalDevices), ptu_header->HW_ExternalDevices); + fprintf(stream_out, "HWSync_Divider = %.*d\n", + (int)sizeof(ptu_header->HWSync_Divider), ptu_header->HWSync_Divider); + fprintf(stream_out, "HWSync_CFDLevel = %.*d\n", + (int)sizeof(ptu_header->HWSync_CFDLevel), ptu_header->HWSync_CFDLevel); + fprintf(stream_out, "HWSync_CFDZeroCross = %.*d\n", + (int)sizeof(ptu_header->HWSync_CFDZeroCross), ptu_header->HWSync_CFDZeroCross); + fprintf(stream_out, "HWSync_Offset = %.*d\n", + (int)sizeof(ptu_header->HWSync_Offset), ptu_header->HWSync_Offset); + fprintf(stream_out, "HWInpChan_ModuleIdx0 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_ModuleIdx0), ptu_header->HWInpChan_ModuleIdx0); + fprintf(stream_out, "HWInpChan_ModuleIdx1 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_ModuleIdx1), ptu_header->HWInpChan_ModuleIdx1); + fprintf(stream_out, "HWInpChan_CFDLevel0 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_CFDLevel0), ptu_header->HWInpChan_CFDLevel0); + fprintf(stream_out, "HWInpChan_CFDLevel1 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_CFDLevel1), ptu_header->HWInpChan_CFDLevel1); + fprintf(stream_out, "HWInpChan_CFDZeroCross0 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_CFDZeroCross0), ptu_header->HWInpChan_CFDZeroCross0); + fprintf(stream_out, "HWInpChan_CFDZeroCross1 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_CFDZeroCross1), ptu_header->HWInpChan_CFDZeroCross1); + fprintf(stream_out, "HWInpChan_Offset0 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_Offset0), ptu_header->HWInpChan_Offset0); + fprintf(stream_out, "HWInpChan_Offset1 = %.*d\n", + (int)sizeof(ptu_header->HWInpChan_Offset1), ptu_header->HWInpChan_Offset1); + fprintf(stream_out, "HW_Markers = %.*d\n", + (int)sizeof(ptu_header->HW_Markers), ptu_header->HW_Markers); + fprintf(stream_out, "HWMarkers_HoldOff = %.*d\n", + (int)sizeof(ptu_header->HWMarkers_HoldOff), ptu_header->HWMarkers_HoldOff); + fprintf(stream_out, "TTResult_SyncRate = %.*d\n", + (int)sizeof(ptu_header->TTResult_SyncRate), ptu_header->TTResult_SyncRate); + fprintf(stream_out, "TTResult_InputRate0 = %.*d\n", + (int)sizeof(ptu_header->TTResult_InputRate0), ptu_header->TTResult_InputRate0); + fprintf(stream_out, "TTResult_InputRate1 = %.*d\n", + (int)sizeof(ptu_header->TTResult_InputRate1), ptu_header->TTResult_InputRate1); + fprintf(stream_out, "TTResult_StopAfter = %.*d\n", + (int)sizeof(ptu_header->TTResult_StopAfter), ptu_header->TTResult_StopAfter); + fprintf(stream_out, "TTResult_NumberOfRecords = %.*d\n", + (int)sizeof(ptu_header->TTResult_NumberOfRecords), ptu_header->TTResult_NumberOfRecords); + + fprintf(stream_out, "HW_BaseResolution = %.*f\n", + (int)sizeof(ptu_header->HW_BaseResolution), ptu_header->HW_BaseResolution); + fprintf(stream_out, "MeasDesc_Resolution = %.*f\n", + (int)sizeof(ptu_header->MeasDesc_Resolution), ptu_header->MeasDesc_Resolution); + fprintf(stream_out, "MeasDesc_GlobalResolution = %.*f\n", + (int)sizeof(ptu_header->MeasDesc_GlobalResolution), ptu_header->MeasDesc_GlobalResolution); + + fprintf(stream_out, "File_GUID = %.*s\n", + (int)sizeof(ptu_header->File_GUID), ptu_header->File_GUID); + fprintf(stream_out, "File_AssuredContent = %.*s\n", + (int)sizeof(ptu_header->File_AssuredContent), ptu_header->File_AssuredContent); + fprintf(stream_out, "CreatorSW_ContentVersion = %.*s\n", + (int)sizeof(ptu_header->CreatorSW_ContentVersion), ptu_header->CreatorSW_ContentVersion); + fprintf(stream_out, "HW_Type = %.*s\n", + (int)sizeof(ptu_header->HW_Type), ptu_header->HW_Type); + fprintf(stream_out, "HW_PartNo = %.*s\n", + (int)sizeof(ptu_header->HW_PartNo), ptu_header->HW_PartNo); + fprintf(stream_out, "HW_Version = %.*s\n", + (int)sizeof(ptu_header->HW_Version), ptu_header->HW_Version); + fprintf(stream_out, "HW_SerialNo = %.*s\n", + (int)sizeof(ptu_header->HW_SerialNo), ptu_header->HW_SerialNo); +/* fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->); + fprintf(stream_out, " = %.*s\n", + (int)sizeof(ptu_header->), ptu_header->);*/ + + + +} \ No newline at end of file diff --git a/src/header.h b/src/header.h index 9d8457d..6c42146 100644 --- a/src/header.h +++ b/src/header.h @@ -1,31 +1,31 @@ /* * Copyright (c) 2011-2014, Thomas Bischof * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without + * + * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, + * + * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. - * - * 3. Neither the name of the Massachusetts Institute of Technology nor the - * names of its contributors may be used to endorse or promote products + * + * 3. Neither the name of the Massachusetts Institute of Technology nor the + * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ @@ -33,18 +33,328 @@ #define HEADER_H_ #include +#include "picoquant.h" + + +#include + +#include +#include +#include + + +// TagTypes (TagHead.Typ) +#define tyEmpty8 0xFFFF0008 +#define tyBool8 0x00000008 +#define tyInt8 0x10000008 +#define tyBitSet64 0x11000008 +#define tyColor8 0x12000008 +#define tyFloat8 0x20000008 +#define tyTDateTime 0x21000008 +#define tyFloat8Array 0x2001FFFF +#define tyAnsiString 0x4001FFFF +#define tyWideString 0x4002FFFF +#define tyBinaryBlob 0xFFFFFFFF + +// RecordTypes +#define rtPicoHarpT3 0x00010303 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $03 (PicoHarp) +#define rtPicoHarpT2 0x00010203 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $03 (PicoHarp) +#define rtHydraHarpT3 0x00010304 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $04 (HydraHarp) +#define rtHydraHarpT2 0x00010204 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $04 (HydraHarp) +#define rtHydraHarp2T3 0x01010304 // (SubID = $01 ,RecFmt: $01) (V2), T-Mode: $03 (T3), HW: $04 (HydraHarp) +#define rtHydraHarp2T2 0x01010204 // (SubID = $01 ,RecFmt: $01) (V2), T-Mode: $02 (T2), HW: $04 (HydraHarp) +#define rtTimeHarp260NT3 0x00010305 // (SubID = $00 ,RecFmt: $01) (V2), T-Mode: $03 (T3), HW: $05 (TimeHarp260N) +#define rtTimeHarp260NT2 0x00010205 // (SubID = $00 ,RecFmt: $01) (V2), T-Mode: $02 (T2), HW: $05 (TimeHarp260N) +#define rtTimeHarp260PT3 0x00010306 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T3), HW: $06 (TimeHarp260P) +#define rtTimeHarp260PT2 0x00010206 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $06 (TimeHarp260P) + +// some important Tag Idents (TagHead.Ident) that we will need to read the most common content of a PTU file +// check the output of this program and consult the tag dictionary if you need more +#define name_TTResultFormat_TTTRRecType "TTResultFormat_TTTRRecType" +#define name_TTResult_NumberOfRecords "TTResult_NumberOfRecords" // Number of TTTR Records in the File; +#define name_MeasDesc_Resolution "MeasDesc_Resolution" // Resolution for the Dtime (T3 Only) +#define name_MeasDesc_GlobalResolution "MeasDesc_GlobalResolution" // Global Resolution of TimeTag(T2) /NSync (T3) +#define name_Header_End "Header_End" // Always appended as last tag (BLOCKEND) + +// The rest of the Tag Idents: We do this so that if the name in the header given changes, you only +// need to change it up here, not in the rest of the code +#define name_File_GUID "File_GUID" //tyAnsiString +#define name_File_AssuredContent "File_AssuredContent" //tyAnsiString +#define name_CreatorSW_ContentVersion "CreatorSW_ContentVersion" //tyAnsiString +#define name_CreatorSW_Name "CreatorSW_Name" //tyAnsiString +#define name_CreatorSW_Version "CreatorSW_Version" //tyAnsiString +#define name_File_CreatingTime "File_CreatingTime" //tyTDateTime +#define name_File_Comment "File_Comment" //tyAnsiString +#define name_Measurement_Mode "Measurement_Mode" //tyInt8 +#define name_Measurement_SubMode "Measurement_SubMode" //tyInt8 +#define name_TTResult_StopReason "TTResult_StopReason" //tyInt8 +#define name_Fast_Load_End "Fast_Load_End" //empty tag? + +#define name_TTResultFormat_BitsPerRecord "TTResultFormat_BitsPerRecord" //tyInt8 +#define name_MeasDesc_BinningFactor "MeasDesc_BinningFactor" //tyInt8 +#define name_MeasDesc_Offset "MeasDesc_Offset" //tyInt8 +#define name_MeasDesc_AcquisitionTime "MeasDesc_AcquisitionTime" //tyInt8 +#define name_MeasDesc_StopAt "MeasDesc_StopAt" //tyInt8 +#define name_MeasDesc_StopOnOvfl "MeasDesc_StopOnOvfl" //tyBool8 +#define name_MeasDesc_Restart "MeasDesc_Restart" //tyBool8 +#define name_CurSWSetting_DispLog "CurSWSetting_DispLog" //tyBool8 +#define name_CurSWSetting_DispAxisTimeFrom "CurSWSetting_DispAxisTimeFrom" //tyInt8 +#define name_CurSWSetting_DispAxisTimeTo "CurSWSetting_DispAxisTimeTo" //tyInt8 +#define name_CurSWSetting_DispAxisCountFrom "CurSWSetting_DispAxisCountFrom" //tyInt8 +#define name_CurSWSetting_DispAxisCountTo "CurSWSetting_DispAxisCountTo" //tyInt8 +#define name_CurSWSetting_DispCurves "CurSWSetting_DispCurves" //tyInt8 +#define name_CurSWSetting_DispCurve_MapTo0 "CurSWSetting_DispCurve_MapTo(0)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show0 "CurSWSetting_DispCurve_Show(0)" //tyBool8 +#define name_CurSWSetting_DispCurve_MapTo1 "CurSWSetting_DispCurve_MapTo(1)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show1 "CurSWSetting_DispCurve_Show(1)" //tyBool8 +#define name_CurSWSetting_DispCurve_MapTo2 "CurSWSetting_DispCurve_MapTo(2)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show2 "CurSWSetting_DispCurve_Show(2)" //tyBool8 +#define name_CurSWSetting_DispCurve_MapTo3 "CurSWSetting_DispCurve_MapTo(3)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show3 "CurSWSetting_DispCurve_Show(3)" //tyBool8 +#define name_CurSWSetting_DispCurve_MapTo4 "CurSWSetting_DispCurve_MapTo(4)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show4 "CurSWSetting_DispCurve_Show(4)" //tyBool8 +#define name_CurSWSetting_DispCurve_MapTo5 "CurSWSetting_DispCurve_MapTo(5)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show5 "CurSWSetting_DispCurve_Show(5)" //tyBool8 +#define name_CurSWSetting_DispCurve_MapTo6 "CurSWSetting_DispCurve_MapTo(6)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show6 "CurSWSetting_DispCurve_Show(6)" //tyBool8 +#define name_CurSWSetting_DispCurve_MapTo7 "CurSWSetting_DispCurve_MapTo(7)" //tyInt8 +#define name_CurSWSetting_DispCurve_Show7 "CurSWSetting_DispCurve_Show(7)" //tyBool8 +#define name_HW_Type "HW_Type" //tyAnsiString +#define name_HW_PartNo "HW_PartNo" //tyAnsiString +#define name_HW_Version "HW_Version" //tyAnsiString +#define name_HW_SerialNo "HW_SerialNo" //tyAnsiString +#define name_HW_Modules "HW_Modules" //tyInt8 +#define name_HWModule_TypeCode0 "HWModule_TypeCode(0)" //tyInt8 +#define name_HWModule_VersCode0 "HWModule_VersCode(0)" //tyInt8 +#define name_HWModule_TypeCode1 "HWModule_TypeCode(1)" //tyInt8 +#define name_HWModule_VersCode1 "HWModule_VersCode(1)" //tyInt8 +#define name_HWModule_TypeCode2 "HWModule_TypeCode(2)" //tyInt8 +#define name_HWModule_VersCode2 "HWModule_VersCode(2)" //tyInt8 +#define name_HW_BaseResolution "HW_BaseResolution" //tyFloat8 +#define name_HW_InpChannels "HW_InpChannels" //tyInt8 +#define name_HW_ExternalRefClock "HW_ExternalRefClock" //tyBool8 +#define name_HW_ExternalDevices "HW_ExternalDevices" //tyInt8 +#define name_HWSync_Divider "HWSync_Divider" //tyInt8 +#define name_HWSync_CFDLevel "HWSync_CFDLevel" //tyInt8 +#define name_HWSync_CFDZeroCross "HWSync_CFDZeroCross" //tyInt8 +#define name_HWSync_Offset "HWSync_Offset" //tyInt8 +#define name_HWInpChan_ModuleIdx0 "HWInpChan_ModuleIdx(0)" //tyInt8 +#define name_HWInpChan_CFDLevel0 "HWInpChan_CFDLevel(0)" //tyInt8 +#define name_HWInpChan_CFDZeroCross0 "HWInpChan_CFDZeroCross(0)" //tyInt8 +#define name_HWInpChan_Offset0 "HWInpChan_Offset(0)" //tyInt8 +#define name_HWInpChan_Enabled0 "HWInpChan_Enabled(0)" //tyBool8 +#define name_HWInpChan_ModuleIdx1 "HWInpChan_ModuleIdx(1)" //tyInt8 +#define name_HWInpChan_CFDLevel1 "HWInpChan_CFDLevel(1)" //tyInt8 +#define name_HWInpChan_CFDZeroCross1 "HWInpChan_CFDZeroCross(1)" //tyInt8 +#define name_HWInpChan_Offset1 "HWInpChan_Offset(1)" //tyInt8 +#define name_HWInpChan_Enabled1 "HWInpChan_Enabled(1)" //tyBool8 + +#define name_HW_Markers "HW_Markers" //tyInt8 +#define name_HWMarkers_RisingEdge0 "HWMarkers_RisingEdge(0)" //tyBool8 +#define name_HWMarkers_RisingEdge1 "HWMarkers_RisingEdge(1)" //tyBool8 +#define name_HWMarkers_RisingEdge2 "HWMarkers_RisingEdge(2)" //tyBool8 +#define name_HWMarkers_RisingEdge3 "HWMarkers_RisingEdge(3)" //tyBool8 +#define name_HWMarkers_Enabled0 "HWMarkers_Enabled(0)" //tyBool8 +#define name_HWMarkers_Enabled1 "HWMarkers_Enabled(1)" //tyBool8 +#define name_HWMarkers_Enabled2 "HWMarkers_Enabled(2)" //tyBool8 +#define name_HWMarkers_Enabled3 "HWMarkers_Enabled(3)" //tyBool8 +#define name_HWMarkers_HoldOff "HWMarkers_HoldOff" //tyInt8 + +#define name_TTResult_SyncRate "TTResult_SyncRate" //tyInt8 +#define name_TTResult_InputRate0 "TTResult_InputRate(0)" //tyInt8 +#define name_TTResult_InputRate1 "TTResult_InputRate(1)" //tyInt8 +#define name_TTResult_StopAfter "TTResult_StopAfter" //tyInt8 + + +// RecordTypes +#define rtPicoHarpT3 0x00010303 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $03 (PicoHarp) +#define rtPicoHarpT2 0x00010203 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $03 (PicoHarp) +#define rtHydraHarpT3 0x00010304 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $03 (T3), HW: $04 (HydraHarp) +#define rtHydraHarpT2 0x00010204 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $04 (HydraHarp) +#define rtHydraHarp2T3 0x01010304 // (SubID = $01 ,RecFmt: $01) (V2), T-Mode: $03 (T3), HW: $04 (HydraHarp) +#define rtHydraHarp2T2 0x01010204 // (SubID = $01 ,RecFmt: $01) (V2), T-Mode: $02 (T2), HW: $04 (HydraHarp) +#define rtTimeHarp260NT3 0x00010305 // (SubID = $00 ,RecFmt: $01) (V2), T-Mode: $03 (T3), HW: $05 (TimeHarp260N) +#define rtTimeHarp260NT2 0x00010205 // (SubID = $00 ,RecFmt: $01) (V2), T-Mode: $02 (T2), HW: $05 (TimeHarp260N) +#define rtTimeHarp260PT3 0x00010306 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T3), HW: $06 (TimeHarp260P) +#define rtTimeHarp260PT2 0x00010206 // (SubID = $00 ,RecFmt: $01) (V1), T-Mode: $02 (T2), HW: $06 (TimeHarp260P) // Enforce byte alignment to ensure cross-platform compatibility. //#pragma pack(4) + // Define the common file header. typedef struct { char Ident[16]; char FormatVersion[6]; } pq_header_t; +typedef struct { + char CreatorName[18]; + char CreatorVersion[12]; + char FileTime[20]; + //char CRLF[2];//seems to be unused for everyone, what does this even stand for? + char Comment[256]; + int32_t NumberOfCurves; + + + + /* Note that Records is the only difference between interactive and + * tttr main headers. Interactive calls this BitsPerHistogBin. + */ + + //booleans + uint32_t MeasDesc_StopOnOvfl; + uint32_t MeasDesc_Restart; + uint32_t CurSWSetting_DispLog; + uint32_t CurSWSetting_DispCurve_Show0; + uint32_t CurSWSetting_DispCurve_Show1; + uint32_t CurSWSetting_DispCurve_Show2; + uint32_t CurSWSetting_DispCurve_Show3; + uint32_t CurSWSetting_DispCurve_Show4; + uint32_t CurSWSetting_DispCurve_Show5; + uint32_t CurSWSetting_DispCurve_Show6; + uint32_t CurSWSetting_DispCurve_Show7; + uint32_t HW_ExternalRefClock; + uint32_t HWInpChan_Enabled0; + uint32_t HWInpChan_Enabled1; + uint32_t HWMarkers_RisingEdge0; + uint32_t HWMarkers_RisingEdge1; + uint32_t HWMarkers_RisingEdge2; + uint32_t HWMarkers_RisingEdge3; + uint32_t HWMarkers_Enabled0; + uint32_t HWMarkers_Enabled1; + uint32_t HWMarkers_Enabled2; + uint32_t HWMarkers_Enabled3; + + //int8 + int32_t Fast_Load_End; + int32_t Measurement_Mode; + int32_t Measurement_SubMode; + int32_t TTResult_StopReason; + int32_t TTResultFormat_TTTRRecType; + int32_t TTResultFormat_BitsPerRecord; + int32_t MeasDesc_BinningFactor; + int32_t MeasDesc_Offset; + int32_t MeasDesc_AcquisitionTime; + int32_t MeasDesc_StopAt; + int32_t CurSWSetting_DispAxisTimeFrom; + int32_t CurSWSetting_DispAxisTimeTo; + int32_t CurSWSetting_DispAxisCountFrom; + int32_t CurSWSetting_DispAxisCountTo; + int32_t CurSWSetting_DispCurves; + int32_t CurSWSetting_DispCurve_MapTo0; + int32_t CurSWSetting_DispCurve_MapTo1; + int32_t CurSWSetting_DispCurve_MapTo2; + int32_t CurSWSetting_DispCurve_MapTo3; + int32_t CurSWSetting_DispCurve_MapTo4; + int32_t CurSWSetting_DispCurve_MapTo5; + int32_t CurSWSetting_DispCurve_MapTo6; + int32_t CurSWSetting_DispCurve_MapTo7; + int32_t HW_Modules; + int32_t HWModule_TypeCode0; + int32_t HWModule_TypeCode1; + int32_t HWModule_TypeCode2; + int32_t HWModule_VersCode0; + int32_t HWModule_VersCode1; + int32_t HWModule_VersCode2; + int32_t HW_InpChannels; + int32_t HW_ExternalDevices; + int32_t HWSync_Divider; + int32_t HWSync_CFDLevel; + int32_t HWSync_CFDZeroCross; + int32_t HWSync_Offset; + int32_t HWInpChan_ModuleIdx0; + int32_t HWInpChan_ModuleIdx1; + int32_t HWInpChan_CFDLevel0; + int32_t HWInpChan_CFDLevel1; + int32_t HWInpChan_CFDZeroCross0; + int32_t HWInpChan_CFDZeroCross1; + int32_t HWInpChan_Offset0; + int32_t HWInpChan_Offset1; + int32_t HW_Markers; + int32_t HWMarkers_HoldOff; + int32_t TTResult_SyncRate; + int32_t TTResult_InputRate0; + int32_t TTResult_InputRate1; + int32_t TTResult_StopAfter; + int32_t TTResult_NumberOfRecords; + + //Float8 + float64_t HW_BaseResolution; + float64_t MeasDesc_Resolution; + float64_t MeasDesc_GlobalResolution; + + //AnsiStrings not in general part + char File_GUID[40]; + char File_AssuredContent[32]; + char CreatorSW_ContentVersion[4]; + char HW_Type[15]; + char HW_PartNo[8]; + char HW_Version[5]; + char HW_SerialNo[8]; + +/* + int32_t BitsPerRecord; + + int32_t ActiveCurve; + int32_t MeasurementMode; + int32_t SubMode; + int32_t Binning; + float64_t Resolution; + int32_t Offset; + int32_t AcquisitionTime; + int32_t StopAt; + int32_t StopOnOvfl; + int32_t Restart; + int32_t DisplayLinLog; + uint32_t DisplayTimeAxisFrom; + uint32_t DisplayTimeAxisTo; + uint32_t DisplayCountAxisFrom; + uint32_t DisplayCountAxisTo; + hh_v20_display_curve_t DisplayCurve[8]; + hh_v20_param_t Param[3]; + int32_t RepeatMode; + int32_t RepeatsPerCurve; + int32_t RepeatTime; + int32_t RepeatWaitTime; + char ScriptName[20]; + char HardwareIdent[16]; + char HardwarePartNo[8]; + int32_t HardwareSerial; + int32_t NumberOfModules; + hh_v20_module_t ModuleInfo[10]; + float64_t BaseResolution; + int64_t InputsEnabled; + int32_t InputChannelsPresent; + int32_t RefClockSource; + int32_t ExtDevices; + int32_t MarkerSettings; + int32_t SyncDivider; + int32_t SyncCFDLevel; + int32_t SyncCFDZeroCross; + int32_t SyncOffset; + + //whats up with this??? Why only point?? ******************************************* + hh_v20_input_channel_t *InpChan; + int32_t *InputRate;*/ +} ptu_header_t; + +// A Tag entry +typedef struct { + char Ident[32]; // Identifier of the tag + int Idx; // Index for multiple tags or -1 + uint32_t Typ; // Type of tag ty..... see const section + long long TagValue; // Value of tag. +} TagHead_t; + +time_t TDateTime_TimeT(double Convertee); + int pq_header_read(FILE *in_stream, pq_header_t *pq_header); +int ptu_header_parse(FILE *in_stream, ptu_header_t *ptu_header); + void pq_header_printf(FILE *out_stream, pq_header_t *pq_header); void pq_header_fwrite(FILE *out_stream, pq_header_t *pq_header); +void ptu_header_fwrite(FILE *stream_out, ptu_header_t *ptu_header); +void ptu_header_printf(FILE *out_stream, ptu_header_t *ptu_header); -#endif +#endif diff --git a/src/hydraharp/hh_v10_interactive.c b/src/hydraharp/hh_v10_interactive.c index 53959da..91ee83d 100644 --- a/src/hydraharp/hh_v10_interactive.c +++ b/src/hydraharp/hh_v10_interactive.c @@ -29,305 +29,305 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include - -#include "hh_v10.h" - -#include "../hydraharp.h" -#include "../header.h" -#include "../error.h" - -int hh_v10_interactive_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, hh_v10_header_t *hh_header, - options_t *options) { - int result; - hh_v10_interactive_t *interactive; - int i; - - /* Read interactive header. */ - result = hh_v10_interactive_header_read(stream_in, - hh_header, &interactive); - if ( result != PQ_SUCCESS ) { - error("Failed while reading interactive header.\n"); - } else { - if ( options->print_header ) { - if ( options->binary_out ) { - pq_header_fwrite(stream_out, pq_header); - hh_v10_header_fwrite(stream_out, hh_header); - hh_v10_interactive_header_fwrite(stream_out, hh_header, - interactive); - } else { - pq_header_printf(stream_out, pq_header); - hh_v10_header_printf(stream_out, hh_header); - hh_v10_interactive_header_printf(stream_out, hh_header, - interactive); - } - } else if ( options->print_resolution ) { - for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { - pq_resolution_print(stream_out, i, - interactive->Curve[i].Resolution, options); - } - } else { - /* Read and print interactive data. */ - result = hh_v10_interactive_data_read(stream_in, hh_header, - interactive); - - if ( result == PQ_SUCCESS ) { - hh_v10_interactive_data_print(stream_out, hh_header, - interactive, options); - } else { - error("Failed while reading interactive data.\n"); - } - - debug("Freeing interactive data.\n"); - hh_v10_interactive_data_free(hh_header, interactive); - } - } - - /* Clean and return. */ - - debug("Freeing interactive header.\n"); - hh_v10_interactive_header_free(&interactive); - return(result); -} - -int hh_v10_interactive_header_read(FILE *stream_in, - hh_v10_header_t *hh_header, - hh_v10_interactive_t **interactive) { - /* Read the static header data. This is a precusor to reading the - * curve data itself, but separating the two gives us a chance to - * do some simpler debugging. These can be merged in the future for - * speed, but for now speed is not an issue. - */ - size_t n_read; - - *interactive = (hh_v10_interactive_t *)malloc(sizeof(hh_v10_interactive_t)); - - if ( *interactive == NULL ) { - error("Could not allocate memory for interactive header.\n"); - hh_v10_interactive_header_free(interactive); - return(PQ_ERROR_MEM); - } - - (*interactive)->Curve = (hh_v10_curve_t *)malloc( - hh_header->NumberOfCurves*sizeof(hh_v10_curve_t)); - - if ( (*interactive)->Curve == NULL ) { - error("Could not allocate memory for curve headers.\n"); - hh_v10_interactive_header_free(interactive); - return(PQ_ERROR_MEM); - } - - debug("Reading curve header data.\n"); - n_read = fread((*interactive)->Curve, - sizeof(hh_v10_curve_t), - hh_header->NumberOfCurves, - stream_in); - - if ( n_read != hh_header->NumberOfCurves ) { - error("Could not read curve headers.\n"); - hh_v10_interactive_header_free(interactive); - return(PQ_ERROR_IO); - } - - return(PQ_SUCCESS); -} - -void hh_v10_interactive_header_free(hh_v10_interactive_t **interactive) { - if ( *interactive != NULL ) { - free((*interactive)->Curve); - free(*interactive); - } -} - -void hh_v10_interactive_header_printf(FILE *stream_out, - hh_v10_header_t *hh_header, - hh_v10_interactive_t *interactive) { - int i; - int j; - - for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { - fprintf(stream_out, "Curve[%d].CurveIndex = %"PRId32"\n", - i, interactive->Curve[i].CurveIndex); - fprintf(stream_out, "Curve[%d].TimeOfRecording = %s", - i, ctime32(&interactive->Curve[i].TimeOfRecording)); - fprintf(stream_out, "Curve[%d].HardwareIdent = %.*s\n", - i, - (int)sizeof(interactive->Curve[i].HardwareIdent), - interactive->Curve[i].HardwareIdent); - fprintf(stream_out, "Curve[%d].HardwareVersion = %.*s\n", - i, - (int)sizeof(interactive->Curve[i].HardwareVersion), - interactive->Curve[i].HardwareVersion); - fprintf(stream_out, "Curve[%d].HardwareSerial = %"PRId32"\n", - i, interactive->Curve[i].HardwareSerial); - fprintf(stream_out, "Curve[%d].NoOfModules = %"PRId32"\n", - i, interactive->Curve[i].NoOfModules); - - for ( j = 0; j < hh_header->NumberOfModules; j++ ) { - fprintf(stream_out, "Curve[%d].Module[%d].Model = %"PRId32"\n", - i, j, interactive->Curve[i].Module[j].Model); - fprintf(stream_out, "Curve[%d].Module[%d].Version = %"PRId32"\n", - i, j, interactive->Curve[i].Module[j].Version); - } - - fprintf(stream_out, "Curve[%d].BaseResolution = %"PRIf64"\n", - i, interactive->Curve[i].BaseResolution); - fprintf(stream_out, "Curve[%d].InputsEnabled = %"PRId64"\n", - i, interactive->Curve[i].InputsEnabled); - fprintf(stream_out, "Curve[%d].InpChanPresent = %"PRId32"\n", - i, interactive->Curve[i].InpChanPresent); - fprintf(stream_out, "Curve[%d].RefClockSource = %"PRId32"\n", - i, interactive->Curve[i].RefClockSource); - fprintf(stream_out, "Curve[%d].ExtDevices = %"PRId32"\n", - i, interactive->Curve[i].ExtDevices); - fprintf(stream_out, "Curve[%d].MarkerSettings = %"PRId32"\n", - i, interactive->Curve[i].MarkerSettings); - fprintf(stream_out, "Curve[%d].SyncDivider = %"PRId32"\n", - i, interactive->Curve[i].SyncDivider); - fprintf(stream_out, "Curve[%d].SyncCFDLevel = %"PRId32"\n", - i, interactive->Curve[i].SyncCFDLevel); - fprintf(stream_out, "Curve[%d].SyncCFDZero = %"PRId32"\n", - i, interactive->Curve[i].SyncCFDZero); - fprintf(stream_out, "Curve[%d].SyncOffset = %"PRId32"\n", - i, interactive->Curve[i].SyncOffset); - fprintf(stream_out, "Curve[%d].InpModuleIdx = %"PRId32"\n", - i, interactive->Curve[i].InpModuleIdx); - fprintf(stream_out, "Curve[%d].InpCFDLevel = %"PRId32"\n", - i, interactive->Curve[i].InpCFDLevel); - fprintf(stream_out, "Curve[%d].InpCFDZeroCross = %"PRId32"\n", - i, interactive->Curve[i].InpCFDZeroCross); - fprintf(stream_out, "Curve[%d].InpOffset = %"PRId32"\n", - i, interactive->Curve[i].InpOffset); - fprintf(stream_out, "Curve[%d].InpChannel = %"PRId32"\n", - i, interactive->Curve[i].InpChannel); - fprintf(stream_out, "Curve[%d].MeasMode = %"PRId32"\n", - i, interactive->Curve[i].MeasMode); - fprintf(stream_out, "Curve[%d].SubMode = %"PRId32"\n", - i, interactive->Curve[i].SubMode); - fprintf(stream_out, "Curve[%d].Binning = %"PRId32"\n", - i, interactive->Curve[i].Binning); - fprintf(stream_out, "Curve[%d].Resolution = %"PRIf64"\n", - i, interactive->Curve[i].Resolution); - fprintf(stream_out, "Curve[%d].Offset = %"PRId32"\n", - i, interactive->Curve[i].Offset); - fprintf(stream_out, "Curve[%d].AcquisitionTime = %"PRId32"\n", - i, interactive->Curve[i].AcquisitionTime); - fprintf(stream_out, "Curve[%d].StopAfter = %"PRId32"\n", - i, interactive->Curve[i].StopAfter); - fprintf(stream_out, "Curve[%d].StopReason = %"PRId32"\n", - i, interactive->Curve[i].StopReason); - fprintf(stream_out, "Curve[%d].P1 = %"PRIf32"\n", - i, interactive->Curve[i].P1); - fprintf(stream_out, "Curve[%d].P2 = %"PRIf32"\n", - i, interactive->Curve[i].P2); - fprintf(stream_out, "Curve[%d].P3 = %"PRIf32"\n", - i, interactive->Curve[i].P3); - fprintf(stream_out, "Curve[%d].SyncRate = %"PRId32"\n", - i, interactive->Curve[i].SyncRate); - fprintf(stream_out, "Curve[%d].InputRate = %"PRId32"\n", - i, interactive->Curve[i].InputRate); - fprintf(stream_out, "Curve[%d].HistCountRate = %"PRId32"\n", - i, interactive->Curve[i].HistCountRate); - fprintf(stream_out, "Curve[%d].IntegralCount = %"PRId64"\n", - i, interactive->Curve[i].IntegralCount); - fprintf(stream_out, "Curve[%d].HistogramBins = %"PRId32"\n", - i, interactive->Curve[i].HistogramBins); - fprintf(stream_out, "Curve[%d].DataOffset = %"PRId32"\n", - i, interactive->Curve[i].DataOffset); - } -} - -void hh_v10_interactive_header_fwrite(FILE *stream_out, - hh_v10_header_t *hh_header, - hh_v10_interactive_t *interactive) { - fwrite(interactive->Curve, - sizeof(hh_v10_curve_t), - hh_header->NumberOfCurves, - stream_out); -} - - -/* - * Interactive data. - */ -int hh_v10_interactive_data_read(FILE *stream_in, - hh_v10_header_t *hh_header, - hh_v10_interactive_t *interactive) { - /* Now the curve data. Allocate this as a 2d array and populate it - * as such. - */ - size_t n_read; - int i; - - interactive->Counts = (uint32_t **)malloc( - hh_header->NumberOfCurves*sizeof(uint32_t *)); - - if ( interactive->Counts == NULL ) { - error("Could not allocate memory for curve data.\n"); - return(PQ_ERROR_MEM); - } - - for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { - debug("Reading data for curve %d.\n", i); - interactive->Counts[i] = (uint32_t *)malloc( - interactive->Curve[i].HistogramBins*sizeof(uint32_t)); - - if ( interactive->Counts[i] == NULL ) { - error("Could not allocate memory for curve %d data.\n", i); - return(PQ_ERROR_MEM); - } - - n_read = fread(interactive->Counts[i], sizeof(uint32_t), - interactive->Curve[i].HistogramBins, stream_in); - if ( n_read != interactive->Curve[i].HistogramBins ) { - error("Could not read data for curve %d.\n", i); - return(PQ_ERROR_IO); - } - } - return(PQ_SUCCESS); -} - -void hh_v10_interactive_data_free(hh_v10_header_t *hh_header, - hh_v10_interactive_t *interactive) { - int i; - - if ( interactive->Counts != NULL ) { - for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { - free(interactive->Counts[i]); - } - - free(interactive->Counts); - } -} - -void hh_v10_interactive_data_print(FILE *stream_out, - hh_v10_header_t *hh_header, - hh_v10_interactive_t *interactive, - options_t *options) { - unsigned int i; - int j; - int64_t origin; - int64_t time_step; - pq_interactive_bin_t bin; - pq_interactive_bin_print_t print; - - if ( options->binary_out ) { - print = pq_interactive_bin_fwrite; - } else { - print = pq_interactive_bin_printf; - } - - for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { - bin.curve = i; - origin = (int64_t)(interactive->Curve[i].Offset*1e3); - time_step = (int64_t)round(interactive->Curve[i].Resolution); - for ( j = 0; j < interactive->Curve[i].HistogramBins; j++ ) { - bin.bin_left = origin + time_step*j; - bin.bin_right = bin.bin_left + time_step; - bin.counts = interactive->Counts[i][j]; - print(stream_out, &bin); - } - } -} + +#include "hh_v10.h" + +#include "../hydraharp.h" +#include "../header.h" +#include "../error.h" + +int hh_v10_interactive_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, hh_v10_header_t *hh_header, + options_t *options) { + int result; + hh_v10_interactive_t *interactive; + int i; + + /* Read interactive header. */ + result = hh_v10_interactive_header_read(stream_in, + hh_header, &interactive); + if ( result != PQ_SUCCESS ) { + error("Failed while reading interactive header.\n"); + } else { + if ( options->print_header ) { + if ( options->binary_out ) { + pq_header_fwrite(stream_out, pq_header); + hh_v10_header_fwrite(stream_out, hh_header); + hh_v10_interactive_header_fwrite(stream_out, hh_header, + interactive); + } else { + pq_header_printf(stream_out, pq_header); + hh_v10_header_printf(stream_out, hh_header); + hh_v10_interactive_header_printf(stream_out, hh_header, + interactive); + } + } else if ( options->print_resolution ) { + for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { + pq_resolution_print(stream_out, i, + interactive->Curve[i].Resolution, options); + } + } else { + /* Read and print interactive data. */ + result = hh_v10_interactive_data_read(stream_in, hh_header, + interactive); + + if ( result == PQ_SUCCESS ) { + hh_v10_interactive_data_print(stream_out, hh_header, + interactive, options); + } else { + error("Failed while reading interactive data.\n"); + } + + debug("Freeing interactive data.\n"); + hh_v10_interactive_data_free(hh_header, interactive); + } + } + + /* Clean and return. */ + + debug("Freeing interactive header.\n"); + hh_v10_interactive_header_free(&interactive); + return(result); +} + +int hh_v10_interactive_header_read(FILE *stream_in, + hh_v10_header_t *hh_header, + hh_v10_interactive_t **interactive) { + /* Read the static header data. This is a precusor to reading the + * curve data itself, but separating the two gives us a chance to + * do some simpler debugging. These can be merged in the future for + * speed, but for now speed is not an issue. + */ + size_t n_read; + + *interactive = (hh_v10_interactive_t *)malloc(sizeof(hh_v10_interactive_t)); + + if ( *interactive == NULL ) { + error("Could not allocate memory for interactive header.\n"); + hh_v10_interactive_header_free(interactive); + return(PQ_ERROR_MEM); + } + + (*interactive)->Curve = (hh_v10_curve_t *)malloc( + hh_header->NumberOfCurves*sizeof(hh_v10_curve_t)); + + if ( (*interactive)->Curve == NULL ) { + error("Could not allocate memory for curve headers.\n"); + hh_v10_interactive_header_free(interactive); + return(PQ_ERROR_MEM); + } + + debug("Reading curve header data.\n"); + n_read = fread((*interactive)->Curve, + sizeof(hh_v10_curve_t), + hh_header->NumberOfCurves, + stream_in); + + if ( n_read != hh_header->NumberOfCurves ) { + error("Could not read curve headers.\n"); + hh_v10_interactive_header_free(interactive); + return(PQ_ERROR_IO); + } + + return(PQ_SUCCESS); +} + +void hh_v10_interactive_header_free(hh_v10_interactive_t **interactive) { + if ( *interactive != NULL ) { + free((*interactive)->Curve); + free(*interactive); + } +} + +void hh_v10_interactive_header_printf(FILE *stream_out, + hh_v10_header_t *hh_header, + hh_v10_interactive_t *interactive) { + int i; + int j; + + for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { + fprintf(stream_out, "Curve[%d].CurveIndex = %"PRId32"\n", + i, interactive->Curve[i].CurveIndex); + fprintf(stream_out, "Curve[%d].TimeOfRecording = %s", + i, ctime32(&interactive->Curve[i].TimeOfRecording)); + fprintf(stream_out, "Curve[%d].HardwareIdent = %.*s\n", + i, + (int)sizeof(interactive->Curve[i].HardwareIdent), + interactive->Curve[i].HardwareIdent); + fprintf(stream_out, "Curve[%d].HardwareVersion = %.*s\n", + i, + (int)sizeof(interactive->Curve[i].HardwareVersion), + interactive->Curve[i].HardwareVersion); + fprintf(stream_out, "Curve[%d].HardwareSerial = %"PRId32"\n", + i, interactive->Curve[i].HardwareSerial); + fprintf(stream_out, "Curve[%d].NoOfModules = %"PRId32"\n", + i, interactive->Curve[i].NoOfModules); + + for ( j = 0; j < hh_header->NumberOfModules; j++ ) { + fprintf(stream_out, "Curve[%d].Module[%d].Model = %"PRId32"\n", + i, j, interactive->Curve[i].Module[j].Model); + fprintf(stream_out, "Curve[%d].Module[%d].Version = %"PRId32"\n", + i, j, interactive->Curve[i].Module[j].Version); + } + + fprintf(stream_out, "Curve[%d].BaseResolution = %"PRIf64"\n", + i, interactive->Curve[i].BaseResolution); + fprintf(stream_out, "Curve[%d].InputsEnabled = %"PRId64"\n", + i, interactive->Curve[i].InputsEnabled); + fprintf(stream_out, "Curve[%d].InpChanPresent = %"PRId32"\n", + i, interactive->Curve[i].InpChanPresent); + fprintf(stream_out, "Curve[%d].RefClockSource = %"PRId32"\n", + i, interactive->Curve[i].RefClockSource); + fprintf(stream_out, "Curve[%d].ExtDevices = %"PRId32"\n", + i, interactive->Curve[i].ExtDevices); + fprintf(stream_out, "Curve[%d].MarkerSettings = %"PRId32"\n", + i, interactive->Curve[i].MarkerSettings); + fprintf(stream_out, "Curve[%d].SyncDivider = %"PRId32"\n", + i, interactive->Curve[i].SyncDivider); + fprintf(stream_out, "Curve[%d].SyncCFDLevel = %"PRId32"\n", + i, interactive->Curve[i].SyncCFDLevel); + fprintf(stream_out, "Curve[%d].SyncCFDZero = %"PRId32"\n", + i, interactive->Curve[i].SyncCFDZero); + fprintf(stream_out, "Curve[%d].SyncOffset = %"PRId32"\n", + i, interactive->Curve[i].SyncOffset); + fprintf(stream_out, "Curve[%d].InpModuleIdx = %"PRId32"\n", + i, interactive->Curve[i].InpModuleIdx); + fprintf(stream_out, "Curve[%d].InpCFDLevel = %"PRId32"\n", + i, interactive->Curve[i].InpCFDLevel); + fprintf(stream_out, "Curve[%d].InpCFDZeroCross = %"PRId32"\n", + i, interactive->Curve[i].InpCFDZeroCross); + fprintf(stream_out, "Curve[%d].InpOffset = %"PRId32"\n", + i, interactive->Curve[i].InpOffset); + fprintf(stream_out, "Curve[%d].InpChannel = %"PRId32"\n", + i, interactive->Curve[i].InpChannel); + fprintf(stream_out, "Curve[%d].MeasMode = %"PRId32"\n", + i, interactive->Curve[i].MeasMode); + fprintf(stream_out, "Curve[%d].SubMode = %"PRId32"\n", + i, interactive->Curve[i].SubMode); + fprintf(stream_out, "Curve[%d].Binning = %"PRId32"\n", + i, interactive->Curve[i].Binning); + fprintf(stream_out, "Curve[%d].Resolution = %"PRIf64"\n", + i, interactive->Curve[i].Resolution); + fprintf(stream_out, "Curve[%d].Offset = %"PRId32"\n", + i, interactive->Curve[i].Offset); + fprintf(stream_out, "Curve[%d].AcquisitionTime = %"PRId32"\n", + i, interactive->Curve[i].AcquisitionTime); + fprintf(stream_out, "Curve[%d].StopAfter = %"PRId32"\n", + i, interactive->Curve[i].StopAfter); + fprintf(stream_out, "Curve[%d].StopReason = %"PRId32"\n", + i, interactive->Curve[i].StopReason); + fprintf(stream_out, "Curve[%d].P1 = %"PRIf32"\n", + i, interactive->Curve[i].P1); + fprintf(stream_out, "Curve[%d].P2 = %"PRIf32"\n", + i, interactive->Curve[i].P2); + fprintf(stream_out, "Curve[%d].P3 = %"PRIf32"\n", + i, interactive->Curve[i].P3); + fprintf(stream_out, "Curve[%d].SyncRate = %"PRId32"\n", + i, interactive->Curve[i].SyncRate); + fprintf(stream_out, "Curve[%d].InputRate = %"PRId32"\n", + i, interactive->Curve[i].InputRate); + fprintf(stream_out, "Curve[%d].HistCountRate = %"PRId32"\n", + i, interactive->Curve[i].HistCountRate); + fprintf(stream_out, "Curve[%d].IntegralCount = %"PRId64"\n", + i, interactive->Curve[i].IntegralCount); + fprintf(stream_out, "Curve[%d].HistogramBins = %"PRId32"\n", + i, interactive->Curve[i].HistogramBins); + fprintf(stream_out, "Curve[%d].DataOffset = %"PRId32"\n", + i, interactive->Curve[i].DataOffset); + } +} + +void hh_v10_interactive_header_fwrite(FILE *stream_out, + hh_v10_header_t *hh_header, + hh_v10_interactive_t *interactive) { + fwrite(interactive->Curve, + sizeof(hh_v10_curve_t), + hh_header->NumberOfCurves, + stream_out); +} + + +/* + * Interactive data. + */ +int hh_v10_interactive_data_read(FILE *stream_in, + hh_v10_header_t *hh_header, + hh_v10_interactive_t *interactive) { + /* Now the curve data. Allocate this as a 2d array and populate it + * as such. + */ + size_t n_read; + int i; + + interactive->Counts = (uint32_t **)malloc( + hh_header->NumberOfCurves*sizeof(uint32_t *)); + + if ( interactive->Counts == NULL ) { + error("Could not allocate memory for curve data.\n"); + return(PQ_ERROR_MEM); + } + + for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { + debug("Reading data for curve %d.\n", i); + interactive->Counts[i] = (uint32_t *)malloc( + interactive->Curve[i].HistogramBins*sizeof(uint32_t)); + + if ( interactive->Counts[i] == NULL ) { + error("Could not allocate memory for curve %d data.\n", i); + return(PQ_ERROR_MEM); + } + + n_read = fread(interactive->Counts[i], sizeof(uint32_t), + interactive->Curve[i].HistogramBins, stream_in); + if ( n_read != interactive->Curve[i].HistogramBins ) { + error("Could not read data for curve %d.\n", i); + return(PQ_ERROR_IO); + } + } + return(PQ_SUCCESS); +} + +void hh_v10_interactive_data_free(hh_v10_header_t *hh_header, + hh_v10_interactive_t *interactive) { + int i; + + if ( interactive->Counts != NULL ) { + for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { + free(interactive->Counts[i]); + } + + free(interactive->Counts); + } +} + +void hh_v10_interactive_data_print(FILE *stream_out, + hh_v10_header_t *hh_header, + hh_v10_interactive_t *interactive, + options_t *options) { + unsigned int i; + int j; + int64_t origin; + int64_t time_step; + pq_interactive_bin_t bin; + pq_interactive_bin_print_t print; + + if ( options->binary_out ) { + print = pq_interactive_bin_fwrite; + } else { + print = pq_interactive_bin_printf; + } + + for ( i = 0; i < hh_header->NumberOfCurves; i++ ) { + bin.curve = i; + origin = (int64_t)(interactive->Curve[i].Offset*1e3); + time_step = (int64_t)round(interactive->Curve[i].Resolution); + for ( j = 0; j < interactive->Curve[i].HistogramBins; j++ ) { + bin.bin_left = origin + time_step*j; + bin.bin_right = bin.bin_left + time_step; + bin.counts = interactive->Counts[i][j]; + print(stream_out, &bin); + } + } +} diff --git a/src/hydraharp/hh_v10_tttr.c b/src/hydraharp/hh_v10_tttr.c index 3a95897..1c74655 100644 --- a/src/hydraharp/hh_v10_tttr.c +++ b/src/hydraharp/hh_v10_tttr.c @@ -29,287 +29,287 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include - -#include "../hydraharp.h" -#include "hh_v10.h" - -#include "../error.h" - -void hh_v10_t2_init(hh_v10_header_t *hh_header, - hh_v10_tttr_header_t *tttr_header, - tttr_t *tttr) { - tttr->sync_channel = hh_header->InputChannelsPresent; - tttr->origin = 0; - tttr->overflows = 0; - tttr->overflow_increment = HH_T2_OVERFLOW / 2; - tttr->sync_rate = tttr_header->SyncRate; - tttr->resolution_float = HH_BASE_RESOLUTION; - tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); -} - -int hh_v10_t2_decode(FILE *stream_in, tttr_t *tttr, t2_t *t2) { - size_t n_read; - hh_v10_t2_record_t record; - - n_read = fread(&record, sizeof(record), 1, stream_in); - - if ( n_read != 1 ) { - if ( !feof(stream_in) ) { - error("Could not read t2 record.\n"); - return(PQ_ERROR_IO); - } else { - return(PQ_ERROR_EOF); - } - } else { - if ( record.special ) { - if ( record.channel == 63 ) { - /* Overflow */ - tttr->overflows++; - tttr->origin += tttr->overflow_increment; - return(PQ_RECORD_OVERFLOW); - } else if ( record.channel == 0 ) { - /* - * Sync record. - * Label the sync channel as 1 greater than the maximum - * signal channel index. - */ - t2->channel = tttr->sync_channel; - t2->time = tttr->origin + record.time/2; - return(PQ_RECORD_T2); - } else { - /* External marker. */ - t2->time = record.channel; - return(PQ_RECORD_MARKER); - } - } else { - /* See the ht2 documentation for this, but the gist is that - * the counts are registered at double the rate of the reported - * resolution, so one count is actually 0.5ps, not 1ps. Cut - * the integer values for time in half to get the correct - * result. - */ - t2->channel = record.channel; - t2->time = tttr->origin + record.time/2; - return(PQ_RECORD_T2); - } - } -} - -/* - * - * Reading and interpreting for t3 mode. - * - */ -void hh_v10_t3_init(hh_v10_header_t *hh_header, - hh_v10_tttr_header_t *tttr_header, - tttr_t *tttr) { - tttr->sync_channel = hh_header->InputChannelsPresent; - tttr->origin = 0; - tttr->overflows = 0; - tttr->overflow_increment = HH_T3_OVERFLOW; - tttr->sync_rate = tttr_header->SyncRate; - tttr->resolution_float = hh_header->Resolution*1e-12; - tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); -} - -int hh_v10_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { - size_t n_read; - hh_v10_t3_record_t record; - - n_read = fread(&record, sizeof(record), 1, stream_in); - - if ( n_read != 1 ) { - if ( !feof(stream_in) ) { - error("Could not read t3 record.\n"); - return(PQ_ERROR_IO); - } else { - return(PQ_ERROR_EOF); - } - } else { - if ( record.special ) { - if ( record.channel == 63 ) { - /* Overflow */ - tttr->overflows++;; - tttr->origin += HH_T3_OVERFLOW; - return(PQ_RECORD_OVERFLOW); - } else { - /* External marker. */ - t3->time = record.channel; - return(PQ_RECORD_MARKER); - } - } else { - t3->channel = record.channel; - t3->pulse = tttr->origin + record.nsync; - t3->time = record.dtime * tttr->resolution_int; - return(PQ_RECORD_T3); - } - } -} - -/* - * - * Streaming for t2 or t3 mode. - * - */ -int hh_v10_tttr_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, hh_v10_header_t *hh_header, - options_t *options) { - hh_v10_tttr_header_t *tttr_header; - int result; - - result = hh_v10_tttr_header_read(stream_in, &tttr_header); - - if ( result != PQ_SUCCESS ) { - error("Failed while reading tttr header.\n"); - return(result); - } else { - if ( options->print_header ) { - if ( options->binary_out ) { - pq_header_fwrite(stream_out, pq_header); - hh_v10_header_fwrite(stream_out, hh_header); - hh_v10_tttr_header_fwrite(stream_out, tttr_header); - } else { - pq_header_printf(stream_out, pq_header); - hh_v10_header_printf(stream_out, hh_header); - hh_v10_tttr_header_printf(stream_out, tttr_header); - } - - result = PQ_SUCCESS; - } else { - if ( hh_header->MeasurementMode == HH_MODE_T2 ) { - debug("Found mode ht2.\n"); - result = hh_v10_t2_stream(stream_in, stream_out, - hh_header, tttr_header, options); - } else if ( hh_header->MeasurementMode == HH_MODE_T3 ) { - debug("Found mode ht3.\n"); - result = hh_v10_t3_stream(stream_in, stream_out, - hh_header, tttr_header, options); - } else { - debug("Unrecognized mode.\n"); - result = PQ_ERROR_MODE; - } - } - } - - debug("Freeing tttr header.\n"); - hh_v10_tttr_header_free(&tttr_header); - return(result); -} - -int hh_v10_t2_stream(FILE *stream_in, FILE *stream_out, - hh_v10_header_t *hh_header, - hh_v10_tttr_header_t *tttr_header, options_t *options) { - tttr_t tttr; - - hh_v10_t2_init(hh_header, tttr_header, &tttr); - - if ( options->print_resolution ) { - pq_resolution_print(stream_out, -1, - tttr.resolution_float*1e12, options); - return(PQ_SUCCESS); - } else { - return(pq_t2_stream(stream_in, stream_out, - hh_v10_t2_decode, &tttr, options)); - } -} - -int hh_v10_t3_stream(FILE *stream_in, FILE *stream_out, - hh_v10_header_t *hh_header, - hh_v10_tttr_header_t *tttr_header, options_t *options) { - tttr_t tttr; - - hh_v10_t3_init(hh_header, tttr_header, &tttr); - - if ( options->print_resolution ) { - pq_resolution_print(stream_out, -1, - tttr.resolution_float*1e12, options); - return(PQ_SUCCESS); - } else { - return(pq_t3_stream(stream_in, stream_out, - hh_v10_t3_decode, &tttr, options)); - } -} - -/* - * - * Header for tttr mode (t2, t3) - * - */ -int hh_v10_tttr_header_read(FILE *stream_in, - hh_v10_tttr_header_t **tttr_header) { - size_t n_read; - - *tttr_header = (hh_v10_tttr_header_t *)malloc(sizeof(hh_v10_tttr_header_t)); - - if ( *tttr_header == NULL ) { - error("Could not allocate tttr header.\n"); - return(PQ_ERROR_MEM); - } - - n_read = fread(*tttr_header, - sizeof(hh_v10_tttr_header_t)-sizeof(uint32_t *), - 1, - stream_in); - if ( n_read != 1 ) { - error("Could not read tttr header.\n"); - hh_v10_tttr_header_free(tttr_header); - return(PQ_ERROR_IO); - } - - (*tttr_header)->ImgHdr = (uint32_t *)malloc( - (*tttr_header)->ImgHdrSize*sizeof(uint32_t)); - if ( (*tttr_header)->ImgHdr == NULL ) { - error("Could not allocate memory for tttr image header.\n"); - hh_v10_tttr_header_free(tttr_header); - return(PQ_ERROR_MEM); - } - - n_read = fread((*tttr_header)->ImgHdr, - sizeof(uint32_t), - (*tttr_header)->ImgHdrSize, - stream_in); - if ( n_read != (*tttr_header)->ImgHdrSize ) { - error("Could not read Hydraharp tttr image header.\n"); - hh_v10_tttr_header_free(tttr_header); - return(PQ_ERROR_IO); - } - - return(PQ_SUCCESS); -} - -void hh_v10_tttr_header_free(hh_v10_tttr_header_t **tttr_header) { - if ( *tttr_header != NULL ) { - free((*tttr_header)->ImgHdr); - free(*tttr_header); - } -} - -void hh_v10_tttr_header_printf(FILE *stream_out, - hh_v10_tttr_header_t *tttr_header) { - int i; - - fprintf(stream_out, "SyncRate = %"PRId32"\n", tttr_header->SyncRate); - fprintf(stream_out, "StopAfter = %"PRId32"\n", tttr_header->StopAfter); - fprintf(stream_out, "StopReason = %"PRId32"\n", tttr_header->StopReason); - fprintf(stream_out, "ImgHdrSize = %"PRId32"\n", tttr_header->ImgHdrSize); - fprintf(stream_out, "NumRecords = %"PRId64"\n", tttr_header->NumRecords); - - for ( i = 0; i < tttr_header->ImgHdrSize; i++ ) { - fprintf(stream_out, "ImgHdr[%d] = %"PRIu32"\n", i, - tttr_header->ImgHdr[i]); - } -} - -void hh_v10_tttr_header_fwrite(FILE *stream_out, - hh_v10_tttr_header_t *tttr_header) { - fwrite(tttr_header, - sizeof(hh_v10_tttr_header_t) - sizeof(uint32_t *), - 1, - stream_out); - fwrite(tttr_header->ImgHdr, - sizeof(uint32_t), - tttr_header->ImgHdrSize, - stream_out); -} +#include +#include + +#include "../hydraharp.h" +#include "hh_v10.h" + +#include "../error.h" + +void hh_v10_t2_init(hh_v10_header_t *hh_header, + hh_v10_tttr_header_t *tttr_header, + tttr_t *tttr) { + tttr->sync_channel = hh_header->InputChannelsPresent; + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = HH_T2_OVERFLOW / 2; + tttr->sync_rate = tttr_header->SyncRate; + tttr->resolution_float = HH_BASE_RESOLUTION; + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + +int hh_v10_t2_decode(FILE *stream_in, tttr_t *tttr, t2_t *t2) { + size_t n_read; + hh_v10_t2_record_t record; + + n_read = fread(&record, sizeof(record), 1, stream_in); + + if ( n_read != 1 ) { + if ( !feof(stream_in) ) { + error("Could not read t2 record.\n"); + return(PQ_ERROR_IO); + } else { + return(PQ_ERROR_EOF); + } + } else { + if ( record.special ) { + if ( record.channel == 63 ) { + /* Overflow */ + tttr->overflows++; + tttr->origin += tttr->overflow_increment; + return(PQ_RECORD_OVERFLOW); + } else if ( record.channel == 0 ) { + /* + * Sync record. + * Label the sync channel as 1 greater than the maximum + * signal channel index. + */ + t2->channel = tttr->sync_channel; + t2->time = tttr->origin + record.time/2; + return(PQ_RECORD_T2); + } else { + /* External marker. */ + t2->time = record.channel; + return(PQ_RECORD_MARKER); + } + } else { + /* See the ht2 documentation for this, but the gist is that + * the counts are registered at double the rate of the reported + * resolution, so one count is actually 0.5ps, not 1ps. Cut + * the integer values for time in half to get the correct + * result. + */ + t2->channel = record.channel; + t2->time = tttr->origin + record.time/2; + return(PQ_RECORD_T2); + } + } +} + +/* + * + * Reading and interpreting for t3 mode. + * + */ +void hh_v10_t3_init(hh_v10_header_t *hh_header, + hh_v10_tttr_header_t *tttr_header, + tttr_t *tttr) { + tttr->sync_channel = hh_header->InputChannelsPresent; + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = HH_T3_OVERFLOW; + tttr->sync_rate = tttr_header->SyncRate; + tttr->resolution_float = hh_header->Resolution*1e-12; + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + +int hh_v10_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { + size_t n_read; + hh_v10_t3_record_t record; + + n_read = fread(&record, sizeof(record), 1, stream_in); + + if ( n_read != 1 ) { + if ( !feof(stream_in) ) { + error("Could not read t3 record.\n"); + return(PQ_ERROR_IO); + } else { + return(PQ_ERROR_EOF); + } + } else { + if ( record.special ) { + if ( record.channel == 63 ) { + /* Overflow */ + tttr->overflows++;; + tttr->origin += HH_T3_OVERFLOW; + return(PQ_RECORD_OVERFLOW); + } else { + /* External marker. */ + t3->time = record.channel; + return(PQ_RECORD_MARKER); + } + } else { + t3->channel = record.channel; + t3->pulse = tttr->origin + record.nsync; + t3->time = record.dtime * tttr->resolution_int; + return(PQ_RECORD_T3); + } + } +} + +/* + * + * Streaming for t2 or t3 mode. + * + */ +int hh_v10_tttr_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, hh_v10_header_t *hh_header, + options_t *options) { + hh_v10_tttr_header_t *tttr_header; + int result; + + result = hh_v10_tttr_header_read(stream_in, &tttr_header); + + if ( result != PQ_SUCCESS ) { + error("Failed while reading tttr header.\n"); + return(result); + } else { + if ( options->print_header ) { + if ( options->binary_out ) { + pq_header_fwrite(stream_out, pq_header); + hh_v10_header_fwrite(stream_out, hh_header); + hh_v10_tttr_header_fwrite(stream_out, tttr_header); + } else { + pq_header_printf(stream_out, pq_header); + hh_v10_header_printf(stream_out, hh_header); + hh_v10_tttr_header_printf(stream_out, tttr_header); + } + + result = PQ_SUCCESS; + } else { + if ( hh_header->MeasurementMode == HH_MODE_T2 ) { + debug("Found mode ht2.\n"); + result = hh_v10_t2_stream(stream_in, stream_out, + hh_header, tttr_header, options); + } else if ( hh_header->MeasurementMode == HH_MODE_T3 ) { + debug("Found mode ht3.\n"); + result = hh_v10_t3_stream(stream_in, stream_out, + hh_header, tttr_header, options); + } else { + debug("Unrecognized mode.\n"); + result = PQ_ERROR_MODE; + } + } + } + + debug("Freeing tttr header.\n"); + hh_v10_tttr_header_free(&tttr_header); + return(result); +} + +int hh_v10_t2_stream(FILE *stream_in, FILE *stream_out, + hh_v10_header_t *hh_header, + hh_v10_tttr_header_t *tttr_header, options_t *options) { + tttr_t tttr; + + hh_v10_t2_init(hh_header, tttr_header, &tttr); + + if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + tttr.resolution_float*1e12, options); + return(PQ_SUCCESS); + } else { + return(pq_t2_stream(stream_in, stream_out, + hh_v10_t2_decode, &tttr, options)); + } +} + +int hh_v10_t3_stream(FILE *stream_in, FILE *stream_out, + hh_v10_header_t *hh_header, + hh_v10_tttr_header_t *tttr_header, options_t *options) { + tttr_t tttr; + + hh_v10_t3_init(hh_header, tttr_header, &tttr); + + if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + tttr.resolution_float*1e12, options); + return(PQ_SUCCESS); + } else { + return(pq_t3_stream(stream_in, stream_out, + hh_v10_t3_decode, &tttr, options)); + } +} + +/* + * + * Header for tttr mode (t2, t3) + * + */ +int hh_v10_tttr_header_read(FILE *stream_in, + hh_v10_tttr_header_t **tttr_header) { + size_t n_read; + + *tttr_header = (hh_v10_tttr_header_t *)malloc(sizeof(hh_v10_tttr_header_t)); + + if ( *tttr_header == NULL ) { + error("Could not allocate tttr header.\n"); + return(PQ_ERROR_MEM); + } + + n_read = fread(*tttr_header, + sizeof(hh_v10_tttr_header_t)-sizeof(uint32_t *), + 1, + stream_in); + if ( n_read != 1 ) { + error("Could not read tttr header.\n"); + hh_v10_tttr_header_free(tttr_header); + return(PQ_ERROR_IO); + } + + (*tttr_header)->ImgHdr = (uint32_t *)malloc( + (*tttr_header)->ImgHdrSize*sizeof(uint32_t)); + if ( (*tttr_header)->ImgHdr == NULL ) { + error("Could not allocate memory for tttr image header.\n"); + hh_v10_tttr_header_free(tttr_header); + return(PQ_ERROR_MEM); + } + + n_read = fread((*tttr_header)->ImgHdr, + sizeof(uint32_t), + (*tttr_header)->ImgHdrSize, + stream_in); + if ( n_read != (*tttr_header)->ImgHdrSize ) { + error("Could not read Hydraharp tttr image header.\n"); + hh_v10_tttr_header_free(tttr_header); + return(PQ_ERROR_IO); + } + + return(PQ_SUCCESS); +} + +void hh_v10_tttr_header_free(hh_v10_tttr_header_t **tttr_header) { + if ( *tttr_header != NULL ) { + free((*tttr_header)->ImgHdr); + free(*tttr_header); + } +} + +void hh_v10_tttr_header_printf(FILE *stream_out, + hh_v10_tttr_header_t *tttr_header) { + int i; + + fprintf(stream_out, "SyncRate = %"PRId32"\n", tttr_header->SyncRate); + fprintf(stream_out, "StopAfter = %"PRId32"\n", tttr_header->StopAfter); + fprintf(stream_out, "StopReason = %"PRId32"\n", tttr_header->StopReason); + fprintf(stream_out, "ImgHdrSize = %"PRId32"\n", tttr_header->ImgHdrSize); + fprintf(stream_out, "NumRecords = %"PRId64"\n", tttr_header->NumRecords); + + for ( i = 0; i < tttr_header->ImgHdrSize; i++ ) { + fprintf(stream_out, "ImgHdr[%d] = %"PRIu32"\n", i, + tttr_header->ImgHdr[i]); + } +} + +void hh_v10_tttr_header_fwrite(FILE *stream_out, + hh_v10_tttr_header_t *tttr_header) { + fwrite(tttr_header, + sizeof(hh_v10_tttr_header_t) - sizeof(uint32_t *), + 1, + stream_out); + fwrite(tttr_header->ImgHdr, + sizeof(uint32_t), + tttr_header->ImgHdrSize, + stream_out); +} diff --git a/src/hydraharp/hh_v20.c b/src/hydraharp/hh_v20.c index 6bcec5b..0be3690 100644 --- a/src/hydraharp/hh_v20.c +++ b/src/hydraharp/hh_v20.c @@ -83,7 +83,7 @@ int hh_v20_dispatch(FILE *stream_in, FILE *stream_out, pq_header_t *pq_header, * Routines for the header common to all files. * */ -hh_v20_header_t *hh_v20_header_alloc(int input_channels) { +hh_v20_header_t *hh_v20_header_alloc(int input_channels) {//it seems no one ever calls this??????????? hh_v20_header_t *hh_header = NULL; hh_header = (hh_v20_header_t *)malloc(sizeof(hh_v20_header_t)); diff --git a/src/hydraharp/hh_v20.h b/src/hydraharp/hh_v20.h index 0b32ebc..5c1bdde 100644 --- a/src/hydraharp/hh_v20.h +++ b/src/hydraharp/hh_v20.h @@ -246,4 +246,10 @@ void hh_v20_tttr_header_printf(FILE *stream_out, void hh_v20_tttr_header_fwrite(FILE *stream_out, hh_v20_tttr_header_t *tttr_header); +int ptu_hh_v20_t2_stream(FILE *stream_in, FILE *stream_out, + ptu_header_t *ptu_header, options_t *options); + +int ptu_hh_v20_t3_stream(FILE *stream_in, FILE *stream_out, + ptu_header_t *ptu_header, options_t *options); + #endif diff --git a/src/hydraharp/hh_v20_tttr.c b/src/hydraharp/hh_v20_tttr.c index 293fa7c..5414613 100644 --- a/src/hydraharp/hh_v20_tttr.c +++ b/src/hydraharp/hh_v20_tttr.c @@ -49,6 +49,16 @@ void hh_v20_t2_init(hh_v20_header_t *hh_header, tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); } +void ptu_hh_v20_t2_init(ptu_header_t *header,tttr_t *tttr) { + tttr->sync_channel = header->HW_InpChannels; + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = HH_T2_OVERFLOW; + tttr->sync_rate = header->TTResult_SyncRate; + tttr->resolution_float = header->MeasDesc_GlobalResolution;//was *1e-12 but I think that's wrong because it's built in + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + int hh_v20_t2_decode(FILE *stream_in, tttr_t *tttr, t2_t *t2) { size_t n_read; hh_v20_t2_record_t record; @@ -115,6 +125,16 @@ void hh_v20_t3_init(hh_v20_header_t *hh_header, tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); } +void ptu_hh_v20_t3_init(ptu_header_t *header,tttr_t *tttr) { + tttr->sync_channel = header->HW_InpChannels; + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = HH_T3_OVERFLOW; + tttr->sync_rate = header->TTResult_SyncRate; + tttr->resolution_float = header->MeasDesc_BinningFactor*1e-12;//was *1e-12 but I think that's wrong because it's built in + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + int hh_v20_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { size_t n_read; hh_v20_t3_record_t record; @@ -233,6 +253,38 @@ int hh_v20_t3_stream(FILE *stream_in, FILE *stream_out, } } +int ptu_hh_v20_t2_stream(FILE *stream_in, FILE *stream_out, + ptu_header_t *ptu_header, options_t *options){ + tttr_t tttr; + + ptu_hh_v20_t2_init(ptu_header, &tttr);//write + + if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + tttr.resolution_float*1e12, options); + return(PQ_SUCCESS); + } else { + return(pq_t2_stream(stream_in, stream_out, + hh_v20_t2_decode, &tttr, options));//is this the right decode? + } +} + +int ptu_hh_v20_t3_stream(FILE *stream_in, FILE *stream_out, + ptu_header_t *ptu_header, options_t *options){ + tttr_t tttr; + + ptu_hh_v20_t3_init(ptu_header, &tttr);//write + + if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + tttr.resolution_float*1e12, options); + return(PQ_SUCCESS); + } else { + return(pq_t3_stream(stream_in, stream_out, + hh_v20_t3_decode, &tttr, options));//is this the right decode? + } +} + /* * * Header for tttr mode (t2, t3) diff --git a/src/options.c b/src/options.c index db3574d..5e5cbd6 100644 --- a/src/options.c +++ b/src/options.c @@ -61,7 +61,7 @@ void description() { " v6.0 (thd, t3r)\n" "Picoharp: v2.0 (phd, pt2, pt3)\n" "Hydraharp: v1.0 (hhd, ht2, ht3)\n" -" v2.0 (hhd, ht2, ht3)\n" +" v2.0 (hhd, ht2, ht3, ptu)\n" "\n" "Data formats (csv):\n" "(times are integers in picoseconds, bin edges are floats in nanoseconds)\n" diff --git a/src/picoquant.c b/src/picoquant.c index 4879038..f81f52b 100644 --- a/src/picoquant.c +++ b/src/picoquant.c @@ -53,14 +53,17 @@ int pq_dispatch(FILE *stream_in, FILE *stream_out, options_t *options) { pq_dispatch_t dispatch; /* Do the actual work, if we have no errors. */ - result = pq_header_read(stream_in, &pq_header); + result = pq_header_read(stream_in, &pq_header);//replace this to return dispatch + // make it handle ptu --> decided to leave this, let it recognize ptu magic if ( result ) { error("Could not read string header.\n"); } else { dispatch = pq_dispatch_get(options, &pq_header); if ( dispatch == NULL ) { - error("Could not identify board %s.\n", pq_header.Ident); + error("Could not identify board %s.\n", pq_header.Ident, pq_header.FormatVersion, ftell);//changed + //trying to find out if after original bit we're in the right place to start using the ptu + //header read } else { result = dispatch(stream_in, stream_out, &pq_header, options); @@ -72,7 +75,9 @@ int pq_dispatch(FILE *stream_in, FILE *stream_out, options_t *options) { pq_dispatch_t pq_dispatch_get(options_t *options, pq_header_t *pq_header) { - if ( ! strcmp(pq_header->Ident, "PicoHarp 300") ) { + if ( ! strcmp(pq_header->Ident, "PQTTTR")){ + return(ptu_dispatch);//changed + } else if ( ! strcmp(pq_header->Ident, "PicoHarp 300") ) { return(ph_dispatch); } else if ( ! strcmp(pq_header->Ident, "TimeHarp 200") ) { return(th_dispatch); diff --git a/src/picoquant.h b/src/picoquant.h index 7a917bb..9f154d5 100644 --- a/src/picoquant.h +++ b/src/picoquant.h @@ -38,10 +38,16 @@ #include "options.h" #include "header.h" +#include "ptu_dispatch.h" + + + + #include "interactive.h" #include "t2.h" #include "t3.h" + // General board and mode dispatch typedef int (*pq_dispatch_t)(FILE *, FILE *, pq_header_t *, options_t *); diff --git a/src/picoquant_main.c b/src/picoquant_main.c index 1997593..bc8de5a 100644 --- a/src/picoquant_main.c +++ b/src/picoquant_main.c @@ -36,6 +36,7 @@ #include "options.h" #include "picoquant.h" #include "files.h" +//#include "ptu_dispatch.h" int main(int argc, char *argv[]) { /* This software is designed to read in Picoquant data files and diff --git a/src/ptu_dispatch.c b/src/ptu_dispatch.c new file mode 100644 index 0000000..8becac3 --- /dev/null +++ b/src/ptu_dispatch.c @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2011-2014, Thomas Bischof + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the Massachusetts Institute of Technology nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include "hydraharp.h" + +#include "hydraharp/hh_v20.h" + +//for when eventually we can also handle ptu from other hardware +#include "hydraharp/hh_v10.h" + +#include "picoharp.h" +#include "picoharp/ph_v20.h" + +#include "timeharp.h" +#include "timeharp/th_v20.h" +#include "timeharp/th_v30.h" +#include "timeharp/th_v50.h" +#include "timeharp/th_v60.h" + +#include "error.h" + + +//from ptu demo: +#include +#include + +#include +#include +#include + + +bool IsT2; +long long RecNum; +long long oflcorrection; +long long truensync, truetime; +int m, c; +double GlobRes = 0.0; +double Resolution = 0.0; +uint32_t dlen = 0; +uint32_t cnt_0=0, cnt_1=0; + +/*void tttr_init(ptu_header_t *ptu_header, tttr_t *tttr) { + tttr->sync_channel = ptu_header.InputChannelsPresent; + tttr->origin = 0; + tttr->overflows = 0; + if isT2{ + tttr->overflow_increment = HH_T2_OVERFLOW; + tttr->resolution_float = HH_BASE_RESOLUTION; + } else { + tttr->overflow_increment = HH_T3_OVERFLOW; + tttr->resolution_float = ptu_header.Resolution*1e-12; + }//NOW THIS ONLY WORKS FOR HH + tttr->sync_rate = tttr_header->SyncRate; + + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +}*/ + +// TDateTime (in file) to time_t (standard C) conversion + + + + + +int ptu_dispatch(FILE *in_stream, FILE *out_stream, pq_header_t *pq_header, + options_t *options) { + int result; + //reparse header as a ptu file + ptu_header_t ptu_header; + + result = ptu_header_parse(in_stream, &ptu_header); + + if ( result ) { + error("Could not read string header.\n"); + } + //find hardware + uint32_t isT2 = get_recordtype(ptu_header.TTResultFormat_TTTRRecType);//need to define header, record type + + sprintf(pq_header->FormatVersion, "%s", ptu_header.CreatorSW_ContentVersion); + + //read it! + //if ( isT2 == '\0' ) { + // error("Board is not supported in ptu format: %s.\n", pq_header->Ident, pq_header->FormatVersion, ftell); + //} else + if ( options->print_header ) { + if ( options->binary_out ) { + ptu_header_fwrite(out_stream, &ptu_header); + } else { + ptu_header_printf(out_stream, &ptu_header); + } + } else if ( options->print_mode ) { + if ( ptu_header.Measurement_Mode == HH_MODE_INTERACTIVE ) { + fprintf(out_stream, "interactive\n"); + } else if ( ptu_header.Measurement_Mode == HH_MODE_T2 ) { + fprintf(out_stream, "t2\n"); + } else if ( ptu_header.Measurement_Mode == HH_MODE_T3 ) { + fprintf(out_stream, "t3\n"); + } else { + error("Measurement mode not recognized: %"PRId32".\n", + ptu_header.Measurement_Mode); + return(PQ_ERROR_MODE); + } + } else if ( isT2) { + result = ptu_hh_v20_t2_stream(in_stream, out_stream, &ptu_header, options); + } else { + result = ptu_hh_v20_t3_stream(in_stream, out_stream, &ptu_header, options); + } + + return(result); +} + + +int get_recordtype(int32_t RecordType){ //only hydraharp gives correct version + // TTTR Record type + switch (RecordType) + { + case rtHydraHarp2T2: + return true;//DOES THIS WORK? + //return ptu_hh_v20_t2_stream(FILE *stream_in, FILE *stream_out, + // ptu_header_t *ptu_header, options_t *options) + //fprintf(fpout, "HydraHarp V2 T2 data\n"); + //fprintf(fpout,"\nrecord# chan nsync truetime/ps\n"); + break; + case rtHydraHarp2T3: + return false;//DOES THIS WORK? + //return ptu_hh_v20_t3_stream(FILE *stream_in, FILE *stream_out, + // ptu_header_t *ptu_header, options_t *options) + //fprintf(fpout, "HydraHarp V2 T3 data\n"); + //fprintf(fpout,"\nrecord# chan nsync truetime/ns dtime\n"); + break; + + //need to add errors + + /* + + //none of the rest of these are set up, only hydraharp + case rtPicoHarpT2: + fprintf(fpout, "PicoHarp T2 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ps\n"); + return(ph_dispatch); + break; + case rtPicoHarpT3: + fprintf(fpout, "PicoHarp T3 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ns dtime\n"); + return(ph_dispatch); + break; + case rtHydraHarpT2: + fprintf(fpout, "HydraHarp V1 T2 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ps\n"); + return(hh_dispatch); + break; + case rtHydraHarpT3: + fprintf(fpout, "HydraHarp V1 T3 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ns dtime\n"); + return(hh_dispatch); + break; + case rtTimeHarp260NT3: + fprintf(fpout, "TimeHarp260N T3 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ns dtime\n"); + return(th_dispatch); + break; + case rtTimeHarp260NT2: + fprintf(fpout, "TimeHarp260N T2 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ps\n"); + return(th_dispatch); + break; + case rtTimeHarp260PT3: + fprintf(fpout, "TimeHarp260P T3 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ns dtime\n"); + return(th_dispatch); + break; + case rtTimeHarp260PT2: + fprintf(fpout, "TimeHarp260P T2 data\n"); + fprintf(fpout,"\nrecord# chan nsync truetime/ps\n"); + return(th_dispatch); + break;*/ + default: + error("Unknown record type: 0x%X\n 0x%X\n ", RecordType); + return '\0'; + } + +} diff --git a/src/ptu_dispatch.h b/src/ptu_dispatch.h new file mode 100644 index 0000000..a9ead26 --- /dev/null +++ b/src/ptu_dispatch.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2011-2014, Thomas Bischof + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the Massachusetts Institute of Technology nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef PTU_DISPATCH_H_ +#define PTU_DISPATCH_H_ + +#include +#include "picoquant.h" +#include "tttr.h" + + + + +int ptu_dispatch(FILE *in_stream, FILE *out_stream, pq_header_t *pq_header, options_t *options); + + + +int get_recordtype(int32_t RecordType); + +#endif diff --git a/src/timeharp/th_v20_interactive.c b/src/timeharp/th_v20_interactive.c index eff1c5a..3be87cb 100644 --- a/src/timeharp/th_v20_interactive.c +++ b/src/timeharp/th_v20_interactive.c @@ -29,195 +29,195 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include - -#include "../timeharp.h" -#include "th_v20.h" - -#include "../error.h" -#include "../interactive.h" - -/* - * - * Streaming for interactive files. - * - */ -int th_v20_interactive_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v20_header_t *th_header, - options_t *options) { - int result; - th_v20_interactive_t *interactive; - int i; - - /* Read interactive header. */ - result = th_v20_interactive_read(stream_in, th_header, &interactive); - if ( result != PQ_SUCCESS ) { - error("Failed while reading interactive header.\n"); - } else { - if ( options->print_header ) { - pq_header_printf(stream_out, pq_header); - th_v20_header_printf(stream_out, th_header); - th_v20_interactive_header_printf(stream_out, th_header, - &interactive); - } else if ( options->print_resolution ) { - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - pq_resolution_print(stream_out, i, - (interactive[i].Resolution*1e3), options); - } - } else { - /* Read and print interactive data. */ - th_v20_interactive_data_print(stream_out, th_header, - &interactive, options); - } - } - - /* Clean and return. */ - debug("Freeing interactive header.\n"); - th_v20_interactive_free(th_header, &interactive); - return(PQ_SUCCESS); -} - -int th_v20_interactive_read(FILE *stream_in, - th_v20_header_t *th_header, - th_v20_interactive_t **interactive) { - int i; - size_t n_read; - - *interactive = (th_v20_interactive_t *)malloc(sizeof(th_v20_interactive_t)* - th_header->NumberOfCurves); - if ( *interactive == NULL ) { - error("Could not allocate interactive data.\n"); - return(PQ_ERROR_MEM); - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - n_read = fread(&(*interactive)[i], - sizeof(th_v20_interactive_t) - sizeof(uint32_t *), 1, - stream_in); - if ( n_read != 1 ) { - error("Could not read data for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - - (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* - th_header->NumberOfChannels); - if ( (*interactive)[i].Counts == NULL ) { - error("Could not allocate memory for counts of curve " - "%"PRId32".\n", i); - return(PQ_ERROR_MEM); - } - - n_read = fread((*interactive)[i].Counts, - sizeof(uint32_t), th_header->NumberOfChannels, - stream_in); - if ( n_read != th_header->NumberOfChannels ) { - error("Could not read counts for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - } - - return(PQ_SUCCESS); -} - -void th_v20_interactive_free(th_v20_header_t *th_header, - th_v20_interactive_t **interactive) { - int i; - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - free((*interactive)[i].Counts); - } - free(*interactive); -} - - -void th_v20_interactive_header_printf(FILE *stream_out, - th_v20_header_t *th_header, - th_v20_interactive_t **interactive) { - int i; - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", - i, (*interactive)[i].CurveIndex); - fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", - i, ctime32(&(*interactive)[i].TimeOfRecording)); - fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", - i, (*interactive)[i].BoardSerial); - fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", - i, (*interactive)[i].CFDZeroCross); - fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", - i, (*interactive)[i].CFDDiscrMin); - fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", - i, (*interactive)[i].SyncLevel); - fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", - i, (*interactive)[i].CurveOffset); - fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", - i, (*interactive)[i].RoutingChannel); - fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", - i, (*interactive)[i].SubMode); - fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", - i, (*interactive)[i].MeasMode); - fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", - i, (*interactive)[i].P1); - fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", - i, (*interactive)[i].P2); - fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", - i, (*interactive)[i].P3); - fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", - i, (*interactive)[i].RangeNo); - fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", - i, (*interactive)[i].Offset); - fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", - i, (*interactive)[i].AcquisitionTime); - fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", - i, (*interactive)[i].StopAfter); - fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", - i, (*interactive)[i].StopReason); - fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", - i, (*interactive)[i].SyncRate); - fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", - i, (*interactive)[i].CFDCountRate); - fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", - i, (*interactive)[i].TDCCountRate); - fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", - i, (*interactive)[i].IntegralCount); - fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", - i, (*interactive)[i].Resolution); - fprintf(stream_out, "Crv[%d].reserve1 = %"PRId32"\n", - i, (*interactive)[i].reserve1); - fprintf(stream_out, "Crv[%d].reserve2 = %"PRId32"\n", - i, (*interactive)[i].reserve2); - } -} - -void th_v20_interactive_data_print(FILE *stream_out, - th_v20_header_t *th_header, - th_v20_interactive_t **interactive, - options_t *options) { - unsigned int i; - int j; - pq_interactive_bin_t bin; - float64_t origin; - float64_t time_step; - - pq_interactive_bin_print_t print; - - if ( options->binary_out ) { - print = pq_interactive_bin_fwrite; - } else { - print = pq_interactive_bin_printf; - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - bin.curve = i; - - origin = (*interactive)[i].Offset*1e3; +#include +#include + +#include "../timeharp.h" +#include "th_v20.h" + +#include "../error.h" +#include "../interactive.h" + +/* + * + * Streaming for interactive files. + * + */ +int th_v20_interactive_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v20_header_t *th_header, + options_t *options) { + int result; + th_v20_interactive_t *interactive; + int i; + + /* Read interactive header. */ + result = th_v20_interactive_read(stream_in, th_header, &interactive); + if ( result != PQ_SUCCESS ) { + error("Failed while reading interactive header.\n"); + } else { + if ( options->print_header ) { + pq_header_printf(stream_out, pq_header); + th_v20_header_printf(stream_out, th_header); + th_v20_interactive_header_printf(stream_out, th_header, + &interactive); + } else if ( options->print_resolution ) { + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + pq_resolution_print(stream_out, i, + (interactive[i].Resolution*1e3), options); + } + } else { + /* Read and print interactive data. */ + th_v20_interactive_data_print(stream_out, th_header, + &interactive, options); + } + } + + /* Clean and return. */ + debug("Freeing interactive header.\n"); + th_v20_interactive_free(th_header, &interactive); + return(PQ_SUCCESS); +} + +int th_v20_interactive_read(FILE *stream_in, + th_v20_header_t *th_header, + th_v20_interactive_t **interactive) { + int i; + size_t n_read; + + *interactive = (th_v20_interactive_t *)malloc(sizeof(th_v20_interactive_t)* + th_header->NumberOfCurves); + if ( *interactive == NULL ) { + error("Could not allocate interactive data.\n"); + return(PQ_ERROR_MEM); + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + n_read = fread(&(*interactive)[i], + sizeof(th_v20_interactive_t) - sizeof(uint32_t *), 1, + stream_in); + if ( n_read != 1 ) { + error("Could not read data for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + + (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* + th_header->NumberOfChannels); + if ( (*interactive)[i].Counts == NULL ) { + error("Could not allocate memory for counts of curve " + "%"PRId32".\n", i); + return(PQ_ERROR_MEM); + } + + n_read = fread((*interactive)[i].Counts, + sizeof(uint32_t), th_header->NumberOfChannels, + stream_in); + if ( n_read != th_header->NumberOfChannels ) { + error("Could not read counts for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + } + + return(PQ_SUCCESS); +} + +void th_v20_interactive_free(th_v20_header_t *th_header, + th_v20_interactive_t **interactive) { + int i; + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + free((*interactive)[i].Counts); + } + free(*interactive); +} + + +void th_v20_interactive_header_printf(FILE *stream_out, + th_v20_header_t *th_header, + th_v20_interactive_t **interactive) { + int i; + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", + i, (*interactive)[i].CurveIndex); + fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", + i, ctime32(&(*interactive)[i].TimeOfRecording)); + fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", + i, (*interactive)[i].BoardSerial); + fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", + i, (*interactive)[i].CFDZeroCross); + fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", + i, (*interactive)[i].CFDDiscrMin); + fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", + i, (*interactive)[i].SyncLevel); + fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", + i, (*interactive)[i].CurveOffset); + fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", + i, (*interactive)[i].RoutingChannel); + fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", + i, (*interactive)[i].SubMode); + fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", + i, (*interactive)[i].MeasMode); + fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", + i, (*interactive)[i].P1); + fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", + i, (*interactive)[i].P2); + fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", + i, (*interactive)[i].P3); + fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", + i, (*interactive)[i].RangeNo); + fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", + i, (*interactive)[i].Offset); + fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", + i, (*interactive)[i].AcquisitionTime); + fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", + i, (*interactive)[i].StopAfter); + fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", + i, (*interactive)[i].StopReason); + fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", + i, (*interactive)[i].SyncRate); + fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", + i, (*interactive)[i].CFDCountRate); + fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", + i, (*interactive)[i].TDCCountRate); + fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", + i, (*interactive)[i].IntegralCount); + fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", + i, (*interactive)[i].Resolution); + fprintf(stream_out, "Crv[%d].reserve1 = %"PRId32"\n", + i, (*interactive)[i].reserve1); + fprintf(stream_out, "Crv[%d].reserve2 = %"PRId32"\n", + i, (*interactive)[i].reserve2); + } +} + +void th_v20_interactive_data_print(FILE *stream_out, + th_v20_header_t *th_header, + th_v20_interactive_t **interactive, + options_t *options) { + unsigned int i; + int j; + pq_interactive_bin_t bin; + float64_t origin; + float64_t time_step; + + pq_interactive_bin_print_t print; + + if ( options->binary_out ) { + print = pq_interactive_bin_fwrite; + } else { + print = pq_interactive_bin_printf; + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + bin.curve = i; + + origin = (*interactive)[i].Offset*1e3; time_step = (*interactive)[i].Resolution*1e3; - for ( j = 0; j < th_header->NumberOfChannels; j++ ) { - bin.bin_left = origin + j*time_step; - bin.bin_right = bin.bin_left + time_step; - bin.counts = (*interactive)[i].Counts[j]; - - print(stream_out, &bin); - } - } -} + for ( j = 0; j < th_header->NumberOfChannels; j++ ) { + bin.bin_left = origin + j*time_step; + bin.bin_right = bin.bin_left + time_step; + bin.counts = (*interactive)[i].Counts[j]; + + print(stream_out, &bin); + } + } +} diff --git a/src/timeharp/th_v20_tttr.c b/src/timeharp/th_v20_tttr.c index 88ec749..d1e75cc 100644 --- a/src/timeharp/th_v20_tttr.c +++ b/src/timeharp/th_v20_tttr.c @@ -29,174 +29,174 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include - -#include "th_v20.h" - -#include "../timeharp.h" -#include "../error.h" - -/* - * - * TTTR mode routines. - * - */ -void th_v20_t3_init(th_v20_header_t *th_header, - th_v20_tttr_header_t *tttr_header, - tttr_t *tttr) { - tttr->origin = 0; - tttr->overflows = 0; - tttr->overflow_increment = TH_TTTR_OVERFLOW; - tttr->sync_rate = tttr_header->SyncRate; - tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; - tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); -} - -int th_v20_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { - size_t n_read; - th_v20_tttr_record_t record; - - n_read = fread(&record, sizeof(record), 1, stream_in); - - if ( n_read != 1 ) { - if ( ! feof(stream_in) ) { - error("Could not read t3 record.\n"); - return(PQ_ERROR_IO); - } else { - return(PQ_ERROR_EOF); - } - } else { - if ( record.Valid ) { - /* Normal record. */ - t3->channel = record.Channel; - t3->pulse = tttr->origin; - t3->time = record.TimeTag; - return(PQ_RECORD_T3); - } else { - /* Special record. */ - if ( 0x800 & record.Channel ) { - /* Overflow */ - tttr->overflows++; - tttr->origin += tttr->overflow_increment; - return(PQ_RECORD_OVERFLOW); - } else { - t3->channel = 0x800; - t3->pulse = record.Channel; - return(PQ_RECORD_MARKER); - } - } - } -} - -int th_v20_t3_stream(FILE *stream_in, FILE *stream_out, - th_v20_header_t *th_header, th_v20_tttr_header_t *tttr_header, - options_t *options) { - tttr_t tttr; - - th_v20_t3_init(th_header, tttr_header, &tttr); - - if ( options->to_t2 ) { - error("T3 -> T2 not supported for Timeharp.\n"); - return(PQ_ERROR_OPTIONS); - } else { - return(pq_t3_stream(stream_in, stream_out, - th_v20_t3_decode, &tttr, options)); - } -} - -int th_v20_tttr_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v20_header_t *th_header, - options_t *options) { - th_v20_tttr_header_t *tttr_header; - int result; - - result = th_v20_tttr_header_read(stream_in, &tttr_header); - if ( result != PQ_SUCCESS ) { - error("Failed while reading the tttr header.\n"); - } else { - if ( options->print_header ) { - if ( options->binary_out ) { - pq_header_fwrite(stream_out, pq_header); - th_v20_header_fwrite(stream_out, th_header); - th_v20_tttr_header_fwrite(stream_out, tttr_header); - } else { - pq_header_printf(stream_out, pq_header); - th_v20_header_printf(stream_out, th_header); - th_v20_tttr_header_printf(stream_out, tttr_header); - } - - result = PQ_SUCCESS; - } else if ( options->print_resolution ) { - pq_resolution_print(stream_out, -1, - (th_header->Brd[0].Resolution*1e3), options); - result = PQ_SUCCESS; - } else { - result = th_v20_t3_stream(stream_in, stream_out, - th_header, tttr_header, options); - } - } - - debug("Freeing tttr header.\n"); - th_v20_tttr_header_free(&tttr_header); - return(result); -} - -int th_v20_tttr_header_read(FILE *stream_in, - th_v20_tttr_header_t **tttr_header) { - size_t n_read; - - *tttr_header = (th_v20_tttr_header_t *)malloc(sizeof(th_v20_tttr_header_t)); - - if ( *tttr_header == NULL ) { - error("Could not allocate tttr header.\n"); - return(PQ_ERROR_MEM); - } - - n_read = fread(tttr_header, sizeof(th_v20_tttr_header_t), 1, stream_in); - if ( n_read != 1 ) { - error("Could not read tttr header.\n"); - th_v20_tttr_header_free(tttr_header); - return(PQ_ERROR_IO); - } - - return(PQ_SUCCESS); -} - -void th_v20_tttr_header_free(th_v20_tttr_header_t **tttr_header) { - free(*tttr_header); -} - -void th_v20_tttr_header_printf(FILE *stream_out, - th_v20_tttr_header_t *tttr_header) { - int i; - - fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", - tttr_header->TTTRGlobClock); - - for ( i = 0; i < 6; i++ ) { - fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", - i, tttr_header->Reserved[i]); - } - - fprintf(stream_out, "SyncRate = %"PRId32"\n", - tttr_header->SyncRate); - fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", - tttr_header->AverageCFDRate); - fprintf(stream_out, "StopAfter = %"PRId32"\n", - tttr_header->StopAfter); - fprintf(stream_out, "StopReason = %"PRId32"\n", - tttr_header->StopReason); - fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", - tttr_header->NumberOfRecords); - -} - -void th_v20_tttr_header_fwrite(FILE *stream_out, - th_v20_tttr_header_t *tttr_header) { - fwrite(tttr_header, - sizeof(th_v20_tttr_header_t), - 1, - stream_out); -} +#include +#include +#include + +#include "th_v20.h" + +#include "../timeharp.h" +#include "../error.h" + +/* + * + * TTTR mode routines. + * + */ +void th_v20_t3_init(th_v20_header_t *th_header, + th_v20_tttr_header_t *tttr_header, + tttr_t *tttr) { + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = TH_TTTR_OVERFLOW; + tttr->sync_rate = tttr_header->SyncRate; + tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + +int th_v20_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { + size_t n_read; + th_v20_tttr_record_t record; + + n_read = fread(&record, sizeof(record), 1, stream_in); + + if ( n_read != 1 ) { + if ( ! feof(stream_in) ) { + error("Could not read t3 record.\n"); + return(PQ_ERROR_IO); + } else { + return(PQ_ERROR_EOF); + } + } else { + if ( record.Valid ) { + /* Normal record. */ + t3->channel = record.Channel; + t3->pulse = tttr->origin; + t3->time = record.TimeTag; + return(PQ_RECORD_T3); + } else { + /* Special record. */ + if ( 0x800 & record.Channel ) { + /* Overflow */ + tttr->overflows++; + tttr->origin += tttr->overflow_increment; + return(PQ_RECORD_OVERFLOW); + } else { + t3->channel = 0x800; + t3->pulse = record.Channel; + return(PQ_RECORD_MARKER); + } + } + } +} + +int th_v20_t3_stream(FILE *stream_in, FILE *stream_out, + th_v20_header_t *th_header, th_v20_tttr_header_t *tttr_header, + options_t *options) { + tttr_t tttr; + + th_v20_t3_init(th_header, tttr_header, &tttr); + + if ( options->to_t2 ) { + error("T3 -> T2 not supported for Timeharp.\n"); + return(PQ_ERROR_OPTIONS); + } else { + return(pq_t3_stream(stream_in, stream_out, + th_v20_t3_decode, &tttr, options)); + } +} + +int th_v20_tttr_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v20_header_t *th_header, + options_t *options) { + th_v20_tttr_header_t *tttr_header; + int result; + + result = th_v20_tttr_header_read(stream_in, &tttr_header); + if ( result != PQ_SUCCESS ) { + error("Failed while reading the tttr header.\n"); + } else { + if ( options->print_header ) { + if ( options->binary_out ) { + pq_header_fwrite(stream_out, pq_header); + th_v20_header_fwrite(stream_out, th_header); + th_v20_tttr_header_fwrite(stream_out, tttr_header); + } else { + pq_header_printf(stream_out, pq_header); + th_v20_header_printf(stream_out, th_header); + th_v20_tttr_header_printf(stream_out, tttr_header); + } + + result = PQ_SUCCESS; + } else if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + (th_header->Brd[0].Resolution*1e3), options); + result = PQ_SUCCESS; + } else { + result = th_v20_t3_stream(stream_in, stream_out, + th_header, tttr_header, options); + } + } + + debug("Freeing tttr header.\n"); + th_v20_tttr_header_free(&tttr_header); + return(result); +} + +int th_v20_tttr_header_read(FILE *stream_in, + th_v20_tttr_header_t **tttr_header) { + size_t n_read; + + *tttr_header = (th_v20_tttr_header_t *)malloc(sizeof(th_v20_tttr_header_t)); + + if ( *tttr_header == NULL ) { + error("Could not allocate tttr header.\n"); + return(PQ_ERROR_MEM); + } + + n_read = fread(tttr_header, sizeof(th_v20_tttr_header_t), 1, stream_in); + if ( n_read != 1 ) { + error("Could not read tttr header.\n"); + th_v20_tttr_header_free(tttr_header); + return(PQ_ERROR_IO); + } + + return(PQ_SUCCESS); +} + +void th_v20_tttr_header_free(th_v20_tttr_header_t **tttr_header) { + free(*tttr_header); +} + +void th_v20_tttr_header_printf(FILE *stream_out, + th_v20_tttr_header_t *tttr_header) { + int i; + + fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", + tttr_header->TTTRGlobClock); + + for ( i = 0; i < 6; i++ ) { + fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", + i, tttr_header->Reserved[i]); + } + + fprintf(stream_out, "SyncRate = %"PRId32"\n", + tttr_header->SyncRate); + fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", + tttr_header->AverageCFDRate); + fprintf(stream_out, "StopAfter = %"PRId32"\n", + tttr_header->StopAfter); + fprintf(stream_out, "StopReason = %"PRId32"\n", + tttr_header->StopReason); + fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", + tttr_header->NumberOfRecords); + +} + +void th_v20_tttr_header_fwrite(FILE *stream_out, + th_v20_tttr_header_t *tttr_header) { + fwrite(tttr_header, + sizeof(th_v20_tttr_header_t), + 1, + stream_out); +} diff --git a/src/timeharp/th_v30_interactive.c b/src/timeharp/th_v30_interactive.c index 8592c96..4a9eb7e 100644 --- a/src/timeharp/th_v30_interactive.c +++ b/src/timeharp/th_v30_interactive.c @@ -29,196 +29,196 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include +#include +#include #include - -#include "../timeharp.h" -#include "th_v30.h" - -#include "../error.h" -#include "../interactive.h" - -/* - * - * Streaming for interactive files. - * - */ -int th_v30_interactive_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v30_header_t *th_header, - options_t *options) { - int result; - th_v30_interactive_t *interactive; - int i; - - /* Read interactive header. */ - result = th_v30_interactive_read(stream_in, th_header, &interactive); - if ( result != PQ_SUCCESS ) { - error("Failed while reading interactive header.\n"); - } else { - if ( options->print_header ) { - pq_header_printf(stream_out, pq_header); - th_v30_header_printf(stream_out, th_header); - th_v30_interactive_header_printf(stream_out, th_header, - &interactive); - } else if ( options->print_resolution ) { - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - pq_resolution_print(stream_out, i, - (interactive[i].Resolution*1e3), options); - } - } else { - /* Read and print interactive data. */ - th_v30_interactive_data_print(stream_out, th_header, - &interactive, options); - } - } - - /* Clean and return. */ - debug("Freeing interactive header.\n"); - th_v30_interactive_free(th_header, &interactive); - return(PQ_SUCCESS); -} - -int th_v30_interactive_read(FILE *stream_in, - th_v30_header_t *th_header, - th_v30_interactive_t **interactive) { - int i; - size_t n_read; - - *interactive = (th_v30_interactive_t *)malloc(sizeof(th_v30_interactive_t)* - th_header->NumberOfCurves); - if ( *interactive == NULL ) { - error("Could not allocate interactive data.\n"); - return(PQ_ERROR_MEM); - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - n_read = fread(&(*interactive)[i], - sizeof(th_v30_interactive_t) - sizeof(uint32_t *), 1, - stream_in); - if ( n_read != 1 ) { - error("Could not allocate memory for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - - (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* - th_header->NumberOfChannels); - if ( (*interactive)[i].Counts == NULL ) { - error("Could not allocate memory for counts of curve " - "%"PRId32".\n", i); - return(PQ_ERROR_MEM); - } - - n_read = fread((*interactive)[i].Counts, - sizeof(uint32_t), th_header->NumberOfChannels, - stream_in); - if ( n_read != th_header->NumberOfChannels ) { - error("Could not read counts for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - } - - return(PQ_SUCCESS); -} - -void th_v30_interactive_free(th_v30_header_t *th_header, - th_v30_interactive_t **interactive) { - int i; - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - free((*interactive)[i].Counts); - } - free(*interactive); -} - - -void th_v30_interactive_header_printf(FILE *stream_out, - th_v30_header_t *th_header, - th_v30_interactive_t **interactive) { - int i; - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", - i, (*interactive)[i].CurveIndex); - fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", - i, ctime32(&(*interactive)[i].TimeOfRecording)); - fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", - i, (*interactive)[i].BoardSerial); - fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", - i, (*interactive)[i].CFDZeroCross); - fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", - i, (*interactive)[i].CFDDiscrMin); - fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", - i, (*interactive)[i].SyncLevel); - fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", - i, (*interactive)[i].CurveOffset); - fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", - i, (*interactive)[i].RoutingChannel); - fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", - i, (*interactive)[i].SubMode); - fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", - i, (*interactive)[i].MeasMode); - fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", - i, (*interactive)[i].P1); - fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", - i, (*interactive)[i].P2); - fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", - i, (*interactive)[i].P3); - fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", - i, (*interactive)[i].RangeNo); - fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", - i, (*interactive)[i].Offset); - fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", - i, (*interactive)[i].AcquisitionTime); - fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", - i, (*interactive)[i].StopAfter); - fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", - i, (*interactive)[i].StopReason); - fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", - i, (*interactive)[i].SyncRate); - fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", - i, (*interactive)[i].CFDCountRate); - fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", - i, (*interactive)[i].TDCCountRate); - fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", - i, (*interactive)[i].IntegralCount); - fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", - i, (*interactive)[i].Resolution); - fprintf(stream_out, "Crv[%d].reserve1 = %"PRId32"\n", - i, (*interactive)[i].reserve1); - fprintf(stream_out, "Crv[%d].reserve2 = %"PRId32"\n", - i, (*interactive)[i].reserve2); - } -} - -void th_v30_interactive_data_print(FILE *stream_out, - th_v30_header_t *th_header, - th_v30_interactive_t **interactive, - options_t *options) { - unsigned int i; - int j; - pq_interactive_bin_t bin; - float64_t origin; - float64_t time_step; - - pq_interactive_bin_print_t print; - - if ( options->binary_out ) { - print = pq_interactive_bin_fwrite; - } else { - print = pq_interactive_bin_printf; - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - bin.curve = i; - - origin = (*interactive)[i].Offset*1e3; - time_step = (*interactive)[i].Resolution*1e3; - for ( j = 0; j < th_header->NumberOfChannels; j++ ) { - bin.bin_left = origin + j*time_step; - bin.bin_right = bin.bin_left + time_step; - bin.counts = (*interactive)[i].Counts[j]; - - print(stream_out, &bin); - } - } -} + +#include "../timeharp.h" +#include "th_v30.h" + +#include "../error.h" +#include "../interactive.h" + +/* + * + * Streaming for interactive files. + * + */ +int th_v30_interactive_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v30_header_t *th_header, + options_t *options) { + int result; + th_v30_interactive_t *interactive; + int i; + + /* Read interactive header. */ + result = th_v30_interactive_read(stream_in, th_header, &interactive); + if ( result != PQ_SUCCESS ) { + error("Failed while reading interactive header.\n"); + } else { + if ( options->print_header ) { + pq_header_printf(stream_out, pq_header); + th_v30_header_printf(stream_out, th_header); + th_v30_interactive_header_printf(stream_out, th_header, + &interactive); + } else if ( options->print_resolution ) { + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + pq_resolution_print(stream_out, i, + (interactive[i].Resolution*1e3), options); + } + } else { + /* Read and print interactive data. */ + th_v30_interactive_data_print(stream_out, th_header, + &interactive, options); + } + } + + /* Clean and return. */ + debug("Freeing interactive header.\n"); + th_v30_interactive_free(th_header, &interactive); + return(PQ_SUCCESS); +} + +int th_v30_interactive_read(FILE *stream_in, + th_v30_header_t *th_header, + th_v30_interactive_t **interactive) { + int i; + size_t n_read; + + *interactive = (th_v30_interactive_t *)malloc(sizeof(th_v30_interactive_t)* + th_header->NumberOfCurves); + if ( *interactive == NULL ) { + error("Could not allocate interactive data.\n"); + return(PQ_ERROR_MEM); + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + n_read = fread(&(*interactive)[i], + sizeof(th_v30_interactive_t) - sizeof(uint32_t *), 1, + stream_in); + if ( n_read != 1 ) { + error("Could not allocate memory for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + + (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* + th_header->NumberOfChannels); + if ( (*interactive)[i].Counts == NULL ) { + error("Could not allocate memory for counts of curve " + "%"PRId32".\n", i); + return(PQ_ERROR_MEM); + } + + n_read = fread((*interactive)[i].Counts, + sizeof(uint32_t), th_header->NumberOfChannels, + stream_in); + if ( n_read != th_header->NumberOfChannels ) { + error("Could not read counts for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + } + + return(PQ_SUCCESS); +} + +void th_v30_interactive_free(th_v30_header_t *th_header, + th_v30_interactive_t **interactive) { + int i; + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + free((*interactive)[i].Counts); + } + free(*interactive); +} + + +void th_v30_interactive_header_printf(FILE *stream_out, + th_v30_header_t *th_header, + th_v30_interactive_t **interactive) { + int i; + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", + i, (*interactive)[i].CurveIndex); + fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", + i, ctime32(&(*interactive)[i].TimeOfRecording)); + fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", + i, (*interactive)[i].BoardSerial); + fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", + i, (*interactive)[i].CFDZeroCross); + fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", + i, (*interactive)[i].CFDDiscrMin); + fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", + i, (*interactive)[i].SyncLevel); + fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", + i, (*interactive)[i].CurveOffset); + fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", + i, (*interactive)[i].RoutingChannel); + fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", + i, (*interactive)[i].SubMode); + fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", + i, (*interactive)[i].MeasMode); + fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", + i, (*interactive)[i].P1); + fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", + i, (*interactive)[i].P2); + fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", + i, (*interactive)[i].P3); + fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", + i, (*interactive)[i].RangeNo); + fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", + i, (*interactive)[i].Offset); + fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", + i, (*interactive)[i].AcquisitionTime); + fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", + i, (*interactive)[i].StopAfter); + fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", + i, (*interactive)[i].StopReason); + fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", + i, (*interactive)[i].SyncRate); + fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", + i, (*interactive)[i].CFDCountRate); + fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", + i, (*interactive)[i].TDCCountRate); + fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", + i, (*interactive)[i].IntegralCount); + fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", + i, (*interactive)[i].Resolution); + fprintf(stream_out, "Crv[%d].reserve1 = %"PRId32"\n", + i, (*interactive)[i].reserve1); + fprintf(stream_out, "Crv[%d].reserve2 = %"PRId32"\n", + i, (*interactive)[i].reserve2); + } +} + +void th_v30_interactive_data_print(FILE *stream_out, + th_v30_header_t *th_header, + th_v30_interactive_t **interactive, + options_t *options) { + unsigned int i; + int j; + pq_interactive_bin_t bin; + float64_t origin; + float64_t time_step; + + pq_interactive_bin_print_t print; + + if ( options->binary_out ) { + print = pq_interactive_bin_fwrite; + } else { + print = pq_interactive_bin_printf; + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + bin.curve = i; + + origin = (*interactive)[i].Offset*1e3; + time_step = (*interactive)[i].Resolution*1e3; + for ( j = 0; j < th_header->NumberOfChannels; j++ ) { + bin.bin_left = origin + j*time_step; + bin.bin_right = bin.bin_left + time_step; + bin.counts = (*interactive)[i].Counts[j]; + + print(stream_out, &bin); + } + } +} diff --git a/src/timeharp/th_v30_tttr.c b/src/timeharp/th_v30_tttr.c index 6e10838..23be89f 100644 --- a/src/timeharp/th_v30_tttr.c +++ b/src/timeharp/th_v30_tttr.c @@ -29,173 +29,173 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include - -#include "th_v30.h" - -#include "../timeharp.h" -#include "../error.h" - -/* - * - * TTTR mode routines. - * - */ -void th_v30_t3_init(th_v30_header_t *th_header, - th_v30_tttr_header_t *tttr_header, - tttr_t *tttr) { - tttr->origin = 0; - tttr->overflows = 0; - tttr->overflow_increment = TH_TTTR_OVERFLOW; - tttr->sync_rate = tttr_header->SyncRate; - tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; - tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); -} - -int th_v30_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { - size_t n_read; - th_v30_tttr_record_t record; - - n_read = fread(&record, sizeof(record), 1, stream_in); - - if ( n_read != 1 ) { - if ( ! feof(stream_in) ) { - error("Could not read t3 record.\n"); - return(PQ_ERROR_IO); - } else { - return(PQ_ERROR_EOF); - } - } else { - if ( record.Valid ) { - /* Normal record. */ - t3->channel = 0; - t3->pulse = tttr->origin + record.TimeTag; - t3->time = record.Data; - return(PQ_RECORD_T3); - } else { - /* Special record. */ - if ( 0x800 & record.Data ) { - /* Overflow */ - tttr->overflows++; - tttr->origin += tttr->overflow_increment; - return(PQ_RECORD_OVERFLOW); - } else { - t3->channel = 0x800; - t3->pulse = record.Data & 0x07; - return(PQ_RECORD_MARKER); - } - } - } -} - -int th_v30_t3_stream(FILE *stream_in, FILE *stream_out, - th_v30_header_t *th_header, th_v30_tttr_header_t *tttr_header, - options_t *options) { - tttr_t tttr; - - th_v30_t3_init(th_header, tttr_header, &tttr); - - if ( options->to_t2 ) { - error("T3 -> T2 not supported for Timeharp.\n"); - return(PQ_ERROR_OPTIONS); - } else { - return(pq_t3_stream(stream_in, stream_out, - th_v30_t3_decode, &tttr, options)); - } -} - -int th_v30_tttr_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v30_header_t *th_header, - options_t *options) { - th_v30_tttr_header_t *tttr_header; - int result; - - result = th_v30_tttr_header_read(stream_in, &tttr_header); - if ( result != PQ_SUCCESS ) { - error("Failed while reading the tttr header.\n"); - } else { - if ( options->print_header ) { - if ( options->binary_out ) { - pq_header_fwrite(stream_out, pq_header); - th_v30_header_fwrite(stream_out, th_header); - th_v30_tttr_header_fwrite(stream_out, tttr_header); - } else { - pq_header_printf(stream_out, pq_header); - th_v30_header_printf(stream_out, th_header); - th_v30_tttr_header_printf(stream_out, tttr_header); - } - - result = PQ_SUCCESS; - } else if ( options->print_resolution ) { - pq_resolution_print(stream_out, -1, - (th_header->Brd[0].Resolution*1e3), options); - result = PQ_SUCCESS; - } else { - result = th_v30_t3_stream(stream_in, stream_out, - th_header, tttr_header, options); - } - } - - debug("Freeing tttr header.\n"); - th_v30_tttr_header_free(&tttr_header); - return(result); -} - -int th_v30_tttr_header_read(FILE *stream_in, - th_v30_tttr_header_t **tttr_header) { - size_t n_read; - - *tttr_header = (th_v30_tttr_header_t *)malloc(sizeof(th_v30_tttr_header_t)); - - if ( *tttr_header == NULL ) { - error("Could not allocate tttr header.\n"); - return(PQ_ERROR_MEM); - } - - n_read = fread(*tttr_header, sizeof(th_v30_tttr_header_t), 1, stream_in); - if ( n_read != 1 ) { - error("Could not read tttr header.\n"); - th_v30_tttr_header_free(tttr_header); - return(PQ_ERROR_IO); - } - - return(PQ_SUCCESS); -} - -void th_v30_tttr_header_free(th_v30_tttr_header_t **tttr_header) { - free(*tttr_header); -} - -void th_v30_tttr_header_printf(FILE *stream_out, - th_v30_tttr_header_t *tttr_header) { - int i; - - fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", - tttr_header->TTTRGlobClock); - - for ( i = 0; i < 6; i++ ) { - fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", - i, tttr_header->Reserved[i]); - } - - fprintf(stream_out, "SyncRate = %"PRId32"\n", - tttr_header->SyncRate); - fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", - tttr_header->AverageCFDRate); - fprintf(stream_out, "StopAfter = %"PRId32"\n", - tttr_header->StopAfter); - fprintf(stream_out, "StopReason = %"PRId32"\n", - tttr_header->StopReason); - fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", - tttr_header->NumberOfRecords); -} - -void th_v30_tttr_header_fwrite(FILE *stream_out, - th_v30_tttr_header_t *tttr_header) { - fwrite(tttr_header, - sizeof(th_v30_tttr_header_t), - 1, - stream_out); -} +#include +#include +#include + +#include "th_v30.h" + +#include "../timeharp.h" +#include "../error.h" + +/* + * + * TTTR mode routines. + * + */ +void th_v30_t3_init(th_v30_header_t *th_header, + th_v30_tttr_header_t *tttr_header, + tttr_t *tttr) { + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = TH_TTTR_OVERFLOW; + tttr->sync_rate = tttr_header->SyncRate; + tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + +int th_v30_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { + size_t n_read; + th_v30_tttr_record_t record; + + n_read = fread(&record, sizeof(record), 1, stream_in); + + if ( n_read != 1 ) { + if ( ! feof(stream_in) ) { + error("Could not read t3 record.\n"); + return(PQ_ERROR_IO); + } else { + return(PQ_ERROR_EOF); + } + } else { + if ( record.Valid ) { + /* Normal record. */ + t3->channel = 0; + t3->pulse = tttr->origin + record.TimeTag; + t3->time = record.Data; + return(PQ_RECORD_T3); + } else { + /* Special record. */ + if ( 0x800 & record.Data ) { + /* Overflow */ + tttr->overflows++; + tttr->origin += tttr->overflow_increment; + return(PQ_RECORD_OVERFLOW); + } else { + t3->channel = 0x800; + t3->pulse = record.Data & 0x07; + return(PQ_RECORD_MARKER); + } + } + } +} + +int th_v30_t3_stream(FILE *stream_in, FILE *stream_out, + th_v30_header_t *th_header, th_v30_tttr_header_t *tttr_header, + options_t *options) { + tttr_t tttr; + + th_v30_t3_init(th_header, tttr_header, &tttr); + + if ( options->to_t2 ) { + error("T3 -> T2 not supported for Timeharp.\n"); + return(PQ_ERROR_OPTIONS); + } else { + return(pq_t3_stream(stream_in, stream_out, + th_v30_t3_decode, &tttr, options)); + } +} + +int th_v30_tttr_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v30_header_t *th_header, + options_t *options) { + th_v30_tttr_header_t *tttr_header; + int result; + + result = th_v30_tttr_header_read(stream_in, &tttr_header); + if ( result != PQ_SUCCESS ) { + error("Failed while reading the tttr header.\n"); + } else { + if ( options->print_header ) { + if ( options->binary_out ) { + pq_header_fwrite(stream_out, pq_header); + th_v30_header_fwrite(stream_out, th_header); + th_v30_tttr_header_fwrite(stream_out, tttr_header); + } else { + pq_header_printf(stream_out, pq_header); + th_v30_header_printf(stream_out, th_header); + th_v30_tttr_header_printf(stream_out, tttr_header); + } + + result = PQ_SUCCESS; + } else if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + (th_header->Brd[0].Resolution*1e3), options); + result = PQ_SUCCESS; + } else { + result = th_v30_t3_stream(stream_in, stream_out, + th_header, tttr_header, options); + } + } + + debug("Freeing tttr header.\n"); + th_v30_tttr_header_free(&tttr_header); + return(result); +} + +int th_v30_tttr_header_read(FILE *stream_in, + th_v30_tttr_header_t **tttr_header) { + size_t n_read; + + *tttr_header = (th_v30_tttr_header_t *)malloc(sizeof(th_v30_tttr_header_t)); + + if ( *tttr_header == NULL ) { + error("Could not allocate tttr header.\n"); + return(PQ_ERROR_MEM); + } + + n_read = fread(*tttr_header, sizeof(th_v30_tttr_header_t), 1, stream_in); + if ( n_read != 1 ) { + error("Could not read tttr header.\n"); + th_v30_tttr_header_free(tttr_header); + return(PQ_ERROR_IO); + } + + return(PQ_SUCCESS); +} + +void th_v30_tttr_header_free(th_v30_tttr_header_t **tttr_header) { + free(*tttr_header); +} + +void th_v30_tttr_header_printf(FILE *stream_out, + th_v30_tttr_header_t *tttr_header) { + int i; + + fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", + tttr_header->TTTRGlobClock); + + for ( i = 0; i < 6; i++ ) { + fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", + i, tttr_header->Reserved[i]); + } + + fprintf(stream_out, "SyncRate = %"PRId32"\n", + tttr_header->SyncRate); + fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", + tttr_header->AverageCFDRate); + fprintf(stream_out, "StopAfter = %"PRId32"\n", + tttr_header->StopAfter); + fprintf(stream_out, "StopReason = %"PRId32"\n", + tttr_header->StopReason); + fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", + tttr_header->NumberOfRecords); +} + +void th_v30_tttr_header_fwrite(FILE *stream_out, + th_v30_tttr_header_t *tttr_header) { + fwrite(tttr_header, + sizeof(th_v30_tttr_header_t), + 1, + stream_out); +} diff --git a/src/timeharp/th_v50_interactive.c b/src/timeharp/th_v50_interactive.c index b7bfa34..9f57d31 100644 --- a/src/timeharp/th_v50_interactive.c +++ b/src/timeharp/th_v50_interactive.c @@ -29,195 +29,195 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include - -#include "../timeharp.h" -#include "th_v50.h" - -#include "../error.h" -#include "../interactive.h" - -/* - * - * Streaming for interactive files. - * - */ -int th_v50_interactive_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v50_header_t *th_header, - options_t *options) { - int result; - th_v50_interactive_t *interactive; - int i; - - /* Read interactive header. */ - result = th_v50_interactive_read(stream_in, th_header, &interactive); - if ( result != PQ_SUCCESS ) { - error("Failed while reading interactive header.\n"); - } else { - if ( options->print_header ) { - pq_header_printf(stream_out, pq_header); - th_v50_header_printf(stream_out, th_header); - th_v50_interactive_header_printf(stream_out, th_header, - &interactive); - } else if ( options->print_resolution ) { - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - pq_resolution_print(stream_out, i, - (interactive[i].Resolution*1e3), options); - } - } else { - /* Read and print interactive data. */ - th_v50_interactive_data_print(stream_out, th_header, - &interactive, options); - } - } - - /* Clean and return. */ - debug("Freeing interactive header.\n"); - th_v50_interactive_free(th_header, &interactive); - return(PQ_SUCCESS); -} - -int th_v50_interactive_read(FILE *stream_in, - th_v50_header_t *th_header, - th_v50_interactive_t **interactive) { - int i; - size_t n_read; - - *interactive = (th_v50_interactive_t *)malloc(sizeof(th_v50_interactive_t)* - th_header->NumberOfCurves); - if ( *interactive == NULL ) { - error("Could not allocate interactive data.\n"); - return(PQ_ERROR_MEM); - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - n_read = fread(&(*interactive)[i], - sizeof(th_v50_interactive_t) - sizeof(uint32_t *), 1, - stream_in); - if ( n_read != 1 ) { - error("Could not allocate memory for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - - (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* - th_header->NumberOfChannels); - if ( (*interactive)[i].Counts == NULL ) { - error("Could not allocate memory for counts of curve " - "%"PRId32".\n", i); - return(PQ_ERROR_MEM); - } - - n_read = fread((*interactive)[i].Counts, - sizeof(uint32_t), th_header->NumberOfChannels, - stream_in); - if ( n_read != th_header->NumberOfChannels ) { - error("Could not read counts for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - } - - return(PQ_SUCCESS); -} - -void th_v50_interactive_free(th_v50_header_t *th_header, - th_v50_interactive_t **interactive) { - int i; - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - free((*interactive)[i].Counts); - } - free(*interactive); -} - - -void th_v50_interactive_header_printf(FILE *stream_out, - th_v50_header_t *th_header, - th_v50_interactive_t **interactive) { - int i; - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", - i, (*interactive)[i].CurveIndex); - fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", - i, ctime32(&(*interactive)[i].TimeOfRecording)); - fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", - i, (*interactive)[i].BoardSerial); - fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", - i, (*interactive)[i].CFDZeroCross); - fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", - i, (*interactive)[i].CFDDiscrMin); - fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", - i, (*interactive)[i].SyncLevel); - fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", - i, (*interactive)[i].CurveOffset); - fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", - i, (*interactive)[i].RoutingChannel); - fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", - i, (*interactive)[i].SubMode); - fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", - i, (*interactive)[i].MeasMode); - fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", - i, (*interactive)[i].P1); - fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", - i, (*interactive)[i].P2); - fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", - i, (*interactive)[i].P3); - fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", - i, (*interactive)[i].RangeNo); - fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", - i, (*interactive)[i].Offset); - fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", - i, (*interactive)[i].AcquisitionTime); - fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", - i, (*interactive)[i].StopAfter); - fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", - i, (*interactive)[i].StopReason); - fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", - i, (*interactive)[i].SyncRate); - fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", - i, (*interactive)[i].CFDCountRate); - fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", - i, (*interactive)[i].TDCCountRate); - fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", - i, (*interactive)[i].IntegralCount); - fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", - i, (*interactive)[i].Resolution); - fprintf(stream_out, "Crv[%d].ExtDevices = %"PRId32"\n", - i, (*interactive)[i].ExtDevices); - fprintf(stream_out, "Crv[%d].Reserved = %"PRId32"\n", - i, (*interactive)[i].Reserved); - } -} - -void th_v50_interactive_data_print(FILE *stream_out, - th_v50_header_t *th_header, - th_v50_interactive_t **interactive, - options_t *options) { - unsigned int i; - int j; - pq_interactive_bin_t bin; - float64_t origin; - float64_t time_step; - - pq_interactive_bin_print_t print; - - if ( options->binary_out ) { - print = pq_interactive_bin_fwrite; - } else { - print = pq_interactive_bin_printf; - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - bin.curve = i; - - origin = (float64_t)(*interactive)[i].Offset*1e3; - time_step = (*interactive)[i].Resolution*1e3; - for ( j = 0; j < th_header->NumberOfChannels; j++ ) { - bin.bin_left = origin + j*time_step; - bin.bin_right = bin.bin_left + time_step; - bin.counts = (*interactive)[i].Counts[j]; - - print(stream_out, &bin); - } - } -} +#include +#include + +#include "../timeharp.h" +#include "th_v50.h" + +#include "../error.h" +#include "../interactive.h" + +/* + * + * Streaming for interactive files. + * + */ +int th_v50_interactive_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v50_header_t *th_header, + options_t *options) { + int result; + th_v50_interactive_t *interactive; + int i; + + /* Read interactive header. */ + result = th_v50_interactive_read(stream_in, th_header, &interactive); + if ( result != PQ_SUCCESS ) { + error("Failed while reading interactive header.\n"); + } else { + if ( options->print_header ) { + pq_header_printf(stream_out, pq_header); + th_v50_header_printf(stream_out, th_header); + th_v50_interactive_header_printf(stream_out, th_header, + &interactive); + } else if ( options->print_resolution ) { + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + pq_resolution_print(stream_out, i, + (interactive[i].Resolution*1e3), options); + } + } else { + /* Read and print interactive data. */ + th_v50_interactive_data_print(stream_out, th_header, + &interactive, options); + } + } + + /* Clean and return. */ + debug("Freeing interactive header.\n"); + th_v50_interactive_free(th_header, &interactive); + return(PQ_SUCCESS); +} + +int th_v50_interactive_read(FILE *stream_in, + th_v50_header_t *th_header, + th_v50_interactive_t **interactive) { + int i; + size_t n_read; + + *interactive = (th_v50_interactive_t *)malloc(sizeof(th_v50_interactive_t)* + th_header->NumberOfCurves); + if ( *interactive == NULL ) { + error("Could not allocate interactive data.\n"); + return(PQ_ERROR_MEM); + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + n_read = fread(&(*interactive)[i], + sizeof(th_v50_interactive_t) - sizeof(uint32_t *), 1, + stream_in); + if ( n_read != 1 ) { + error("Could not allocate memory for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + + (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* + th_header->NumberOfChannels); + if ( (*interactive)[i].Counts == NULL ) { + error("Could not allocate memory for counts of curve " + "%"PRId32".\n", i); + return(PQ_ERROR_MEM); + } + + n_read = fread((*interactive)[i].Counts, + sizeof(uint32_t), th_header->NumberOfChannels, + stream_in); + if ( n_read != th_header->NumberOfChannels ) { + error("Could not read counts for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + } + + return(PQ_SUCCESS); +} + +void th_v50_interactive_free(th_v50_header_t *th_header, + th_v50_interactive_t **interactive) { + int i; + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + free((*interactive)[i].Counts); + } + free(*interactive); +} + + +void th_v50_interactive_header_printf(FILE *stream_out, + th_v50_header_t *th_header, + th_v50_interactive_t **interactive) { + int i; + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", + i, (*interactive)[i].CurveIndex); + fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", + i, ctime32(&(*interactive)[i].TimeOfRecording)); + fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", + i, (*interactive)[i].BoardSerial); + fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", + i, (*interactive)[i].CFDZeroCross); + fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", + i, (*interactive)[i].CFDDiscrMin); + fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", + i, (*interactive)[i].SyncLevel); + fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", + i, (*interactive)[i].CurveOffset); + fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", + i, (*interactive)[i].RoutingChannel); + fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", + i, (*interactive)[i].SubMode); + fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", + i, (*interactive)[i].MeasMode); + fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", + i, (*interactive)[i].P1); + fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", + i, (*interactive)[i].P2); + fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", + i, (*interactive)[i].P3); + fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", + i, (*interactive)[i].RangeNo); + fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", + i, (*interactive)[i].Offset); + fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", + i, (*interactive)[i].AcquisitionTime); + fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", + i, (*interactive)[i].StopAfter); + fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", + i, (*interactive)[i].StopReason); + fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", + i, (*interactive)[i].SyncRate); + fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", + i, (*interactive)[i].CFDCountRate); + fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", + i, (*interactive)[i].TDCCountRate); + fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", + i, (*interactive)[i].IntegralCount); + fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", + i, (*interactive)[i].Resolution); + fprintf(stream_out, "Crv[%d].ExtDevices = %"PRId32"\n", + i, (*interactive)[i].ExtDevices); + fprintf(stream_out, "Crv[%d].Reserved = %"PRId32"\n", + i, (*interactive)[i].Reserved); + } +} + +void th_v50_interactive_data_print(FILE *stream_out, + th_v50_header_t *th_header, + th_v50_interactive_t **interactive, + options_t *options) { + unsigned int i; + int j; + pq_interactive_bin_t bin; + float64_t origin; + float64_t time_step; + + pq_interactive_bin_print_t print; + + if ( options->binary_out ) { + print = pq_interactive_bin_fwrite; + } else { + print = pq_interactive_bin_printf; + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + bin.curve = i; + + origin = (float64_t)(*interactive)[i].Offset*1e3; + time_step = (*interactive)[i].Resolution*1e3; + for ( j = 0; j < th_header->NumberOfChannels; j++ ) { + bin.bin_left = origin + j*time_step; + bin.bin_right = bin.bin_left + time_step; + bin.counts = (*interactive)[i].Counts[j]; + + print(stream_out, &bin); + } + } +} diff --git a/src/timeharp/th_v50_tttr.c b/src/timeharp/th_v50_tttr.c index e8448ab..01b1b08 100644 --- a/src/timeharp/th_v50_tttr.c +++ b/src/timeharp/th_v50_tttr.c @@ -29,201 +29,201 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include - -#include "th_v50.h" - -#include "../timeharp.h" -#include "../error.h" - -/* - * - * TTTR mode routines. - * - */ -void th_v50_t3_init(th_v50_header_t *th_header, - th_v50_tttr_header_t *tttr_header, - tttr_t *tttr) { - tttr->origin = 0; - tttr->overflows = 0; - tttr->overflow_increment = TH_TTTR_OVERFLOW; - tttr->sync_rate = tttr_header->SyncRate; - tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; - tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); -} - -int th_v50_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { - size_t n_read; - th_v50_tttr_record_t record; - - n_read = fread(&record, sizeof(record), 1, stream_in); - - if ( n_read != 1 ) { - if ( ! feof(stream_in) ) { - error("Could not read t3 record.\n"); - return(PQ_ERROR_IO); - } else { - return(PQ_ERROR_EOF); - } - } else { - if ( record.Valid ) { - /* Normal record. */ - t3->channel = 0; - t3->pulse = tttr->origin + record.TimeTag; - t3->time = record.Data; - return(PQ_RECORD_T3); - } else { - /* Special record. */ - if ( 0x800 & record.Data ) { - /* Overflow */ - tttr->overflows++; - tttr->origin += tttr->overflow_increment; - return(PQ_RECORD_OVERFLOW); - } else { - t3->channel = 0x800; - t3->pulse = record.Data & 0x07; - return(PQ_RECORD_MARKER); - } - } - } -} - -int th_v50_t3_stream(FILE *stream_in, FILE *stream_out, - th_v50_header_t *th_header, th_v50_tttr_header_t *tttr_header, - options_t *options) { - tttr_t tttr; - - th_v50_t3_init(th_header, tttr_header, &tttr); - - if ( options->to_t2 ) { - error("T3 -> T2 not supported for Timeharp.\n"); - return(PQ_ERROR_OPTIONS); - } else { - return(pq_t3_stream(stream_in, stream_out, - th_v50_t3_decode, &tttr, options)); - } -} - -int th_v50_tttr_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v50_header_t *th_header, - options_t *options) { - th_v50_tttr_header_t *tttr_header; - int result; - - result = th_v50_tttr_header_read(stream_in, &tttr_header); - if ( result != PQ_SUCCESS ) { - error("Failed while reading the tttr header.\n"); - } else { - if ( options->print_header ) { - if ( options->binary_out ) { - pq_header_fwrite(stream_out, pq_header); - th_v50_header_fwrite(stream_out, th_header); - th_v50_tttr_header_fwrite(stream_out, tttr_header); - } else { - pq_header_printf(stream_out, pq_header); - th_v50_header_printf(stream_out, th_header); - th_v50_tttr_header_printf(stream_out, tttr_header); - } - - result = PQ_SUCCESS; - } else if ( options->print_resolution ) { - pq_resolution_print(stream_out, -1, - (th_header->Brd[0].Resolution*1e3), options); - result = PQ_SUCCESS; - } else { - result = th_v50_t3_stream(stream_in, stream_out, - th_header, tttr_header, options); - } - } - - debug("Freeing tttr header.\n"); - th_v50_tttr_header_free(&tttr_header); - return(result); -} - -int th_v50_tttr_header_read(FILE *stream_in, - th_v50_tttr_header_t **tttr_header) { - size_t n_read; - - *tttr_header = (th_v50_tttr_header_t *)malloc(sizeof(th_v50_tttr_header_t)); - - if ( *tttr_header == NULL ) { - error("Could not allocate tttr header.\n"); - return(PQ_ERROR_MEM); - } - - n_read = fread(*tttr_header, - sizeof(th_v50_tttr_header_t) - sizeof(int32_t *), - 1, - stream_in); - if ( n_read != 1 ) { - error("Could not read tttr header.\n"); - th_v50_tttr_header_free(tttr_header); - return(PQ_ERROR_IO); - } - - if ( (*tttr_header)->SpecHeaderLength > 0 ) { - (*tttr_header)->SpecHeader = (int32_t *)malloc( - sizeof(int32_t)*(*tttr_header)->SpecHeaderLength); - if ( (*tttr_header)->SpecHeader == NULL ) { - error("Could not allocate special header.\n"); - th_v50_tttr_header_free(tttr_header); - return(PQ_ERROR_MEM); - } - } - - return(PQ_SUCCESS); -} - -void th_v50_tttr_header_free(th_v50_tttr_header_t **tttr_header) { - if ( *tttr_header != NULL ) { - free((*tttr_header)->SpecHeader); - } - - free(*tttr_header); -} - -void th_v50_tttr_header_printf(FILE *stream_out, - th_v50_tttr_header_t *tttr_header) { - int i; - - fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", - tttr_header->TTTRGlobClock); - - for ( i = 0; i < 6; i++ ) { - fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", - i, tttr_header->Reserved[i]); - } - - fprintf(stream_out, "SyncRate = %"PRId32"\n", - tttr_header->SyncRate); - fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", - tttr_header->AverageCFDRate); - fprintf(stream_out, "StopAfter = %"PRId32"\n", - tttr_header->StopAfter); - fprintf(stream_out, "StopReason = %"PRId32"\n", - tttr_header->StopReason); - fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", - tttr_header->NumberOfRecords); - fprintf(stream_out, "SpecHeaderLength = %"PRId32"\n", - tttr_header->SpecHeaderLength); - - for ( i = 0; i < tttr_header->SpecHeaderLength; i++ ) { - fprintf(stream_out, "SpecHeader[%d] = %"PRId32"\n", - i, tttr_header->SpecHeader[i]); - } -} - -void th_v50_tttr_header_fwrite(FILE *stream_out, - th_v50_tttr_header_t *tttr_header) { - fwrite(tttr_header, - sizeof(th_v50_tttr_header_t) - sizeof(int32_t *), - 1, - stream_out); - fwrite(tttr_header->SpecHeader, - sizeof(int32_t), - tttr_header->SpecHeaderLength, - stream_out); -} +#include +#include +#include + +#include "th_v50.h" + +#include "../timeharp.h" +#include "../error.h" + +/* + * + * TTTR mode routines. + * + */ +void th_v50_t3_init(th_v50_header_t *th_header, + th_v50_tttr_header_t *tttr_header, + tttr_t *tttr) { + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = TH_TTTR_OVERFLOW; + tttr->sync_rate = tttr_header->SyncRate; + tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + +int th_v50_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { + size_t n_read; + th_v50_tttr_record_t record; + + n_read = fread(&record, sizeof(record), 1, stream_in); + + if ( n_read != 1 ) { + if ( ! feof(stream_in) ) { + error("Could not read t3 record.\n"); + return(PQ_ERROR_IO); + } else { + return(PQ_ERROR_EOF); + } + } else { + if ( record.Valid ) { + /* Normal record. */ + t3->channel = 0; + t3->pulse = tttr->origin + record.TimeTag; + t3->time = record.Data; + return(PQ_RECORD_T3); + } else { + /* Special record. */ + if ( 0x800 & record.Data ) { + /* Overflow */ + tttr->overflows++; + tttr->origin += tttr->overflow_increment; + return(PQ_RECORD_OVERFLOW); + } else { + t3->channel = 0x800; + t3->pulse = record.Data & 0x07; + return(PQ_RECORD_MARKER); + } + } + } +} + +int th_v50_t3_stream(FILE *stream_in, FILE *stream_out, + th_v50_header_t *th_header, th_v50_tttr_header_t *tttr_header, + options_t *options) { + tttr_t tttr; + + th_v50_t3_init(th_header, tttr_header, &tttr); + + if ( options->to_t2 ) { + error("T3 -> T2 not supported for Timeharp.\n"); + return(PQ_ERROR_OPTIONS); + } else { + return(pq_t3_stream(stream_in, stream_out, + th_v50_t3_decode, &tttr, options)); + } +} + +int th_v50_tttr_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v50_header_t *th_header, + options_t *options) { + th_v50_tttr_header_t *tttr_header; + int result; + + result = th_v50_tttr_header_read(stream_in, &tttr_header); + if ( result != PQ_SUCCESS ) { + error("Failed while reading the tttr header.\n"); + } else { + if ( options->print_header ) { + if ( options->binary_out ) { + pq_header_fwrite(stream_out, pq_header); + th_v50_header_fwrite(stream_out, th_header); + th_v50_tttr_header_fwrite(stream_out, tttr_header); + } else { + pq_header_printf(stream_out, pq_header); + th_v50_header_printf(stream_out, th_header); + th_v50_tttr_header_printf(stream_out, tttr_header); + } + + result = PQ_SUCCESS; + } else if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + (th_header->Brd[0].Resolution*1e3), options); + result = PQ_SUCCESS; + } else { + result = th_v50_t3_stream(stream_in, stream_out, + th_header, tttr_header, options); + } + } + + debug("Freeing tttr header.\n"); + th_v50_tttr_header_free(&tttr_header); + return(result); +} + +int th_v50_tttr_header_read(FILE *stream_in, + th_v50_tttr_header_t **tttr_header) { + size_t n_read; + + *tttr_header = (th_v50_tttr_header_t *)malloc(sizeof(th_v50_tttr_header_t)); + + if ( *tttr_header == NULL ) { + error("Could not allocate tttr header.\n"); + return(PQ_ERROR_MEM); + } + + n_read = fread(*tttr_header, + sizeof(th_v50_tttr_header_t) - sizeof(int32_t *), + 1, + stream_in); + if ( n_read != 1 ) { + error("Could not read tttr header.\n"); + th_v50_tttr_header_free(tttr_header); + return(PQ_ERROR_IO); + } + + if ( (*tttr_header)->SpecHeaderLength > 0 ) { + (*tttr_header)->SpecHeader = (int32_t *)malloc( + sizeof(int32_t)*(*tttr_header)->SpecHeaderLength); + if ( (*tttr_header)->SpecHeader == NULL ) { + error("Could not allocate special header.\n"); + th_v50_tttr_header_free(tttr_header); + return(PQ_ERROR_MEM); + } + } + + return(PQ_SUCCESS); +} + +void th_v50_tttr_header_free(th_v50_tttr_header_t **tttr_header) { + if ( *tttr_header != NULL ) { + free((*tttr_header)->SpecHeader); + } + + free(*tttr_header); +} + +void th_v50_tttr_header_printf(FILE *stream_out, + th_v50_tttr_header_t *tttr_header) { + int i; + + fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", + tttr_header->TTTRGlobClock); + + for ( i = 0; i < 6; i++ ) { + fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", + i, tttr_header->Reserved[i]); + } + + fprintf(stream_out, "SyncRate = %"PRId32"\n", + tttr_header->SyncRate); + fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", + tttr_header->AverageCFDRate); + fprintf(stream_out, "StopAfter = %"PRId32"\n", + tttr_header->StopAfter); + fprintf(stream_out, "StopReason = %"PRId32"\n", + tttr_header->StopReason); + fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", + tttr_header->NumberOfRecords); + fprintf(stream_out, "SpecHeaderLength = %"PRId32"\n", + tttr_header->SpecHeaderLength); + + for ( i = 0; i < tttr_header->SpecHeaderLength; i++ ) { + fprintf(stream_out, "SpecHeader[%d] = %"PRId32"\n", + i, tttr_header->SpecHeader[i]); + } +} + +void th_v50_tttr_header_fwrite(FILE *stream_out, + th_v50_tttr_header_t *tttr_header) { + fwrite(tttr_header, + sizeof(th_v50_tttr_header_t) - sizeof(int32_t *), + 1, + stream_out); + fwrite(tttr_header->SpecHeader, + sizeof(int32_t), + tttr_header->SpecHeaderLength, + stream_out); +} diff --git a/src/timeharp/th_v60_interactive.c b/src/timeharp/th_v60_interactive.c index b76adc4..fb522bc 100644 --- a/src/timeharp/th_v60_interactive.c +++ b/src/timeharp/th_v60_interactive.c @@ -29,195 +29,195 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include - -#include "../timeharp.h" -#include "th_v60.h" - -#include "../error.h" -#include "../interactive.h" - -/* - * - * Streaming for interactive files. - * - */ -int th_v60_interactive_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v60_header_t *th_header, - options_t *options) { - int result; - th_v60_interactive_t *interactive; - int i; - - /* Read interactive header. */ - result = th_v60_interactive_read(stream_in, th_header, &interactive); - if ( result != PQ_SUCCESS ) { - error("Failed while reading interactive header.\n"); - } else { - if ( options->print_header ) { - pq_header_printf(stream_out, pq_header); - th_v60_header_printf(stream_out, th_header); - th_v60_interactive_header_printf(stream_out, th_header, - &interactive); - } else if ( options->print_resolution ) { - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - pq_resolution_print(stream_out, i, - (interactive[i].Resolution*1e3), options); - } - } else { - /* Read and print interactive data. */ - th_v60_interactive_data_print(stream_out, th_header, - &interactive, options); - } - } - - /* Clean and return. */ - debug("Freeing interactive header.\n"); - th_v60_interactive_free(th_header, &interactive); - return(PQ_SUCCESS); -} - -int th_v60_interactive_read(FILE *stream_in, - th_v60_header_t *th_header, - th_v60_interactive_t **interactive) { - int i; - size_t n_read; - - *interactive = (th_v60_interactive_t *)malloc(sizeof(th_v60_interactive_t)* - th_header->NumberOfCurves); - if ( *interactive == NULL ) { - error("Could not allocate interactive data.\n"); - return(PQ_ERROR_MEM); - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - n_read = fread(&(*interactive)[i], - sizeof(th_v60_interactive_t) - sizeof(uint32_t *), 1, - stream_in); - if ( n_read != 1 ) { - error("Could not allocate memory for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - - (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* - th_header->NumberOfChannels); - if ( (*interactive)[i].Counts == NULL ) { - error("Could not allocate memory for counts of curve " - "%"PRId32".\n", i); - return(PQ_ERROR_MEM); - } - - n_read = fread((*interactive)[i].Counts, - sizeof(uint32_t), th_header->NumberOfChannels, - stream_in); - if ( n_read != th_header->NumberOfChannels ) { - error("Could not read counts for curve %"PRId32".\n", i); - return(PQ_ERROR_IO); - } - } - - return(PQ_SUCCESS); -} - -void th_v60_interactive_free(th_v60_header_t *th_header, - th_v60_interactive_t **interactive) { - int i; - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - free((*interactive)[i].Counts); - } - free(*interactive); -} - - -void th_v60_interactive_header_printf(FILE *stream_out, - th_v60_header_t *th_header, - th_v60_interactive_t **interactive) { - int i; - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", - i, (*interactive)[i].CurveIndex); - fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", - i, ctime32(&(*interactive)[i].TimeOfRecording)); - fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", - i, (*interactive)[i].BoardSerial); - fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", - i, (*interactive)[i].CFDZeroCross); - fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", - i, (*interactive)[i].CFDDiscrMin); - fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", - i, (*interactive)[i].SyncLevel); - fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", - i, (*interactive)[i].CurveOffset); - fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", - i, (*interactive)[i].RoutingChannel); - fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", - i, (*interactive)[i].SubMode); - fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", - i, (*interactive)[i].MeasMode); - fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", - i, (*interactive)[i].P1); - fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", - i, (*interactive)[i].P2); - fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", - i, (*interactive)[i].P3); - fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", - i, (*interactive)[i].RangeNo); - fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", - i, (*interactive)[i].Offset); - fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", - i, (*interactive)[i].AcquisitionTime); - fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", - i, (*interactive)[i].StopAfter); - fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", - i, (*interactive)[i].StopReason); - fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", - i, (*interactive)[i].SyncRate); - fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", - i, (*interactive)[i].CFDCountRate); - fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", - i, (*interactive)[i].TDCCountRate); - fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", - i, (*interactive)[i].IntegralCount); - fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", - i, (*interactive)[i].Resolution); - fprintf(stream_out, "Crv[%d].ExtDevices = %"PRId32"\n", - i, (*interactive)[i].ExtDevices); - fprintf(stream_out, "Crv[%d].Reserved = %"PRId32"\n", - i, (*interactive)[i].Reserved); - } -} - -void th_v60_interactive_data_print(FILE *stream_out, - th_v60_header_t *th_header, - th_v60_interactive_t **interactive, - options_t *options) { - unsigned int i; - int j; - pq_interactive_bin_t bin; - float64_t origin; - float64_t time_step; - - pq_interactive_bin_print_t print; - - if ( options->binary_out ) { - print = pq_interactive_bin_fwrite; - } else { - print = pq_interactive_bin_printf; - } - - for ( i = 0; i < th_header->NumberOfCurves; i++ ) { - bin.curve = i; - - origin = (float64_t)(*interactive)[i].Offset*1e3; - time_step = (*interactive)[i].Resolution*1e3; - for ( j = 0; j < th_header->NumberOfChannels; j++ ) { - bin.bin_left = origin + j*time_step; - bin.bin_right = bin.bin_left + time_step; - bin.counts = (*interactive)[i].Counts[j]; - - print(stream_out, &bin); - } - } -} +#include +#include + +#include "../timeharp.h" +#include "th_v60.h" + +#include "../error.h" +#include "../interactive.h" + +/* + * + * Streaming for interactive files. + * + */ +int th_v60_interactive_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v60_header_t *th_header, + options_t *options) { + int result; + th_v60_interactive_t *interactive; + int i; + + /* Read interactive header. */ + result = th_v60_interactive_read(stream_in, th_header, &interactive); + if ( result != PQ_SUCCESS ) { + error("Failed while reading interactive header.\n"); + } else { + if ( options->print_header ) { + pq_header_printf(stream_out, pq_header); + th_v60_header_printf(stream_out, th_header); + th_v60_interactive_header_printf(stream_out, th_header, + &interactive); + } else if ( options->print_resolution ) { + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + pq_resolution_print(stream_out, i, + (interactive[i].Resolution*1e3), options); + } + } else { + /* Read and print interactive data. */ + th_v60_interactive_data_print(stream_out, th_header, + &interactive, options); + } + } + + /* Clean and return. */ + debug("Freeing interactive header.\n"); + th_v60_interactive_free(th_header, &interactive); + return(PQ_SUCCESS); +} + +int th_v60_interactive_read(FILE *stream_in, + th_v60_header_t *th_header, + th_v60_interactive_t **interactive) { + int i; + size_t n_read; + + *interactive = (th_v60_interactive_t *)malloc(sizeof(th_v60_interactive_t)* + th_header->NumberOfCurves); + if ( *interactive == NULL ) { + error("Could not allocate interactive data.\n"); + return(PQ_ERROR_MEM); + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + n_read = fread(&(*interactive)[i], + sizeof(th_v60_interactive_t) - sizeof(uint32_t *), 1, + stream_in); + if ( n_read != 1 ) { + error("Could not allocate memory for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + + (*interactive)[i].Counts = (uint32_t *)malloc(sizeof(uint32_t)* + th_header->NumberOfChannels); + if ( (*interactive)[i].Counts == NULL ) { + error("Could not allocate memory for counts of curve " + "%"PRId32".\n", i); + return(PQ_ERROR_MEM); + } + + n_read = fread((*interactive)[i].Counts, + sizeof(uint32_t), th_header->NumberOfChannels, + stream_in); + if ( n_read != th_header->NumberOfChannels ) { + error("Could not read counts for curve %"PRId32".\n", i); + return(PQ_ERROR_IO); + } + } + + return(PQ_SUCCESS); +} + +void th_v60_interactive_free(th_v60_header_t *th_header, + th_v60_interactive_t **interactive) { + int i; + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + free((*interactive)[i].Counts); + } + free(*interactive); +} + + +void th_v60_interactive_header_printf(FILE *stream_out, + th_v60_header_t *th_header, + th_v60_interactive_t **interactive) { + int i; + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + fprintf(stream_out, "Crv[%d].CurveIndex = %"PRId32"\n", + i, (*interactive)[i].CurveIndex); + fprintf(stream_out, "Crv[%d].TimeOfRecording = %s", + i, ctime32(&(*interactive)[i].TimeOfRecording)); + fprintf(stream_out, "Crv[%d].BoardSerial = %"PRId32"\n", + i, (*interactive)[i].BoardSerial); + fprintf(stream_out, "Crv[%d].CFDZeroCross = %"PRId32"\n", + i, (*interactive)[i].CFDZeroCross); + fprintf(stream_out, "Crv[%d].CFDDiscrMin = %"PRId32"\n", + i, (*interactive)[i].CFDDiscrMin); + fprintf(stream_out, "Crv[%d].SyncLevel = %"PRId32"\n", + i, (*interactive)[i].SyncLevel); + fprintf(stream_out, "Crv[%d].CurveOffset = %"PRId32"\n", + i, (*interactive)[i].CurveOffset); + fprintf(stream_out, "Crv[%d].RoutingChannel = %"PRId32"\n", + i, (*interactive)[i].RoutingChannel); + fprintf(stream_out, "Crv[%d].SubMode = %"PRId32"\n", + i, (*interactive)[i].SubMode); + fprintf(stream_out, "Crv[%d].MeasMode = %"PRId32"\n", + i, (*interactive)[i].MeasMode); + fprintf(stream_out, "Crv[%d].P1 = %"PRIf32"\n", + i, (*interactive)[i].P1); + fprintf(stream_out, "Crv[%d].P2 = %"PRIf32"\n", + i, (*interactive)[i].P2); + fprintf(stream_out, "Crv[%d].P3 = %"PRIf32"\n", + i, (*interactive)[i].P3); + fprintf(stream_out, "Crv[%d].RangeNo = %"PRId32"\n", + i, (*interactive)[i].RangeNo); + fprintf(stream_out, "Crv[%d].Offset = %"PRId32"\n", + i, (*interactive)[i].Offset); + fprintf(stream_out, "Crv[%d].AcquisitionTime = %"PRId32"\n", + i, (*interactive)[i].AcquisitionTime); + fprintf(stream_out, "Crv[%d].StopAfter = %"PRId32"\n", + i, (*interactive)[i].StopAfter); + fprintf(stream_out, "Crv[%d].StopReason = %"PRId32"\n", + i, (*interactive)[i].StopReason); + fprintf(stream_out, "Crv[%d].SyncRate = %"PRId32"\n", + i, (*interactive)[i].SyncRate); + fprintf(stream_out, "Crv[%d].CFDCountRate = %"PRId32"\n", + i, (*interactive)[i].CFDCountRate); + fprintf(stream_out, "Crv[%d].TDCCountRate = %"PRId32"\n", + i, (*interactive)[i].TDCCountRate); + fprintf(stream_out, "Crv[%d].IntegralCount = %"PRId32"\n", + i, (*interactive)[i].IntegralCount); + fprintf(stream_out, "Crv[%d].Resolution = %"PRIf32"\n", + i, (*interactive)[i].Resolution); + fprintf(stream_out, "Crv[%d].ExtDevices = %"PRId32"\n", + i, (*interactive)[i].ExtDevices); + fprintf(stream_out, "Crv[%d].Reserved = %"PRId32"\n", + i, (*interactive)[i].Reserved); + } +} + +void th_v60_interactive_data_print(FILE *stream_out, + th_v60_header_t *th_header, + th_v60_interactive_t **interactive, + options_t *options) { + unsigned int i; + int j; + pq_interactive_bin_t bin; + float64_t origin; + float64_t time_step; + + pq_interactive_bin_print_t print; + + if ( options->binary_out ) { + print = pq_interactive_bin_fwrite; + } else { + print = pq_interactive_bin_printf; + } + + for ( i = 0; i < th_header->NumberOfCurves; i++ ) { + bin.curve = i; + + origin = (float64_t)(*interactive)[i].Offset*1e3; + time_step = (*interactive)[i].Resolution*1e3; + for ( j = 0; j < th_header->NumberOfChannels; j++ ) { + bin.bin_left = origin + j*time_step; + bin.bin_right = bin.bin_left + time_step; + bin.counts = (*interactive)[i].Counts[j]; + + print(stream_out, &bin); + } + } +} diff --git a/src/timeharp/th_v60_tttr.c b/src/timeharp/th_v60_tttr.c index 67eb8af..242facf 100644 --- a/src/timeharp/th_v60_tttr.c +++ b/src/timeharp/th_v60_tttr.c @@ -29,201 +29,201 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include - -#include "th_v60.h" - -#include "../timeharp.h" -#include "../error.h" - -/* - * - * TTTR mode routines. - * - */ -void th_v60_t3_init(th_v60_header_t *th_header, - th_v60_tttr_header_t *tttr_header, - tttr_t *tttr) { - tttr->origin = 0; - tttr->overflows = 0; - tttr->overflow_increment = TH_TTTR_OVERFLOW; - tttr->sync_rate = tttr_header->SyncRate; - tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; - tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); -} - -int th_v60_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { - size_t n_read; - th_v60_tttr_record_t record; - - n_read = fread(&record, sizeof(record), 1, stream_in); - - if ( n_read != 1 ) { - if ( ! feof(stream_in) ) { - error("Could not read t3 record.\n"); - return(PQ_ERROR_IO); - } else { - return(PQ_ERROR_EOF); - } - } else { - if ( record.Valid ) { - /* Normal record. */ - t3->channel = record.Data; - t3->pulse = tttr->origin; - t3->time = record.TimeTag; - return(PQ_RECORD_T3); - } else { - /* Special record. */ - if ( 0x800 & record.Data ) { - /* Overflow */ - tttr->overflows++; - tttr->origin += tttr->overflow_increment; - return(PQ_RECORD_OVERFLOW); - } else { - t3->channel = 0x800; - t3->pulse = record.Data & 0x07; - return(PQ_RECORD_MARKER); - } - } - } -} - -int th_v60_t3_stream(FILE *stream_in, FILE *stream_out, - th_v60_header_t *th_header, th_v60_tttr_header_t *tttr_header, - options_t *options) { - tttr_t tttr; - - th_v60_t3_init(th_header, tttr_header, &tttr); - - if ( options->to_t2 ) { - error("T3 -> T2 not supported for Timeharp.\n"); - return(PQ_ERROR_OPTIONS); - } else { - return(pq_t3_stream(stream_in, stream_out, - th_v60_t3_decode, &tttr, options)); - } -} - -int th_v60_tttr_stream(FILE *stream_in, FILE *stream_out, - pq_header_t *pq_header, th_v60_header_t *th_header, - options_t *options) { - th_v60_tttr_header_t *tttr_header; - int result; - - result = th_v60_tttr_header_read(stream_in, &tttr_header); - if ( result != PQ_SUCCESS ) { - error("Failed while reading the tttr header.\n"); - } else { - if ( options->print_header ) { - if ( options->binary_out ) { - pq_header_fwrite(stream_out, pq_header); - th_v60_header_fwrite(stream_out, th_header); - th_v60_tttr_header_fwrite(stream_out, tttr_header); - } else { - pq_header_printf(stream_out, pq_header); - th_v60_header_printf(stream_out, th_header); - th_v60_tttr_header_printf(stream_out, tttr_header); - } - - result = PQ_SUCCESS; - } else if ( options->print_resolution ) { - pq_resolution_print(stream_out, -1, - (th_header->Brd[0].Resolution*1e3), options); - result = PQ_SUCCESS; - } else { - result = th_v60_t3_stream(stream_in, stream_out, - th_header, tttr_header, options); - } - } - - debug("Freeing tttr header.\n"); - th_v60_tttr_header_free(&tttr_header); - return(result); -} - -int th_v60_tttr_header_read(FILE *stream_in, - th_v60_tttr_header_t **tttr_header) { - size_t n_read; - - *tttr_header = (th_v60_tttr_header_t *)malloc(sizeof(th_v60_tttr_header_t)); - - if ( *tttr_header == NULL ) { - error("Could not allocate tttr header.\n"); - return(PQ_ERROR_MEM); - } - - n_read = fread(*tttr_header, - sizeof(th_v60_tttr_header_t) - sizeof(int32_t *), - 1, - stream_in); - if ( n_read != 1 ) { - error("Could not read tttr header.\n"); - th_v60_tttr_header_free(tttr_header); - return(PQ_ERROR_IO); - } - - if ( (*tttr_header)->SpecHeaderLength > 0 ) { - (*tttr_header)->SpecHeader = (int32_t *)malloc( - sizeof(int32_t)*(*tttr_header)->SpecHeaderLength); - if ( (*tttr_header)->SpecHeader == NULL ) { - error("Could not allocate special header.\n"); - th_v60_tttr_header_free(tttr_header); - return(PQ_ERROR_MEM); - } - } - - return(PQ_SUCCESS); -} - -void th_v60_tttr_header_free(th_v60_tttr_header_t **tttr_header) { - if ( *tttr_header != NULL ) { - free((*tttr_header)->SpecHeader); - } - - free(*tttr_header); -} - -void th_v60_tttr_header_printf(FILE *stream_out, - th_v60_tttr_header_t *tttr_header) { - int i; - - fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", - tttr_header->TTTRGlobClock); - - for ( i = 0; i < 5; i++ ) { - fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", - i, tttr_header->Reserved[i]); - } - - fprintf(stream_out, "SyncRate = %"PRId32"\n", - tttr_header->SyncRate); - fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", - tttr_header->AverageCFDRate); - fprintf(stream_out, "StopAfter = %"PRId32"\n", - tttr_header->StopAfter); - fprintf(stream_out, "StopReason = %"PRId32"\n", - tttr_header->StopReason); - fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", - tttr_header->NumberOfRecords); - fprintf(stream_out, "SpecHeaderLength = %"PRId32"\n", - tttr_header->SpecHeaderLength); - - for ( i = 0; i < tttr_header->SpecHeaderLength; i++ ) { - fprintf(stream_out, "SpecHeader[%d] = %"PRId32"\n", - i, tttr_header->SpecHeader[i]); - } -} - -void th_v60_tttr_header_fwrite(FILE *stream_out, - th_v60_tttr_header_t *tttr_header) { - fwrite(tttr_header, - sizeof(th_v60_tttr_header_t) - sizeof(int32_t *), - 1, - stream_out); - fwrite(tttr_header->SpecHeader, - sizeof(int32_t), - tttr_header->SpecHeaderLength, - stream_out); -} +#include +#include +#include + +#include "th_v60.h" + +#include "../timeharp.h" +#include "../error.h" + +/* + * + * TTTR mode routines. + * + */ +void th_v60_t3_init(th_v60_header_t *th_header, + th_v60_tttr_header_t *tttr_header, + tttr_t *tttr) { + tttr->origin = 0; + tttr->overflows = 0; + tttr->overflow_increment = TH_TTTR_OVERFLOW; + tttr->sync_rate = tttr_header->SyncRate; + tttr->resolution_float = th_header->Brd[0].Resolution*1e-9; + tttr->resolution_int = floor(fabs(tttr->resolution_float*1e12)); +} + +int th_v60_t3_decode(FILE *stream_in, tttr_t *tttr, t3_t *t3) { + size_t n_read; + th_v60_tttr_record_t record; + + n_read = fread(&record, sizeof(record), 1, stream_in); + + if ( n_read != 1 ) { + if ( ! feof(stream_in) ) { + error("Could not read t3 record.\n"); + return(PQ_ERROR_IO); + } else { + return(PQ_ERROR_EOF); + } + } else { + if ( record.Valid ) { + /* Normal record. */ + t3->channel = record.Data; + t3->pulse = tttr->origin; + t3->time = record.TimeTag; + return(PQ_RECORD_T3); + } else { + /* Special record. */ + if ( 0x800 & record.Data ) { + /* Overflow */ + tttr->overflows++; + tttr->origin += tttr->overflow_increment; + return(PQ_RECORD_OVERFLOW); + } else { + t3->channel = 0x800; + t3->pulse = record.Data & 0x07; + return(PQ_RECORD_MARKER); + } + } + } +} + +int th_v60_t3_stream(FILE *stream_in, FILE *stream_out, + th_v60_header_t *th_header, th_v60_tttr_header_t *tttr_header, + options_t *options) { + tttr_t tttr; + + th_v60_t3_init(th_header, tttr_header, &tttr); + + if ( options->to_t2 ) { + error("T3 -> T2 not supported for Timeharp.\n"); + return(PQ_ERROR_OPTIONS); + } else { + return(pq_t3_stream(stream_in, stream_out, + th_v60_t3_decode, &tttr, options)); + } +} + +int th_v60_tttr_stream(FILE *stream_in, FILE *stream_out, + pq_header_t *pq_header, th_v60_header_t *th_header, + options_t *options) { + th_v60_tttr_header_t *tttr_header; + int result; + + result = th_v60_tttr_header_read(stream_in, &tttr_header); + if ( result != PQ_SUCCESS ) { + error("Failed while reading the tttr header.\n"); + } else { + if ( options->print_header ) { + if ( options->binary_out ) { + pq_header_fwrite(stream_out, pq_header); + th_v60_header_fwrite(stream_out, th_header); + th_v60_tttr_header_fwrite(stream_out, tttr_header); + } else { + pq_header_printf(stream_out, pq_header); + th_v60_header_printf(stream_out, th_header); + th_v60_tttr_header_printf(stream_out, tttr_header); + } + + result = PQ_SUCCESS; + } else if ( options->print_resolution ) { + pq_resolution_print(stream_out, -1, + (th_header->Brd[0].Resolution*1e3), options); + result = PQ_SUCCESS; + } else { + result = th_v60_t3_stream(stream_in, stream_out, + th_header, tttr_header, options); + } + } + + debug("Freeing tttr header.\n"); + th_v60_tttr_header_free(&tttr_header); + return(result); +} + +int th_v60_tttr_header_read(FILE *stream_in, + th_v60_tttr_header_t **tttr_header) { + size_t n_read; + + *tttr_header = (th_v60_tttr_header_t *)malloc(sizeof(th_v60_tttr_header_t)); + + if ( *tttr_header == NULL ) { + error("Could not allocate tttr header.\n"); + return(PQ_ERROR_MEM); + } + + n_read = fread(*tttr_header, + sizeof(th_v60_tttr_header_t) - sizeof(int32_t *), + 1, + stream_in); + if ( n_read != 1 ) { + error("Could not read tttr header.\n"); + th_v60_tttr_header_free(tttr_header); + return(PQ_ERROR_IO); + } + + if ( (*tttr_header)->SpecHeaderLength > 0 ) { + (*tttr_header)->SpecHeader = (int32_t *)malloc( + sizeof(int32_t)*(*tttr_header)->SpecHeaderLength); + if ( (*tttr_header)->SpecHeader == NULL ) { + error("Could not allocate special header.\n"); + th_v60_tttr_header_free(tttr_header); + return(PQ_ERROR_MEM); + } + } + + return(PQ_SUCCESS); +} + +void th_v60_tttr_header_free(th_v60_tttr_header_t **tttr_header) { + if ( *tttr_header != NULL ) { + free((*tttr_header)->SpecHeader); + } + + free(*tttr_header); +} + +void th_v60_tttr_header_printf(FILE *stream_out, + th_v60_tttr_header_t *tttr_header) { + int i; + + fprintf(stream_out, "TTTRGlobCLock = %"PRId32"\n", + tttr_header->TTTRGlobClock); + + for ( i = 0; i < 5; i++ ) { + fprintf(stream_out, "Reserved[%d] = %"PRId32"\n", + i, tttr_header->Reserved[i]); + } + + fprintf(stream_out, "SyncRate = %"PRId32"\n", + tttr_header->SyncRate); + fprintf(stream_out, "AverageCFDRate = %"PRId32"\n", + tttr_header->AverageCFDRate); + fprintf(stream_out, "StopAfter = %"PRId32"\n", + tttr_header->StopAfter); + fprintf(stream_out, "StopReason = %"PRId32"\n", + tttr_header->StopReason); + fprintf(stream_out, "NumberOfRecords = %"PRId32"\n", + tttr_header->NumberOfRecords); + fprintf(stream_out, "SpecHeaderLength = %"PRId32"\n", + tttr_header->SpecHeaderLength); + + for ( i = 0; i < tttr_header->SpecHeaderLength; i++ ) { + fprintf(stream_out, "SpecHeader[%d] = %"PRId32"\n", + i, tttr_header->SpecHeader[i]); + } +} + +void th_v60_tttr_header_fwrite(FILE *stream_out, + th_v60_tttr_header_t *tttr_header) { + fwrite(tttr_header, + sizeof(th_v60_tttr_header_t) - sizeof(int32_t *), + 1, + stream_out); + fwrite(tttr_header->SpecHeader, + sizeof(int32_t), + tttr_header->SpecHeaderLength, + stream_out); +}