{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "\n",
    "import sys\n",
    "from Firefly.data_reader import FIREreader,SimpleFIREreader,TweenParams"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Convert FIRE data\n",
    "In this example notebook we demonstrate how to use the `Firefly.data_reader.FIREreader` sub-class which creates specialized data files for FIRE formatted data. The details of how the `FIREreader` class is \"specialized\" see the <a href=\"https://ageller.github.io/Firefly/docs/build/html/reference/api/api.html\">API documentation</a> and to see the example of this output visit <a href=\"https://ageller.github.io/Firefly/src/Firefly/index.html\">the live demo version</a>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "thetas = np.arange(0,360,1)\n",
    "xs = np.sin(thetas/180*np.pi)*(-20)\n",
    "zs = np.cos(thetas/180*np.pi)*(-20)\n",
    "coords = np.zeros((thetas.size,3))\n",
    "coords[:,0] = xs\n",
    "coords[:,-1] = zs\n",
    "\n",
    "my_tween = TweenParams(coords,duration=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "JSONdir: /Users/agurvich/FIREData_50_1 -- is not a sub-directory of Firefly/static/data. \n",
      "This may produce confusing or inoperable results. As such, we will create a symlink for you when you  dumpToJSON.\n",
      "Loading ptype 0\n",
      "/Users/agurvich/research/snaps/isolated_disks/Control_G4_20/snapdir_050/snapshot_050.hdf5\n",
      "Loading ptype 0\n",
      "/Users/agurvich/research/snaps/isolated_disks/Control_G4_20/snapdir_050/snapshot_050.hdf5\n",
      "JSONdir: /Users/agurvich/FIREData_50_10 -- is not a sub-directory of Firefly/static/data. \n",
      "This may produce confusing or inoperable results. As such, we will create a symlink for you when you  dumpToJSON.\n",
      "Loading ptype 0\n",
      "/Users/agurvich/research/snaps/isolated_disks/Control_G4_20/snapdir_050/snapshot_050.hdf5\n",
      "Loading ptype 0\n",
      "/Users/agurvich/research/snaps/isolated_disks/Control_G4_20/snapdir_050/snapshot_050.hdf5\n",
      "JSONdir: /Users/agurvich/FIREData_50_100 -- is not a sub-directory of Firefly/static/data. \n",
      "This may produce confusing or inoperable results. As such, we will create a symlink for you when you  dumpToJSON.\n",
      "Loading ptype 0\n",
      "/Users/agurvich/research/snaps/isolated_disks/Control_G4_20/snapdir_050/snapshot_050.hdf5\n",
      "Loading ptype 0\n",
      "/Users/agurvich/research/snaps/isolated_disks/Control_G4_20/snapdir_050/snapshot_050.hdf5\n"
     ]
    }
   ],
   "source": [
    "def make_test_scene(decimation):\n",
    "    ## create a FIRE reader object\n",
    "    reader = FIREreader(\n",
    "        ## path to directory containing (optionally multiple) .hdf5 files\n",
    "        snapdir = \"/Users/agurvich/research/snaps/isolated_disks/Control_G4_20/snapdir_050/\",\n",
    "        ## the snapshot number, best to provide separately in order to disambiguate\n",
    "        snapnum = 50,\n",
    "        ## particle types one would like to extract from .hdf5 files\n",
    "        ptypes=[0,0],\n",
    "        ## what to call them in the UI\n",
    "        UInames=['Gas','Gas1'],\n",
    "        ## by what factor would we like to reduce the data for performance stability and disk space concerns\n",
    "        decimation_factors=[decimation,decimation],\n",
    "        ## what fields would we like to extract\n",
    "        fields=['Density','Velocities','Temperature'],\n",
    "        ## do we want to take the magnitude of any of these fields?\n",
    "        magFlags=[False,False,False],\n",
    "        ## do we want to take the log? \n",
    "        logFlags=[True,False,True],\n",
    "        ## which fields do we want to be able to filter on?\n",
    "        filterFlags=[True,True,True],\n",
    "        ## which fields do we want to be able to colormap by?\n",
    "        colormapFlags=[True,True,True],\n",
    "        ## where should the output .json files be saved to? \n",
    "        ##  if a relative path is given, like here, saves to $HOME/<JSONdir>\n",
    "        ##  and creates a soft-link to Firefly/static/data\n",
    "        JSONdir=\"FIREData_50_%d\"%decimation,\n",
    "        ## overwrite the existing startup.json file\n",
    "        write_startup='append',\n",
    "        tweenParams=my_tween)\n",
    "\n",
    "    ## fetch data from .hdf5 files\n",
    "    reader.loadData()\n",
    "\n",
    "    ## set the color and size of the gas and star particles\n",
    "    ##  to be aesthetically pleasing\n",
    "    reader.settings['color']['Gas']=[1,0,0,1]\n",
    "    reader.settings['sizeMult']['Gas']=1\n",
    "    reader.settings['color']['Gas1']=[1,0,0,1]\n",
    "    reader.settings['sizeMult']['Gas1']=1\n",
    "    reader.settings['camera'] = [0,0,-20]\n",
    "    reader.settings['showfps'] = True\n",
    "    reader.settings['start_tween'] = True\n",
    "\n",
    "    return reader\n",
    "\n",
    "target_data = os.path.join(os.environ['HOME'],'FPS_test','static','data')\n",
    "for decimation in [1,10,100]:\n",
    "    reader = make_test_scene(decimation)\n",
    "    \n",
    "    if decimation == 1:\n",
    "        reader.copyFireflySourceToTarget(\"FPS_test\")\n",
    "    else:\n",
    "        reader.static_data_dir = target_data\n",
    "        reader.dumpToJSON(symlink=False)\n",
    "    \n",
    "    "
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "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.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
