{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Final stage of HELP data processing\n",
    "\n",
    "This notebook collates the final output files ready for writing to csv for ingestion to a VO server. At the bottom of the notebook we also summarise the pipeline products which are processed on a given field. This are generated using the dmu32 meta_main.yml files which contain links to the XID+, CIGALE and photo-z catalogues which feed in to the final catalogues for publishing.\n",
    "\n",
    "Summary of notebook:\n",
    "\n",
    "- Take DR1 masterlist suffixes from overview table\n",
    "- Find dmu32 full table names and write to a file\n",
    "- Create summary of all the data products per field using the dmu32 meta_main.yml files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This notebook was run with herschelhelp_internal version: \n",
      "017bb1e (Mon Jun 18 14:58:59 2018 +0100) [with local modifications]\n",
      "This notebook was executed on: \n",
      "2021-01-21 17:55:33.203798\n"
     ]
    }
   ],
   "source": [
    "from  herschelhelp_internal  import git_version\n",
    "print(\"This notebook was run with herschelhelp_internal version: \\n{}\".format(git_version()))\n",
    "import datetime\n",
    "print(\"This notebook was executed on: \\n{}\".format(datetime.datetime.now()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/pyenv/versions/3.7.2/lib/python3.7/site-packages/matplotlib/__init__.py:855: MatplotlibDeprecationWarning: \n",
      "examples.directory is deprecated; in the future, examples will be found relative to the 'datapath' directory.\n",
      "  \"found relative to the 'datapath' directory.\".format(key))\n",
      "/opt/pyenv/versions/3.7.2/lib/python3.7/site-packages/matplotlib/__init__.py:846: MatplotlibDeprecationWarning: \n",
      "The text.latex.unicode rcparam was deprecated in Matplotlib 2.2 and will be removed in 3.1.\n",
      "  \"2.2\", name=key, obj_type=\"rcparam\", addendum=addendum)\n",
      "/opt/pyenv/versions/3.7.2/lib/python3.7/site-packages/seaborn/apionly.py:9: UserWarning: As seaborn no longer sets a default style on import, the seaborn.apionly module is deprecated. It will be removed in a future version.\n",
      "  warnings.warn(msg, UserWarning)\n"
     ]
    }
   ],
   "source": [
    "from astropy.table import Table, Column\n",
    "from astropy import units as u\n",
    "import numpy as np\n",
    "import glob\n",
    "from pymoc import MOC\n",
    "import hashlib\n",
    "from herschelhelp_internal.masterlist import find_last_ml_suffix\n",
    "\n",
    "import yaml\n",
    "\n",
    "import os\n",
    "import time\n",
    "\n",
    "import humanfriendly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "TODAY = os.environ.get('SUFFIX', time.strftime(\"_%Y%m%d\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The definition of HELP PDR1\n",
    "Here we take the DR1 definition from the dmu32 yaml files which are the definition of the final and official files. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "yaml_files = glob.glob('./*/meta_main.yml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "field_yamls = [yaml.load(open(f, 'r')) for f in yaml_files]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'field': 'SA13',\n",
       " 'region': 'dmu_products/dmu2/dmu2_field_coverages/SA13_MOC.fits',\n",
       " 'surveys': ['LegacySurvey', 'UHS'],\n",
       " 'masterlist': 'dmu_products/dmu1/dmu1_ml_SA13/data/master_catalogue_sa13_20180501.fits',\n",
       " 'depths': 'dmu_products/dmu1/dmu1_ml_SA13/data/depths_sa13_20180501.fits',\n",
       " 'flags': 'None',\n",
       " 'xid': ['dmu_products/dmu26/dmu26_XID+SPIRE_SA13/data/dmu26_XID+SPIRE_SA13_cat_20191024.fits'],\n",
       " 'photoz': 'dmu_products/dmu24/dmu24_SA13/data/SA13_DESI-DR7_Zou_et_al_2019_photo_z_withhelp_id.fits',\n",
       " 'cigale': 'dmu_products/dmu28/dmu28_SA13/data/zphot/HELP_final_results.fits',\n",
       " 'cigale_ldust_prediction': 'dmu_products/dmu28/dmu28_SA13/data/SA13_Ldust_prediction_results.fits',\n",
       " 'final': 'dmu_products/dmu32/dmu32_SA13/data/SA13_20180501.fits'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "field_yamls[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SA13\n",
      "../dmu32/dmu32_SA13/data/SA13_20180501.fits\n",
      "Bootes\n",
      "../dmu32/dmu32_Bootes/data/Bootes_20190701.fits\n",
      "xFLS\n",
      "../dmu32/dmu32_xFLS/data/xFLS_20180501.fits\n",
      "GAMA-15\n",
      "../dmu32/dmu32_GAMA-15/data/GAMA-15_20180213.fits\n",
      "GAMA-12\n",
      "../dmu32/dmu32_GAMA-12/data/GAMA-12_20180218.fits\n",
      "HDF-N\n",
      "../dmu32/dmu32_HDF-N/data/HDF-N_20180427.fits\n",
      "HATLAS-NGP\n",
      "../dmu32/dmu32_NGP/data/NGP_20180219.fits\n",
      "ELAIS-N2\n",
      "../dmu32/dmu32_ELAIS-N2/data/ELAIS-N2_20180218.fits\n",
      "EGS\n",
      "../dmu32/dmu32_EGS/data/EGS_20180501.fits\n",
      "COSMOS\n",
      "../dmu32/dmu32_COSMOS/data/COSMOS_20190402.fits\n",
      "HATLAS-SGP\n",
      "../dmu32/dmu32_SGP/data/SGP_20180221.fits\n",
      "Lockman-SWIRE\n",
      "../dmu32/dmu32_Lockman-SWIRE/data/Lockman-SWIRE_20180219.fits\n",
      "XMM-LSS\n",
      "../dmu32/dmu32_XMM-LSS/data/XMM-LSS_20190328.fits\n",
      "ELAIS-N1\n",
      "../dmu32/dmu32_ELAIS-N1/data/ELAIS-N1_20171016.fits\n",
      "AKARI-SEP\n",
      "../dmu32/dmu32_AKARI-SEP/data/AKARI-SEP_20180221.fits\n",
      "GAMA-09\n",
      "../dmu32/dmu32_GAMA-09/data/GAMA-09_20180601.fits\n",
      "AKARI-NEP\n",
      "../dmu32/dmu32_AKARI-NEP/data/AKARI-NEP_20180215.fits\n",
      "ELAIS-S1\n",
      "../dmu32/dmu32_ELAIS-S1/data/ELAIS-S1_20180416.fits\n",
      "XMM-13hr\n",
      "../dmu32/dmu32_XMM-13hr/data/XMM-13hr_20180501.fits\n",
      "Herschel-Stripe-82\n",
      "../dmu32/dmu32_Herschel-Stripe-82/data/Herschel-Stripe-82_20180307.fits\n",
      "CDFS-SWIRE\n",
      "../dmu32/dmu32_CDFS-SWIRE/data/CDFS-SWIRE_20180613.fits\n",
      "SPIRE-NEP\n",
      "../dmu32/dmu32_SPIRE-NEP/data/SPIRE-NEP_20180220.fits\n",
      "SSDF\n",
      "../dmu32/dmu32_SSDF/data/SSDF_20180221.fits\n"
     ]
    }
   ],
   "source": [
    "GAVO_FOLDER = '/mnt/hedam/data_vo/'\n",
    "stilts_command = 'stilts tpipe {in_file} omode=out ofmt=csv out={GAVO_FOLDER}{out_file}'\n",
    "\n",
    "final_data = open('help_to_vo.sh', 'w+')\n",
    "for y in field_yamls:\n",
    "    print(y['field'])\n",
    "    final_help_product = y['final'].replace('dmu_products', '..')\n",
    "    cigale_input = y['cigale']\n",
    "\n",
    "    if os.path.exists(final_help_product):\n",
    "        print(final_help_product)\n",
    "    \n",
    "        #Test with Cigale input files\n",
    "        final_data.write(stilts_command.format(\n",
    "            in_file=final_help_product, \n",
    "            GAVO_FOLDER=GAVO_FOLDER, \n",
    "            out_file='herschelhelp/main/{} \\n'.format(\n",
    "                final_help_product.split('/')[-1].replace('.fits', '.csv')\n",
    "            )\n",
    "        ))\n",
    "        \n",
    "    else:\n",
    "        final_data.write('# No data for {} \\n'.format(y['field']))\n",
    "        \n",
    "    #final_data.write('./dmu32_{}/data/{}_{}.fits'.format(field[0], field[0], field[1]))\n",
    "    \n",
    "final_data.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The out put of this notebook is a shell script which will write all the fits files to csv files in the vo folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "../dmu1/dmu1_ml_SA13/data/depths_sa13_20180501.fits\n",
      "../dmu1/dmu1_ml_Bootes/data/depths_bootes_20190201.fits\n",
      "../dmu1/dmu1_ml_xFLS/data/depths_xfls_20180501.fits\n",
      "../dmu1/dmu1_ml_GAMA-15/data/depths_gama-15_20180213.fits\n",
      "../dmu1/dmu1_ml_GAMA-12/data/depths_gama-12_20180218.fits\n",
      "../dmu1/dmu1_ml_HDF-N/data/depths_hdf-n_20180427.fits\n",
      "../dmu1/dmu1_ml_NGP/data/depths_ngp_20180219.fits\n",
      "../dmu1/dmu1_ml_ELAIS-N2/data/depths_elais-n2_20180218.fits\n",
      "../dmu1/dmu1_ml_EGS/data/depths_egs_20180501.fits\n",
      "../dmu1/dmu1_ml_SGP/data/depths_sgp_20180221.fits\n",
      "../dmu1/dmu1_ml_Lockman-SWIRE/data/depths_lockman-swire_20180219.fits\n",
      "../dmu1/dmu1_ml_ELAIS-N1/data/depths_elais-n1_20171016.fits\n",
      "../dmu1/dmu1_ml_AKARI-SEP/data/depths_akari-sep_20180221.fits\n",
      "../dmu1/dmu1_ml_GAMA-09/data/depths_gama-09_20180601.fits\n",
      "../dmu1/dmu1_ml_AKARI-NEP/data/depths_akari-nep_20180215.fits\n",
      "../dmu1/dmu1_ml_ELAIS-S1/data/depths_elais-s1_20180416.fits\n",
      "../dmu1/dmu1_ml_XMM-13hr/data/depths_xmm-13hr_20180501.fits\n",
      "../dmu1/dmu1_ml_Herschel-Stripe-82/data/depths_herschel-stripe-82_20180307.fits\n",
      "../dmu1/dmu1_ml_SPIRE-NEP/data/depths_spire-nep_20180220.fits\n",
      "../dmu1/dmu1_ml_SSDF/data/depths_ssdf_20180221.fits\n"
     ]
    }
   ],
   "source": [
    "depths_to_vo = open('depths_to_vo.sh', 'w+')\n",
    "for y in field_yamls:\n",
    "    final_depth_product = y['depths'].replace('dmu_products', '..')\n",
    "    \n",
    "\n",
    "    if os.path.exists(final_depth_product):\n",
    "        print(final_depth_product)\n",
    "    \n",
    "        #Test with Cigale input files\n",
    "        depths_to_vo.write(stilts_command.format(\n",
    "            in_file=final_depth_product, \n",
    "            GAVO_FOLDER=GAVO_FOLDER, \n",
    "            out_file='depth/{} \\n'.format(final_depth_product.split('/')[-1].replace('.fits', '.csv'))\n",
    "        ))\n",
    "\n",
    "        \n",
    "    else:\n",
    "        depths_to_vo.write('# No depths for {} \\n'.format(y['field']))\n",
    "        \n",
    "    #final_data.write('./dmu32_{}/data/{}_{}.fits'.format(field[0], field[0], field[1]))\n",
    "    \n",
    "depths_to_vo.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summarise completeness of HELP data sets\n",
    "\n",
    "Here we get information about what is available on each field to summarise the data products available per field. We take the cigale, xid+ and photo-z filenames from the per field meta_main.yml files here and check they are there and how large they are. This then given a summary of all the data present."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "dr1 = Table()\n",
    "dr1['field'] = [y['field'] for y in field_yamls]\n",
    "dr1.sort('field')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "fields_info = yaml.load(open(\"../dmu2/meta_main.yml\", 'r'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dr1['objects']             =np.full(len(dr1), 0, dtype=int)\n",
    "dr1['dr1_file']            =np.full(len(dr1), 0, dtype=np.dtype('U250'))\n",
    "dr1['dr1_file_hash']       =np.full(len(dr1), 0, dtype=np.dtype('U250'))\n",
    "dr1['area_sq_degrees']     =np.full(len(dr1), 0, dtype='float64')\n",
    "dr1['file_size_bytes']     =np.full(len(dr1), 0, dtype=int)\n",
    "dr1['file_size_readable']  =np.full(len(dr1), 0, dtype=np.dtype('U250'))\n",
    "dr1['xid_objects']         =np.full(len(dr1), 0, dtype=int)\n",
    "dr1['photoz_objects']      =np.full(len(dr1), 0, dtype=int)\n",
    "dr1['cigale_objects']      =np.full(len(dr1), 0, dtype=int)\n",
    "dr1['blind_objects']      =np.full(len(dr1), 0, dtype=int)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def file_as_bytes(file):\n",
    "    with file:\n",
    "        return file.read()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SA13:\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/pyenv/versions/3.7.2/lib/python3.7/site-packages/astropy/table/column.py:965: RuntimeWarning: invalid value encountered in greater\n",
      "  return getattr(self.data, op)(other)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bootes:\n",
      "xFLS:\n",
      "GAMA-15:\n",
      "GAMA-12:\n",
      "HDF-N:\n",
      "Problem reading dmu_products/dmu32/dmu32_HDF-N/data/HDF-N_20180427.fits\n",
      "HATLAS-NGP:\n",
      "ELAIS-N2:\n",
      "EGS:\n",
      "COSMOS:\n",
      "HATLAS-SGP:\n",
      "Lockman-SWIRE:\n",
      "XMM-LSS:\n",
      "ELAIS-N1:\n",
      "AKARI-SEP:\n",
      "GAMA-09:\n",
      "AKARI-NEP:\n",
      "ELAIS-S1:\n",
      "XMM-13hr:\n",
      "Herschel-Stripe-82:\n",
      "CDFS-SWIRE:\n",
      "SPIRE-NEP:\n",
      "SSDF:\n"
     ]
    }
   ],
   "source": [
    "for y in field_yamls:\n",
    "    print(y['field'] + ':')\n",
    "    this_row = dr1['field'] == y['field']\n",
    "    final = y['final'].replace('dmu_products/', '../')\n",
    "    moc = y['region'].replace('dmu_products/', '../')\n",
    "    try:\n",
    "        cat = Table.read(final)\n",
    "        \n",
    "        \n",
    "        dr1['objects'][this_row]            = len(cat)\n",
    "        dr1['dr1_file'][this_row]           = y['final']\n",
    "        dr1['dr1_file_hash'][this_row]      = hashlib.md5(file_as_bytes(open(final, 'rb'))).hexdigest()\n",
    "        dr1['area_sq_degrees'][this_row]    = help_moc = MOC(filename=moc).area_sq_deg\n",
    "        size = os.stat(final).st_size\n",
    "        dr1['file_size_bytes'][this_row]    = size\n",
    "\n",
    "        dr1['file_size_readable'][this_row] = humanfriendly.format_size(size)\n",
    "        dr1['xid_objects'][this_row]        = np.sum(cat['f_spire_500']>0)\n",
    "        dr1['photoz_objects'][this_row]     =  np.sum(cat['redshift']>0)\n",
    "        dr1['cigale_objects'][this_row]     = np.sum(cat['cigale_sfr']>0)\n",
    "        \n",
    "        blind = Table.read('../dmu22/dmu22_{}/data/dmu22_XID+SPIRE_{}_BLIND_Matched_MF.fits'.format(\n",
    "            y['field'],y['field']))\n",
    "        dr1['blind_objects'][this_row]     = len(blind)\n",
    "    \n",
    "    except:\n",
    "        print('Problem reading {}'.format(y['final']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'../dmu32/dmu32_SSDF/data/SSDF_20180221.fits'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<i>Table length=23</i>\n",
       "<table id=\"table139664686751024-725361\" class=\"table-striped table-bordered table-condensed\">\n",
       "<thead><tr><th>idx</th><th>field</th><th>objects</th><th>dr1_file</th><th>dr1_file_hash</th><th>area_sq_degrees</th><th>file_size_bytes</th><th>file_size_readable</th><th>xid_objects</th><th>photoz_objects</th><th>cigale_objects</th><th>blind_objects</th></tr></thead>\n",
       "<tr><td>0</td><td>AKARI-NEP</td><td>531746</td><td>dmu_products/dmu32/dmu32_AKARI-NEP/data/AKARI-NEP_20180215.fits</td><td>5b33074e816785e05407952f03e78354</td><td>9.194732358779467</td><td>433440000</td><td>433.44 MB</td><td>31441</td><td>107228</td><td>1239</td><td>9848</td></tr>\n",
       "<tr><td>1</td><td>AKARI-SEP</td><td>844172</td><td>dmu_products/dmu32/dmu32_AKARI-SEP/data/AKARI-SEP_20180221.fits</td><td>56889960f654742f80329e8ebb0053d5</td><td>8.713306475131118</td><td>668652480</td><td>668.65 MB</td><td>108119</td><td>139059</td><td>566</td><td>20169</td></tr>\n",
       "<tr><td>2</td><td>Bootes</td><td>3398098</td><td>dmu_products/dmu32/dmu32_Bootes/data/Bootes_20190701.fits</td><td>6d2f18ef25a42999d805b258c751fde2</td><td>11.42815299095486</td><td>5556003840</td><td>5.56 GB</td><td>495159</td><td>1570512</td><td>38980</td><td>30566</td></tr>\n",
       "<tr><td>3</td><td>CDFS-SWIRE</td><td>2171051</td><td>dmu_products/dmu32/dmu32_CDFS-SWIRE/data/CDFS-SWIRE_20180613.fits</td><td>14a8d5110b0be3a9d54c0fa6045ab727</td><td>12.971246403717068</td><td>6059603520</td><td>6.06 GB</td><td>283406</td><td>136944</td><td>9308</td><td>40880</td></tr>\n",
       "<tr><td>4</td><td>COSMOS</td><td>2599374</td><td>dmu_products/dmu32/dmu32_COSMOS/data/COSMOS_20190402.fits</td><td>c900763f01cd327b84fafa3defe46151</td><td>5.083863478496816</td><td>10954031040</td><td>10.95 GB</td><td>25898</td><td>691502</td><td>15747</td><td>12603</td></tr>\n",
       "<tr><td>5</td><td>EGS</td><td>1412613</td><td>dmu_products/dmu32/dmu32_EGS/data/EGS_20180501.fits</td><td>74ce7e6bd7a982141d7558fad62b38df</td><td>3.566383275122158</td><td>4018965120</td><td>4.02 GB</td><td>223598</td><td>1182503</td><td>4159</td><td>9551</td></tr>\n",
       "<tr><td>6</td><td>ELAIS-N1</td><td>4026292</td><td>dmu_products/dmu32/dmu32_ELAIS-N1/data/ELAIS-N1_20171016.fits</td><td>97ccb7d86c92aa9e9ba0657c9e737276</td><td>13.507484555454763</td><td>6007360320</td><td>6.01 GB</td><td>269611</td><td>2714686</td><td>49985</td><td>34501</td></tr>\n",
       "<tr><td>7</td><td>ELAIS-N2</td><td>1783240</td><td>dmu_products/dmu32/dmu32_ELAIS-N2/data/ELAIS-N2_20180218.fits</td><td>09628a8b89fd5eeea27d6059988eb900</td><td>9.167479903991113</td><td>2321884800</td><td>2.32 GB</td><td>86591</td><td>120723</td><td>6798</td><td>19483</td></tr>\n",
       "<tr><td>8</td><td>ELAIS-S1</td><td>1655564</td><td>dmu_products/dmu32/dmu32_ELAIS-S1/data/ELAIS-S1_20180416.fits</td><td>e1e31bd8ae8b7646c47aaec6a40e2cf4</td><td>9.002940646885508</td><td>2231798400</td><td>2.23 GB</td><td>194276</td><td>1013582</td><td>25393</td><td>22743</td></tr>\n",
       "<tr><td>9</td><td>GAMA-09</td><td>12937982</td><td>dmu_products/dmu32/dmu32_GAMA-09/data/GAMA-09_20180601.fits</td><td>6712784e0dd54abaca5dc46b82a2c7a5</td><td>62.01393417284915</td><td>21839469120</td><td>21.84 GB</td><td>1386659</td><td>8833874</td><td>130293</td><td>112461</td></tr>\n",
       "<tr><td>10</td><td>GAMA-12</td><td>12369415</td><td>dmu_products/dmu32/dmu32_GAMA-12/data/GAMA-12_20180218.fits</td><td>ba0974bee7ab13a2468e235fb3426928</td><td>62.70933280772107</td><td>16476194880</td><td>16.48 GB</td><td>1099477</td><td>8569951</td><td>108139</td><td>112471</td></tr>\n",
       "<tr><td>11</td><td>GAMA-15</td><td>14232880</td><td>dmu_products/dmu32/dmu32_GAMA-15/data/GAMA-15_20180213.fits</td><td>832509b2d9b09a9848dfe40695d298cb</td><td>61.70114565980842</td><td>18716371200</td><td>18.72 GB</td><td>1236395</td><td>10083210</td><td>117234</td><td>116436</td></tr>\n",
       "<tr><td>12</td><td>HATLAS-NGP</td><td>6759591</td><td>dmu_products/dmu32/dmu32_NGP/data/NGP_20180219.fits</td><td>e74f531dcfee1d6886f528a7af95e6e5</td><td>177.6981971077162</td><td>5962037760</td><td>5.96 GB</td><td>1233547</td><td>3166952</td><td>185290</td><td>344635</td></tr>\n",
       "<tr><td>13</td><td>HATLAS-SGP</td><td>29790690</td><td>dmu_products/dmu32/dmu32_SGP/data/SGP_20180221.fits</td><td>d16d97a07cca1f4c2552206a2b51dc68</td><td>294.5684540900642</td><td>47039604480</td><td>47.04 GB</td><td>3511594</td><td>17054138</td><td>352804</td><td>497501</td></tr>\n",
       "<tr><td>14</td><td>HDF-N</td><td>130679</td><td>dmu_products/dmu32/dmu32_HDF-N/data/HDF-N_20180427.fits</td><td>eda45995ea7811f618393675056ff003</td><td>0.6717832558166041</td><td>190630080</td><td>190.63 MB</td><td>834</td><td>7435</td><td>0</td><td>0</td></tr>\n",
       "<tr><td>15</td><td>Herschel-Stripe-82</td><td>50196455</td><td>dmu_products/dmu32/dmu32_Herschel-Stripe-82/data/Herschel-Stripe-82_20180307.fits</td><td>2e39482611301daf8a0c1e4cad573842</td><td>363.22747930641884</td><td>133673333760</td><td>133.67 GB</td><td>2976447</td><td>21509448</td><td>250644</td><td>232589</td></tr>\n",
       "<tr><td>16</td><td>Lockman-SWIRE</td><td>4366298</td><td>dmu_products/dmu32/dmu32_Lockman-SWIRE/data/Lockman-SWIRE_20180219.fits</td><td>72c4ab422be2081358fb50ba3cb9a867</td><td>22.413863402949218</td><td>5990670720</td><td>5.99 GB</td><td>242065</td><td>1377139</td><td>46719</td><td>54106</td></tr>\n",
       "<tr><td>17</td><td>SA13</td><td>9799</td><td>dmu_products/dmu32/dmu32_SA13/data/SA13_20180501.fits</td><td>c2e822dab8edfcf4b178e31c245b1f77</td><td>0.27416379328431184</td><td>4011840</td><td>4.01 MB</td><td>812</td><td>2884</td><td>70</td><td>315</td></tr>\n",
       "<tr><td>18</td><td>SPIRE-NEP</td><td>2674</td><td>dmu_products/dmu32/dmu32_SPIRE-NEP/data/SPIRE-NEP_20180220.fits</td><td>e24491b6b9f3db62e7d6e96e4a384ff3</td><td>0.1280660469377391</td><td>1578240</td><td>1.58 MB</td><td>562</td><td>935</td><td>71</td><td>374</td></tr>\n",
       "<tr><td>19</td><td>SSDF</td><td>12661903</td><td>dmu_products/dmu32/dmu32_SSDF/data/SSDF_20180221.fits</td><td>fc789fa2236f6fc3f2a1cce9e8aa8256</td><td>111.11624949343539</td><td>9737064000</td><td>9.74 GB</td><td>4395253</td><td>9250727</td><td>305576</td><td>196895</td></tr>\n",
       "<tr><td>20</td><td>XMM-13hr</td><td>38629</td><td>dmu_products/dmu32/dmu32_XMM-13hr/data/XMM-13hr_20180501.fits</td><td>da7c644f7aeee09034af7040439b32e0</td><td>0.7570240166583633</td><td>15747840</td><td>15.75 MB</td><td>3563</td><td>10773</td><td>670</td><td>1218</td></tr>\n",
       "<tr><td>21</td><td>XMM-LSS</td><td>8705837</td><td>dmu_products/dmu32/dmu32_XMM-LSS/data/XMM-LSS_20190328.fits</td><td>53e3b49a42cc1900ff06aad3763bc475</td><td>21.75457939331374</td><td>29547829440</td><td>29.55 GB</td><td>360500</td><td>6124027</td><td>61888</td><td>50362</td></tr>\n",
       "<tr><td>22</td><td>xFLS</td><td>977148</td><td>dmu_products/dmu32/dmu32_xFLS/data/xFLS_20180501.fits</td><td>983336d6ca349d809ea831cba71a9b1b</td><td>7.44043242140754</td><td>1187294400</td><td>1.19 GB</td><td>52187</td><td>100993</td><td>5944</td><td>19757</td></tr>\n",
       "</table><style>table.dataTable {clear: both; width: auto !important; margin: 0 !important;}\n",
       ".dataTables_info, .dataTables_length, .dataTables_filter, .dataTables_paginate{\n",
       "display: inline-block; margin-right: 1em; }\n",
       ".paginate_button { margin-right: 5px; }\n",
       "</style>\n",
       "<script>\n",
       "\n",
       "var astropy_sort_num = function(a, b) {\n",
       "    var a_num = parseFloat(a);\n",
       "    var b_num = parseFloat(b);\n",
       "\n",
       "    if (isNaN(a_num) && isNaN(b_num))\n",
       "        return ((a < b) ? -1 : ((a > b) ? 1 : 0));\n",
       "    else if (!isNaN(a_num) && !isNaN(b_num))\n",
       "        return ((a_num < b_num) ? -1 : ((a_num > b_num) ? 1 : 0));\n",
       "    else\n",
       "        return isNaN(a_num) ? -1 : 1;\n",
       "}\n",
       "\n",
       "require.config({paths: {\n",
       "    datatables: 'https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min'\n",
       "}});\n",
       "require([\"datatables\"], function(){\n",
       "    console.log(\"$('#table139664686751024-725361').dataTable()\");\n",
       "    \n",
       "jQuery.extend( jQuery.fn.dataTableExt.oSort, {\n",
       "    \"optionalnum-asc\": astropy_sort_num,\n",
       "    \"optionalnum-desc\": function (a,b) { return -astropy_sort_num(a, b); }\n",
       "});\n",
       "\n",
       "    $('#table139664686751024-725361').dataTable({\n",
       "        order: [],\n",
       "        pageLength: 50,\n",
       "        lengthMenu: [[10, 25, 50, 100, 500, 1000, -1], [10, 25, 50, 100, 500, 1000, 'All']],\n",
       "        pagingType: \"full_numbers\",\n",
       "        columnDefs: [{targets: [0, 2, 5, 6, 8, 9, 10, 11], type: \"optionalnum\"}]\n",
       "    });\n",
       "});\n",
       "</script>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dr1.show_in_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Totals:\n",
      "Area:              1269.0 Square degrees\n",
      "Objects :          171602130\n",
      "XID+ objects :     18217994 (= 11.0 percent)\n",
      "Redshifts :        93769225 (= 55.0 percent)\n",
      "CIGALE objects :   1717517 (= 1.0 percent)\n",
      "Final tables size: 328.63 GB\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"\"\"Totals:\n",
    "Area:              {} Square degrees\n",
    "Objects :          {}\n",
    "XID+ objects :     {} (= {} percent)\n",
    "Redshifts :        {} (= {} percent)\n",
    "CIGALE objects :   {} (= {} percent)\n",
    "Final tables size: {}\n",
    "\"\"\".format(\n",
    "    round(np.sum(dr1['area_sq_degrees'])),\n",
    "    np.sum(dr1['objects']),\n",
    "    np.sum(dr1['xid_objects']), round(100*np.sum(dr1['xid_objects'])/np.sum(dr1['objects'])),\n",
    "    np.sum(dr1['photoz_objects']), round(100*np.sum(dr1['photoz_objects'])/np.sum(dr1['objects'])),\n",
    "    np.sum(dr1['cigale_objects']), round(100*np.sum(dr1['cigale_objects'])/np.sum(dr1['objects'])),\n",
    "    humanfriendly.format_size(np.sum(dr1['file_size_bytes']))\n",
    "))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dr1.write('dr1_data_products_overview{}.csv'.format(TODAY), overwrite=True)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python (herschelhelp_internal)",
   "language": "python",
   "name": "helpint"
  },
  "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.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
