# Writing .Cdf file

**URL:** <https://castor-project.discourse.group/t/writing-cdf-file/24>\
**Category:** General Discussions\
**Created:** [May 15, 2017, 10:55am UTC](https://castor-project.discourse.group/t/writing-cdf-file/24 "2017-05-15T10:55:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konstantinos\_P\_Chatz](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Konstantinos\_P\_Chatz](https://castor-project.discourse.group/u/Konstantinos_P_Chatz)\
**Post date:** [May 15, 2017, 10:55am UTC](https://castor-project.discourse.group/t/writing-cdf-file/24/1 "2017-05-15T10:55:18Z")

</div>

Dear users/developers,

I am trying to create a program that will write experimental results to the appropriate .Cdf ascii format. I am using this loop:

char buffer [20];  
sprintf(buffer, “CDF\_CstrProj.Cdf”);  
pFile = fopen (buffer, “w”);  
for (id1=0; id1\<bins\_elts; id1++)  
{  
for (id2=0; id2\<nCrystalsTot; id2++)  
{  
fprintf(pFile,"%6d “,timeMS[id1][id2]);  
fprintf(pFile,”%6d “,data[id1][id2]);  
fprintf(pFile,”%6d “,id1);  
fprintf(pFile,”%6d ",id2);  
}  
}

Where id1 is the projectionID (0-35) and id2 is the pixelID (0-2997). Data are the counts. I am using integers in data but I have also try it with floats. Then by using a .Cdh I try to reconstruct. There is a problem there. Probably castor-recon does not recognize the file correct, because I am getting this fault repeatedly (the “Projection index” only change:

\*\*\*\*\* oIterativeAlgorithm::StepInnerLoopInsideSubsetLoop() → A problem occured while computing the projection line !  
\*\*\*\*\* iScannerSPECTConv::GetPositionsAndOrientations() → Projection index (538976288) out of range [0:35] !  
\*\*\*\*\* vProjector::Project() → A problem occured while getting positions and orientations from scanner !  
\*\*\*\*\* oProjectorManager::ComputeProjectionLine() → A problem occured while forward projecting an event !

I am attaching the file in order that you take a look and help me.

Thank you in advance,

Konstantinos

[CDF\_CstrProj.Cdh](https://castor-project.discourse.group/uploads/short-url/3mBUuJThTeFtmsdbbvsu5a71efV.Cdh) (640 Bytes)

[CDF\_CstrProj.Cdf](https://castor-project.discourse.group/uploads/short-url/uibem2VHSoqYLr6niymBWkCSCVW.Cdf) (2.88 MB)

---

<div class="post-metadata">

**Author:** ![tmerlin](https://yyz2.discourse-cdn.com/free1/user_avatar/castor-project.discourse.group/tmerlin/32/216_2.png) [@tmerlin](https://castor-project.discourse.group/u/tmerlin)\
**Post date:** [May 15, 2017, 3:24pm UTC](https://castor-project.discourse.group/t/writing-cdf-file/24/2 "2017-05-15T15:24:36Z")

</div>

Hi Konstantinos,

The out of range projection index errors usually means the code can’t correctly read the binary datafile as you said. Judging from the values in your cdf file, I think something went wrong during your conversion. Expected types are float for the data, and unsigned integers (4 bytes) for time & ids. I suggest trying something like this in your program:

uint32\_t id1, id2,\*\* timeMS; float\*\* data;

// recover data

for (id1=0; id1\<bins\_elts; id1++)  
{  
for (id2=0; id2\<nCrystalsTot; id2++)  
{  
fwrite((char\*)(&timeMS[id1][id2]) , sizeof(uint32\_t), 1, pFile);  
fwrite((char\*)(&data[id1][id2]) , sizeof(float), 1, pFile);  
fwrite((char\*)(&id1) , sizeof(uint32\_t), 1, pFile);  
fwrite((char\*)(&id2) , sizeof(uint32\_t), 1, pFile);  
}  
}

Besides, I am not sure why your datafile header states 188811 events, while I would expect 107928 events from your numbers (nb of projections \* nb of pixels).

Best regards,

Thibaut

---

<div class="post-metadata">

**Author:** ![Konstantinos\_P\_Chatz](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Konstantinos\_P\_Chatz](https://castor-project.discourse.group/u/Konstantinos_P_Chatz)\
**Post date:** [May 15, 2017, 8:54pm UTC](https://castor-project.discourse.group/t/writing-cdf-file/24/3 "2017-05-15T20:54:09Z")

</div>

Hello,

It worked, thank you very much. I have one more question.

I created the files I am attaching. There is something wrong again. The reconstructed image is empty… All black…

Can you help me on this?

Many thanks,

Konstantinos

Στις 15-05-2017 18:24, tmerlin έγραψε:

[header.Cdh](https://castor-project.discourse.group/uploads/short-url/d4ruFjGWEoIC7E7kSuupWR708BN.Cdh) (648 Bytes)

[data.Cdf](https://castor-project.discourse.group/uploads/short-url/wk0b8kPUicNCsc7dYIWSNvJK2Rm.Cdf) (1.65 MB)

---

<div class="post-metadata">

**Author:** ![Konstantinos\_P\_Chatz](https://avatars.discourse-cdn.com/v4/letter/k/ac8455/32.png) [@Konstantinos\_P\_Chatz](https://castor-project.discourse.group/u/Konstantinos_P_Chatz)\
**Post date:** [May 15, 2017, 10:18pm UTC](https://castor-project.discourse.group/t/writing-cdf-file/24/4 "2017-05-15T22:18:21Z")

</div>

Hi again,

Sorry about this. I am re-attaching the files. Something is wrong in the previous ones. Question still the same. What is wrong?

Kind Regards,

Konstantinos

Στις 15-05-2017 23:54, Konstantinos P. Chatzipapas, MSc έγραψε:

[header.Cdh](https://castor-project.discourse.group/uploads/short-url/dkvc7fKHhbqF8aEcTl5lEOQie0D.Cdh) (649 Bytes)

[data.Cdf](https://castor-project.discourse.group/uploads/short-url/sDO10BEXMQWKodLEBupqqVIjNeU.Cdf) (1.65 MB)

---

<div class="post-metadata">

**Author:** ![tmerlin](https://yyz2.discourse-cdn.com/free1/user_avatar/castor-project.discourse.group/tmerlin/32/216_2.png) [@tmerlin](https://castor-project.discourse.group/u/tmerlin)\
**Post date:** [May 17, 2017, 7:05pm UTC](https://castor-project.discourse.group/t/writing-cdf-file/24/5 "2017-05-17T19:05:08Z")

</div>

Hi Konstantinos,

Data values in your datafile appear to be very small, around 10^-44 (see attached, Float32). I think there is still something wrong in your conversion.

Best regards,

Thibaut

 ![datak.png](https://global.discourse-cdn.com/free1/uploads/castor_project/original/1X/999c59b146a0162228f626c14f996c4f460de49e.png)
