#!/bin/sh

PARTIALATOR=$1

cat > partialator_merge_check_1.stream << EOF
CrystFEL stream format 2.3
Command line: indexamajig -i dummy.lst -o dummy.stream --kraken=prawn
----- Begin geometry file -----
photon_energy = 9000 eV
clen = 50 mm
panel/min_fs = 0
panel/min_ss = 0
panel/max_fs = 1023
panel/max_ss = 1023
panel/fs = x
panel/ss = y
panel/corner_x = -100
panel/corner_y = -100
panel/res = 1000000
panel/adu_per_photon = 1
panel/data = /data/data_array
----- End geometry file -----
----- Begin chunk -----
Image filename: dummy.h5
photon_energy_eV = 6000.0
beam_bandwidth = 0.05 %
beam_divergence = 1 mrad
--- Begin crystal
Cell parameters 27.74398 27.84377 16.90346 nm, 88.53688 91.11774 118.75944 deg
astar = -0.0283891 +0.0149254 -0.0257273 nm^-1
bstar = -0.0068281 +0.0403989 -0.0005196 nm^-1
cstar = +0.0406926 +0.0052233 -0.0426520 nm^-1
profile_radius = 0.0005 nm^-1
Reflections measured after indexing
   h    k    l          I   sigma(I)       peak background  fs/px  ss/px panel
   1   0   0       100.00       0.00      100.0        0.0  938.0  629.0 panel
End of reflections
--- End crystal
--- Begin crystal
Cell parameters 27.74398 27.84377 16.90346 nm, 88.53688 91.11774 118.75944 deg
astar = -0.0283891 +0.0149254 -0.0257273 nm^-1
bstar = -0.0068281 +0.0403989 -0.0005196 nm^-1
cstar = +0.0406926 +0.0052233 -0.0426520 nm^-1
profile_radius = 0.0005 nm^-1
Reflections measured after indexing
   h    k    l          I   sigma(I)       peak background  fs/px  ss/px panel
   1   0   0       200.00       0.00      100.0        0.0  938.0  629.0 panel
End of reflections
--- End crystal
----- End chunk -----
EOF

# We merge two patterns, without scaling or partiality, the result should just
# be an average.
cat > partialator_merge_check_1_ans.hkl << EOF
CrystFEL reflection list version 2.0
Symmetry: 1
   h    k    l          I    phase   sigma(I)   nmeas
   1    0    0     150.00        -      35.36       2
EOF

$PARTIALATOR -i partialator_merge_check_1.stream \
             -o partialator_merge_check_1.hkl  \
             --model=unity --iterations=0 --no-scale --no-polarisation \
             --no-logs --no-free
if [ $? -ne 0 ]; then
	exit 1
fi

sed -n '/End of reflections/q;p' partialator_merge_check_1.hkl > temp1.hkl
mv temp1.hkl partialator_merge_check_1.hkl
diff partialator_merge_check_1.hkl partialator_merge_check_1_ans.hkl
if [ $? -ne 0 ]; then
	exit 1
fi
rm -f partialator_merge_check_1.stream partialator_merge_check_1.hkl \
      partialator_merge_check_1_ans.hkl partialator.params \
      partialator_merge_check_1.hkl1 partialator_merge_check_1.hkl2 \
      pgraph-iter0.dat pgraph-iter1.dat
exit 0
