{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e0008d3-0f30-4fa8-8ae7-d7f73c188fbd",
   "metadata": {},
   "source": [
    "# Deconstruct CMIP6Plus_CV into per field controlled vocabularies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bed247f5-8cca-417c-b930-58852e3f02e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "import os\n",
    "import json\n",
    "from copy import deepcopy\n",
    "# Tools for calculating MD5 Checksums\n",
    "from cv_checksums import calculate_checksum\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5eef2199-985c-4769-8182-bda7431858ba",
   "metadata": {},
   "source": [
    "Load CV example file from mip-cmor-tables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bec1b12c-cafa-4632-b46f-a69d5433295c",
   "metadata": {},
   "outputs": [],
   "source": [
    "project_cv_file = '../../mip-cmor-tables/Tables/CMIP6Plus_CV.json'\n",
    "with open(project_cv_file) as fhandle:\n",
    "    project_cv_data = json.load(fhandle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5d044af4-0a8f-4315-a31d-e0adb47cea69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['DRS', 'activity_id', 'experiment_id', 'further_info_url', 'license', 'mip_era', 'required_global_attributes', 'source_id', 'sub_experiment_id', 'tracking_id', 'version_metadata'])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "project_cv_data.keys()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06bf643e-630f-4346-9576-e3773feed3fd",
   "metadata": {},
   "source": [
    "Split out each field into a separate JSON file and add its own `version_metadata` entry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4402341f-bf29-4aa1-8921-8fd352e593d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "version_metadata = project_cv_data['version_metadata']\n",
    "mip_era = project_cv_data['mip_era']\n",
    "output_file_location = '..'\n",
    "for field in project_cv_data:\n",
    "    if field == 'version_metadata':\n",
    "        continue\n",
    "    output_file_name = os.path.join(output_file_location, \n",
    "                                    '{}_{}.json'.format(mip_era, field))\n",
    "    output_file_data = {field: deepcopy(project_cv_data[field])}\n",
    "    output_file_data['version_metadata'] = version_metadata\n",
    "    \n",
    "    calculate_checksum(output_file_data)\n",
    "    with open(output_file_name, 'w') as fhandle:\n",
    "        json.dump(output_file_data, fhandle, indent=2, sort_keys=True)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "47756929-1c6a-4c9a-bc08-d3bdea963050",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CMIP6Plus_DRS.json\t\t\t   CMIP6Plus_source_id.json\n",
      "CMIP6Plus_activity_id.json\t\t   CMIP6Plus_sub_experiment_id.json\n",
      "CMIP6Plus_experiment_id.json\t\t   CMIP6Plus_tracking_id.json\n",
      "CMIP6Plus_further_info_url.json\t\t   LICENSE\n",
      "CMIP6Plus_license.json\t\t\t   README.md\n",
      "CMIP6Plus_mip_era.json\t\t\t   src\n",
      "CMIP6Plus_required_global_attributes.json\n"
     ]
    }
   ],
   "source": [
    "!ls .."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc8c07a7-9187-4005-ab37-5b6f89b5d68e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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
}
