aboutsummaryrefslogtreecommitdiff
path: root/julia/CrystFEL/src/reflists.jl
blob: ba7311da2407f7be5c9845712f98f26d1a522699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
module RefLists

using Printf
import ..CrystFEL: libcrystfel
import ..CrystFEL.Symmetry: SymOpList, InternalSymOpList, symmetry_name
export RefList, loadreflist, savereflist!
export Reflection, UnmergedReflection, MergedReflection
export InternalRefList


# The internal libcrystfel structures, not exposed directly
# We only ever have e.g. a Ptr{InternalRefList}, never a real InternalRefList
mutable struct InternalRefList end
mutable struct InternalReflection end
mutable struct InternalRefListIterator end

# The Julian exposed types
abstract type Reflection end

mutable struct RefList{T<:Reflection}
    internalptr::Ptr{InternalRefList}
    symmetry::SymOpList
end

mutable struct RefListIterator
    lastrefl::Ptr{InternalReflection}
    internalptr::Ptr{InternalRefListIterator}
end

mutable struct MergedReflection <: Reflection
    internalptr::Ptr{InternalReflection}
end

mutable struct UnmergedReflection <: Reflection
    internalptr::Ptr{InternalReflection}
end


function RefList{MergedReflection}(sym::SymOpList)
    out = @ccall libcrystfel.reflist_new()::Ptr{InternalRefList}
    if out == C_NULL
        throw(ErrorException("Failed to create RefList"))
    end
    finalizer(RefList{MergedReflection}(out, sym)) do x
        @ccall libcrystfel.reflist_free(x.internalptr::Ptr{InternalRefList})::Cvoid
    end
end


function Base.iterate(reflist::RefList{T}) where T

    rli = Ref{Ptr{InternalRefListIterator}}(C_NULL)
    refl = ccall((:first_refl, libcrystfel),
                 Ptr{InternalReflection}, (Ptr{InternalRefList},Ref{Ptr{InternalRefListIterator}}),
                 reflist.internalptr, rli)

    if refl == C_NULL
        return nothing   # no reflections!
    end

    iter = RefListIterator(refl,rli[])
    finalizer(iter) do x
        ccall((:free_reflistiterator, libcrystfel),
              Cvoid, (Ptr{InternalRefListIterator},), x.internalptr)
    end

    return T(refl),iter

end


function Base.iterate(::RefList{T}, iter) where T

    refl = ccall((:next_refl, libcrystfel),
                 Ptr{InternalReflection}, (Ptr{InternalReflection},Ptr{InternalRefListIterator}),
                 iter.lastrefl, iter.internalptr)

    if refl == C_NULL
        iter.internalptr = C_NULL   # libcrystfel already freed it
        return nothing
    end

    iter.lastrefl = refl

    return T(refl),iter

end


Base.IteratorEltype(::RefList{T}) where T = T
Base.isdone(iter::RefListIterator) = ((iter.internalptr == C_NULL) && (iter.lastrefl != C_NULL))
Base.length(reflist::RefList) = ccall((:num_reflections, libcrystfel),
                                      Cint, (Ptr{InternalRefList},), reflist.internalptr)


function Base.getindex(reflist::RefList{T}, indices) where T

    refl = @ccall libcrystfel.find_refl(reflist.internalptr::Ptr{InternalRefList},
                                        indices[1]::Cint,
                                        indices[2]::Cint,
                                        indices[3]::Cint)::Ptr{InternalReflection}

    if refl == C_NULL
        return nothing
    else
        return T(refl)
    end
end


function Base.get!(reflist::RefList{T}, indices) where T

    refl = @ccall libcrystfel.find_refl(reflist.internalptr::Ptr{InternalRefList},
                                        indices[1]::Cint,
                                        indices[2]::Cint,
                                        indices[3]::Cint)::Ptr{InternalReflection}

    if refl == C_NULL
        return push!(reflist, indices)
    else
        return T(refl)
    end
end


function Base.push!(reflist::RefList{T}, indices) where T

    refl = @ccall libcrystfel.add_refl(reflist.internalptr::Ptr{InternalRefList},
                                       indices[1]::Cint,
                                       indices[2]::Cint,
                                       indices[3]::Cint)::Ptr{InternalReflection}

    if refl == C_NULL
        return nothing
    else
        return T(refl)
    end
end


function loadreflist(filename::AbstractString)

    psym = Ref{Cstring}()
    out = ccall((:read_reflections_2, libcrystfel),
                Ptr{InternalRefList}, (Cstring,Ref{Cstring}),
                filename, psym)
    if out == C_NULL
        throw(ArgumentError("Failed to load reflection list"))
    end

    symmetryname = unsafe_string(psym[])
    return RefList{MergedReflection}(out, SymOpList(symmetryname))

