{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# National (Aggregate) Functions\n",
    "\n",
    "def get_national_pharmacy_data(us_abbr_list: list, verification: bool = False, key: str = 'WaPo'):\n",
    "    '''(string) -> pd.df\n",
    "        Returns all pharmacy totals from all states (Will be large and could take extra time to load) \n",
    "\n",
    "        >>>get_national_pharmacy_data(verification = True)\n",
    "            EXAMPLE OUTPUT\n",
    "    '''\n",
    "    national_pharm_data = pd.DataFrame()\n",
    "\n",
    "    if verification == True:\n",
    "        for state_abbr in us_abbr_list:\n",
    "            print(state_abbr)\n",
    "            state_df = get_summ_total_pharmacies_state(state_abbr, verification, key)\n",
    "            national_pharm_data = national_pharm_data.append(state_df)\n",
    "        return national_pharm_data\n",
    "    else:\n",
    "        print('Problem encountered, not returning data:')\n",
    "        print('Either verification == False')\n",
    "        print('Or problem with API encountered, please verify URL, state and county are correct: ')\n",
    "        \n",
    "def get_national_pharmacy_latlon(us_abbr_list: list, county = \"\", verification: bool = False, key: str = 'WaPo'):\n",
    "    '''(string) -> pd.df\n",
    "        Returns all pharmacy latlon from all states (Will be large and could take extra time to load) \n",
    "\n",
    "        >>>get_national_pharmacy_latlon(verification = True)\n",
    "            EXAMPLE OUTPUT\n",
    "    '''\n",
    "    national_pharm_latlon_data = pd.DataFrame()\n",
    "\n",
    "    if verification == True:\n",
    "        for state_abbr in us_abbr_list:\n",
    "            print(state_abbr)\n",
    "            state_pharm_latlon_df = get_supp_pharmacy_latlon(state_abbr,\"\",verification, key)\n",
    "            national_pharm_latlon_data = national_pharm_latlon_data.append(state_pharm_latlon_df)\n",
    "        return national_pharm_latlon_data\n",
    "    else:\n",
    "        print('Problem encountered, not returning data:')\n",
    "        print('Either verification == False')\n",
    "        print('Or problem with API encountered, please verify URL, state and county are correct: ')\n",
    "        \n",
    "def get_national_pharmacy_tracts(us_abbr_list: list, verification: bool = False, key: str = 'WaPo'):\n",
    "    '''(string) -> pd.df\n",
    "        Returns all pharmacy census tracts from all states (Will be large and could take extra time to load) \n",
    "\n",
    "        >>>get_national_pharmacy_latlon(verification = True)\n",
    "            EXAMPLE OUTPUT\n",
    "    '''\n",
    "    national_pharm_tracts_df = pd.DataFrame()\n",
    "\n",
    "    if verification == True:\n",
    "        for state_abbr in us_abbr_list:\n",
    "            print(state_abbr)\n",
    "            state_pharm_tracts_df = get_supp_pharmacy_tracts(state_abbr,'', verification, key)\n",
    "            national_pharm_tracts_df = national_pharm_tracts_df.append(state_pharm_tracts_df)\n",
    "        return national_pharm_tracts_df\n",
    "    else:\n",
    "        print('Problem encountered, not returning data:')\n",
    "        print('Either verification == False')\n",
    "        print('Or problem with API encountered, please verify URL, state and county are correct: ')\n",
    "        \n",
    "        \n",
    "def national_data_with_geo(us_abbr_list, verification = False, key: str = 'WaPo'):\n",
    "    '''(string) -> geopandas.gdf\n",
    "        Returns all pharmacy data and latlon geometry (Will be large and could take extra time to load) \n",
    "\n",
    "        >>>get_national_pharmacy_latlon(verification = True)\n",
    "            EXAMPLE OUTPUT\n",
    "    '''\n",
    "    if verification == True:\n",
    "        national_data = get_national_pharmacy_data(us_abbr_list, verification, key)\n",
    "\n",
    "        national_pharm_latlon = get_national_pharmacy_latlon(us_abbr_list, \"\", verification, key)\n",
    "        national_pharm_latlon = national_pharm_latlon.rename(str.lower, axis='columns') #match case of other df\n",
    "\n",
    "        national_pharm_tracts = get_national_pharmacy_tracts(us_abbr_list, verification, key)\n",
    "        national_pharm_tracts = national_pharm_tracts.rename(str.lower, axis='columns') #match case of other df\n",
    "\n",
    "        national_pharm_locations = gpd.GeoDataFrame(national_pharm_latlon, geometry = gpd.points_from_xy(\n",
    "            national_pharm_latlon.lon, national_pharm_latlon.lat))\n",
    "\n",
    "        national_pharmacies = pd.merge(national_pharm_locations, national_data, how='left', on='buyer_dea_no')\n",
    "        national_pharmacies = pd.merge(national_pharmacies, national_pharm_tracts, how='left', on='buyer_dea_no')\n",
    "        \n",
    "        total_national_dosage = national_pharmacies.total_dosage_unit.to_list()\n",
    "        total_dosage = [float(i) for i in total_national_dosage]\n",
    "        national_pharmacies['total_dosage'] = np.array(total_dosage)\n",
    "\n",
    "        return national_pharmacies\n",
    "    elif verification == False:\n",
    "        print(\"Please verify this operation as it might take a long time to complete\")"
   ]
  }
 ],
 "metadata": {
  "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.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
