{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "source": [ "This notebook is a brief guide of downloading KPF raw (level 0) data in a given period. It requires PI credentials to access proprietary data. For more detail about PyKOA, please see:\n", "http://koa.ipac.caltech.edu/UserGuide/PyKOA/notebooks/PyKOA_KPF_introduction.html" ], "metadata": { "id": "HozF_O0VM6-B" } }, { "cell_type": "markdown", "source": [ "First, we need to install the KOA python client PyKOA. This line can be removed if PyKOA is already installed." ], "metadata": { "id": "mmPy9VTCNrt-" } }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "T3H-B5qiOHGo", "outputId": "44509c01-4bcb-41d7-f0f8-9e5d963c12d5" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting pykoa\n", " Downloading pykoa-1.7.2-py3-none-any.whl (29 kB)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from pykoa) (2.27.1)\n", "Collecting xmltodict (from pykoa)\n", " Downloading xmltodict-0.13.0-py2.py3-none-any.whl (10.0 kB)\n", "Collecting bs4 (from pykoa)\n", " Downloading bs4-0.0.1.tar.gz (1.1 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from pykoa) (4.9.2)\n", "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from bs4->pykoa) (4.11.2)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->pykoa) (1.26.15)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->pykoa) (2022.12.7)\n", "Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests->pykoa) (2.0.12)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->pykoa) (3.4)\n", "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->bs4->pykoa) (2.4.1)\n", "Building wheels for collected packages: bs4\n", " Building wheel for bs4 (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for bs4: filename=bs4-0.0.1-py3-none-any.whl size=1257 sha256=77420ad9ba0994e75a43004455cd4100b25ff7f5a87f5875385db3dc65d6d613\n", " Stored in directory: /root/.cache/pip/wheels/25/42/45/b773edc52acb16cd2db4cf1a0b47117e2f69bb4eb300ed0e70\n", "Successfully built bs4\n", "Installing collected packages: xmltodict, bs4, pykoa\n", "Successfully installed bs4-0.0.1 pykoa-1.7.2 xmltodict-0.13.0\n" ] } ], "source": [ "!pip install pykoa" ] }, { "cell_type": "markdown", "source": [ "The next steop is to import pre-requisite packages" ], "metadata": { "id": "hhVJIWD0OKsS" } }, { "cell_type": "code", "source": [ "import sys\n", "import io\n", "import os\n", "from pykoa.koa import Koa \n", "from astropy.table import Table,Column" ], "metadata": { "id": "4G2fXjioOKBa" }, "execution_count": 2, "outputs": [] }, { "cell_type": "markdown", "source": [ "Then we need to assign directory that stores the query results:" ], "metadata": { "id": "9jJgI5RBOOOX" } }, { "cell_type": "code", "source": [ "try:\n", " os.mkdir('./output')\n", "except:\n", " print(\" Directory exists already\", flush=True)" ], "metadata": { "id": "OB_GgjpsOXVk" }, "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "source": [ "After that, we use a cookie for KOA log in. We can also spell out the credentials, but for security purpose, it is better to enter the userid and password interactively" ], "metadata": { "id": "Q3-Uu0RiOWm9" } }, { "cell_type": "code", "source": [ "Koa.login ('./tapcookie.txt')\n", "#Koa.login ('./tapcookie.txt', userid='clee', password='salami')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "olSx2E55OcgS", "outputId": "22330f44-1040-42c6-e40d-40a1bb8ac83f" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Userid: clee\n", "Password: ··········\n", "Successfully login as clee\n" ] } ] }, { "cell_type": "markdown", "source": [ "We can then proceed to query KPF data taken in a given period. Note the time period is specified in UT date. The output table will be saved in the designated directory and in IPAC table format." ], "metadata": { "id": "kBQdEWm9OdOs" } }, { "cell_type": "code", "source": [ "Koa.query_datetime ('kpf', \\\n", " '2023-05-30 00:00:00/2023-05-30 23:59:59', \\\n", " './output/kpf_daterange.tbl', overwrite=True, format='ipac', \\\n", " cookiepath='./tapcookie.txt')\n", "\n", "rec = Table.read ('./output/kpf_daterange.tbl',format='ipac')\n", "print (rec)\n", " " ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VaeWQLWROveh", "outputId": "0b3b2c7d-d13d-422d-9c5f-3cd995d4e1d4" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "submitting request...\n", "Result downloaded to file [./output/kpf_daterange.tbl]\n", " koaid ofname ... semid propint\n", "------------------------- ------------------------- ... --------- -------\n", "KP.20230530.03614.55.fits KP.20230530.03614.55.fits ... 2023a_eng 0\n", "KP.20230530.03675.91.fits KP.20230530.03675.91.fits ... 2023a_eng 0\n", "KP.20230530.03737.14.fits KP.20230530.03737.14.fits ... 2023a_eng 0\n", "KP.20230530.03798.47.fits KP.20230530.03798.47.fits ... 2023a_eng 0\n", "KP.20230530.03859.73.fits KP.20230530.03859.73.fits ... 2023a_eng 0\n", "KP.20230530.03920.93.fits KP.20230530.03920.93.fits ... 2023a_eng 0\n", "KP.20230530.03982.15.fits KP.20230530.03982.15.fits ... 2023a_eng 0\n", "KP.20230530.04043.50.fits KP.20230530.04043.50.fits ... 2023a_eng 0\n", "KP.20230530.04104.92.fits KP.20230530.04104.92.fits ... 2023a_eng 0\n", "KP.20230530.04166.11.fits KP.20230530.04166.11.fits ... 2023a_eng 0\n", " ... ... ... ... ...\n", "KP.20230530.10668.33.fits KP.20230530.10668.33.fits ... 2023a_eng 0\n", "KP.20230530.10762.68.fits KP.20230530.10762.68.fits ... 2023a_eng 0\n", "KP.20230530.10829.63.fits KP.20230530.10829.63.fits ... 2023a_eng 0\n", "KP.20230530.10896.79.fits KP.20230530.10896.79.fits ... 2023a_eng 0\n", "KP.20230530.10963.90.fits KP.20230530.10963.90.fits ... 2023a_eng 0\n", "KP.20230530.11030.88.fits KP.20230530.11030.88.fits ... 2023a_eng 0\n", "KP.20230530.11178.40.fits KP.20230530.11178.40.fits ... 2023a_eng 0\n", "KP.20230530.11300.51.fits KP.20230530.11300.51.fits ... 2023a_eng 0\n", "KP.20230530.11422.81.fits KP.20230530.11422.81.fits ... 2023a_eng 0\n", "KP.20230530.11544.89.fits KP.20230530.11544.89.fits ... 2023a_eng 0\n", "KP.20230530.11666.99.fits KP.20230530.11666.99.fits ... 2023a_eng 0\n", "Length = 67 rows\n" ] } ] }, { "cell_type": "markdown", "source": [ "Once we have the query result, we can download files to the designated directory, in this case to dnload_dir_kpf/. If the download function is called several times, it will check the designated directory and only download new files." ], "metadata": { "id": "KV5BDsU4Osxd" } }, { "cell_type": "code", "source": [ "Koa.download ('./output/kpf_daterange.tbl', \n", " 'ipac', \\\n", " 'dnload_dir_kpf' )" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KxRXJeruPLET", "outputId": "e2eff002-07ca-481b-f5d1-9c9619dba47a" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Start downloading 67 koaid data you requested;\n", "please check your outdir: dnload_dir_kpf for progress ....\n", "\n", "A total of 0 new lev0 FITS files downloaded.\n" ] } ] } ] }