end


function savereflist!(reflist::RefList{MergedReflection}, filename::AbstractString)

    r = @ccall libcrystfel.write_reflist_2(filename::Cstring,
                                           reflist.internalptr::Ptr{InternalRefList},
                                           reflist.symmetry.internalptr::Ptr{InternalSymOpList})::Cint

    if r != 0
        throw(ErrorException("Failed to save reflection list"))
    end

end


function Base.show(io::IO, ::MIME"text/plain", reflist::RefList{MergedReflection})
    println(io, "Merged reflection list in point group ", symmetry_name(reflist.symmetry))
    print(io, "   h    k    l  intensity  σ(intens) nmeas")
    let n = 0
        for refl in Iterators.take(reflist, 11)
            if n == 10
                # We have printed 10 already, and are here again.  Truncate...
                print(io, "\n   ⋮    ⋮    ⋮          ⋮          ⋮     ⋮")
                break
            end
            let ind = refl.indices
                write(io, "\n")
                @printf(io, "%4i %4i %4i %10.2f %10.2f %5i", ind[1], ind[2], ind[3],
                        refl.intensity, refl.sigintensity, refl.nmeasurements)
                n += 1
            end
        end
    end
end


function Base.show(io::IO, ::MIME"text/plain", reflist::RefList{UnmergedReflection})
    println(io, "Unmerged reflection list in point group ", symmetry_name(reflist.symmetry))
    print(io, "   h    k    l  intensity  σ(intens)      fs      ss  panel")
    let n = 0
        for refl in Iterators.take(reflist, 11)
            if n == 10
                # We have printed 10 already, and are here again.  Truncate...
                print(io, "\n   ⋮    ⋮    ⋮          ⋮          ⋮       ⋮       ⋮      ⋮")
                break
            end
            let ind = refl.indices,
                pos = refl.detectorposition
                write(io, "\n")
                @printf(io, "%4i %4i %4i %10.2f %10.2f %7.2f %7.2f %6i",
                        ind[1], ind[2], ind[3], refl.intensity, refl.sigintensity,
                        pos.fs, pos.ss, pos.panelnumber)
                n += 1
            end
        end
    end
end


function detectorpos(refl::Reflection)
    pfs = Ref{Cdouble}(0)
    pss = Ref{Cdouble}(0)
    pn = ccall((:get_panel_number, libcrystfel),
               Cint, (Ptr{InternalReflection},), refl.internalptr)
    ccall((:get_detector_pos, libcrystfel),
          Cint, (Ptr{InternalReflection},Ref{Cdouble},Ref{Cdouble}),
          refl.internalptr, pfs, pss)
    (fs=pfs[], ss=pss[], panelnumber=pn)
end


function indices(refl::Reflection)
    h = Ref{Cint}(0)
    k = Ref{Cint}(0)
    l = Ref{Cint}(0)
    ccall((:get_indices, libcrystfel),
          Cint, (Ptr{InternalReflection},Ref{Cint},Ref{Cint},Ref{Cint}),
          refl.internalptr, h, k, l)
    [h[], k[], l[]]
end


function symmetricindices(refl::Reflection)
    h = Ref{Cint}(0)
    k = Ref{Cint}(0)
    l = Ref{Cint}(0)
    ccall((:get_symmetric_indices, libcrystfel),
          Cint, (Ptr{InternalReflection},Ref{Cint},Ref{Cint},Ref{Cint}),
          refl.internalptr, h, k, l)
    [h[], k[], l[]]
end


