

%% generate .nrrd files

% -----===== LOAD =====-----
CDPpath = '\\Mpufs5\data_wnx1\_Data\CDP\CDP005\CDP005a\Formatted\';

PETin   =am2mat([CDPpath 'frames\CDP005a_pet_fdg_FOV50_MBq_fr1.am']);
CTin   =am2mat([CDPpath 'CDP005a_ct_FOV50.am']);

SEGin = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\CDP_data\CDP5_SEG_FullResolution.nrrd');
DOGin = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\CDP_data\CDP5_Doggo_FullResolution.nrrd');

% -----===== GENERATE =====-----
CTimg=permute(imresize(CTin.data, 0.5), [2,1,3]);
PETimg=permute(PETin.data, [2,1,3]);
CTimg=imresize3(CTimg, size(PETimg));

% colorwash(CTimg, PETimg, [-500,500], [0,0.03])

% downsize
Dfactor=2; % should be at least 2
CTimg2 = imresize(CTimg, 1/Dfactor);
PETimg2 =imresize(PETimg, 1/Dfactor);
voxel_size = PETin.voxel_size*Dfactor;
ImStart = PETin.start;

SEG = round(imresize(SEGin, 1/Dfactor));
DOG = round(imresize(DOGin, 1/Dfactor));

% virtual resize
Rfactor = 1;
voxel_size = voxel_size/Rfactor;
ImStart = ImStart/Rfactor;

% -----===== SAVE =====-----
out_path='C:\010-work\003_localGit\WiscPlan_v2\CDP_data\';
filename=[out_path, 'CDP5_CT.nrrd'];
matrix=single(CTimg2);
pixelspacing=voxel_size;
origin = ImStart;
encoding = 'raw';
nrrdWriter(filename, matrix, pixelspacing, origin, encoding);

out_path='C:\010-work\003_localGit\WiscPlan_v2\CDP_data\';
filename=[out_path, 'CDP5_PET.nrrd'];
matrix=PETimg2;
pixelspacing=voxel_size;
origin = ImStart;
encoding = 'raw';
nrrdWriter(filename, matrix, pixelspacing, origin, encoding);

out_path='C:\010-work\003_localGit\WiscPlan_v2\CDP_data\';
filename=[out_path, 'CDP5_SEG.nrrd'];
matrix=SEG;
pixelspacing=voxel_size;
origin = ImStart;
encoding = 'raw';
nrrdWriter(filename, matrix, pixelspacing, origin, encoding);

out_path='C:\010-work\003_localGit\WiscPlan_v2\CDP_data\';
filename=[out_path, 'CDP5_Doggo.nrrd'];
matrix=DOG;
pixelspacing=voxel_size;
origin = ImStart;
encoding = 'raw';
nrrdWriter(filename, matrix, pixelspacing, origin, encoding);

% -----===== TEST =====-----
% CTin=nrrdread('C:\010-work\003_localGit\WiscPlan_v2\CDP_data\CDP5_CT.nrrd');
% PETin=nrrdread('C:\010-work\003_localGit\WiscPlan_v2\CDP_data\CDP5_PET.nrrd');
% 
% colorwash(CTin, PETin, [-500,500], [0,0.03])




