Hi Paul,
Following a discussion with Willem I am turning to you with a bug in PSRCHIVE Python interface. When trying to convert state of an archive from Coherence to Stokes with pam -s I get the correct behaviour, i.e. file is ingested, converted and written out. With the same process in python I am getting the following error on my Mac (Big Sur, Python (Homebrew version) 3.9.2, swig 4.0.2 (Homebrew), gcc 10.2.0 (Homebrew)):
m.serylak@Mac [22 Mar 2021 10:04] ~/> pam -S -e ar.S j0955.ar
j0955.ar.S written to disk
m.serylak@Mac [22 Mar 2021 10:04] ~/> ipython
Python 3.9.2 (default, Mar 15 2021, 17:37:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.21.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import psrchive
In [2]: f = psrchive.Archive_load('j0955.ar')
In [3]: f.convert_state('Stokes')
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Abort trap: 6
I have also tried it on my linux box (Ubuntu 18.04, python 3.6.9, swig 3.0.12-1, gcc 7.5.0):
~~~
m.serylak@Box [22 Mar 2021 10:02] ~> pam -S -e ar.S j0955.ar
j0955.ar.S written to disk
m.serylak@Box [22 Mar 2021 10:02] ~> ipython
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import psrchive
In [2]: f = psrchive.Archive_load('j0955.ar')
In [3]: f.convert_state('Stokes')
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (core dumped)
Googling around did not give me clear answers. There was something about char* returning NULLs etc. (e.g. here https://stackoverflow.com/questions/56552148/how-to-fix-terminate-called-after-throwing-an-instance-of-stdlogic-error-wha). But I could not find such behaviour in the code... I am not sure what the problem could it be.
This must have changed recently as I have scripts which use convert_state and they did work well before... Not sure if this is compiler related.
Also, double checking now, it turns out that the same behaviour will happen if one tries to convert back from Stokes to Coherency. As a matter of fact it affects those as well:
`set_scale()` and `set_state()`. Looks like something to do with passing strings back to C++. But `psrchive.i` has those definitions set correctly I think:
// Convert various enums to/from string
%define %map_enum(TYPE)
%typemap(out) Signal:: ## TYPE {
$result = PyString_FromString( TYPE ## 2string($1).c_str());
}
%typemap(in) Signal:: ## TYPE {
try {
$1 = Signal::string2 ## TYPE (PyString_AsString($input));
} catch (Error &error) {
SWIG_exception(SWIG_RuntimeError,error.get_message().c_str());
}
}
%enddef
%map_enum(State)
%map_enum(Basis)
%map_enum(Scale)
%map_enum(Source)
~~~
I attach the file I used for the above examples. Hope this will give you enough info to get to the bottom of this!
Best regards,
Maciej
Hi Maciej, I can reproduce this on my system. You're right that it used to work, I'll see if I can track it down. In the meantime, if you need a workaround, you can access psrsh commands from python using
execute()ief.execute('state Stokes').OK, this is a python3 "string vs bytes" issue. If you do
f.convert_state(b'Stokes')that also appears to work. I'll see if I can update the wrapper to handle this automatically.I think this is fixed in commit 29373ef4d. Can you give it a try?
Hi Paul, thank you for prompt reply. I tested it on both systems. Functionality restored! Let's close this one.
Diff: