esclev.cpp 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <string.h>

#include "util.h"
#include "minig.h"
#include "gls.h"
#include "esclev.h"

extern FILE *infile, *cffile;    /* command file flag and file descriptor*/
extern int  cflog;               /* command logging flag */

/* This file contains subroutines that are common to all versions */
/* of escl8r and levit8r. */
/* D.C. Radford            Sept 1999 */

/* function defined in drawstring.c */
int drawstring(char *, int, char, float, float, float, float);
/* functions defined in escl8ra.c or lev4d.c */
int combine(char *ans, int nc); /* returns 1 on error */
int disp_dat(int iflag);
int energy(float x, float dx, float *eg, float *deg);
int examine(char *ans, int nc); /* returns 1 on error */
int num_gate(char *ans, int nc); /* returns 1 on error */

char listnam[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz[]|";

/* ======================================================================= */
float get_energy(void)
{
  float eg, x, y, dx, fj1, deg;
  int   nc;
  char  ans[80];

  eg = -1.0f;
  printf("Enter gamma energy using cursor on spectrum window.\n"
       " ...Hit any character;\n"
       " T to type energy, X or button 3 to exit.\n");
  retic(&x, &y, ans);
  if (*ans == 'X' || *ans == 'x') return 0;
  if (*ans == 'T' || *ans == 't') {
    nc = ask(ans, 80, "Gamma energy = ? (rtn to quit)");
    if (nc > 0) ffin(ans, nc, &eg, &fj1, &fj1);
  } else {
    dx = 0.0f;
    energy(x - 0.5f, dx, &eg, &deg);
  }
  return eg;
} /* get_energy */

/* ======================================================================= */
int get_fit_gam(int *jng, short *jgam)
{
  float x1, y1;
  int   igam, ilev;
  int   i, k, iband, askit, nc, ki;
  char  li[80], ans[80], new_ans[80];

/* get gamma indices to be fitted in intensity and/or energy */
/* ask for gammas to be fitted */

  askit = 0;
START:
  if (*jng <= 0 || askit ||
      (*ans != 'L' && *ans != 'l' && *ans != 'B' && * ans != 'b')) {
    printf(" Gammas to be fitted = ?\n"
	 "  ...X to end,\n"
	 "  ...button 2 or L to specify initial level(s),\n"
	 "  ...button 3 or B to specify initial band(s),\n"
	 "  ...A for all gammas in level scheme.\n");
    retic2(&x1, &y1, ans);
    if (!strcmp(ans, "X") || !strcmp(ans, "x")) {
      *jng = 0;
      return 0;
    }
  }
  askit = 1;
  *jng = 0;
  if (*ans == 'A' || *ans == 'a') {
    for (i = 0; i < glsgd.ngammas; ++i) {
      jgam[(*jng)++] = (short) i;
    }
  } else if (*ans == 'L' || *ans == 'l') {
    while (1) {
      if (!(nc = ask(new_ans, 80,
		     " ...Name of initial level = ? (rtn to end)"))) goto START;
      if (nc >= 3 && nc <= 10) {
	strcpy(li, "         ");
	strcpy(li + 10 - nc, new_ans);
	for (ki = 0; ki < glsgd.nlevels; ++ki) {
	  if (!strcmp(glsgd.lev[ki].name, li)) break;
	}
	if (ki < glsgd.nlevels) break;
      }
      printf("*** Bad level name. ***\n");
    }
    for (k = 0; k < levemit.n[ki]; ++k) {
      jgam[(*jng)++] = (short) levemit.l[ki][k];
    }
  } else if (*ans == 'B' || *ans == 'b') {
    while (1) {
      nc = ask(new_ans, 80, " ...Name of initial band = ? (rtn to end)");
      if (nc == 0) goto START;
      if (nc < 9) break;
      printf("*** Bad band name. ***\n");
    }
    strcpy(li, "         ");
    strcpy(li + 8 - nc, new_ans);
    for (ki = 0; ki < glsgd.nlevels; ++ki) {
      if (!strncmp(glsgd.lev[ki].name, li, 8)) {
	for (k = 0; k < levemit.n[ki]; ++k) {
	  jgam[(*jng)++] = (short) levemit.l[ki][k];
	}
      }
    }
  } else if (*ans == 'G') {
    igam = nearest_gamma(x1, y1);
    if (igam < 0) {
      printf("No gamma selected, try again...\n");
      goto START;
    }
    *jng = 1;
    jgam[0] = (short) igam;
  } else if (!strncmp(ans, "X2", 2)) {
    ilev = nearest_level(x1, y1);
    if (ilev < 0) {
      printf("No level selected, try again...\n");
      goto START;
    }
    for (k = 0; k < levemit.n[ilev]; ++k) {
      jgam[(*jng)++] = (short) levemit.l[ilev][k];
    }
  } else if (!strncmp(ans, "X3", 2)) {
    iband = nearest_band(x1, y1);
    if (iband < 0 || iband >= glsgd.nbands) {
      printf("No band selected, try again...\n");
      goto START;
    }
    for (ilev = 0; ilev < glsgd.nlevels; ++ilev) {
      if (glsgd.lev[ilev].band == iband) {
	for (k = 0; k < levemit.n[ilev]; ++k) {
	  jgam[(*jng)++] = (short) levemit.l[ilev][k];
	}
      }
    }
  } else {
    printf(" *** Bad entry, try again... ***\n");
    goto START;
  }
  return 0;
} /* get_fit_gam */

/* ======================================================================= */
int listgam(void)
{
  /* defined in esclev.h:
     char listnam[55] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz[]|" */

  float x, y, eg, dx, fj1, fj2, deg;
  int   igam, iwin, graphics = 0, i, k, ni, nl;
  char  reply, ans[80];

  /* ....define/edit list of gammas */
  while (1) {
    if (!ask(ans, 80, "List name = ? (A to Z, a to z)")) return 0;
    reply = *ans;
    for (nl = 0; nl < 52; ++nl) {
      if (reply == listnam[nl]) break;
    }
    if (nl < 52) break;
  }

  ni = elgd.nlist[nl];
  i = 0;
  while (i < 40) {
    if (graphics) {
      /* get list energies from level scheme or spectrum window */
      if (i < ni) {
    printf("Gamma %c%2.2i? (P for %.1f, T to type energies, X to end)\n",
	     reply, i+1, elgd.list[nl][i]);
      } else {
    printf("Gamma %c%2.2i? (T to type energies, X to end)\n",
	     reply, i+1);
      }
      retic3(&x, &y, ans, &iwin);
      if (*ans == 'X' || *ans == 'x') {
	elgd.nlist[nl] = i;
	return 0;
      }
      if (*ans == 'T' || *ans == 't') {
	graphics = 0;
      } else if (i >= ni || (*ans != 'P' && *ans != 'p')) {
	if (iwin == 2) {
	  if ((igam = nearest_gamma(x, y)) < 0) {
        printf("  ... no gamma selected, try again...\n");
	    continue;
	  }
	  eg = glsgd.gam[igam].e;
	} else {
	  dx = 0.0f;
	  energy(x - 0.5f, dx, &eg, &deg);
	}
	elgd.list[nl][i++] = eg;
      } else {
	i++;
      }

    } else {
      /* get list energies from text window */
      if (i < ni) {
	k = ask(ans, 80, "Gamma %c%2.2i = ? (1 for %.1f,\n"
		"   G to enter energies from graphics, rtn to end)",
		reply, i+1, elgd.list[nl][i]);
      } else {
	k = ask(ans, 80, "Gamma %c%2.2i = ?\n"
		"  (G to enter energies from graphics, rtn to end)",
		reply, i+1);
      }
      if (k < 1) {
	elgd.nlist[nl] = i;
	return 0;
      }
      if (*ans == 'G' || *ans == 'g') {
	graphics = 1;
      } else if (ffin(ans, k, &eg, &fj1, &fj2)) {
    printf("  ... bad entry, try again...\n");
      } else if (i >= ni || eg != 1.0f) {
	elgd.list[nl][i++] = eg;
      } else {
	i++;
      }
    }
  }
  elgd.nlist[nl] = 40;
  return 0;
} /* listgam */

/* ======================================================================= */
int examine_gamma(void)
{
  float x1, y1, egamma, dx, deg;
  int   igam, iwin, nc;
  char  ans[80], command[80];

  printf("Button 1: set gate on gamma\n"
       "Button 2: add gate on gamma\n"
       "Button 3: show gamma\n"
       "      - : subtract gamma\n"
       "      . : logical-type combine gamma\n"
       "    b/f : back/forward in gate history\n"
       " ...hit X on keyboard to exit...\n");
START:
  igam = 0;
  retic3(&x1, &y1, ans, &iwin);
  if (!strcmp(ans, "X") || !strcmp(ans, "x")) {
    return 0;
  } else if (!strcmp(ans, "b") || !strcmp(ans, "B") ||
	     !strcmp(ans, "u") || !strcmp(ans, "U")) {
    undo_esclev(-1, 4);
    goto START;
  } else if (!strcmp(ans, "f") || !strcmp(ans, "F") ||
	     !strcmp(ans, "r") || !strcmp(ans, "R")) {
    undo_esclev(1, 4);
    goto START;
  }
  if (iwin == 2) {
    igam = nearest_gamma(x1, y1);
    if (igam < 0) {
      printf("  ... no gamma selected, try again...\n");
      goto START;
    }
    egamma = glsgd.gam[igam].e;
    printf("\n*** Egamma = %7.2f +- %4.2f     Igamma = %6.2f +- %4.2f\n",
	 glsgd.gam[igam].e, glsgd.gam[igam].de,
	 glsgd.gam[igam].i, glsgd.gam[igam].di);
  } else if (iwin == 1) {
    dx = 0.0f;
    energy(x1 - 0.5f, dx, &egamma, &deg);
  } else {
    printf("Click in spectrum window for gates on peaks,\n"
	 "   or in level scheme window for gates on gammas.\n");
    goto START;
  }
  sprintf(command, " %.2f", egamma);
  nc = strlen(command);
  if (ans[1] == '1') {
    printf("%s\n", command);
    hilite(-1);
    if (num_gate(command, nc)) goto ERR;
    disp_dat(0);
  } else if (ans[1] == '2') {
    *command = '+';
    printf("%s\n", command);
    if (combine(command, nc)) goto ERR;
    disp_dat(0);
  } else if (ans[1] == '3') {
    *command = 'X';
    printf("%s\n", command);
    if (examine(command, nc)) goto ERR;
  } else if (!strcmp(ans, "minus") || !strncmp(ans, "-", 1)) {
    *command = '-';
    printf("%s\n", command);
    if (combine(command, nc)) goto ERR;
    disp_dat(0);
  } else if (!strcmp(ans, "period") || !strncmp(ans, ".", 1)) {
    *command = '.';
    printf("%s\n", command);
    if (combine(command, nc)) goto ERR;
    disp_dat(0);
  }
  goto START;
 ERR:
  printf("Bad energy...\n");
  goto START;
} /* examine_gamma */

/* ======================================================================= */
int comfil(char *filnam, int nc)
{
  static int cfopen = 0;

  strncpy(filnam, "  ", 2);
  if (nc < 3) {
    /* ask for command file name */
  GETFILNAM:
    infile = 0;
    nc = askfn(filnam, 80, "", ".cmd", "Command file name = ?");
    if (nc == 0) return 0;
  }
  
  setext(filnam, ".cmd", 80);
  if (!strncmp(filnam, "END", 3) || !strncmp(filnam, "end", 3)) {
    /* close command file, lu IR = console */
    if (cfopen || cflog) {
      fclose(cffile);
    } else {
      printf("Command file not open.\n");
    }
    cfopen = 0;
    cflog = 0;
    infile = 0;
  } else if (!strncmp(filnam, "CON", 3) || !strncmp(filnam, "con", 3)) {
    if (cfopen) {
      infile = cffile;
    } else {
      printf("Command file not open.\n");
    }
  } else if (!strncmp(filnam, "CHK", 3) || !strncmp(filnam, "chk", 3)) {
    if (cfopen) {
      infile = 0;
      if (!askyn("Proceed? (Y/N)")) return 0;
      infile = cffile;
    } else if (!cflog) {
      printf("Command file not open.\n");
    }
  } else if (!strncmp(filnam, "ERA", 3) || !strncmp(filnam, "era", 3)) {
    erase();
  } else if (!strncmp(filnam, "LOG", 3) || !strncmp(filnam, "log", 3)) {
    if (cfopen || cflog) fclose(cffile);
    while (1) {
      cfopen = 0;
      cflog = 0;
      infile = 0;
      nc = askfn(filnam, 80, "", ".cmd", "File name for command logging = ?");
      if (nc == 0) return 0;
      if (strncmp(filnam, "END", 3) && strncmp(filnam, "end", 3) &&
	  strncmp(filnam, "CON", 3) && strncmp(filnam, "con", 3) &&
	  strncmp(filnam, "CHK", 3) && strncmp(filnam, "chk", 3) &&
	  strncmp(filnam, "ERA", 3) && strncmp(filnam, "era", 3) &&
	  strncmp(filnam, "LOG", 3) && strncmp(filnam, "log", 3)) break;
      printf("*** That is an illegal command file name. ***\n");
    }
    /* open log command file for output
       all input from lu IR to be copied to lu ICF */
    cffile = open_new_file(filnam, 1);
    cflog = 1;
  } else {
    /* CF filename
       open command file for input on lu IR */
    if (cfopen || cflog) fclose(cffile);
    cflog = 0;
    if (!(cffile = fopen(filnam, "r+"))) {
      file_error("open", filnam);
      cfopen = 0;
      goto GETFILNAM;
    }
    cfopen = 1;
    infile = cffile;
  }
  return 0;
} /* comfil */
/* ======================================================================= */
int path(int mode)
{
  static int ordd[MAXLEV];
  float toti, a;
  int   flag = 1, temp, i, j, ig, lf, jj, li;

/* This subroutine calculates all gamma gamma coincidence intensities and all
   branching ratios for gamma rays defined in the level scheme file.  A bubble
   sort is performed on the level energies so that the coincidence intensities
   need only be calculated once per level. ordd is an pointer array into 
   levdata ordered (increasing) by energy.  Branching ratios are calculated
   and stored in levelbr. */

  for (i = 0; i < glsgd.nlevels; ++i) {
    for (j = 0; j < glsgd.ngammas; ++j) {
      elgd.levelbr[j][i] = 0.0f;
    }
  }
  if (mode > 0) {
    /* bubble sort on level energies */
    for (i = 0; i < glsgd.nlevels; ++i) ordd[i] = i;
    while (flag) {
      flag = 0;
      for (i = 0; i < glsgd.nlevels - 1; ++i) {
	if (glsgd.lev[ordd[i]].e > glsgd.lev[ordd[i+1]].e) {
	  temp = ordd[i];
	  ordd[i] = ordd[i+1];
	  ordd[i+1] = temp;
	  flag = 1;
	}
      }
    }
  }
  for (i = 1; i < glsgd.nlevels; ++i) {
/* -----------------------------------find total intensity emitted by level */
    li = ordd[i];
    if (levemit.n[li] > 0) {
      toti = 0.0f;
      for (j = 0; j < levemit.n[li]; ++j) {
	toti += glsgd.gam[levemit.l[li][j]].i * 
	       (glsgd.gam[levemit.l[li][j]].a + 1.0f);
      }
/* -----------------------------------calculate branching ratios */
      if (toti != 0.0f) {
	for (j = 0; j < levemit.n[li]; ++j) {
	  ig = levemit.l[li][j];
	  lf = glsgd.gam[ig].lf;
	  elgd.levelbr[ig][li] = glsgd.gam[ig].i / toti;
/* Since levels are ordered by energy all previous levels are up to date with
   regards to branching ratios, simply add them into new levels and multiply by
   original branching ratio. */
	  a = glsgd.gam[ig].i * (glsgd.gam[ig].a + 1.0f) / toti;
	  for (jj = 0; jj < glsgd.ngammas; ++jj) {
	    elgd.levelbr[jj][li] += elgd.levelbr[jj][lf] * a;
	  }
	}
      }
    }
  }
  return 0;
} /* path */

/* ====================================================================== */
int wrlists(char *ans)
{
  /* defined in esclev.h:
     char listnam[55] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz[]|" */
  float eg;
  int   i, j, nl;
  char  filnam[80];

/* ....write/read gamma-ray lists to/from disk file */
  if (strlen(ans) > 2) {
    strcpy(filnam, ans + 2);
  } else {
  START:
   if (!askfn(filnam, 80, "", ".lis", "List file name = ?")) return 0;
  }
  setext(filnam, ".lis", 80);
  if (*ans == 'W') {
    filez[1] = open_new_file(filnam, 0);
    if (!filez[1]) return 0;
    for (i = 0; i < 52; ++i) {
      if (elgd.nlist[i] > 0) {
	fprintf(filez[1], "%c\n", listnam[i]);
	for (j = 0; j < elgd.nlist[i]; ++j) {
	  fprintf(filez[1], "%7.2f\n", elgd.list[i][j]);
	}
	fprintf(filez[1], "0.0\n");
      }
    }
  } else {
    if (!(filez[1] = open_readonly(filnam))) goto START;
    while (fgets(lx, 120, filez[1])) {
      for (i = 0; i < 52; ++i) {
	if (*lx == listnam[i]) break;
      }
      if ((nl = i) >= 52) continue;
      elgd.nlist[nl] = 0;
      for (i = 0; i < 40 && fgets(lx, 120, filez[1]); ++i) {
	if (1 != sscanf(lx, "%f", &eg)) {
      printf("Cannot read file. WARNING: Lists may contain errors\n.");
	  fclose(filez[1]);
	  goto START;
	}
	if (eg == 0.0f) break;
	elgd.list[nl][i] = eg;
	elgd.nlist[nl] = i+1;
      }
    }
  }
  fclose(filez[1]);
  return 0;
} /* wrlists */

/* ======================================================================= */
int report_curs3(int ix, int iy, int iwin)
{
  float  x, y, e, de;
  int    g, l;

  cvxy(&x, &y, &ix, &iy, 2);
  if (iwin == 1) {
    printf(" y = %.0f", y);
    if ((g = nearest_gamma(x, y)) >= 0) {
      printf(" Gamma %.1f, I = %.2f", glsgd.gam[g].e, glsgd.gam[g].i);
    } else {
      printf("                     ");
    }
    if ((l = nearest_level(x, y)) >= 0) {
      printf(" Level %s, E = %.1f   \r", glsgd.lev[l].name, glsgd.lev[l].e);
    } else {
      printf("                               \r");
    }
  } else {
    energy(x - 0.5f, 0.0f, &e, &de);
    printf(" E = %.1f, y = %.0f                                    \r", e, y);
  }
  fflush(stdout);
  return 0;
}

/* ======================================================================= */
int done_report_curs(void)
{
  printf("                                      "
       "                                    \r");
  fflush(stdout);
  return 0;
}