Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

own implementation file for DS9 integration (compatibility) #232

Merged
merged 1 commit into from
Feb 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ HEADERS = lensed.h \
path.h \
profile.h \
log.h \
ds9.h \
input/objects.h \
input/options.h \
input/ini.h \
Expand All @@ -104,6 +105,7 @@ SOURCES = lensed.c \
path.c \
profile.c \
log.c \
ds9.c \
input/objects.c \
input/options.c \
input/ini.c \
Expand Down
123 changes: 123 additions & 0 deletions src/ds9.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// provide POSIX standard in strict C99 mode
#define _XOPEN_SOURCE 600

#include <stdlib.h>

#ifdef LENSED_XPA

#include <sys/select.h>
#include "xpa.h"

#include "ds9.h"
#include "log.h"

struct ds9_conn
{
void* handle;
char* tmpl;
int frame;
};

void* ds9_connect(char* tmpl)
{
// XPA connection handle
void* handle;

// XPA response
char* buf = NULL;
size_t len = 0;

// create persistent XPA handle
handle = XPAOpen(NULL);
if(!handle)
return NULL;

// new DS9 connection object
struct ds9_conn* ds9 = malloc(sizeof(struct ds9_conn));
if(!ds9)
errori(NULL);

// store handle and template
ds9->handle = handle;
ds9->tmpl = tmpl;

// create frame for images
XPASet(handle, tmpl, "frame new", NULL, NULL, 0, NULL, NULL, 1);

// get newly created frame
XPAGet(handle, tmpl, "frame", NULL, &buf, &len, NULL, NULL, 1);
if(buf)
ds9->frame = atoi(buf);
else
ds9->frame = 999;

// free response buffer
free(buf);

// return DS9 connection
return ds9;
}

void ds9_disconnect(void* p)
{
struct ds9_conn* ds9 = p;
XPAClose(ds9->handle);
free(ds9);
}

const char* ds9_template(void* p)
{
struct ds9_conn* ds9 = p;
return ds9->tmpl;
}

int ds9_frame(void* p)
{
struct ds9_conn* ds9 = p;
return ds9->frame;
}

void ds9_mecube(void* p, void* fits, size_t len)
{
struct ds9_conn* ds9 = p;

// buffer for frame number
char frame[64];

// the XPA mode
char* mode = "ack=false,verify=false,doxpa=false";

// create DS9 frame paramlist
snprintf(frame, 64, "frame %d", ds9->frame);

// set the image array
XPASet(ds9->handle, ds9->tmpl, frame, mode, NULL, 0, NULL, NULL, 1);
XPASet(ds9->handle, ds9->tmpl, "mecube", mode, fits, len, NULL, NULL, 1);
}

#else

void* ds9_connect(char* tmpl)
{
return NULL;
}

void ds9_disconnect(void* p)
{
}

const char* ds9_template(void* p)
{
return NULL;
}

int ds9_frame(void* p)
{
return 0;
}

void ds9_mecube(void* p, void* fits, size_t len)
{
}

#endif
16 changes: 16 additions & 0 deletions src/ds9.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

// connect to a DS9 instance via XPA
void* ds9_connect(char* tmpl);

// disconnect from a DS9 instance
void ds9_disconnect(void* ds9);

// get template name from DS9 connection
const char* ds9_template(void* ds9);

// get frame number from DS9 connection
int ds9_frame(void* ds9);

// show FITS cube in DS9
void ds9_mecube(void* ds9, void* fits, size_t len);
77 changes: 19 additions & 58 deletions src/lensed.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include <signal.h>
#include <setjmp.h>

#ifdef LENSED_XPA
#include "xpa.h"
#endif

#include "multinest.h"

#include "opencl.h"
Expand All @@ -25,6 +21,7 @@
#include "log.h"
#include "path.h"
#include "version.h"
#include "ds9.h"

// jump buffer to exit run
static jmp_buf jmp;
Expand Down Expand Up @@ -1149,65 +1146,31 @@ int main(int argc, char* argv[])
}


/***********
* DS9 XPA *
***********/

#ifdef LENSED_XPA
verbose("XPA");

// create a persistent XPA handle
lensed->xpa = XPAOpen(NULL);
/******************
* DS9 connection *
******************/

if(lensed->xpa)
verbose(" handle created");
else
verbose(" could not create handle!");

// set up DS9 for Lensed if successful
// set up DS9 connection if enabled
if(inp->opts->ds9)
{
// XPA response
char* buf = NULL;
size_t len = 0;

verbose(" DS9 enabled");
verbose("DS9");

// store XPA name for DS9
lensed->ds9 = inp->opts->ds9_name;
// create connection
lensed->ds9 = ds9_connect(inp->opts->ds9_name);

verbose(" XPA name: %s", lensed->ds9);

// set view to tile
XPASet(lensed->xpa, lensed->ds9, "tile yes", NULL, NULL, 0, NULL, NULL, 1);

// create frame for images
XPASet(lensed->xpa, lensed->ds9, "frame new", NULL, NULL, 0, NULL, NULL, 1);

// get newly created frame
XPAGet(lensed->xpa, lensed->ds9, "frame", NULL, &buf, &len, NULL, NULL, 1);
if(buf)
lensed->ds9_frame = atoi(buf);
else
lensed->ds9_frame = 999;

verbose(" frame no: %d", lensed->ds9_frame);

// free response buffer
free(buf);
// check if connection was successful
if(lensed->ds9)
{
verbose(" XPA connected");
verbose(" DS9 template: %s", ds9_template(lensed->ds9));
verbose(" frame number: %d", ds9_frame(lensed->ds9));
}
}
else
{
verbose(" DS9 disabled");

// don't use ds9
// no DS9 connection
lensed->ds9 = NULL;
}
#else
// XPA is not supported
lensed->xpa = NULL;
lensed->ds9 = NULL;
#endif


/***************
Expand Down Expand Up @@ -1425,11 +1388,9 @@ int main(int argc, char* argv[])
* cleaning *
************/

#ifdef LENSED_XPA
// close XPA handle
if(lensed->xpa)
XPAClose(lensed->xpa);
#endif
// disconnect from DS9
if(lensed->ds9)
ds9_disconnect(lensed->ds9);

// free profile
if(lensed->profile)
Expand Down
6 changes: 2 additions & 4 deletions src/lensed.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ struct lensed
profile* unmap_loglike_mem;
}* profile;

// XPA handle and settings
void* xpa;
char* ds9;
int ds9_frame;
// DS9 connection
void* ds9;
};
21 changes: 3 additions & 18 deletions src/nested.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
#include <float.h>
#include <math.h>

#ifdef LENSED_XPA
#include "xpa.h"
#endif

#include "opencl.h"
#include "input.h"
#include "prior.h"
Expand All @@ -16,6 +12,7 @@
#include "lensed.h"
#include "nested.h"
#include "log.h"
#include "ds9.h"

void loglike(double cube[], int* ndim, int* npar, double* lnew, void* lensed_)
{
Expand Down Expand Up @@ -247,34 +244,22 @@ void dumper(int* nsamples, int* nlive, int* npar, double** physlive,
if(lensed->fits)
write_output(lensed->fits, lensed->width, lensed->height, 5, output, names);

#ifdef LENSED_XPA
// send output to DS9 if asked to
if(lensed->ds9)
{
// buffer for frame number
char frame[64];

// the XPA mode
char* XPA_MODE = "ack=false,verify=false,doxpa=false";

// in-memory FITS
void* fits;
size_t len;

// create DS9 frame paramlist
snprintf(frame, 64, "frame %d", lensed->ds9_frame);

// write FITS
len = write_memory(&fits, lensed->width, lensed->height, 5, output, names);

// set the image array
XPASet(lensed->xpa, lensed->ds9, frame, XPA_MODE, NULL, 0, NULL, NULL, 1);
XPASet(lensed->xpa, lensed->ds9, "mecube", XPA_MODE, fits, len, NULL, NULL, 1);
// show FITS
ds9_mecube(lensed->ds9, fits, len);

// free in-memory FITS
free(fits);
}
#endif

// unmap buffers
clEnqueueUnmapMemObject(lensed->queue, image_mem, image_map, 0, NULL, NULL);
Expand Down