Skip to content

API utilities

Functions

convertJD2000(dt)

Convert julianday (seconds) to datetime.

Parameters:

Name Type Description Default
dt float

julianday (seconds)

required

Returns:

Type Description
datetime

datetime of julianday

Source code in leocdf/utilities/utilities.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def convertJD2000(dt: float) -> datetime:
    '''Convert julianday (seconds) to datetime.

    Parameters
    ----------
    dt : float
        julianday (seconds)

    Returns
    -------
    datetime
        datetime of julianday
    '''
    julianday = datetime(2000,1,1).replace(tzinfo=timezone.utc)
    return julianday + timedelta(days = dt) 

mjd2000(dt)

Calculates julianday (seconds) of the specified datetime

Parameters:

Name Type Description Default
dt datetime

Datetime to be converted.

required

Returns:

Type Description
float

Seconds since 01/01/2000.

Source code in leocdf/utilities/utilities.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def mjd2000(dt: datetime) -> float:
    '''Calculates julianday (seconds) of the specified datetime

    Parameters
    ----------
    dt : datetime
        Datetime to be converted.

    Returns
    -------
    float
        Seconds since 01/01/2000.
    '''

    julianday = datetime(2000,1,1).replace(tzinfo=timezone.utc)
    return (dt-julianday).total_seconds()/60/60/24

questionoverwrite(file)

Interactive request for path.

Parameters:

Name Type Description Default
file Path | str

Path that already exists.

required

Returns:

Type Description
Path

New Path that not exists.

Source code in leocdf/utilities/utilities.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def questionoverwrite(file: Path | str) -> Path:
    '''Interactive request for path.

    Parameters
    ----------
    file : Path | str
        Path that already exists.

    Returns
    -------
    Path
        New Path that not exists.
    '''
    while True:
        user_input = input(f"The file '{file}' already exists. Should it be overwritten? (yes/no): ")
        if user_input.lower() in ["yes", "y"]:
            file.unlink()
            print("Continuing...")
            break
        elif user_input.lower() in ["no", "n"]:
            filepath = Path(file).resolve().parents[0]
            file = input("New file name:" )
            file = filepath/file
            if not file.exists():
                print(f"New filename {file} is set.")
                break
        else:
            print("Invalid input. Please enter yes/no.")

    return file.name       

readJson(fileJson)

Reads JSON file.

Parameters:

Name Type Description Default
fileJson Path | str

File name to be read.

required

Returns:

Type Description
dict

Data from JSON file.

Source code in leocdf/utilities/utilities.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def readJson(fileJson: Path | str) -> dict:
    '''Reads JSON file.

    Parameters
    ----------
    fileJson : Path | str
        File name to be read.

    Returns
    -------
    dict
        Data from JSON file.
    '''
    try:
        with open(fileJson, 'r') as jsonfile:
            dataJson = json.load(jsonfile)
        return dataJson

    except FileNotFoundError:
        print(f"No such file: {fileJson}")

    except Exception as err:
        print(err)

Checks