Example #1
def v2calib2sections(filename):
  """The v2calib2sections() function reads calibration information
  stored in new style SLAC calibration file and returns a
  two-dimensional array of Section objects.  The first index in the
  returned array identifies the quadrant, and the second index
  identifies the section within the quadrant.

  @param dirname Directory with calibration information
  @return        Section objects

  from xfel.cftbx.detector.cspad_cbf_tbx import read_slac_metrology
  from scitbx.matrix import sqr
  from xfel.cxi.cspad_ana.cspad_tbx import pixel_size

  # metro is a dictionary where the keys are levels in the detector
  # hierarchy and the values are 'basis' objects
  metro = read_slac_metrology(filename)

  # 90 degree rotation to get into same frame
  reference_frame = sqr((0,-1, 0, 0,
                         1, 0, 0, 0,
                         0, 0, 1, 0,
                         0, 0, 0, 1))

  d = 0
  d_basis = metro[(d,)]

  sections = []
  for q in xrange(4):
    q_basis = metro[(d,q)]
    for s in xrange(8):
      if not (d,q,s) in metro:

      s_basis = metro[(d,q,s)]

      # collapse the transformations from the detector center to the quadrant center
      # to the sensor center
      transform = reference_frame * \
                  d_basis.as_homogenous_transformation() * \
                  q_basis.as_homogenous_transformation() * \

      # an homologous transformation is a 4x4 matrix, with a 3x3 rotation in the
      # upper left corner and the translation in the right-most column. The last
      # row is 0,0,0,1
      ori = sqr((transform[0],transform[1],transform[2],
      angle = ori.r3_rotation_matrix_as_x_y_z_angles(deg=True)[2]

      # move the reference of the sensor so its relative to the upper left of the
      # detector instead of the center of the detector
      center = (1765/2)+(transform[3]/pixel_size),(1765/2)+(transform[7]/pixel_size)

      sections[q].append(Section(angle, center))

  return sections
    print "Write a CBF header from a SLAC metrology file. Parameters:"

  user_phil = []
  for arg in args:
    if (os.path.isfile(arg)) :
      user_phil.append(libtbx.phil.parse("""metrology_file=\"%s\"""" % arg))
    else :
      try :
      except RuntimeError, e :
        raise Sorry("Unrecognized argument '%s' (error: %s)" % (arg, str(e)))

  params = master_phil.fetch(sources=user_phil).extract()
  if (params.metrology_file is None) :
    raise Usage("metrology_file must be defined (either metrology_file=XXX, or the file path alone).")
  assert params.plot is not None
  assert params.out is not None

  print params.metrology_file

  metro = read_slac_metrology(params.metrology_file, plot=params.plot)

  write_cspad_cbf(None, metro, 'cbf', None, params.out, None, 0, header_only=True)

if (__name__ == "__main__") :
  args = sys.argv[1:]
  if annulus:
    if params.resolution is None:
      print "Generating annular gain mask between %f and %f angstroms, assuming a distance %s mm and wavelength %s angstroms" % \
        (params.annulus_inner, params.annulus_outer, params.distance, params.wavelength)
      print "Generating annular gain mask between %f and %f angstroms, assuming a distance %s mm and wavelength %s angstroms. Also, pixels higher than %f angstroms will be set to low gain." % \
        (params.annulus_inner, params.annulus_outer, params.distance, params.wavelength, params.resolution)
  elif params.resolution is not None:
    print "Generating circular gain mask %s angstroms, assuming a distance %s mm and wavelength %s angstroms" % \
      (params.resolution, params.distance, params.wavelength)

  from xfel.cftbx.detector.cspad_cbf_tbx import read_slac_metrology, get_cspad_cbf_handle
  from dxtbx.format.FormatCBFCspad import FormatCBFCspadInMemory
  from dxtbx.model.beam import beam_factory
  metro = read_slac_metrology(path = params.optical_metrology_path)
  cbf = get_cspad_cbf_handle(None, metro, 'cbf', None, "test", None, params.distance, verbose = True, header_only = True)
  img = FormatCBFCspadInMemory(cbf)
  beam = beam_factory.simple(params.wavelength).get_s0()
  beam_center = (0,0)

  data = numpy.zeros((11840,194))

  if annulus:
    inner = params.distance * math.tan(2*math.sinh(params.wavelength/(2*params.annulus_inner)))
    outer = params.distance * math.tan(2*math.sinh(params.wavelength/(2*params.annulus_outer)))
    print "Inner (mm):", inner
    print "Outer (mm):", outer
  if params.resolution is not None:
    radius = params.distance * math.tan(2*math.sinh(params.wavelength/(2*params.resolution)))
    print "Radius (mm):", radius