def set(self, arg=None): """Set wcs from metacode stream""" init_wcs_sizes() if arg is None: # commit immediately if arg=None self.wcs = _setWCSDefault() self.pending = None # print "Default WCS set for plotting window." return # Even in v2.14, arg[] elements are of type int64, but here we cast to # int16 and assume we lose no data wcsStruct = arg[1:].astype(numpy.int16) # Every time set() is called, reset the wcs sizes. We may be plotting # with old-compiled 32-bit tasks, then with new-compiled 32-bit tasks, # then with 64-bit tasks, all within the same PyRAF session. init_wcs_sizes(forceResetTo=int(arg[0] / (1. * WCS_SLOTS))) # Check that eveything is sized as expected if arg[0] != len(wcsStruct): raise IrafError("Inconsistency in length of WCS graphics struct: "+\ str(arg[0])) if len(wcsStruct) != _WCS_RECORD_SIZE * WCS_SLOTS: raise IrafError("Unexpected length of WCS graphics struct: "+\ str(len(wcsStruct))) # Read through the input to populate self.pending SZ = 2 if _IRAF64BIT: SZ = 4 self.pending = [None] * WCS_SLOTS for i in xrange(WCS_SLOTS): record = wcsStruct[_WCS_RECORD_SIZE * i:_WCS_RECORD_SIZE * (i + 1)] # read 8 4-byte floats from beginning of record fvals = numpy.fromstring(ndarr2bytes(record[:8 * SZ]), numpy.float32) if _IRAF64BIT: # seems to send an extra 0-valued int32 after each 4 bytes fvalsView = fvals.reshape(-1, 2).transpose() if fvalsView[1].sum() != 0: raise IrafError("Assumed WCS float padding is non-zero") fvals = fvalsView[0] # read 3 4-byte ints after that ivals = numpy.fromstring(ndarr2bytes(record[8 * SZ:11 * SZ]), numpy.int32) if _IRAF64BIT: # seems to send an extra 0-valued int32 after each 4 bytes ivalsView = ivals.reshape(-1, 2).transpose() if ivalsView[1].sum() != 0: raise IrafError("Assumed WCS int padding is non-zero") ivals = ivalsView[0] self.pending[i] = tuple(fvals) + tuple(ivals) if len(self.pending[i]) != 11: raise IrafError("Unexpected WCS struct record length: "+\ str(len(self.pending[i]))) if self.wcs is None: self.commit()
def set(self, arg=None): """Set wcs from metacode stream""" init_wcs_sizes() if arg is None: # commit immediately if arg=None self.wcs = _setWCSDefault() self.pending = None # print "Default WCS set for plotting window." return # Even in v2.14, arg[] elements are of type int64, but here we cast to # int16 and assume we lose no data wcsStruct = arg[1:].astype(numpy.int16) # Every time set() is called, reset the wcs sizes. We may be plotting # with old-compiled 32-bit tasks, then with new-compiled 32-bit tasks, # then with 64-bit tasks, all within the same PyRAF session. init_wcs_sizes(forceResetTo=int(arg[0]/(1.*WCS_SLOTS))) # Check that eveything is sized as expected if arg[0] != len(wcsStruct): raise IrafError("Inconsistency in length of WCS graphics struct: "+\ str(arg[0])) if len(wcsStruct) != _WCS_RECORD_SIZE*WCS_SLOTS: raise IrafError("Unexpected length of WCS graphics struct: "+\ str(len(wcsStruct))) # Read through the input to populate self.pending SZ = 2 if _IRAF64BIT: SZ = 4 self.pending = [None]*WCS_SLOTS for i in xrange(WCS_SLOTS): record = wcsStruct[_WCS_RECORD_SIZE*i:_WCS_RECORD_SIZE*(i+1)] # read 8 4-byte floats from beginning of record fvals = numpy.fromstring(ndarr2bytes(record[:8*SZ]),numpy.float32) if _IRAF64BIT: # seems to send an extra 0-valued int32 after each 4 bytes fvalsView = fvals.reshape(-1,2).transpose() if fvalsView[1].sum() != 0: raise IrafError("Assumed WCS float padding is non-zero") fvals = fvalsView[0] # read 3 4-byte ints after that ivals = numpy.fromstring(ndarr2bytes(record[8*SZ:11*SZ]),numpy.int32) if _IRAF64BIT: # seems to send an extra 0-valued int32 after each 4 bytes ivalsView = ivals.reshape(-1,2).transpose() if ivalsView[1].sum() != 0: raise IrafError("Assumed WCS int padding is non-zero") ivals = ivalsView[0] self.pending[i] = tuple(fvals) + tuple(ivals) if len(self.pending[i]) != 11: raise IrafError("Unexpected WCS struct record length: "+\ str(len(self.pending[i]))) if self.wcs is None: self.commit()
def flush(self): # grab last part of buffer and delete it metacode = ndarr2bytes(self.gkibuffer.delget()) # only plot if buffer contains something if metacode: # write to a temporary file tmpfn = iraf.mktemp("iraf") + ".gki" fout = open(tmpfn,'wb') fout.write(metacode) fout.close() try: if self.taskname == "stdgraph": # this is to allow users to specify via the # stdgraph device parameter the device they really # want to display to device = iraf.stdgraph.device else: device = self.device #XXX In principle we could read from Stdin by #XXX wrapping the string in a StringIO buffer instead of #XXX writing it to a temporary file. But that will not #XXX work until binary redirection is implemented in #XXX irafexecute #XXX task(Stdin=tmpfn,device=device,generic="yes") # Explicitly set input to sys.__stdin__ to avoid possible # problems with redirection. Sometimes graphics kernel tries # to read from stdin if it is not the default stdin. self.task(tmpfn,device=device,generic="yes",Stdin=sys.__stdin__) finally: os.remove(tmpfn)
def _writeHeader(self, tid, subunit, thingct, x, y, z, t): """Write request to image display""" a = numpy.array([tid, thingct, subunit, 0, x, y, z, t], numpy.int16) # Compute the checksum sum = numpy.add.reduce(a) sum = 0xffff - (sum & 0xffff) a[3] = sum self._write(ndarr2bytes(a))
def _writeHeader(self,tid,subunit,thingct,x,y,z,t): """Write request to image display""" a = numpy.array([tid,thingct,subunit,0,x,y,z,t], numpy.int16) # Compute the checksum sum = numpy.add.reduce(a) sum = 0xffff - (sum & 0xffff) a[3] = sum self._write(ndarr2bytes(a))
def pack(self): """Return the WCS in the original IRAF format (in bytes-string)""" init_wcs_sizes() self.commit() wcsStruct = numpy.zeros(_WCS_RECORD_SIZE * WCS_SLOTS, numpy.int16) pad = tobytes('\x00\x00\x00\x00') if _IRAF64BIT: pad = tobytes('\x00\x00\x00\x00\x00\x00\x00\x00') for i in xrange(WCS_SLOTS): x = self.wcs[i] farr = numpy.array(x[:8], numpy.float32) iarr = numpy.array(x[8:11], numpy.int32) if _IRAF64BIT: # see notes in set(); adding 0-padding after every data point lenf = len(farr) # should be 8 farr_rs = farr.reshape(lenf, 1) # turn array into single column farr = numpy.append(farr_rs, numpy.zeros((lenf, 1), numpy.float32), axis=1) farr = farr.flatten() leni = len(iarr) # should be 3 iarr_rs = iarr.reshape(leni, 1) # turn array into single column iarr = numpy.append(iarr_rs, numpy.zeros((leni, 1), numpy.int32), axis=1) iarr = iarr.flatten() # end-pad? if len(farr) + len(iarr) == (_WCS_RECORD_SIZE // 2): pad = BNULLSTR #for IRAF2.14 or prior; all new vers need end-pad # Pack the wcsStruct - this will throw "ValueError: shape mismatch" # if the padding doesn't bring the size out to exactly the # correct length (_WCS_RECORD_SIZE) wcsStruct[_WCS_RECORD_SIZE*i:_WCS_RECORD_SIZE*(i+1)] = \ numpy.fromstring(ndarr2bytes(farr)+ndarr2bytes(iarr)+pad, numpy.int16) return ndarr2bytes(wcsStruct)
def pack(self): """Return the WCS in the original IRAF format (in bytes-string)""" init_wcs_sizes() self.commit() wcsStruct = numpy.zeros(_WCS_RECORD_SIZE*WCS_SLOTS, numpy.int16) pad = tobytes('\x00\x00\x00\x00') if _IRAF64BIT: pad = tobytes('\x00\x00\x00\x00\x00\x00\x00\x00') for i in xrange(WCS_SLOTS): x = self.wcs[i] farr = numpy.array(x[:8],numpy.float32) iarr = numpy.array(x[8:11],numpy.int32) if _IRAF64BIT: # see notes in set(); adding 0-padding after every data point lenf = len(farr) # should be 8 farr_rs = farr.reshape(lenf,1) # turn array into single column farr = numpy.append(farr_rs, numpy.zeros((lenf,1), numpy.float32), axis=1) farr = farr.flatten() leni = len(iarr) # should be 3 iarr_rs = iarr.reshape(leni,1) # turn array into single column iarr = numpy.append(iarr_rs, numpy.zeros((leni,1), numpy.int32), axis=1) iarr = iarr.flatten() # end-pad? if len(farr)+len(iarr) == (_WCS_RECORD_SIZE//2): pad = BNULLSTR #for IRAF2.14 or prior; all new vers need end-pad # Pack the wcsStruct - this will throw "ValueError: shape mismatch" # if the padding doesn't bring the size out to exactly the # correct length (_WCS_RECORD_SIZE) wcsStruct[_WCS_RECORD_SIZE*i:_WCS_RECORD_SIZE*(i+1)] = \ numpy.fromstring(ndarr2bytes(farr)+ndarr2bytes(iarr)+pad, numpy.int16) return ndarr2bytes(wcsStruct)
def save(self): """Save metacode in a file""" curdir = os.getcwd() if capable.OF_TKFD_IN_EPAR: fname = tkFileDialog.asksaveasfilename(parent=self.top, title="Save Metacode") else: fd = filedlg.PersistSaveFileDialog(self.top, "Save Metacode", "*") if fd.Show() != 1: fd.DialogCleanup() os.chdir(curdir) # in case file dlg moved us return fname = fd.GetFileName() fd.DialogCleanup() os.chdir(curdir) # in case file dlg moved us if not fname: return fh = open(fname, 'wb') fh.write(ndarr2bytes(self.gkibuffer.get())) fh.close()
def flush(self): # grab last part of buffer and delete it metacode = ndarr2bytes(self.gkibuffer.delget()) # only plot if buffer contains something if metacode: # write to a temporary file tmpfn = iraf.mktemp("iraf") + ".gki" fout = open(tmpfn, 'wb') fout.write(metacode) fout.close() try: if self.taskname == "stdgraph": # this is to allow users to specify via the # stdgraph device parameter the device they really # want to display to device = iraf.stdgraph.device else: device = self.device #XXX In principle we could read from Stdin by #XXX wrapping the string in a StringIO buffer instead of #XXX writing it to a temporary file. But that will not #XXX work until binary redirection is implemented in #XXX irafexecute #XXX task(Stdin=tmpfn,device=device,generic="yes") # Explicitly set input to sys.__stdin__ to avoid possible # problems with redirection. Sometimes graphics kernel tries # to read from stdin if it is not the default stdin. self.task(tmpfn, device=device, generic="yes", Stdin=sys.__stdin__) finally: os.remove(tmpfn)
def Asc2IrafString(ascii_string): """translate ascii to IRAF 16-bit string format""" inarr = numpy.fromstring(ascii_string, numpy.int8) # OK if str or uni retval = ndarr2bytes(inarr.astype(numpy.int16)) # log_task_comm('Asc2IrafString (write to task)', retval, False) return retval
import pyraf.iraf # test_probe is a flag that a testing system can use to tell pyraf # (when used as a library, e.g. in stsci_regtest) to print various # diagnostic information that we think might be useful in test logs. # This is different from verbose, because it is more selective. # # There is no interface to activate this feature. Use: # import pyraf.irafexecute # pyraf.irafexecute.test_probe = True test_probe = False #stdgraph = None IPC_PREFIX = ndarr2bytes(numpy.array([01120],numpy.int16)) # weirdo protocol to get output from task back to subprocess # definitions from cl/task.h and lib/clio.h IPCOUT = "IPC$IPCIO-OUT" IPCDONEMSG = "# IPC$IPCIO-FINISHED\n" # set flag indicating big endian or little endian byte order # sys.byteorder was added in Python 2.0 isBigEndian = sys.byteorder == "big" # Create an instance of the stdimage kernel stdimagekernel = gki.GkiController() class IrafProcessError(Exception): def __init__(self, msg, errno=-1, errmsg="", errtask=""):
def append(self, metacode): # Overloads the baseclass implementation. # metacode is array of 16-bit ints self.filehandle.write(ndarr2bytes(metacode))