{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "8ad4167a-e4e7-498d-909a-c04da9f177ed",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Binning with metadata generation, and storing into a NeXus file\n",
    "In this example, we show how to bin the same data used for example 3, but using the values for correction/calibration parameters generated in the example notebook 3, which are locally saved in the file sed_config.yaml. These data and the corresponding (machine and processing) metadata are then stored to a NeXus file following the NXmpes NeXus standard (https://fairmat-experimental.github.io/nexus-fairmat-proposal/9636feecb79bb32b828b1a9804269573256d7696/classes/contributed_definitions/NXmpes.html#nxmpes) using the 'dataconverter' of the pynxtools package (https://github.com/FAIRmat-NFDI/pynxtools)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb045e17-fa89-4c11-9d51-7f06e80d96d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sed\n",
    "from sed.dataset import dataset\n",
    "\n",
    "%matplotlib widget"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "42a6afaa-17dd-4637-ba75-a28c4ead1adf",
   "metadata": {},
   "source": [
    "## Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "34f46d54",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset.get(\"WSe2\") # Put in Path to a storage of at least 20 GByte free space.\n",
    "data_path = dataset.dir # This is the path to the data\n",
    "scandir, _ = dataset.subdirs # scandir contains the data, _ contains the calibration files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23effa13",
   "metadata": {},
   "outputs": [],
   "source": [
    "metadata = {}\n",
    "# manual Meta data. These should ideally come from an Electronic Lab Notebook.\n",
    "#General\n",
    "metadata['experiment_summary'] = 'WSe2 XUV NIR pump probe data.'\n",
    "metadata['entry_title'] = 'Valence Band Dynamics - 800 nm linear s-polarized pump, 0.6 mJ/cm2 absorbed fluence'\n",
    "metadata['experiment_title'] = 'Valence band dynamics of 2H-WSe2'\n",
    "\n",
    "#User\n",
    "# Fill general parameters of NXuser\n",
    "# TODO: discuss how to deal with multiple users?\n",
    "metadata['user0'] = {}\n",
    "metadata['user0']['name'] = 'Julian Maklar'\n",
    "metadata['user0']['role'] = 'Principal Investigator'\n",
    "metadata['user0']['affiliation'] = 'Fritz Haber Institute of the Max Planck Society'\n",
    "metadata['user0']['address'] = 'Faradayweg 4-6, 14195 Berlin'\n",
    "metadata['user0']['email'] = 'maklar@fhi-berlin.mpg.de'\n",
    "\n",
    "#NXinstrument\n",
    "metadata['instrument'] = {}\n",
    "metadata['instrument']['energy_resolution'] = 140.\n",
    "#analyzer\n",
    "metadata['instrument']['analyzer']={}\n",
    "metadata['instrument']['analyzer']['slow_axes'] = \"delay\" # the scanned axes\n",
    "metadata['instrument']['analyzer']['spatial_resolution'] = 10.\n",
    "metadata['instrument']['analyzer']['energy_resolution'] = 110.\n",
    "metadata['instrument']['analyzer']['momentum_resolution'] = 0.08\n",
    "metadata['instrument']['analyzer']['working_distance'] = 4.\n",
    "metadata['instrument']['analyzer']['lens_mode'] = \"6kV_kmodem4.0_30VTOF.sav\"\n",
    "\n",
    "#probe beam\n",
    "metadata['instrument']['beam']={}\n",
    "metadata['instrument']['beam']['probe']={}\n",
    "metadata['instrument']['beam']['probe']['incident_energy'] = 21.7\n",
    "metadata['instrument']['beam']['probe']['incident_energy_spread'] = 0.11\n",
    "metadata['instrument']['beam']['probe']['pulse_duration'] = 20.\n",
    "metadata['instrument']['beam']['probe']['frequency'] = 500.\n",
    "metadata['instrument']['beam']['probe']['incident_polarization'] = [1, 1, 0, 0] # p pol Stokes vector\n",
    "metadata['instrument']['beam']['probe']['extent'] = [80., 80.]\n",
    "#pump beam\n",
    "metadata['instrument']['beam']['pump']={}\n",
    "metadata['instrument']['beam']['pump']['incident_energy'] = 1.55\n",
    "metadata['instrument']['beam']['pump']['incident_energy_spread'] = 0.08\n",
    "metadata['instrument']['beam']['pump']['pulse_duration'] = 35.\n",
    "metadata['instrument']['beam']['pump']['frequency'] = 500.\n",
    "metadata['instrument']['beam']['pump']['incident_polarization'] = [1, -1, 0, 0] # s pol Stokes vector\n",
    "metadata['instrument']['beam']['pump']['incident_wavelength'] = 800.\n",
    "metadata['instrument']['beam']['pump']['average_power'] = 300.\n",
    "metadata['instrument']['beam']['pump']['pulse_energy'] = metadata['instrument']['beam']['pump']['average_power']/metadata['instrument']['beam']['pump']['frequency']#µJ\n",
    "metadata['instrument']['beam']['pump']['extent'] = [230., 265.]\n",
    "metadata['instrument']['beam']['pump']['fluence'] = 0.15\n",
    "\n",
    "#sample\n",
    "metadata['sample']={}\n",
    "metadata['sample']['preparation_date'] = '2019-01-13T10:00:00+00:00'\n",
    "metadata['sample']['preparation_description'] = 'Cleaved'\n",
    "metadata['sample']['sample_history'] = 'Cleaved'\n",
    "metadata['sample']['chemical_formula'] = 'WSe2'\n",
    "metadata['sample']['description'] = 'Sample'\n",
    "metadata['sample']['name'] = 'WSe2 Single Crystal'\n",
    "\n",
    "metadata['file'] = {}\n",
    "metadata['file'][\"trARPES:Carving:TEMP_RBV\"] = 300.\n",
    "metadata['file'][\"trARPES:XGS600:PressureAC:P_RD\"] = 5.e-11\n",
    "metadata['file'][\"KTOF:Lens:Extr:I\"] = -0.12877\n",
    "metadata['file'][\"KTOF:Lens:UDLD:V\"] = 399.99905\n",
    "metadata['file'][\"KTOF:Lens:Sample:V\"] = 17.19976\n",
    "metadata['file'][\"KTOF:Apertures:m1.RBV\"] = 3.729931\n",
    "metadata['file'][\"KTOF:Apertures:m2.RBV\"] = -5.200078\n",
    "metadata['file'][\"KTOF:Apertures:m3.RBV\"] = -11.000425\n",
    "\n",
    "# Sample motor positions\n",
    "metadata['file']['trARPES:Carving:TRX.RBV'] = 7.1900000000000004\n",
    "metadata['file']['trARPES:Carving:TRY.RBV'] = -6.1700200225439552\n",
    "metadata['file']['trARPES:Carving:TRZ.RBV'] = 33.4501953125\n",
    "metadata['file']['trARPES:Carving:THT.RBV'] = 423.30500940561586\n",
    "metadata['file']['trARPES:Carving:PHI.RBV'] = 0.99931647456264949\n",
    "metadata['file']['trARPES:Carving:OMG.RBV'] = 11.002500171914066"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1f82054",
   "metadata": {},
   "outputs": [],
   "source": [
    "# create sed processor using the config file, and collect the meta data from the files:\n",
    "sp = sed.SedProcessor(folder=scandir, config=\"../sed/config/mpes_example_config.yaml\", metadata=metadata, collect_metadata=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85ac3c83",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply jittering to X, Y, t, ADC columns.\n",
    "sp.add_jitter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17586169",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate machine-coordinate data for pose adjustment\n",
    "sp.bin_and_load_momentum_calibration(df_partitions=10, plane=33, width=10, apply=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b99629a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Adjust pose alignment, using stored distortion correction\n",
    "sp.pose_adjustment(xtrans=8, ytrans=7, angle=-4, apply=True, use_correction=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "845f002d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply stored momentum correction\n",
    "sp.apply_momentum_correction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9ae5066",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply stored config momentum calibration\n",
    "sp.apply_momentum_calibration()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb1e2bee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply stored config energy correction\n",
    "sp.apply_energy_correction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c470ffd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply stored config energy calibration\n",
    "sp.append_energy_axis()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0943d349",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply delay calibration\n",
    "delay_range = (-500, 1500)\n",
    "sp.calibrate_delay_axis(delay_range=delay_range, preview=True)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6902fd56-1456-4da6-83a4-0f3f6b831eb6",
   "metadata": {},
   "source": [
    "## Compute final data volume"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7601cd7-cd51-40a9-8fc7-8b7d32ff15d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "axes = ['kx', 'ky', 'energy', 'delay']\n",
    "bins = [100, 100, 200, 50]\n",
    "ranges = [[-2, 2], [-2, 2], [-4, 2], [-600, 1600]]\n",
    "res = sp.compute(bins=bins, axes=axes, ranges=ranges)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44fece87",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save to NXmpes NeXus (including standardized metadata)\n",
    "sp.save(data_path + \"/binned.nxs\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3073b9ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Visualization (requires JupyterLab)\n",
    "from jupyterlab_h5web import H5Web\n",
    "H5Web(data_path + \"/binned.nxs\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee543128",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "728003ee06929e5fa5ff815d1b96bf487266025e4b7440930c6bf4536d02d243"
  },
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}