diff --git a/examples/Apollo12_soils.py b/examples/Apollo12_soils.py index ddc0da45e2ffc78b5197c4536d07ed2d6b445fa6..e7c2faa4b321b6298d521b1576452c77dd770262 100644 --- a/examples/Apollo12_soils.py +++ b/examples/Apollo12_soils.py @@ -1,6 +1,6 @@ -import moondb,sys - +#!/usr/bin/env python3 +import moondb,sys # Let's setup the data filter f = moondb.AnalysisFilter() @@ -11,8 +11,10 @@ f.specimenType = ["SOIL"] results = f.get_results() for r in results: + print('\nDataset: ',r.dataset) + print('Lab,Specie,value') for dr in r.dataResults: - print(dr.laboratory,dr.variable,dr.value,dr.unit) + print(",".join([dr.laboratory,dr.variable,dr.value+dr.unit])) diff --git a/examples/example1.ipynb b/examples/example1.ipynb index d3c69faa90b890b6d56353c01e573754efa92c53..c30e54f3037c45ee7ad73ef29d3a5e0d16ce5084 100644 --- a/examples/example1.ipynb +++ b/examples/example1.ipynb @@ -1,111 +1,246 @@ { "cells": [ { - "cell_type": "code", - "execution_count": 9, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "import moondb" + "## MoonDB Jupyter notebook workbook\n", + "#### Alessandro Frigeri, IAPS/INAF, Rome Italy\n", + "\n", + "\n" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'name': 'Apollo 11'}\n", - "{'name': 'Apollo 12'}\n", - "{'name': 'Apollo 14'}\n", - "{'name': 'Apollo 15'}\n", - "{'name': 'Apollo 16'}\n", - "{'name': 'Apollo 17'}\n", - "{'name': 'Luna 16'}\n", - "{'name': 'Luna 20'}\n", - "{'name': 'Luna 24'}\n" + "The autoreload extension is already loaded. To reload it, use:\n", + " %reload_ext autoreload\n", + "/home/alf/gitwrk/pymoondb/moondb/__init__.py\n" ] } ], "source": [ - "m = moondb.get_missions()" + "# First we import the moondb module\n", + "%load_ext autoreload\n", + "%autoreload 2\n", + "\n", + "%aimport moondb\n", + "\n", + "print(moondb.__file__)" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 105, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Apollo 11',\n", + " 'Apollo 12',\n", + " 'Apollo 14',\n", + " 'Apollo 15',\n", + " 'Apollo 16',\n", + " 'Apollo 17',\n", + " 'Luna 16',\n", + " 'Luna 20',\n", + " 'Luna 24']" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "f = moondb.Filter()\n", - "#f.specimenType = [\"SOIL\"]\n", - "results = f.get_results()" + "# Let's get a list of the missions in MoonDB\n", + "\n", + "mlist = moondb.get_missions()\n", + "\n", + "[m.name for m in mlist]" ] }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MoonDB holds 2991 specimens from Apollo 17.\n" + ] + } + ], + "source": [ + "# Now, we want to know how many specimens are available from the Apollo 17 missions\n", + "\n", + "f = moondb.SpecimenFilter()\n", + "mission = 'Apollo 17'\n", + "f.missionName = [ mission ]\n", + "results = f.get_results()\n", + "specimen_list = [r for r in results]\n", + "\n", + "print(\"MoonDB holds {} specimens from Apollo 17.\".format(len(specimen_list)))" + ] + }, + { + "cell_type": "code", + "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "37034" + "['COMPOSITE',\n", + " 'ROCK',\n", + " 'ROCK/Basalt/Ilmenite Basalt',\n", + " 'ROCK/Basalt/Unclassified Basalt',\n", + " 'ROCK/Breccia/Fragmental Breccia',\n", + " 'ROCK/Breccia/Impact Melt Breccia',\n", + " 'ROCK/Breccia/Regolith Breccia',\n", + " 'ROCK/Breccia/Unclassified Breccia',\n", + " 'ROCK/Crustal/Crustal:Cataclasite',\n", + " 'ROCK/Crustal/Crustal:Norite',\n", + " 'ROCK/Crustal/Crustal:Troctolite',\n", + " 'SOIL/Soil:1-2mm',\n", + " 'SOIL/Soil:2-4mm',\n", + " 'SOIL/Soil:4-10mm',\n", + " 'SOIL/Soil:<1mm',\n", + " 'SOIL/Soil:Unsieved',\n", + " 'Unknown']" ] }, - "execution_count": 34, + "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "len(results)" + "# Get a list of type of specimen of Apollo 17 available in MoonDB\n", + "s_list = [s.specimenType for s in specimen_list]\n", + "list(sorted(set(s_list)))" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'analysisCode': '15100,3#3#0158', 'analyzedMaterial': 'WHOLE ROCK', 'comment': '0.97 g, 950C', 'dataset': 'IRVING, 1972#TABLE 1', 'citation': 'IRVING, 1972', 'dataResults': [{'unit': 'ppm', 'laboratory': 'UNKNOWN', 'variable': 'H', 'methodName': 'UNKNOWN', 'methodComment': 'COMBUSTION (NEW LINE, YIELDING LOW BLANKS)', 'value': '90.0', 'methodCode': 'UNKNOWN'}, {'unit': 'per mil', 'laboratory': 'UNKNOWN', 'variable': 'DELTA_D', 'methodName': 'UNKNOWN', 'methodComment': 'COMBUSTION (NEW LINE, YIELDING LOW BLANKS)', 'value': '-554.0', 'methodCode': 'UNKNOWN'}, {'unit': 'ppm', 'laboratory': 'UNKNOWN', 'variable': 'C', 'methodName': 'UNKNOWN', 'methodComment': 'COMBUSTION (NEW LINE, YIELDING LOW BLANKS)', 'value': '99.0', 'methodCode': 'UNKNOWN'}, {'unit': 'per mil', 'laboratory': 'UNKNOWN', 'variable': 'DELTA_C13', 'methodName': 'UNKNOWN', 'methodComment': 'COMBUSTION (NEW LINE, YIELDING LOW BLANKS)', 'value': '7.9', 'methodCode': 'UNKNOWN'}]}\n" + "2991\n" ] } ], "source": [ - "an = print(results[0])" + "#\n", + "# Let's see the analyses done on the first specimen\n", + "#\n", + "\n", + "s_list = [s for s in specimen_list]\n", + "\n", + "print(len(s_list))\n", + "\n", + "s0 = s_list[0]\n", + "\n", + "an_list = [a for a in s0.get_analyses()]" ] }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 109, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY (code MC-ICP-MS), result of CACULATED (code CALC)]\n" + ] + } + ], + "source": [ + "# now we check the results of the first analysis\n", + "\n", + "#result_of\n", + "\n", + "print(an_list[0].dataResultsObj)" + ] }, { "cell_type": "code", - "execution_count": 32, - "metadata": {}, + "execution_count": 103, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "None\n" + "Apollo 17 specimen: 76535,16\n", + "Result: Os 8.5 ppb (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: Ir 55.0 ppb (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: Ru 81.0 ppb (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: Pt 54.0 ppb (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: Pd 89.0 ppb (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: Re 9.0 ppb (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: Re187_Os188 5.1 Not Applicable (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: Os187_Os188 0.1511 Not Applicable (MULTICOLLECTOR INDUCTIVELY COUPLED PLASMA MASS SPECTROMETRY)\n", + "Result: G_Os(T) -348.0 Not Applicable (CACULATED)\n" ] } ], "source": [ - "print(anaobj)" + "\n", + "\n", + "\n", + "print(mission+\" specimen: \",s0.specimenName)\n", + "for r in an_list[0].dataResultsObj:\n", + " print('Result: {} {} {} ({})'.format(r.variable,r.value,r.unit,r.methodName))" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -130,7 +265,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/examples/specimen.py b/examples/specimen.py old mode 100644 new mode 100755 index 9aa96f178909c774e869a0cf297ad437b2b958a6..d295d5f3d65dd1710c408f31b650666c8cef8618 --- a/examples/specimen.py +++ b/examples/specimen.py @@ -1,4 +1,22 @@ +#!/usr/bin/env python3 + import moondb s_12023 = moondb.get_specimens(sc=['12023'])[0] +#print(s_12023) + +child = moondb.get_specimens(sc=['12023,114'])[0] + +#print(' A N A L Y S E S ') +a = child.get_analyses() + + +print(a[0]) + +a0_results = a[0].dataResultsObj + + +print('Result: {} {} {}'.format(a0_results[0].variable,a0_results[0].value,a0_results[0].unit,a0_results[0].methodName)) + + diff --git a/examples/specimens_weight.py b/examples/total_specimens_weight.py similarity index 55% rename from examples/specimens_weight.py rename to examples/total_specimens_weight.py index 7e446da5f975cf3b402dbaa7856b183353bbdac8..8acd8140eb16acd2fc43bce4f51dc84052f9db0b 100644 --- a/examples/specimens_weight.py +++ b/examples/total_specimens_weight.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import moondb,sys @@ -16,22 +18,7 @@ for m in moon_missions: weight_cum += weight print("MoonDB holds {:.3f} kg of specimens from {}".format(weight/1000.0,m.name)) -print("MoonDB contains a total of {:.3f} kg of specimen from the Moon!".format(weight_cum/1000.0)) - - -f = moondb.AnalysisFilter() -f.mission = ["Apollo 11"] -f.analyte = ["Na2O","CaO"] -f.specimenType = ["SOIL"] - - - -results = f.get_results() +print("\nMoonDB contains a total of {:.3f} kg of specimen from the Moon!".format(weight_cum/1000.0)) -for r in results: - for dr in r.dataResults: - print(dr) - #print(dr.laboratory,dr.variable,dr.value,dr.unit) - diff --git a/moondb/__pycache__/__init__.cpython-37.pyc b/moondb/__pycache__/__init__.cpython-37.pyc index 4ec4ae538fe0135d4b4557ef750fe953cfffc882..adf878aed3b8987b69e4ea9b800bebce4a87163d 100644 Binary files a/moondb/__pycache__/__init__.cpython-37.pyc and b/moondb/__pycache__/__init__.cpython-37.pyc differ diff --git a/moondb/__pycache__/core.cpython-37.pyc b/moondb/__pycache__/core.cpython-37.pyc index 563654845ade61e7230a3d4fbc4a3b2f8ef6331a..96bb62901a8a7c6de0f215953d4f30ea33a80d2e 100644 Binary files a/moondb/__pycache__/core.cpython-37.pyc and b/moondb/__pycache__/core.cpython-37.pyc differ diff --git a/moondb/__pycache__/helpers.cpython-37.pyc b/moondb/__pycache__/helpers.cpython-37.pyc index c7e04cde9e47a478e03dff61959bf3d407710eb1..9c6fa48130fde75483ad9a95d797f6f6d62d5f4d 100644 Binary files a/moondb/__pycache__/helpers.cpython-37.pyc and b/moondb/__pycache__/helpers.cpython-37.pyc differ diff --git a/moondb/core.py b/moondb/core.py index 8083d633199e598b4911a488202d3fd74f2e06c8..9272b71362ffb5519adf2d60bb9e8e4eb931588f 100644 --- a/moondb/core.py +++ b/moondb/core.py @@ -2,17 +2,19 @@ # # (c) 2019 Alessandro Frigeri, Istituto Nazionale di Astrofisica # -# MoonDB Python module -# https://realpython.com/api-integration-in-python/ +# pymoondb: MoonDB Python module + import sys import json import urllib.parse import urllib3,socket import logging -from dataclasses import dataclass +from dataclasses import dataclass, field from collections import namedtuple +#from dataclasses import dataclass, field + import requests #import attr @@ -35,6 +37,35 @@ class Landmark: point = "POINT ({} {})" return point.format(self.longitude,self.latitude) +@dataclass +class Result: + unit: str + laboratory: str + variable: str + methodName: str + methodComment:str + value: str + methodCode: str + + def __repr__(self): + return "result of "+self.methodName+" (code "+self.methodCode+")" + +@dataclass +class Analysis: + analysisCode: str + analyzedMaterial: str + comment: str + dataset: str + citation: str + dataResults: list = field(default_factory=list) + dataResultsObj: list = field(default_factory=list) + + def __repr__(self): + return "Analysis:"+self.analysisCode+" on "+self.analyzedMaterial+" material" + + def __str__(self): + return self.analysisCode+" on "+self.analyzedMaterial + @dataclass class Specimen: specimenCode: str @@ -52,6 +83,28 @@ class Specimen: pristinityDate: str description: str + def get_analyses(self): + an_obj_list = [] + n,an_list = _get_resp('/data/specimen/'+self.specimenCode) + for a in an_list: + r_o_list = [] + for r in a['dataResults']: + r_o = Result(**r) + r_o_list.append(r_o) + #print(r_o) + a['dataResultsObj'] = r_o_list + an_o = Analysis(**a) + an_obj_list.append(an_o) + return an_obj_list + + + + + + +@dataclass +class People: + name: str class SpecimenType: def __init__(self,name): @@ -99,8 +152,8 @@ class AnalyisMethod: def __init__(self,name): self.name = name -class Analysis: - __slots__ = ('analysisCode','analyzedMaterial','comment','dataset','citation','dataResults') +#class Analysis: +# __slots__ = ('analysisCode','analyzedMaterial','comment','dataset','citation','dataResults') class DataResult: __slots__ = ('unit', 'laboratory', 'variable', 'methodName', 'methodComment', 'value', 'methodCode') @@ -199,7 +252,6 @@ def get_specimens(sc=None,mn=None,ln=None,sty=None,ste=None): for s in sp_list: # dict unpack - # print(s) s_o = Specimen(**s) sp_obj_list.append(s_o)