diff --git a/s2e/prop/exfel_spb_day1_beamline.py b/s2e/prop/exfel_spb_day1_beamline.py new file mode 100644 index 0000000..169b6a1 --- /dev/null +++ b/s2e/prop/exfel_spb_day1_beamline.py @@ -0,0 +1,99 @@ +# Module holding the SPB/SFX beamline (nanofocus, KB mirrors) at European XFEL. + +import wpg.optical_elements +from wpg.optical_elements import Use_PP +from opd import defineOPD +import numpy as np +import os + + +def get_beamline(): + """ Setup and return the WPG.Beamline object representing the SPB/SFX nanofocus beamline (KB mirrors). + + :return: beamline (type wpg.Beamline) + """ + + ### Geometry ### + src_to_hom1 = 257.8 # Distance source to HOM 1 [m] + src_to_hom2 = 267.8 # Distance source to HOM 2 [m] + src_to_crl = 887.8 # Distance source to CRL [m] + src_to_exp = 920.42 # Distance source to experiment [m] + + #Incidence angle at HOM + theta_om = 3.6e-3 # [rad] + + om_mirror_length = 0.8 # [m] + om_clear_ap = om_mirror_length*theta_om + + + #define the beamline: + beamline = Beamline() + zoom=1 + + # Define HOM1 = Aperture + Wavefront distortion. + aperture_x_to_y_ratio = 1 + hom1_aperture = Aperture(shape='r',ap_or_ob='a',Dx=om_clear_ap,Dy=om_clear_ap/aperture_x_to_y_ratio) + + # Append to beamline. + beamline.append( hom1_aperture, Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom) ) + + # Free space propagation from hom1 to hom2 + hom1_to_hom2_drift = Drift(src_to_hom2 - src_to_hom1) + beamline.append( hom1_to_hom2_drift, Use_PP(semi_analytical_treatment=0)) + + # Define HOM2. + zoom = 1.0 + hom2_aperture = Aperture('r','a', om_clear_ap, om_clear_ap/aperture_x_to_y_ratio) + beamline.append( hom2_aperture, Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom)) + + #drift to CRL aperture + hom2_to_crl_drift = Drift( src_to_crl - src_to_hom2 ) + beamline.append( hom2_to_crl_drift, Use_PP(semi_analytical_treatment=1)) + + # Circular Aperture before CRL. + crl_front_aperture_diameter = 2.8e-3 + crl_front_aperture = Aperture('c','a', crl_front_aperture_diameter, crl_front_aperture_diameter) + + ### Define CRL + crl_focussing_plane = 3 # Both horizontal and vertical. + crl_delta = 4.8308e-06 # Refractive index decrement (n = 1- delta - i*beta) @ 8.4 keV + crl_attenuation_length = 6.053e-3 # Attenuation length [m], Henke data. + crl_shape = 1 # Parabolic lenses + crl_aperture = 3.0e-3 # [m] + crl_curvature_radius = 5.8e-3 # [m] + crl_number_of_lenses = 19 + crl_wall_thickness = 8.0e-5 # Thickness + crl_center_horizontal_coordinate = 0.0 + crl_center_vertical_coordinate = 0.0 + crl_initial_photon_energy = 8.48e3 # [eV] + crl_final_photon_energy = 8.52e3 # [eV] + + crl = CRL( _foc_plane=crl_focussing_plane, + _delta=crl_delta, + _atten_len=crl_attenuation_length, + _shape=crl_shape, + _apert_h=crl_aperture, + _apert_v=crl_aperture, + _r_min=crl_curvature_radius, + _n=crl_number_of_lenses, + _wall_thick=crl_wall_thickness, + _xc=crl_center_horizontal_coordinate, + _yc=crl_center_vertical_coordinate, + _void_cen_rad=None, + _e_start=crl_initial_photon_energy, + _e_fin=crl_final_photon_energy, + ) + + zoom = 0.6 + beamline.append( crl_front_aperture, Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom/0.1) ) + beamline.append( crl, Use_PP(semi_analytical_treatment=0, zoom=1, sampling=1) ) + + # Drift to focus aperture + crl_to_exp_drift = Drift( src_to_exp - src_to_crl ) + beamline.append( crl_to_exp_drift, Use_PP(semi_analytical_treatment=1, zoom=1, sampling=1) ) + + return beamline + + + + diff --git a/s2e/prop/exfel_spb_kb_beamline.py b/s2e/prop/exfel_spb_kb_beamline.py new file mode 100644 index 0000000..cd84472 --- /dev/null +++ b/s2e/prop/exfel_spb_kb_beamline.py @@ -0,0 +1,99 @@ +# Module holding WPG beamlines at European XFEL. + +import wpg.optical_elements +from wpg.optical_elements import Use_PP +from opd import defineOPD +import numpy as np +import os + + +def get_beamline(): + """ Setup and return the WPG.Beamline object representing the SPB/SFX nanofocus beamline (KB mirrors). + + :return: beamline (type wpg.Beamline) + """ + + # Distances + distance0 = 246.5 + distance1 = 683.5 + distance = distance0 + distance1 + + # Focal lengths. + f_hfm = 3.0 # nominal focal length for HFM KB + f_vfm = 1.9 # nominal focal length for VFM KB + distance_hfm_vfm = f_hfm - f_vfm + distance_foc = 1. /(1./f_vfm + 1. / (distance + distance_hfm_vfm)) + + # Mirror incidence angles + theta_om = 3.5e-3 # offset mirrors incidence angle + theta_kb = 3.5e-3 # KB mirrors incidence angle + + # Mirror lengths + om_mirror_length = 0.8; om_clear_ap = om_mirror_length*theta_om + kb_mirror_length = 0.9; kb_clear_ap = kb_mirror_length*theta_kb + + # Drifts. + drift0 = wpg.optical_elements.Drift(distance0) + drift1 = wpg.optical_elements.Drift(distance1) + drift_in_kb = wpg.optical_elements.Drift(distance_hfm_vfm) + drift_to_foc = wpg.optical_elements.Drift(distance_foc) + + # Mirror apertures. + ap0 = wpg.optical_elements.Aperture('r','a', 120.e-6, 120.e-6) + ap1 = wpg.optical_elements.Aperture('r','a', om_clear_ap, 2*om_clear_ap) + ap_kb = wpg.optical_elements.Aperture('r','a', kb_clear_ap, kb_clear_ap) + + # Mirror definitions. + hfm = wpg.optical_elements.Mirror_elliptical( + orient='x', + p=distance, + q=(distance_hfm_vfm+distance_foc), + thetaE=theta_kb, + theta0=theta_kb, + length=0.9 + ) + vfm = wpg.optical_elements.Mirror_elliptical( + orient='y', + p=(distance+distance_hfm_vfm), + q=distance_foc, + thetaE=theta_kb, + theta0=theta_kb, + length=0.9 + ) + + + # Wavefront distortions due to mirror profile + wf_dist_om = wpg.optical_elements.WF_dist(1500, 100, om_clear_ap, 2*om_clear_ap) + defineOPD(wf_dist_om, os.path.join('data_common','mirror2.dat'), 2, '\t', 'x', + theta_kb, scale=2) + + wf_dist_hfm = wpg.optical_elements.WF_dist(1500, 100, kb_clear_ap, kb_clear_ap) + defineOPD(wf_dist_hfm, os.path.join(mirror_data_dir,'mirror1.dat'), 2, '\t', 'x', + theta_kb, scale=2, stretching=kb_mirror_length/0.8) + + wf_dist_vfm = wpg.optical_elements.WF_dist(1100, 1500, kb_clear_ap, kb_clear_ap) + defineOPD(wf_dist_vfm, os.path.join(mirror_data_dir,'mirror2.dat'), 2, ' ', 'y', + theta_kb, scale=2, stretching=kb_mirror_length/0.8) + + + # Assemble the beamline with PP parameters. + bl0 = wpg.Beamline() + bl0.append(ap0, Use_PP(semi_analytical_treatment=0, + zoom=14.4, + sampling=1/1.6)) + bl0.append(drift0,Use_PP(semi_analytical_treatment=0)) + bl0.append(ap1, Use_PP(zoom=0.8)) + #bl0.append(ap1, Use_PP(zoom=1.6, sampling=1/1.5)) + bl0.append(wf_dist_om, Use_PP()) + bl0.append(drift1, Use_PP(semi_analytical_treatment=1)) + bl0.append(ap_kb, Use_PP(zoom = 6.4, sampling = 1/16.)) + #bl0.append(ap_kb, Use_PP(zoom=5.4, sampling=1/6.4)) + bl0.append(hfm, Use_PP()) + bl0.append(wf_dist_hfm, Use_PP()) + bl0.append(drift_in_kb, Use_PP(semi_analytical_treatment=1)) + bl0.append(vfm, Use_PP()) + bl0.append(wf_dist_vfm, Use_PP()) + bl0.append(drift_to_foc, Use_PP(semi_analytical_treatment=1)) + + # All done, return. + return bl0 diff --git a/s2e/prop/my_s2e_beamline.py b/s2e/prop/my_s2e_beamline.py deleted file mode 100644 index e27257c..0000000 --- a/s2e/prop/my_s2e_beamline.py +++ /dev/null @@ -1,110 +0,0 @@ -import numpy as np -import os -from wpg import Wavefront -import wpg.optical_elements -from wpg.optical_elements import Use_PP - -#mirror_data_dir = '/data/S2E/modules/prop/data_common' -mirror_data_dir = 'data_common' - -def defineOPD(opTrErMirr, mdatafile, ncol, delim, orient, theta, scale=1., stretching=1.): - """ - Define optical path difference (OPD) from mirror profile - - :param opTrErMirr: the struct with wave front distortions from mirror susrface errors - :param mdatafile: an ascii file with mirror profile data - :param ncol: number of columns in the mirror profile file - :param delim: delimiter between numbers in an row, can be space (' '), tab '\t', etc - :param orient: mirror orientation, 'x' (horizontal) or 'y' (vertical) - :param theta: mirror incidence angle - :param scale: scaling factor for the mirror profile height errors - :param stretching: scaling factor for the mirror profile x-axis (a hack, should be removed ASAP) - """ - - heightProfData = np.loadtxt(mdatafile).T - heightProfData[0,:] = heightProfData[0,:] * stretching - wpg.useful_code.srwutils.AuxTransmAddSurfHeightProfileScaled(opTrErMirr, heightProfData, orient, theta, scale) - # if isIpynb: - # pylab.figure(); pylab.plot(heightProfData[0],heightProfData[ncol-1]*1e9) - # pylab.title('profile from %s' %mdatafile);pylab.xlabel('x (m)');pylab.ylabel('h (nm)') - -def get_beamline(): - """ - This function will called in propagation script. - - :return: Beamline. - """ - - distance0 = 246.5 - distance1 = 683.5 - distance = distance0 + distance1 - f_hfm = 3.0 # nominal focal length for HFM KB - f_vfm = 1.9 # nominal focal length for VFM KB - distance_hfm_vfm = f_hfm - f_vfm - distance_foc = 1. /(1./f_vfm + 1. / (distance + distance_hfm_vfm)) - theta_om = 3.5e-3 # offset mirrors incidence angle - theta_kb = 3.5e-3 # KB mirrors incidence angle - - drift0 = wpg.optical_elements.Drift(distance0) - drift1 = wpg.optical_elements.Drift(distance1) - drift_in_kb = wpg.optical_elements.Drift(distance_hfm_vfm) - drift_to_foc = wpg.optical_elements.Drift(distance_foc) - - om_mirror_length = 0.8; om_clear_ap = om_mirror_length*theta_om - kb_mirror_length = 0.9; kb_clear_ap = kb_mirror_length*theta_kb - ap0 = wpg.optical_elements.Aperture('r','a', 120.e-6, 120.e-6) - ap1 = wpg.optical_elements.Aperture('r','a', om_clear_ap, 2*om_clear_ap) - ap_kb = wpg.optical_elements.Aperture('r','a', kb_clear_ap, kb_clear_ap) - hfm = wpg.optical_elements.Mirror_elliptical( - _p=distance, _q=(distance_hfm_vfm+distance_foc), _ang_graz=theta_kb, - _r_sag=1.e+40, _size_tang=0.9, _nvx=np.cos(theta_kb), _nvy=0, - _nvz=-np.sin(theta_kb), _tvx=-np.sin(theta_kb), _tvy=0, _x=0, _y=0, - _treat_in_out=1) - vfm = wpg.optical_elements.Mirror_elliptical( - _p=(distance+distance_hfm_vfm), _q=distance_foc, _ang_graz=theta_kb, - _r_sag=1.e+40, _size_tang=0.9, _nvx=0, _nvy=np.cos(theta_kb), - _nvz=-np.sin(theta_kb), _tvx=0, _tvy=-np.sin(theta_kb), _x=0, _y=0, - _treat_in_out=1) - wf_dist_om = wpg.optical_elements.WF_dist(1500, 100, om_clear_ap, 2*om_clear_ap) - defineOPD(wf_dist_om, os.path.join(mirror_data_dir,'mirror2.dat'), 2, '\t', 'x', - theta_kb, scale=2) - # if isIpynb: - # meshT = wf_dist_om.mesh - # opdTmp=np.array(wf_dist_om.arTr)[1::2].reshape(meshT.ny,meshT.nx) - # figure(); pylab.imshow(opdTmp,extent=[meshT.xStart,meshT.xFin,meshT.yStart,meshT.yFin]) - # pylab.title('OPD [m]');pylab.xlabel('x (m)'); pylab.ylabel('y (m)') - - wf_dist_hfm = wpg.optical_elements.WF_dist(1500, 100, kb_clear_ap, kb_clear_ap) - defineOPD(wf_dist_hfm, os.path.join(mirror_data_dir,'mirror1.dat'), 2, '\t', 'x', - theta_kb, scale=2, stretching=kb_mirror_length/0.8) - # if isIpynb: - # meshT = wf_dist_hfm.mesh - # opdTmp=np.array(wf_dist_hfm.arTr)[1::2].reshape(meshT.ny,meshT.nx) - # figure(); pylab.imshow(opdTmp,extent=[meshT.xStart,meshT.xFin,meshT.yStart,meshT.yFin]) - # pylab.title('OPD [m]');pylab.xlabel('x (m)'); pylab.ylabel('y (m)') - - wf_dist_vfm = wpg.optical_elements.WF_dist(1100, 1500, kb_clear_ap, kb_clear_ap) - defineOPD(wf_dist_vfm, os.path.join(mirror_data_dir,'mirror2.dat'), 2, ' ', 'y', - theta_kb, scale=2, stretching=kb_mirror_length/0.8) - - # if isIpynb: - # meshT = wf_dist_vfm.mesh - # opdTmp=np.array(wf_dist_vfm.arTr)[1::2].reshape(meshT.ny,meshT.nx) - # figure(); pylab.imshow(opdTmp,extent=[meshT.xStart,meshT.xFin,meshT.yStart,meshT.yFin]) - # pylab.title('OPD [m]');pylab.xlabel('x (m)'); pylab.ylabel('y (m)') - - bl0 = wpg.Beamline() - bl0.append(ap0, Use_PP(semi_analytical_treatment=0, zoom=14.4, sampling=1/1.6)) - bl0.append(drift0,Use_PP(semi_analytical_treatment=0)) - bl0.append(ap1, Use_PP(zoom=0.8)) #bl0.append(ap1, Use_PP(zoom=1.6, sampling=1/1.5)) - bl0.append(wf_dist_om, Use_PP()) - bl0.append(drift1, Use_PP(semi_analytical_treatment=1)) - bl0.append(ap_kb, Use_PP(zoom = 6.4, sampling = 1/16.))#bl0.append(ap_kb, Use_PP(zoom=5.4, sampling=1/6.4)) - bl0.append(hfm, Use_PP()) - bl0.append(wf_dist_hfm, Use_PP()) - bl0.append(drift_in_kb, Use_PP(semi_analytical_treatment=1)) - bl0.append(vfm, Use_PP()) - bl0.append(wf_dist_vfm, Use_PP()) - bl0.append(drift_to_foc, Use_PP(semi_analytical_treatment=1)) - - return bl0 diff --git a/s2e/prop/my_s2e_beamline.py b/s2e/prop/my_s2e_beamline.py new file mode 120000 index 0000000..7dcb734 --- /dev/null +++ b/s2e/prop/my_s2e_beamline.py @@ -0,0 +1 @@ +exfel_spb_kb_beamline.py \ No newline at end of file diff --git a/s2e/prop/opd.py b/s2e/prop/opd.py new file mode 100644 index 0000000..d478ec2 --- /dev/null +++ b/s2e/prop/opd.py @@ -0,0 +1,29 @@ +from wpg.useful_code.srwutils import AuxTransmAddSurfHeightProfileScaled as profile_tool +import numpy as np + + +def defineOPD(opTrErMirr, + mdatafile, + ncol, + delim, + orient, + theta, + scale=1., + stretching=1.): + """ + Define optical path difference (OPD) from mirror profile + + :param opTrErMirr: The struct with wave front distortions from + mirror susrface errors + :param mdatafile: An ascii file with mirror profile data + :param ncol: Number of columns in the mirror profile file + :param delim: Delimiter between numbers in an row, can be space (' '), tab '\t', etc. + :param orient: Mirror orientation, 'x' (horizontal) or 'y' (vertical) + :param theta: Mirror incidence angle + :param scale: Scaling factor for the mirror profile height errors + :param stretching: Scaling factor for the mirror profile x-axis (a hack, should be removed ASAP) + """ + + heightProfData = np.loadtxt(mdatafile).T + heightProfData[0, :] = heightProfData[0, :] * stretching + profile_tool(opTrErMirr, heightProfData, orient, theta, scale) diff --git a/s2e/prop/propagateSE.py b/s2e/prop/propagateSE.py index 87269f9..c354893 100644 --- a/s2e/prop/propagateSE.py +++ b/s2e/prop/propagateSE.py @@ -4,7 +4,7 @@ # In[ ]: isS2E = True # True if running at S2E server -isIpynb = False # True if ipython notebook, in python script should be false +isIpynb = False # True if ipython notebook, in python script should be false # In[ ]: @@ -33,6 +33,8 @@ import wpg.optical_elements from wpg.optical_elements import Use_PP from wpg.useful_code import srwutils + +from opd import defineOPD #from wpg.srwlib import srwl,SRWLOptD,SRWLOptA,SRWLOptC,SRWLOptT,SRWLOptL, SRWLOpt @@ -41,7 +43,7 @@ def mkdir_p(path): """ Create directory tree, if not exists (mkdir -p) - + :param path: Path to be created """ try: @@ -55,33 +57,13 @@ def mkdir_p(path): # In[ ]: -def defineOPD(opTrErMirr, mdatafile, ncol, delim, orient, theta, scale=1., stretching=1.): - """ - Define optical path difference (OPD) from mirror profile - - :param opTrErMirr: the struct with wave front distortions from mirror susrface errors - :param mdatafile: an ascii file with mirror profile data - :param ncol: number of columns in the mirror profile file - :param delim: delimiter between numbers in an row, can be space (' '), tab '\t', etc - :param orient: mirror orientation, 'x' (horizontal) or 'y' (vertical) - :param theta: mirror incidence angle - :param scale: scaling factor for the mirror profile height errors - :param stretching: scaling factor for the mirror profile x-axis (a hack, should be removed ASAP) - """ - heightProfData = np.loadtxt(mdatafile).T - heightProfData[0,:] = heightProfData[0,:] * stretching - wpg.useful_code.srwutils.AuxTransmAddSurfHeightProfileScaled(opTrErMirr, heightProfData, orient, theta, scale) - if isIpynb: - pylab.figure(); pylab.plot(heightProfData[0],heightProfData[ncol-1]*1e9) - pylab.title('profile from %s' %mdatafile);pylab.xlabel('x (m)');pylab.ylabel('h (nm)') - # In[ ]: def add_history(wf_file_name, history_file_name): """ Add history from pearent file to propagated file - + :param wf_file_name: output file :param history_file_name: peraent file """ @@ -89,17 +71,17 @@ def add_history(wf_file_name, history_file_name): with h5py.File(history_file_name) as history_h5: if 'history' in wf_h5: del wf_h5['history'] - + wf_h5.create_group('/history/parent/') wf_h5.create_group('/history/parent/detail') - + for k in history_h5: if k=='history': try: history_h5.copy('/history/parent', wf_h5['history']['parent']) except KeyError: pass - + elif not k == 'data': history_h5.copy(k,wf_h5['history']['parent']['detail']) else: @@ -111,7 +93,7 @@ def add_history(wf_file_name, history_file_name): def calculate_fwhm(wfr): """ Calculate FWHM of the beam calculating number of point bigger then max/2 throuhgt center of the image - + :param wfr: wavefront :return: {'fwhm_x':fwhm_x, 'fwhm_y': fwhm_y} in [m] """ @@ -157,7 +139,7 @@ def get_intensity_on_axis(wfr): def propagate(in_fname, out_fname): """ Propagate wavefront - + :param in_file: input wavefront file :param out_file: output file """ @@ -171,14 +153,14 @@ def propagate(in_fname, out_fname): f_vfm = 1.9 # nominal focal length for VFM KB distance_hfm_vfm = f_hfm - f_vfm distance_foc = 1. /(1./f_vfm + 1. / (distance + distance_hfm_vfm)) - theta_om = 3.5e-3 # offset mirrors incidence angle - theta_kb = 3.5e-3 # KB mirrors incidence angle - + theta_om = 3.5e-3 # offset mirrors incidence angle + theta_kb = 3.5e-3 # KB mirrors incidence angle + drift0 = wpg.optical_elements.Drift(distance0) drift1 = wpg.optical_elements.Drift(distance1) drift_in_kb = wpg.optical_elements.Drift(distance_hfm_vfm) drift_to_foc = wpg.optical_elements.Drift(distance_foc) - + om_mirror_length = 0.8; om_clear_ap = om_mirror_length*theta_om kb_mirror_length = 0.9; kb_clear_ap = kb_mirror_length*theta_kb ap0 = wpg.optical_elements.Aperture('r','a', 120.e-6, 120.e-6) @@ -198,7 +180,7 @@ def propagate(in_fname, out_fname): opdTmp=np.array(wf_dist_om.arTr)[1::2].reshape(meshT.ny,meshT.nx) figure(); pylab.imshow(opdTmp,extent=[meshT.xStart,meshT.xFin,meshT.yStart,meshT.yFin]) pylab.title('OPD [m]');pylab.xlabel('x (m)'); pylab.ylabel('y (m)') - + wf_dist_hfm = wpg.optical_elements.WF_dist(1500, 100, kb_clear_ap, kb_clear_ap) defineOPD(wf_dist_hfm, os.path.join(mirror_data_dir,'mirror1.dat'), 2, '\t', 'x', theta_kb, scale=2, stretching=kb_mirror_length/0.8) @@ -206,18 +188,18 @@ def propagate(in_fname, out_fname): meshT = wf_dist_hfm.mesh opdTmp=np.array(wf_dist_hfm.arTr)[1::2].reshape(meshT.ny,meshT.nx) figure(); pylab.imshow(opdTmp,extent=[meshT.xStart,meshT.xFin,meshT.yStart,meshT.yFin]) - pylab.title('OPD [m]');pylab.xlabel('x (m)'); pylab.ylabel('y (m)') - + pylab.title('OPD [m]');pylab.xlabel('x (m)'); pylab.ylabel('y (m)') + wf_dist_vfm = wpg.optical_elements.WF_dist(1100, 1500, kb_clear_ap, kb_clear_ap) defineOPD(wf_dist_vfm, os.path.join(mirror_data_dir,'mirror2.dat'), 2, ' ', 'y', theta_kb, scale=2, stretching=kb_mirror_length/0.8) - + if isIpynb: meshT = wf_dist_vfm.mesh opdTmp=np.array(wf_dist_vfm.arTr)[1::2].reshape(meshT.ny,meshT.nx) figure(); pylab.imshow(opdTmp,extent=[meshT.xStart,meshT.xFin,meshT.yStart,meshT.yFin]) pylab.title('OPD [m]');pylab.xlabel('x (m)'); pylab.ylabel('y (m)') - + bl0 = wpg.Beamline() bl0.append(ap0, Use_PP(semi_analytical_treatment=0, zoom=14.4, sampling=1/1.6)) bl0.append(drift0,Use_PP(semi_analytical_treatment=0)) @@ -234,35 +216,35 @@ def propagate(in_fname, out_fname): if isIpynb: print bl0 - + wpg.srwlib.srwl.SetRepresElecField(wf._srwl_wf, 'f') - + sz0 = get_intensity_on_axis(wf); wf.custom_fields['/misc/spectrum0'] = sz0 - + bl0.propagate(wf) - + sz1 = get_intensity_on_axis(wf); wf.custom_fields['/misc/spectrum1'] = sz1 - + wpg.srwlib.srwl.SetRepresElecField(wf._srwl_wf, 't') - + #Resizing: decreasing Range of Horizontal and Vertical Position: wpg.srwlib.srwl.ResizeElecField(wf._srwl_wf, 'c', [0, 0.25, 1, 0.25, 1]); - + fwhm = calculate_fwhm(wf) - + wf.custom_fields['/misc/xFWHM'] = fwhm['fwhm_x'] wf.custom_fields['/misc/yFWHM'] = fwhm['fwhm_y'] wf.custom_fields['/params/beamline/printout'] = str(bl0) - + wf.custom_fields['/info/contact'] = [ 'Name: Liubov Samoylova', 'Email: liubov.samoylova@xfel.eu', 'Name: Alexey Buzmakov', 'Email: buzmakov@gmail.com'] wf.custom_fields['/info/data_description'] = 'This dataset contains infromation about wavefront propagated through beamline (WPG and SRW frameworks).' wf.custom_fields['/info/method_description'] = """WPG, WaveProperGator (http://github.com/samoylv/WPG)is an interactive simulation framework for coherent X-ray wavefront propagation.\nSRW, Synchrotron Radiation Workshop (http://github.com/ochubar/SRW), is a physical optics computer code for simulation of the radiation wavefront propagation through optical systems of beamlines as well as detailed characteristics of Synchrotron Radiation (SR) generated by relativistic electrons in magnetic fields of arbitrary configuration.""" wf.custom_fields['/info/package_version'] = '2014.1' - + print('Saving the wavefront data after propagation:' + out_fname) mkdir_p(os.path.dirname(out_fname)) wf.store_hdf5(out_fname) @@ -285,11 +267,11 @@ def propagate_wrapper(params): def directory_process(in_dname, out_dname, cpu_number): """ Process directory with in_dname\FELsource_out*.h5 files and store it after propagation in out_dname\prop_out*.h5 files - + :param in_dname: input directory name :param out_dname: ouput directory name :param cpu_number: NUmber of CPUs for parallel computing - + """ input_dir = in_dname input_files = glob(os.path.join(input_dir, 'FELsource_out*.h5')) @@ -299,11 +281,11 @@ def directory_process(in_dname, out_dname, cpu_number): out_file_name = in_file_name.replace('FELsource_out','prop_out') print 'out_file_name:',out_file_name out_files.append(os.path.join(out_dname, out_file_name)) - + print 'Found {} HDF5 files in {}'.format(len(input_files), in_dname) - + batch_params = zip(input_files, out_files) - + p=multiprocessing.Pool(processes=cpu_number) # map(propagate_wrapper, batch_params) p.map(propagate_wrapper, batch_params, chunksize=1) @@ -318,30 +300,30 @@ def main(): parser = OptionParser() parser.add_option("--input-file", dest="in_fname", help="Input wavefront file") parser.add_option("--output-file", dest="out_fname", help="Output wavefront file") - + parser.add_option("--input-directory", dest="in_dname", help="Input directory with wavefront files") parser.add_option("--output-directory", dest="out_dname", help="Output directory with wavefront files") parser.add_option("-n", "--cpu-number", dest="cpu_number", default=int((multiprocessing.cpu_count()+1)/2), help="Number of cores for batch wavefronts propagation, default value NUMBER_OF_CPU/2") (options, args) = parser.parse_args() - - + + if not (options.in_fname or options.in_dname): # if filename is not given parser.error('Input filename or directiry not specified, use --input-file or --input-directory options') - return - + return + if not (options.out_fname or options.out_dname): # if filename is not given parser.error('Output filename or directiry not specified, use --output-file or --output-directory options') return - + if options.in_dname and options.out_dname: print 'Input directory {}, output directory {}, number of cores {}'.format( options.in_dname, options.out_dname, options.cpu_number) print 'Batch propagation started' directory_process(options.in_dname, options.out_dname, int(options.cpu_number)) print 'Batch propagation finished' - + elif options.in_fname and options.out_fname: print 'Input file {}, output file {}'.format(options.in_fname, options.out_fname) propagate(options.in_fname, options.out_fname)