function Base.getproperty(refl::Reflection, name::Symbol)
    if name === :intensity
        ccall((:get_intensity, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :sigintensity
        ccall((:get_esd_intensity, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :partiality
        ccall((:get_partiality, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :khalf
        ccall((:get_khalf, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :kpred
        ccall((:get_kpred, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :excitationerror
        ccall((:get_exerr, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :lorentzfactor
        ccall((:get_lorentz, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :phase
        ccall((:get_phase, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :peak
        ccall((:get_peak, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :meanbackground
        ccall((:get_mean_bg, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :temp1
        ccall((:get_temp1, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :temp2
        ccall((:get_temp2, libcrystfel),
              Cdouble, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :nmeasurements
        ccall((:get_redundancy, libcrystfel),
              Cint, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :flag
        ccall((:get_flag, libcrystfel),
              Cint, (Ptr{InternalReflection},), refl.internalptr)
    elseif name === :detectorposition
        detectorpos(refl)
    elseif name === :indices
        indices(refl)
    elseif name === :symmetricindices
        symmetricindices(refl)
    else
        getfield(refl, name)
    end
end


function Base.setproperty!(refl::Reflection, name::Symbol, val)
    if name === :intensity
        @ccall libcrystfel.set_intensity(refl.internalptr::Ptr{InternalReflection},
                                         val::Cdouble)::Cvoid
    elseif name === :sigintensity
        @ccall libcrystfel.set_esd_intensity(refl.internalptr::Ptr{InternalReflection},
                                             val::Cdouble)::Cvoid
    elseif name === :partiality
        @ccall libcrystfel.set_partiality(refl.internalptr::Ptr{InternalReflection},
                                          val::Cdouble)::Cvoid
    elseif name === :khalf
        @ccall libcrystfel.set_khalf(refl.internalptr::Ptr{InternalReflection},
                                     val::Cdouble)::Cvoid
    elseif name === :kpred
        @ccall libcrystfel.set_kpred(refl.internalptr::Ptr{InternalReflection},
                                     val::Cdouble)::Cvoid
    elseif name === :excitationerror
        @ccall libcrystfel.set_exerr(refl.internalptr::Ptr{InternalReflection},
                                     val::Cdouble)::Cvoid
    elseif name === :lorentzfactor
        @ccall libcrystfel.set_lorentz(refl.internalptr::Ptr{InternalReflection},
                                       val::Cdouble)::Cvoid
    elseif name === :phase
        @ccall libcrystfel.set_phase(refl.internalptr::Ptr{InternalReflection},
                                     val::Cdouble)::Cvoid
    elseif name === :peak
        @ccall libcrystfel.set_peak(refl.internalptr::Ptr{InternalReflection},
                                    val::Cdouble)::Cvoid
    elseif name === :meanbackground
        @ccall libcrystfel.set_mean_bg(refl.internalptr::Ptr{InternalReflection},
                                       val::Cdouble)::Cvoid
    elseif name === :temp1
        @ccall libcrystfel.set_temp1(refl.internalptr::Ptr{InternalReflection},
                                     val::Cdouble)::Cvoid
    elseif name === :temp2
        @ccall libcrystfel.set_temp2(refl.internalptr::Ptr{InternalReflection},
                                     val::Cdouble)::Cvoid
    elseif name === :nmeasurements
        @ccall libcrystfel.set_redundancy(refl.internalptr::Ptr{InternalReflection},
                                          val::Cint)::Cvoid
    elseif name === :flag
        @ccall libcrystfel.set_flag(refl.internalptr::Ptr{InternalReflection},
                                    val::Cint)::Cvoid
    elseif name === :detectorposition
        @ccall libcrystfel.set_detector_pos(refl.internalptr::Ptr{InternalReflection},
                                            val.fs::Cdouble, val.ss::Cdouble)::Cvoid
        @ccall libcrystfel.set_panel_number(refl.internalptr::Ptr{InternalReflection},
                                            val.panelnumber::Cint)::Cvoid
    elseif name === :indices
        throw(ErrorException("Cannot set the indices of a Reflection"))
    elseif name === :symmetricindices
        @ccall libcrystfel.set_symmetric_indices(refl.internalptr::Ptr{InternalReflection},
                                                 val[1]::Cint, val[2]::Cint, val[3]::Cint)::Cvoid
    else
        setfield!(refl, name, val)
    end
end


function Base.propertynames(::UnmergedReflection; private=false)
    names = (:intensity,:sigintensity,:partiality,:khalf,:kpred,:lorentzfactor,
             :excitationerror,:phase,:peak,:meanbackground,:temp1,:temp2,
             :nmeasurements,:flag,:detectorposition,:indices,:symmetricindices)
    if private
        tuple(names..., :internalptr)
    else
        names
    end
end


function Base.propertynames(::MergedReflection; private=false)
    names = (:intensity,:sigintensity,:phase,:temp1,:temp2,
             :nmeasurements,:flag,:indices,:symmetricindices)
    if private
        tuple(names..., :internalptr)
    else
        names
    end
end


function Base.show(io::IO, refl::MergedReflection)
    write(io, "MergedReflection(")
    show(io, refl.indices)
    write(io, ", intensity=")
    show(io, refl.intensity)
    write(io, ", σ(intensity)=")
    show(io, refl.sigintensity)
    write(io, ", nmeasurements=")
    show(io, refl.nmeasurements)
    write(io, ")")
end


function Base.show(io::IO, refl::UnmergedReflection)
    write(io, "UnmergedReflection(")
    show(io, refl.indices)
    write(io, " at ");
    show(io, refl.detectorposition)
    write(io, ", intensity=")
    show(io, refl.intensity)
    write(io, ", σ(intensity)=")
    show(io, refl.sigintensity)
    write(io, ")")
end


end  # of module