diff --git a/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb b/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb
index af08e704474fc49dfb65baf0cd8c63fa020ce6af..fa42a7ac209591b2a4f1adceab87f92e58c35ddc 100644
--- a/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb
+++ b/notebooks/AGIPD/Chracterize_AGIPD_Gain_PC_NBC.ipynb
@@ -38,10 +38,10 @@
    "source": [
     "in_folder = '/gpfs/exfel/exp/MID/201931/p900091/raw' # path to input data, required\n",
     "modules = [1,] # modules to work on, required, range allowed\n",
-    "out_folder = \"/gpfs/exfel/exp/SPB/201931/p900091/usr/PC/\" # path to output to, required\n",
+    "out_folder = \"/gpfs/exfel/exp/MID/201931/p900091/usr/PC/4.5_250/\" # path to output to, required\n",
     "runs = [30, 23, 24, 25, 26, 27, 28, 29] # runs to use, required, range allowed\n",
     "n_sequences = 3 # number of sequence files, starting for 0 to evaluate\n",
-    "cluster_profile = \"noDB44\" # The ipcluster profile to use\n",
+    "cluster_profile = \"noDB\" # The ipcluster profile to use\n",
     "local_output = True # output constants locally\n",
     "db_output = False # output constants to database\n",
     "bias_voltage = 300 # detector bias voltage\n",
@@ -53,7 +53,10 @@
     "instrument = \"MID\"\n",
     "high_res_badpix_3d = False # set this to True if you need high-resolution 3d bad pixel plots. Runtime: ~ 1h\n",
     "acq_rate = 0. # the detector acquisition rate, use 0 to try to auto-determine\n",
-    "dont_use_dir_date = False"
+    "use_dir_creation_date = True\n",
+    "creation_time = \"\" # To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC.ms e.g. 2019-07-04 11:02:41.00\n",
+    "gain_setting = 0.1 # gain setting can have value 0 or 1, Default=0.1 for no (None) gain-setting\n",
+    "karabo_da_control = \"AGIPD1MCTRL00\" # karabo DA for control infromation"
    ]
   },
   {
@@ -68,14 +71,17 @@
    "outputs": [],
    "source": [
     "# imports, usually no need to change anything here\n",
-    "import os\n",
+    "from datetime import datetime\n",
+    "import dateutil.parser\n",
     "import h5py\n",
+    "import os\n",
+    "\n",
+    "\n",
     "import numpy as np\n",
     "import matplotlib\n",
     "matplotlib.use(\"Qt4Agg\")\n",
     "import matplotlib.pyplot as plt\n",
     "%matplotlib inline\n",
-    "\n",
     "# make sure a cluster is running with ipcluster start --n=32, give it a while to start\n",
     "from ipyparallel import Client\n",
     "\n",
@@ -88,13 +94,13 @@
     "warnings.filterwarnings('ignore')\n",
     "\n",
     "from iCalibrationDB import ConstantMetaData, Constants, Conditions, Detectors, Versions\n",
-    "\n",
     "from cal_tools.tools import gain_map_files, parse_runs, run_prop_seq_from_path, get_notebook_name, get_dir_creation_date\n",
     "from cal_tools.influx import InfluxLogger\n",
     "from cal_tools.enums import BadPixels\n",
     "from cal_tools.plotting import show_overview, plot_badpix_3d\n",
-    "from cal_tools.agipdlib import get_acq_rate, get_num_cells\n",
-    "from datetime import datetime\n",
+    "from cal_tools.agipdlib import get_acq_rate, get_num_cells, get_gain_setting\n",
+    "\n",
+    "\n",
     "\n",
     "IL_MODE = interlaced \n",
     "maxcells = mem_cells if not interlaced else mem_cells*2\n",
@@ -121,8 +127,7 @@
     "else:\n",
     "    loc = \"MID_DET_AGIPD1M-1\"\n",
     "    dinstance = \"AGIPD1M2\"\n",
-    "print(\"Detector in use is {}\".format(loc))\n",
-    "\n"
+    "print(\"Detector in use is {}\".format(loc))"
    ]
   },
   {
@@ -148,7 +153,7 @@
     "run = runs[0]\n",
     "bursts_per_file = []\n",
     "channel = 0\n",
-    "creation_time=None\n",
+    "\n",
     "for seq in range(seqs):\n",
     "    fname = os.path.join(path_temp.format(run),\n",
     "                         image_name_temp.format(run, channel, seq))\n",
@@ -158,7 +163,19 @@
     "        acq_rate = get_acq_rate(fname, loc, channel)\n",
     "        print(\"Acquisition rate set from file: {} MHz\".format(acq_rate))\n",
     "    \n",
-    "    if not dont_use_dir_date:\n",
+    "    # Define constant creation time.\n",
+    "    if creation_time:\n",
+    "        try:\n",
+    "            creation_time = datetime.strptime(creation_time, '%Y-%m-%d %H:%M:%S.%f')\n",
+    "        except Exception as e:\n",
+    "            print(f\"creation_time value error: {e}.\" \n",
+    "                   \"Use same format as YYYY-MM-DD HR:MN:SC.ms e.g. 2019-07-04 11:02:41.00/n\")\n",
+    "            creation_time = None\n",
+    "            print(\"Given creation time wont be used.\")\n",
+    "    else:\n",
+    "        creation_time = None\n",
+    "\n",
+    "    if not creation_time and use_dir_creation_date:\n",
     "        creation_time = get_dir_creation_date(in_folder, run)\n",
     "    \n",
     "    if mem_cells == 0:\n",
@@ -181,6 +198,30 @@
     "    print(\"Using {} as creation time\".format(creation_time.isoformat()))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "control_fname = f'{in_folder}/r{runs[0]:04d}/RAW-R{runs[0]:04d}-{karabo_da_control}-S00000.h5'\n",
+    "\n",
+    "if gain_setting == 0.1:\n",
+    "    if creation_time.replace(tzinfo=None) < dateutil.parser.parse('2020-01-31'):\n",
+    "        print(\"Set gain-setting to None for runs taken before 2020-01-31\")\n",
+    "        gain_setting = None\n",
+    "    else:\n",
+    "        try:\n",
+    "            gain_setting = get_gain_setting(control_fname, h5path_ctrl)\n",
+    "        except Exception as e:\n",
+    "            print(f'Error while reading gain setting from: \\n{control_fname}')\n",
+    "            print(e)\n",
+    "            print(\"Gain setting is not found in the control information\")\n",
+    "            print(\"Data will not be processed\")\n",
+    "            sequences = []\n",
+    "print(f\"Gain setting: {gain_setting}\")"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -524,7 +565,8 @@
     "                                                     Constants.AGIPD.Offset(),\n",
     "                                                     Conditions.Dark.AGIPD(\n",
     "                                                         memory_cells=mem_cells,\n",
-    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate),\n",
+    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate,\n",
+    "                                                         gain_setting=gain_setting),\n",
     "                                                         np.zeros((128, 512, mem_cells, 3)),\n",
     "                                                     cal_db_interface, creation_time=creation_time)\n",
     "    print(\"Offset for {} was injected on {}\".format(qm, when))\n",
