meteortools.ukmondb.trajPickle

 1# Copyright (C) 2018-2023 Mark McIntyre
 2import pickle
 3import tempfile
 4import pandas as pd
 5import os
 6# imports are required to load the picklefiles
 7try:
 8    from wmpl.Trajectory.CorrelateRMS import TrajectoryReduced, DatabaseJSON # noqa: F401
 9    from wmpl.Trajectory.CorrelateRMS import MeteorObsRMS, PlateparDummy, MeteorPointRMS # noqa: F401
10except:
11    print('WMPL not available')
12
13try:
14    from ..ukmondb.getLiveImages import _download
15except Exception:
16    from meteortools.ukmondb.getLiveImages import _download
17
18
19def getTrajPickle(trajname, outdir=None):
20    """ Retrieve a the pickled trajectory for a matched detection 
21    
22    Arguments:  
23        trajname:   [string] Name of the trajectory eg 20230502_025228.374_UK_BE
24        outdir:     [string] default None. Where to save the pickle. 
25
26    Returns:
27        WMPL traj object
28
29    Notes:
30        WMPL must be in the $PYTHONPATH for this function to be available
31
32    """
33    apiurl = 'https://api.ukmeteors.co.uk/pickle/getpickle'
34    fmt = 'pickle'
35    apicall = f'{apiurl}?reqval={trajname}&format={fmt}'
36    matchpickle = pd.read_json(apicall, lines=True)
37    if outdir is None:
38        outdir = tempfile.mkdtemp()
39        saveme = False
40    else:
41        saveme = True
42    if 'url' in matchpickle:
43        _download(matchpickle.url[0], outdir, matchpickle.filename[0])
44        localfile = os.path.join(outdir, matchpickle.filename[0])
45        with open(localfile, 'rb') as f:
46            traj = pickle.load(f, encoding='latin1')
47        if saveme is False:
48            try:
49                os.remove(localfile)
50            except:
51                print(f'unable to remove {localfile}')
52    else:
53        traj = None
54    return traj
def getTrajPickle(trajname, outdir=None):
20def getTrajPickle(trajname, outdir=None):
21    """ Retrieve a the pickled trajectory for a matched detection 
22    
23    Arguments:  
24        trajname:   [string] Name of the trajectory eg 20230502_025228.374_UK_BE
25        outdir:     [string] default None. Where to save the pickle. 
26
27    Returns:
28        WMPL traj object
29
30    Notes:
31        WMPL must be in the $PYTHONPATH for this function to be available
32
33    """
34    apiurl = 'https://api.ukmeteors.co.uk/pickle/getpickle'
35    fmt = 'pickle'
36    apicall = f'{apiurl}?reqval={trajname}&format={fmt}'
37    matchpickle = pd.read_json(apicall, lines=True)
38    if outdir is None:
39        outdir = tempfile.mkdtemp()
40        saveme = False
41    else:
42        saveme = True
43    if 'url' in matchpickle:
44        _download(matchpickle.url[0], outdir, matchpickle.filename[0])
45        localfile = os.path.join(outdir, matchpickle.filename[0])
46        with open(localfile, 'rb') as f:
47            traj = pickle.load(f, encoding='latin1')
48        if saveme is False:
49            try:
50                os.remove(localfile)
51            except:
52                print(f'unable to remove {localfile}')
53    else:
54        traj = None
55    return traj

Retrieve a the pickled trajectory for a matched detection

Arguments:
trajname: [string] Name of the trajectory eg 20230502_025228.374_UK_BE outdir: [string] default None. Where to save the pickle.

Returns: WMPL traj object

Notes: WMPL must be in the $PYTHONPATH for this function to be available