diff options
Diffstat (limited to 'src/dirax.c')
-rw-r--r-- | src/dirax.c | 172 |
1 files changed, 47 insertions, 125 deletions
diff --git a/src/dirax.c b/src/dirax.c index d27cdac..19a6e26 100644 --- a/src/dirax.c +++ b/src/dirax.c @@ -35,14 +35,12 @@ #include "displaywindow.h" #include "reproject.h" - typedef enum { DIRAX_INPUT_NONE, DIRAX_INPUT_LINE, DIRAX_INPUT_PROMPT } DirAxInputType; - static void dirax_parseline(const char *line, ControlContext *ctx) { int i, rf; @@ -58,14 +56,14 @@ static void dirax_parseline(const char *line, ControlContext *ctx) if ( strstr(line, "reflections from file") ) { displaywindow_error("DirAx can't understand this data.", - ctx->dw); + ctx->dw); return; } /* Is this the first line of a unit cell specification? */ rf = 0; i = 0; while ( (i<strlen(line)) && ((line[i] == 'R') - || (line[i] == 'D') || (line[i] == ' ')) ) { + || (line[i] == 'D') || (line[i] == ' ')) ) { if ( line[i] == 'R' ) rf = 1; if ( (line[i] == 'D') && rf ) { ctx->dirax_read_cell = 1; @@ -73,15 +71,12 @@ static void dirax_parseline(const char *line, ControlContext *ctx) free(ctx->cell); } ctx->cell = malloc(sizeof(Basis)); - ctx->cell->a.x = 0.0; - ctx->cell->a.y = 0.0; - ctx->cell->a.z = 0.0; - ctx->cell->b.x = 0.0; - ctx->cell->b.y = 0.0; - ctx->cell->b.z = 0.0; - ctx->cell->c.x = 0.0; - ctx->cell->c.y = 0.0; - ctx->cell->c.z = 0.0; + ctx->cell->a.x = 0.0; ctx->cell->a.y = 0.0; + ctx->cell->a.z = 0.0; + ctx->cell->b.x = 0.0; ctx->cell->b.y = 0.0; + ctx->cell->b.z = 0.0; + ctx->cell->c.x = 0.0; ctx->cell->c.y = 0.0; + ctx->cell->c.z = 0.0; return; } i++; @@ -92,27 +87,24 @@ static void dirax_parseline(const char *line, ControlContext *ctx) /* First row of unit cell values */ float x1, x2, x3; sscanf(line, "%f %f %f", &x1, &x2, &x3); - ctx->cell->a.x = x1*1e10; - ctx->cell->b.x = x2*1e10; - ctx->cell->c.x = x3*1e10; + ctx->cell->a.x = x1*1e10; ctx->cell->b.x = x2*1e10; + ctx->cell->c.x = x3*1e10; ctx->dirax_read_cell++; return; } else if ( ctx->dirax_read_cell == 2 ) { /* First row of unit cell values */ float y1, y2, y3; sscanf(line, "%f %f %f", &y1, &y2, &y3); - ctx->cell->a.y = y1*1e10; - ctx->cell->b.y = y2*1e10; - ctx->cell->c.y = y3*1e10; + ctx->cell->a.y = y1*1e10; ctx->cell->b.y = y2*1e10; + ctx->cell->c.y = y3*1e10; ctx->dirax_read_cell++; return; } else if ( ctx->dirax_read_cell == 3 ) { /* First row of unit cell values */ float z1, z2, z3; sscanf(line, "%f %f %f", &z1, &z2, &z3); - ctx->cell->a.z = z1*1e10; - ctx->cell->b.z = z2*1e10; - ctx->cell->c.z = z3*1e10; + ctx->cell->a.z = z1*1e10; ctx->cell->b.z = z2*1e10; + ctx->cell->c.z = z3*1e10; printf("DX: Read a reciprocal unit cell\n"); displaywindow_update(ctx->dw); reproject_lattice_changed(ctx); @@ -123,7 +115,6 @@ static void dirax_parseline(const char *line, ControlContext *ctx) ctx->dirax_read_cell = 0; } - static void dirax_sendline(const char *line, ControlContext *ctx) { char *copy; @@ -140,7 +131,6 @@ static void dirax_sendline(const char *line, ControlContext *ctx) free(copy); } - /* Send a "user" command to DirAx, failing if DirAx is not idle */ static void dirax_sendline_if_idle(const char *line, ControlContext *ctx) { @@ -152,7 +142,6 @@ static void dirax_sendline_if_idle(const char *line, ControlContext *ctx) dirax_sendline(line, ctx); } - static void dirax_send_next(ControlContext *ctx) { switch ( ctx->dirax_step ) { @@ -182,7 +171,7 @@ static void dirax_send_next(ControlContext *ctx) } case 5 : { - dirax_sendline("levelfit 200\n", ctx); + dirax_sendline("levelfit 300\n", ctx); ctx->dirax_step++; break; } @@ -207,7 +196,6 @@ static void dirax_send_next(ControlContext *ctx) } } - static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition, ControlContext *ctx) { @@ -240,7 +228,7 @@ static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition, /* See if there's a full line in the buffer yet */ for ( i=0; i<ctx->dirax_rbufpos-1; i++ ) { - /* Means the last value looked at is rbufpos-2 */ + /* Means the last value looked at is rbufpos-2 */ /* Is there a prompt in the buffer? */ if ( i+7 <= ctx->dirax_rbufpos ) { @@ -255,7 +243,7 @@ static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition, } if ( (ctx->dirax_rbuffer[i] == '\r') - && (ctx->dirax_rbuffer[i+1] == '\n') ) { + && (ctx->dirax_rbuffer[i+1] == '\n') ) { block_ready = 1; type = DIRAX_INPUT_LINE; break; @@ -281,11 +269,11 @@ static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition, if ( block_buffer[0] == '\r' ) { memmove(block_buffer, - block_buffer+1, i); + block_buffer+1, i); } dirax_parseline(block_buffer, - ctx); + ctx); free(block_buffer); endbit_length = i+2; @@ -302,28 +290,29 @@ static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition, } default : { - printf("DX: Unrecognised input " - "mode (this never happens!)\n"); + printf( + "DX: Unrecognised input mode (this never happens!)\n"); abort(); } } - /* Now the block's been parsed, it should - * be forgotten about */ - memmove(ctx->dirax_rbuffer, ctx->dirax_rbuffer - + endbit_length, ctx->dirax_rbuflen - - endbit_length); + /* Now the block's been parsed, it should be + * forgotten about */ + memmove(ctx->dirax_rbuffer, + ctx->dirax_rbuffer + endbit_length, + ctx->dirax_rbuflen - endbit_length); /* Subtract the number of bytes removed */ ctx->dirax_rbufpos = ctx->dirax_rbufpos - - endbit_length; - new_rbuflen = ctx->dirax_rbuflen-endbit_length; + - endbit_length; + new_rbuflen = ctx->dirax_rbuflen + - endbit_length; if ( new_rbuflen == 0 ) { new_rbuflen = 256; } ctx->dirax_rbuffer = realloc(ctx->dirax_rbuffer, - new_rbuflen); + new_rbuflen); ctx->dirax_rbuflen = new_rbuflen; } else { @@ -332,10 +321,10 @@ static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition, /* More buffer space is needed */ ctx->dirax_rbuffer = realloc( - ctx->dirax_rbuffer, - ctx->dirax_rbuflen + 256); + ctx->dirax_rbuffer, + ctx->dirax_rbuflen + 256); ctx->dirax_rbuflen = ctx->dirax_rbuflen - + 256; + + 256; /* The new space gets used at the next * read, shortly... */ @@ -349,81 +338,25 @@ static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition, } return TRUE; - } - void dirax_stop(ControlContext *ctx) { dirax_sendline_if_idle("end\n", ctx); } - void dirax_rerun(ControlContext *ctx) { dirax_sendline_if_idle("go\n", ctx); ctx->dirax_step = 7; } - -static void dirax_send_random_selection(ReflectionList *r, int n, FILE *fh) -{ - char *used; - int i; - - used = malloc(n*sizeof(char)); - - for ( i=0; i<n; i++ ) { - used[i] = '-'; - } - - i = 0; - while ( i < 1000 ) { - - Reflection *ref; - int j; - long long int ra; - - ra = ((long long int)random() * (long long int)n); - ra /= RAND_MAX; - if ( used[ra] == 'U' ) { - printf("%lli - already used\n", ra); - continue; - } - - printf("Selected reflection %lli\n", ra); - - /* Dig out the correct reflection. A little faffy - * because of the linked list */ - ref = r->reflections; - for ( j=0; j<ra; j++ ) { - ref = ref->next; - } - - /* Limit resolution of reflections */ - if ( ( (ref->x/1e9)*(ref->x/1e9) - + (ref->y/1e9)*(ref->y/1e9) - + (ref->z/1e9)*(ref->z/1e9) ) > (20e9)*(20e9) ) - continue; - - fprintf(fh, "%10f %10f %10f %8f\n", - ref->x/1e10, ref->y/1e10, - ref->z/1e10, ref->intensity); - used[ra] = 'U'; - - i++; - - } -} - - void dirax_invoke(ControlContext *ctx) { FILE *fh; Reflection *ref; unsigned int opts; int saved_stderr; - int n; if ( ctx->dirax ) { dirax_rerun(ctx); @@ -437,23 +370,12 @@ void dirax_invoke(ControlContext *ctx) printf("DX: Couldn't open temporary file dtr.drx\n"); return; } - fprintf(fh, "%f\n", 0.5); /* Lie about the wavelength. DirAx can't - * handle the truth. */ - - n = ctx->reflectionlist->n_reflections; - printf("DX: There are %i reflections - ", n); - if ( n > 1000 ) { - printf("sending a random selection to DirAx\n"); - dirax_send_random_selection(ctx->reflectionlist, n, fh); - } else { - printf("sending them all to DirAx\n"); - ref = ctx->reflectionlist->reflections; - while ( ref ) { - fprintf(fh, "%10f %10f %10f %8f\n", - ref->x/1e10, ref->y/1e10, - ref->z/1e10, ref->intensity); - ref = ref->next; - } + fprintf(fh, "%f\n", 0.5); /* Lie about the wavelength. */ + ref = ctx->reflectionlist->reflections; + while ( ref ) { + fprintf(fh, "%10f %10f %10f %8f\n", ref->x/1e10, ref->y/1e10, + ref->z/1e10, ref->intensity); + ref = ref->next; } fclose(fh); @@ -494,11 +416,12 @@ void dirax_invoke(ControlContext *ctx) ctx->dirax_read_cell = 0; ctx->dirax = g_io_channel_unix_new(ctx->dirax_pty); g_io_add_watch(ctx->dirax, G_IO_IN | G_IO_HUP, (GIOFunc)dirax_readable, - ctx); + ctx); displaywindow_update_dirax(ctx, ctx->dw); -} + return; +} /* Despite being part of the same module, this has very little to do with * invoking DirAx */ @@ -527,14 +450,14 @@ ReflectionList *dirax_load(const char *filename) if ( line[ptr] == '!' ) continue; if ( line[ptr] == '\n' ) continue; if ( line[ptr] == '\r' ) continue; - if ( sscanf(line+ptr, "%f %f %f\n", - &theta, &phib, &chib) == 3 ) { + if ( sscanf(line+ptr, "%f %f %f\n", &theta, &phib, + &chib) == 3 ) { double s, x, y, z; float blah, intensity; - /* Try to find an intensity value. Use dummy value if - * it fails */ + /* Try to find an intensity value. Use dummy value + * if it fails */ if ( sscanf(line+ptr, "%f %f %f %f\n", &blah, &blah, &blah, &intensity) != 4 ) { intensity = 1.0; @@ -571,12 +494,11 @@ ReflectionList *dirax_load(const char *filename) fclose(fh); return list; -} +} int dirax_is_drxfile(const char *filename) { - FILE *fh; float lambda; char line[256]; |