@@ -534,7 +576,8 @@
     "                                                     Constants.AGIPD.Noise(),\n",
     "                                                     Conditions.Dark.AGIPD(\n",
     "                                                         memory_cells=mem_cells,\n",
-    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate),\n",
+    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate,\n",
+    "                                                         gain_setting=gain_setting),\n",
     "                                                         np.zeros((128, 512, mem_cells, 3)),\n",
     "                                                     cal_db_interface, creation_time=creation_time)\n",
     "    print(\"Noise for {} was injected on {}\".format(qm, when))\n",
@@ -544,7 +587,8 @@
     "                                                     Constants.AGIPD.ThresholdsDark(),\n",
     "                                                     Conditions.Dark.AGIPD(\n",
     "                                                         memory_cells=mem_cells,\n",
-    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate),\n",
+    "                                                         bias_voltage=bias_voltage, acquisition_rate=acq_rate,\n",
+    "                                                         gain_setting=gain_setting),\n",
     "                                                         np.zeros((128, 512, mem_cells, 3)),\n",
     "                                                     cal_db_interface, creation_time=creation_time)\n",
     "    print(\"Threshold for {} was injected on {}\".format(qm, when))\n",
@@ -1344,6 +1388,16 @@
     "    store_file.close()"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "proposal = list(filter(None, in_folder.strip('/').split('/')))[-2]\n",
+    "file_loc = proposal + ' ' + ' '.join(list(map(str,runs)))"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
@@ -1355,8 +1409,6 @@
    },
    "outputs": [],
    "source": [
-    "#if instrument == \"MID\":\n",
-    "#    acq_rate = None\n",
     "if db_output:\n",
     "    for qm, r in fres.items():\n",
     "        metadata = ConstantMetaData()\n",
@@ -1366,11 +1418,18 @@
     "\n",
     "        # set the operating condition\n",
     "        condition = Conditions.Dark.AGIPD(memory_cells=maxcells, bias_voltage=bias_voltage,\n",
-    "                                          acquisition_rate=acq_rate)\n",
+    "                                          acquisition_rate=acq_rate, gain_setting=gain_setting)\n",
     "        metadata.detector_condition = condition\n",
     "\n",
     "        # specify the a version for this constant\n",
-    "        metadata.calibration_constant_version = Versions.Now(device=getattr(getattr(Detectors, dinstance), qm))\n",
+    "        \n",
+    "        if creation_time is None:\n",
+    "            metadata.calibration_constant_version = Versions.Now(device=getattr(Detectors.AGIPD1M1, qm))\n",
+    "        else:\n",
+    "            metadata.calibration_constant_version = Versions.Timespan(device=getattr(Detectors.AGIPD1M1, qm),\n",
+    "                                                                      start=creation_time)\n",
+    "        \n",
+    "        metadata.calibration_constant_version.raw_data_location = file_loc\n",
     "        metadata.send(cal_db_interface)\n",
     "        \n",
     "        # bad pixels\n",
@@ -1382,11 +1441,16 @@
     "        \n",
     "        # set the operating condition\n",
     "        condition = Conditions.Dark.AGIPD(memory_cells=maxcells, bias_voltage=bias_voltage,\n",
-    "                                          acquisition_rate=acq_rate)\n",
+    "                                          acquisition_rate=acq_rate, gain_setting=gain_setting)\n",
     "        metadata.detector_condition = condition\n",
     "\n",
     "        # specify the a version for this constant\n",
-    "        metadata.calibration_constant_version = Versions.Now(device=getattr(Detectors.AGIPD1M1, qm))\n",
+    "        if creation_time is None:\n",
+    "            metadata.calibration_constant_version = Versions.Now(device=getattr(Detectors.AGIPD1M1, qm))\n",
+    "        else:\n",
+    "            metadata.calibration_constant_version = Versions.Timespan(device=getattr(Detectors.AGIPD1M1, qm),\n",
+    "                                                                      start=creation_time)\n",
+    "        metadata.calibration_constant_version.raw_data_location = file_loc\n",
     "        metadata.send(cal_db_interface)"
    ]
   },