1 year ago
#237425
Balthus89
Reading memory through a Python script invoked by JUCE
I wrote a Python script which has the task to download a FITS file, in order to read the data of a star, through the exploitation of the library astropy
. Unfortunately, I can't write anything to the disk, because the returned pValue
is always NULL when my program try to access the memory on the disk.
This is the C++ code written with the JUCE Framework:
#pragma once
#include <Windows.h>
#include <iostream>
#include <JuceHeader.h>
#include <Python.h>
void myFunction()
{
Py_Initialize();
PyRun_SimpleString("import pandas as pd");
PyRun_SimpleString("from datetime import date");
PyRun_SimpleString("import time");
PyRun_SimpleString("import requests");
PyRun_SimpleString("import json");
PyRun_SimpleString("from astroquery.mast import Observations");
PyRun_SimpleString("from astropy.coordinates import EarthLocation, AltAz, SkyCoord");
PyRun_SimpleString("import astropy.units as u");
PyRun_SimpleString("import os");
PyRun_SimpleString("from astropy.io import fits");
PyRun_SimpleString("import importTry");
PyObject* pyName = PyUnicode_FromString("hello");
PyObject* pyModule = PyImport_Import(pyName);
if (pyModule != NULL)
{
PyObject* pyDict = PyModule_GetDict(pyModule);
PyObject* pyClass = PyDict_GetItemString(pyDict, "Hello");
if (PyCallable_Check(pyClass)) {
PyObject* pyInstance = PyObject_CallObject(pyClass, NULL);
PyObject* pValue = PyObject_CallMethod(pyInstance, "print_hello", NULL);
if (pValue == NULL)
{
DBG("ERROR");
}
else
{
auto result = _PyUnicode_AsString(pValue);
DBG(result);
}
}
}
Py_Finalize();
}
This is the Python script:
from datetime import date
import time
from typing import final
import requests
from astroquery.mast import Observations
from astropy.coordinates import EarthLocation, AltAz, SkyCoord
import astropy.units as u
from astropy.io import fits
import pandas as pd
import os
import json
import importTry
class Hello:
def __init__(self):
pass
def print_hello(self):
def getSkyCoord(ra, dec):
skyCoord = SkyCoord(ra=ra * u.deg, dec=dec * u.deg, representation_type='spherical')
return skyCoord
def getAltAz(skyCoord, observerTime, location):
altAz = skyCoord.transform_to(AltAz(obstime=observerTime, location=location))
return altAz
def getAltAngle(altAz):
alt = (altAz.alt * u.deg).value
return alt
def getAzAngle(altAz):
az = (altAz.az * u.deg).value
return az
def getConstellation(name_flamsteed_full):
name_split = str(name_flamsteed_full).split('-')
constellation = name_split[1]
return constellation
db = importTry.data
bright_stars = pd.DataFrame.from_dict(db, orient='index')
# Getting the current date
today = date.today()
theTime = time.strftime("%H:%M:%S")
observerTime = str(today) + " " + str(theTime)
# Getting the current location. It is necessary to be connected to
# the internet
location = requests.get('http://ipinfo.io/json').json()
location = EarthLocation.of_address(location['city'] + ", " + location['region'])
bright_stars['SkyCoord'] = bright_stars.apply(lambda row : getSkyCoord(row['ra'], row['dec']), axis=1)
bright_stars['AltAz'] = bright_stars.apply(lambda row: getAltAz(row['SkyCoord'], observerTime, location), axis=1)
bright_stars['Altitude'] = bright_stars.apply(lambda row: getAltAngle(row['AltAz']), axis=1)
bright_stars['Azimuth'] = bright_stars.apply(lambda row: getAzAngle(row['AltAz']), axis=1)
bright_stars = bright_stars.drop(['SkyCoord', 'AltAz'], axis=1)
bright_stars['Constellation'] = bright_stars.apply(lambda row: getConstellation(row['name_flamsteed_full']), axis=1)
bright_stars = bright_stars[bright_stars['Constellation'] != '']
bright_stars = bright_stars.reset_index(drop=True)
visible_stars = bright_stars[bright_stars['Altitude'] >= 0]
not_visible_stars = bright_stars[bright_stars['Altitude'] < 0]
camelopardalis = visible_stars[visible_stars["name_flamsteed_full"].str.contains("Cam", na=False)]
a_star = camelopardalis.sample()
skyCoord = SkyCoord(ra=float(a_star['ra']) * u.deg, dec=float(a_star['dec']) * u.deg, representation_type='spherical')
radius = 0.5 * u.arcminute
obs_object = Observations.query_object(skyCoord.to_string(), radius=radius)
timeseries_df = obs_object[obs_object['dataproduct_type'] == 'timeseries']
filename = timeseries_df[0]['dataURL'].split('/')[2]
url_start = 'https://mast.stsci.edu/api/v0.1/Download/file?uri=mast:TESS/product/'
url_complete = url_start + filename
data = fits.open(name=url_complete, mode='denywrite', cache=False
return url_complete
When I get to the line data = fits.open(name=url_complete, mode='denywrite', cache=False)
, I get an error. I suspect that the problem resides in the fact that I can't perform any I/O operations when I launch a Python script from JUCE. Any suggestion on how to manage this situation?
python
astropy
fits
juce
0 Answers
Your Answer