|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from hexrd import constants as cnst |
| 4 | +from hexrd.rotations import discreteFiber |
| 5 | +from hexrd.transforms import xfcapi |
| 6 | + |
| 7 | + |
| 8 | +def _pick_to_fiber(pick_coords, eta_ome_maps, map_index, step=0.5, |
| 9 | + beam_vec=None, chi=0., as_expmap=True): |
| 10 | + """ |
| 11 | + Returns the orientations for the specified fiber parameters. |
| 12 | +
|
| 13 | + Parameters |
| 14 | + ---------- |
| 15 | + pick_coords : array_like |
| 16 | + The (2, ) list/vector containing the pick coordinates as (eta, omega) |
| 17 | + in DEGREES. This corresponds to the (column, row) or (x, y) dimensions |
| 18 | + on the map. |
| 19 | + eta_ome_maps : hexrd.xrdutil.utils.EtaOmeMaps |
| 20 | + The eta-omega maps. |
| 21 | + map_index : int |
| 22 | + The index of the current map. |
| 23 | + step : scalar, optional |
| 24 | + The step size along the fiber in DEGREES. The default is 0.5. |
| 25 | + chi : scalar, optional |
| 26 | + The chi angle from the associated instrument (specifically, |
| 27 | + `instr.chi`). The default is 0. |
| 28 | + beam_vec : array_like, optional |
| 29 | + The beam vector of the associated instrument (specifically, |
| 30 | + `instr.beam_vector`). The default is None (giving [0, 0, -1]). |
| 31 | + as_expmap : bool, optional |
| 32 | + Flag for converting the output from qauternions to exponential map. |
| 33 | + The default is True. |
| 34 | +
|
| 35 | + Returns |
| 36 | + ------- |
| 37 | + qfib : numpy.ndarray |
| 38 | + The array containing the fiber points as quaternions or exponential |
| 39 | + map parameters, according to the `as_expmap` kwarg. |
| 40 | +
|
| 41 | + """ |
| 42 | + pick_coords = np.atleast_1d(pick_coords).flatten() |
| 43 | + |
| 44 | + if beam_vec is None: |
| 45 | + beam_vec = cnst.beam_vec |
| 46 | + |
| 47 | + ndiv = int(np.round(360./float(step))) |
| 48 | + |
| 49 | + # grab the planeData instance from the maps |
| 50 | + # !!! this should have a copy of planeData that has hkls consistent with |
| 51 | + # the map data. |
| 52 | + pd = eta_ome_maps.planeData |
| 53 | + bmat = pd.latVecOps['B'] |
| 54 | + |
| 55 | + # the crystal direction (plane normal) |
| 56 | + crys_dir = pd.hkls[:, map_index].reshape(3, 1) |
| 57 | + |
| 58 | + # the sample direction |
| 59 | + tth = pd.getTTh()[map_index] # !!! in radians |
| 60 | + angs = np.atleast_2d(np.hstack([tth, np.radians(pick_coords)])) |
| 61 | + samp_dir = xfcapi.anglesToGVec( |
| 62 | + angs, bHat_l=beam_vec, chi=chi |
| 63 | + ).reshape(3, 1) |
| 64 | + |
| 65 | + # make the fiber |
| 66 | + qfib = discreteFiber( |
| 67 | + crys_dir, samp_dir, |
| 68 | + B=bmat, ndiv=ndiv, |
| 69 | + invert=False, |
| 70 | + csym=pd.getQSym(), ssym=None |
| 71 | + )[0] |
| 72 | + |
| 73 | + if as_expmap: |
| 74 | + phis = 2.*np.arccos(qfib[0, :]) |
| 75 | + ns = xfcapi.unitRowVector(qfib[1:, :].T) |
| 76 | + expmaps = phis*ns.T |
| 77 | + return expmaps.T # (3, ndiv) |
| 78 | + else: |
| 79 | + return qfib.T # (4, ndiv) |
| 80 | + |
| 81 | + |
| 82 | +def _angles_from_orientation(instr, eta_ome_maps, orientation): |
| 83 | + """ |
| 84 | + Return the (eta, omega) angles for a specified orientation consistent with |
| 85 | + input EtaOmeMaps. |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + instr : hexrd.instrument.HEDMInstrument |
| 90 | + The instrument instance used to generate the EtaOmeMaps. |
| 91 | + eta_ome_maps : hexrd.xrdutil.utils.EtaOmeMaps |
| 92 | + The eta-omega maps. |
| 93 | + orientation : array_like |
| 94 | + Either a (3, ) or (4, ) element vector specifying an orientation. |
| 95 | +
|
| 96 | + Raises |
| 97 | + ------ |
| 98 | + RuntimeError |
| 99 | + If orientation has more than 4 elements. |
| 100 | +
|
| 101 | + Returns |
| 102 | + ------- |
| 103 | + simulated_angles : list |
| 104 | + A list with length = len(eta_ome_maps.dataStore) containing the angular |
| 105 | + coordinates of all valid reflections for each map. If no valid points |
| 106 | + exist for a particular map, the entry contains `None`. Otherwise, |
| 107 | + the entry is a (2, p) array of the (eta, omega) coordinates in DEGREES |
| 108 | + for the p valid reflections. |
| 109 | +
|
| 110 | + """ |
| 111 | + plane_data = eta_ome_maps.planeData |
| 112 | + |
| 113 | + # angle ranges from maps |
| 114 | + eta_range = (eta_ome_maps.etaEdges[0], eta_ome_maps.etaEdges[-1]) |
| 115 | + ome_range = (eta_ome_maps.omeEdges[0], eta_ome_maps.omeEdges[-1]) |
| 116 | + ome_period = eta_ome_maps.omeEdges[0] + np.r_[0., 2*np.pi] |
| 117 | + |
| 118 | + # need the hklids |
| 119 | + hklids = [i['hklID'] for i in plane_data.hklDataList] |
| 120 | + |
| 121 | + expmap = np.atleast_1d(orientation).flatten() |
| 122 | + if len(expmap) == 4: |
| 123 | + # have a quat; convert here |
| 124 | + phi = 2.*np.arccos(expmap[0]) |
| 125 | + n = xfcapi.unitRowVector(expmap[1:]) |
| 126 | + expmap = phi*n |
| 127 | + elif len(expmap) > 4: |
| 128 | + raise RuntimeError( |
| 129 | + "orientation must be a single exponential map or quaternion" |
| 130 | + ) |
| 131 | + |
| 132 | + grain_param_list = [np.hstack([expmap, cnst.zeros_3, cnst.identity_6x1]), ] |
| 133 | + sim_dict = instr.simulate_rotation_series( |
| 134 | + plane_data, grain_param_list, |
| 135 | + eta_ranges=[eta_range, ], ome_ranges=[ome_range, ], |
| 136 | + ome_period=ome_period, wavelength=None |
| 137 | + ) |
| 138 | + |
| 139 | + rids = [] |
| 140 | + angs = [] |
| 141 | + for sim in sim_dict.values(): |
| 142 | + rids.append(sim[0][0]) |
| 143 | + angs.append(sim[2][0]) |
| 144 | + rids = np.hstack(rids) |
| 145 | + angs = np.vstack(angs) |
| 146 | + |
| 147 | + simulated_angles = [] |
| 148 | + for rid in hklids: |
| 149 | + this_idx = rids == rid |
| 150 | + if np.any(this_idx): |
| 151 | + simulated_angles.append( |
| 152 | + np.degrees(np.atleast_2d(angs[this_idx, 1:])) |
| 153 | + ) |
| 154 | + else: |
| 155 | + simulated_angles.append(None) |
| 156 | + |
| 157 | + return simulated_angles |
0 commit comments