Create Xref-Html Frames                    Remove All Frames
file:d:/code/LRC/Splicer/splicer_code_ver2.2/splicer_functions.c        (Tue Mar 14 16:29:16 2000 )


   1: /*
   2:   splicer_functions.c
   3: */
   4: #include <stdio.h>
   5: #include <math.h>
   6: #include <time.h>
   7: #include <malloc.h>
   8: #include <Xm/Xm.h>
   9: #include <Xm/Text.h>
  10: #include <Xm/List.h>
  11: #include "splicer-creation-c.h"
  12: #include "splicer_vars.h"
  13: #include "EquationMenu.h"
  14: #include <X11/cursorfont.h>
  15: 
  16: #define NUM_OF_ARGS (3)
  17: 
  18: void SpManageMessage(info)
  19: MessageInfo info;
  20: {
  21:         XmString        xmstring; 
  22:   XmFontList  fontlist;
  23:  
  24:   XtManageChild(XmMessageBoxGetChild(messageBoxInfo,XmDIALOG_CANCEL_BUTTON));
  25:         xmstring=XmCvtCTToXmString(info.label); 
  26:         XtVaGetValues(messageBoxInfo,XmNlabelFontList,&fontlist,NULL);
  27:         XtVaSetValues(messageBoxInfo,XmNmessageString,xmstring, 
  28:                 XmNdialogType,info.type,XmNdefaultButton,info.default_button,
  29:     XmNwidth,XmStringWidth(fontlist,xmstring)+75,NULL); 
  30:         XtUnmanageChild(XmMessageBoxGetChild(messageBoxInfo,XmDIALOG_HELP_BUTTON));
  31:         if(info.type==XmDIALOG_WARNING || info.type==XmDIALOG_ERROR)
  32:                 XtUnmanageChild(XmMessageBoxGetChild(messageBoxInfo,XmDIALOG_CANCEL_BUTTON));
  33:   XmStringFree(xmstring);
  34:   XtManageChild(messageBoxInfo);
  35:   XtPopup(XtParent(messageBoxInfo),XtGrabNone);
  36: }
  37: 
  38: void SpExit()
  39: {
  40:   FILE  *defaults_ptr;
  41: 
  42: /* write defaults file */
  43:   if((defaults_ptr=fopen(defaults_filename,"w"))==NULL)
  44:     (void)fprintf(stderr,"splicer: Warning: cannot open defaults file %s for writing.\n", defaults_filename);
  45:   else
  46:   {
  47:     (void)fprintf(stderr,"splicer: set defaults written to %s.\n",defaults_filename);
  48:     Spwrite_defaults(defaults_ptr); /* write defaults file */
  49:     (void)fclose(defaults_ptr);
  50:   }
  51:   
  52: /* clean up */
  53:   SpCleanUp(EXIT);
  54: }
  55: void SpCleanUp(cflag)
  56: int cflag;
  57: {
  58:   int i;
  59:   extern  Widget  pushButtonOpenStrat;
  60:         time_t  tp;
  61: 
  62:         if(cflag == CANCEL) {
  63:           return;
  64:         }
  65: 
  66:   if(cflag==EXIT)
  67:   {
  68:     if(defaults_filename!=NULL) free(defaults_filename);
  69:     if(current_path!=NULL) free(current_path);
  70:     if(report.ptr!=NULL) fclose(report.ptr);
  71:                 if(preview_input_path!=NULL) free(preview_input_path);
  72:     for(i=0;i<strat_file_cnt;i++)
  73:     {
  74:       if(strat[i] && (int)strlen((char*)strat[i])>0)
  75:         XtFree(strat[i]);
  76:     }
  77:     strat_file_cnt=0;
  78:                 XtFree(open_data_directory);
  79:     XtFree(outdata_filename);
  80:     XtFree(outdata_dirname);
  81:     XtFree(outspdata_filename);
  82:     XtFree(outspdata_dirname);
  83:     XtFree(cull.filename);
  84:     XtFree(cull.dirname);
  85:     XtFree(affine.filename);
  86:     XtFree(affine.dirname);
  87:     XtFree(outstrat.filename);
  88:     XtFree(outstrat.dirname);
  89:     XtFree(splicertable.filename);
  90:     XtFree(splicertable.dirname);
  91:                 if(report_filename) 
  92:                 {
  93:                   XtFree(report_filename);
  94:                 }
  95:                 if(report_dirname)
  96:                 {
  97:                   XtFree(report_dirname);
  98:                 }
  99:   }
 100:   if(cflag==ALL || cflag==INPUTDATA)
 101:   {
 102:                 if(report_filename) 
 103:                 {
 104:                   XtFree(report_filename);
 105:                   report_filename = NULL;
 106:                 }
 107:                 if(report_dirname)
 108:                 {
 109:                   XtFree(report_dirname);
 110:                   report_dirname = NULL;
 111:                 }
 112:     done_reading=done_cull=done_smooth=done_decimate=done_affine=False;
 113:     datafileopen=False;
 114:                 for(i=0; i<dataset_cnt; ++i) {
 115:       SpFreeData(data[i]);
 116:                   if(data[i])
 117:                   {
 118:                     free(data[i]);
 119:                     data[i]=NULL;
 120:                   }
 121:                 }
 122:                 if(data)
 123:                 {
 124:                   free(data);
 125:                   data=NULL;
 126:                 }
 127:                 dset = 0;
 128:                 dataset_cnt = 0;
 129:                 XtUnmanageChild(optionMenuDataSet);
 130:                 XtUnmanageChild(pushButtonDataSet1);
 131:                 XtUnmanageChild(pushButtonDataSet2);
 132:                 XtUnmanageChild(pushButtonDataSet3);
 133:                 XtUnmanageChild(pushButtonDataSet4);
 134:                 XtUnmanageChild(pushButtonDataSet5);
 135:                 XtUnmanageChild(pushButtonDataSet6);
 136:                 XtUnmanageChild(pushButtonDataSet7);
 137:                 XtUnmanageChild(pushButtonDataSet8);
 138:                 XtUnmanageChild(pushButtonDataSet9);
 139:     have_data=DONT;
 140:                 have_affine = DONT;
 141:     have_tie=DONT;
 142:     have_shift=DONT;
 143:     have_corr=DONT;
 144:                 have_splice=DONT;
 145:     have_spcorr=DONT;
 146:                 did_affine = NO;
 147:                 saved_affine = NO;
 148:                 did_agemodel = NO;
 149:                 saved_agemodel = NO;
 150:                 agemodel_cnt = 0;
 151:                 lastaffinehole = -1;
 152:                 lastaffinecore = -1;
 153:                 XmListDeleteAllItems(listConstructAgeModel);
 154:               /*  XtSetSensitive(pushButtonDataBase,True); */
 155:                 XtSetSensitive(pushButtonOpenData,True);
 156:                 XtSetSensitive(pushButtonOpenStrat,False);
 157:                 XtSetSensitive(pushButtonOpenAffine,True);
 158:                 if(XtIsManaged(XtParent(drawingAreaEdit))) {
 159:                   reason_for_redraw = DRAW_CLEAR;
 160:                   DrawDrawingAreaEdit(drawingAreaEdit, (XtPointer)NULL, (XtPointer)NULL);
 161:                 }
 162:     XmListDeleteAllItems(listOpenSelected);
 163:     if(report.saveflag)
 164:     {
 165:       report.saveflag=False;
 166:       fclose(report.ptr);
 167:     }
 168:     if((charptr=(char*)calloc(200,CHAR_SIZE))==NULL)
 169:       SpBadMemAlloc("SpCleanUp 1");
 170:           (void)time(&tp);
 171:           (void)sprintf(charptr,"Start of Session: \n%s",ctime(&tp));
 172:     XmTextReplace(textReport,0,XmTextGetLastPosition(textReport),charptr);
 173:     free(charptr);
 174:                 XtSetSensitive(cascadeButtonSave,FALSE);
 175:                 XtSetSensitive(pushButtonSaveCull,FALSE);
 176:                 XtSetSensitive(pushButtonSaveAffine,FALSE);
 177:                 XtSetSensitive(pushButtonSaveSplicerTable,FALSE);
 178:                 XtSetSensitive(pushButtonSaveStrat,FALSE);
 179:                 XtSetSensitive(pushButtonSaveData,FALSE);
 180:                 XtSetSensitive(pushButtonSaveSplicerData,FALSE);
 181:                 XtSetSensitive(cascadeButtonClear,FALSE);
 182:                 XtSetSensitive(pushButtonClearAll,FALSE);
 183:                 XtSetSensitive(pushButtonClearComposite,FALSE);
 184:                 XtSetSensitive(pushButtonClearSplice,FALSE);
 185:                 XtSetSensitive(pushButtonClearStrat,FALSE);
 186:                 XtSetSensitive(pushButtonComposite,FALSE);
 187:                 XtSetSensitive(cascadeButtonSplice,FALSE);
 188:              /*   XtSetSensitive(pushButtonDisplay,FALSE); */
 189:                 XtSetSensitive(pushButtonViewOffsets,FALSE);
 190:                 XtSetSensitive(pushButtonViewAgeDepth,FALSE);
 191:                 XmToggleButtonSetState(toggleButtonPlotUnsmooth, True, NULL);
 192:                 XmToggleButtonSetState(toggleButtonPlotSmooth, False, NULL);
 193:                 XmToggleButtonSetState(toggleButtonPlotBoth, False, NULL);
 194:                 XtSetSensitive(toggleButtonPlotSmooth,FALSE);
 195:                 XtSetSensitive(toggleButtonPlotUnsmooth,FALSE);
 196:                 XtSetSensitive(toggleButtonPlotBoth,FALSE);
 197:                 XtSetSensitive(cascadeButtonDataManip, False);
 198: 
 199:                 if(are_agemodelling == YES) {
 200:                   /* unmanage the construct age model form
 201:                   */
 202:                   are_agemodelling = NO;
 203:                   have_splicetoage = DONT;
 204:                   if(XtIsManaged(XtParent(drawingAreaSpliceToAge))) {
 205:                     XtUnmanageChild(formSpliceToAge);
 206:                   }
 207:                   XtUnmanageChild(formConstructAgeModel);
 208:                   XtVaSetValues(XtParent(formAgeDepth), XmNwidth, 600, NULL);
 209:                   XtSetSensitive(formAgeDepthControls, False);
 210:                 }
 211:   }
 212:   if(cflag==ALL || cflag==INPUTSTRAT)
 213:   {
 214:     stratfileopen=False;
 215:     if(strat_file_cnt>0 && strat[strat_file_cnt-1])
 216:     {
 217:                    for(i=0; i<strat_file_cnt; ++i) {
 218:       if(strat[i]->data)
 219:       {
 220:         free(strat[i]->data);
 221:         strat[i]->data=NULL;
 222:       }
 223:       free(strat[i]);
 224:       strat[i]=NULL;
 225:                    }
 226:     }
 227:                 strat_file_cnt = 0;
 228:     have_strat=DONT;
 229:   }
 230:   if(cflag==ALL || cflag==INPUTSPLICERTABLE)
 231:   {
 232:     splicer_read_done=splicer_table_done=False;
 233:     sptable_cnt=0;
 234:     if(splicertable.data)
 235:     {
 236:       free(splicertable.data);
 237:       splicertable.data=NULL;
 238:     }
 239:     splicertable.data_cnt= 0;
 240:                 splicertable.alloc_cnt=0;
 241:     have_splice=DONT;
 242:     have_splice_tie=DONT;
 243:     have_splice_shift=DONT;
 244:     have_core_to_splice=DONT;
 245:                 did_splicing = NO;
 246:                 saved_splice = NO;
 247:                 if(XtIsManaged(XtParent(drawingAreaView))) {
 248:                   reason_for_redraw = DRAW_CLEAR;
 249:                   DrawDrawingAreaView(drawingAreaView, (XtPointer)NULL, (XtPointer)NULL);
 250:                 }
 251:                 XtSetSensitive(pushButtonSaveSplicerTable,False);
 252:                 XtSetSensitive(pushButtonSaveSplicerData,False);
 253:                 XtSetSensitive(pushButtonClearSplice,FALSE);
 254:                 if(cflag == INPUTSPLICERTABLE) {
 255:                   XtSetSensitive(pushButtonOpenSplicerTable,True);
 256:                 }
 257:                 else if(cflag == ALL) {
 258:                   XtSetSensitive(pushButtonOpenSplicerTable, False);
 259:                 }
 260: 
 261:                 if(are_agemodelling == YES) {
 262:                   if(XtIsManaged(XtParent(drawingAreaSpliceToAge))) {
 263:                     XClearWindow(XtDisplay(drawingAreaSpliceToAge), XtWindow(drawingAreaSpliceToAge));
 264:                     XtUnmanageChild(formSpliceToAge);
 265:                   }
 266:                   XtSetSensitive(pushButtonPlotSpliceVsAge, False);
 267:                   XtSetSensitive(pushButtonSaveSpliceToAge, False);
 268:                   have_splicetoage = DONT;
 269:                 }
 270:   }
 271:         if(cflag == ALL || cflag == TIMESERIES)
 272:         {
 273:           have_time_series = DONT;
 274:           have_timeseries_tie = DONT;
 275:           have_splicetoage_tie = DONT;
 276:           last_splicetoage_tie = NONE;
 277:           free(timeseries.age);
 278:           free(timeseries.val);
 279:           timeseries.age = NULL;
 280:           timeseries.val = NULL;
 281:         }
 282: }
 283: 
 284: void SpGetEquationValues(equ)
 285: Equation  *equ;
 286: {
 287:   Widget  *tmp_widget;
 288:   int SpGetSign();
 289: /* get text values */
 290:   charptr=XmTextGetString(textFieldCullValue1);
 291:   equ->value1=(float)atof(charptr);
 292:   XtFree(charptr);
 293:   charptr=XmTextGetString(textFieldCullValue2);
 294:   equ->value2=(float)atof(charptr);
 295:   XtFree(charptr);
 296: /* get signs */
 297:   equ->sign1=SpGetSign(equationMenuCull1);
 298:   equ->sign2=SpGetSign(equationMenuCull2);
 299: /* get join */
 300:   XtVaGetValues(optionMenuCullJoin,XmNmenuHistory,&tmp_widget,NULL);
 301:   if(*tmp_widget==pushButtonCullOnly)
 302:     equ->join=ONLY;
 303:   else
 304:     equ->join=OR;
 305: }
 306: 
 307: int SpGetSign(equ_widget)
 308: EquationMenuData  equ_widget;
 309: {
 310:   Widget  tmp_widget;
 311:   int value;
 312: 
 313:   XtVaGetValues(equ_widget.pulldownMenuEquation,XmNmenuHistory,&tmp_widget,NULL);
 314:   if(tmp_widget==equ_widget.pushButtonLessThan)
 315:     value=LESS_THAN;
 316:   else if(tmp_widget==equ_widget.pushButtonLessThanEqual)
 317:     value=LESS_THAN_OR_EQUAL_TO;
 318:   else if(tmp_widget==equ_widget.pushButtonGreaterThan)
 319:     value=GREATER_THAN;
 320:   else if(tmp_widget==equ_widget.pushButtonGreaterThanEqual)
 321:     value=GREATER_THAN_OR_EQUAL_TO;
 322:   else
 323:     value=EQUAL_TO;
 324:   return(value);
 325: }
 326: void SpInitialize(argc,argv)
 327: int argc;
 328: char  **argv;
 329: {
 330:   char  *tmpfile;
 331:   FILE  *fptmp;
 332:   int error=FALSE,i,path_len;
 333:   void  exit();
 334: 
 335: /* initialize values */
 336: /*  data[0]->numfiles=0;
 337: */
 338:   report.lineno=2;
 339:   affine.usetable=FALSE;
 340:   if((defaults_filename= (char *)calloc(25,CHAR_SIZE))==NULL)
 341:     SpBadMemAlloc("SpInitialize 1");
 342:   (void)strcpy(defaults_filename,".splicerdefaults");
 343: /* get current path */
 344:   tmpfile=NULL;
 345:   tmpfile=tmpnam(tmpfile);
 346:   if((charptr=(char*)calloc((int)strlen(tmpfile)*2+50,CHAR_SIZE))==NULL)
 347:     SpBadMemAlloc("SpInitialize 2");
 348:   (void)sprintf(charptr,"pwd|wc -c>%s;pwd>>%s",tmpfile,tmpfile);
 349:   (void)system(charptr);
 350:   free(charptr);
 351:   fptmp=fopen(tmpfile,"r");
 352:   (void)fscanf(fptmp,"%d",&path_len);
 353:   if((current_path = (char*)calloc(path_len+5,CHAR_SIZE))==NULL)
 354:     SpBadMemAlloc("SpInitialize 3");
 355:   (void)fscanf(fptmp,"%s",current_path);
 356:   fclose(fptmp);
 357:         (void)remove(tmpfile);
 358: /* make preview input path large, it is going to be used in such a way
 359:    that calloc/recalloc will be impractable and its size could vary greatly
 360: */
 361:         if((preview_input_path = (char*)calloc(1000,CHAR_SIZE))==NULL)
 362:                 SpBadMemAlloc("SpInitialize 3a");
 363:         (void)sprintf(preview_input_path, "%s", current_path);
 364: 
 365: /* loop through arguments */
 366:   if(argc>1)
 367:   {
 368:     for(i=1;i<argc && !error;i++)
 369:     {
 370:       if(!strcmp(argv[i],"-f")) /* defaults file */
 371:       {
 372:         if((charptr=(char*)realloc((char*)defaults_filename,((int)strlen(argv[++i])+1)*CHAR_SIZE))==NULL)
 373:           SpBadMemAlloc("SpInitialize 4");
 374:         else
 375:           defaults_filename=charptr;
 376:         (void)strcpy(defaults_filename,argv[i]);
 377:       }
 378:       else
 379:         error=True;
 380:         
 381:     }
 382:   }
 383: /* read defaults file */
 384:   if(!Spread_defaults(defaults_filename))
 385:   {
 386:     (void)fprintf(stderr,"splicer: Warning: cannot read defaults file %s.\n", defaults_filename);
 387:     (void)fprintf(stderr,"\t\tUsing program defaults.\n");
 388:   }
 389:   else
 390:     (void)fprintf(stderr,"splicer: Using defaults file %s.\n",defaults_filename);
 391: 
 392:   /* check args for foreground and background color
 393:      save the color names for the hsplicer program
 394:   */
 395:   (void)sprintf(prgfgcolor, "black");
 396:   (void)sprintf(prgbgcolor, "white");
 397:   prgfg = NO;
 398:   prgbg = NO;
 399:   if(argc >= 3) {
 400:     for(i=0; i<argc; ++i) {
 401:       if(strcmp(argv[i], "-fg") == 0) {
 402:         if(i < argc-1) {
 403:           (void)sprintf(prgfgcolor, "");
 404:           (void)strcpy(prgfgcolor, argv[i+1]);
 405:           prgfg = YES;
 406:         }
 407:       }
 408:       if(strcmp(argv[i], "-bg") == 0) {
 409:         if(i < argc-1) {
 410:           (void)sprintf(prgbgcolor, "");
 411:           (void)strcpy(prgbgcolor, argv[i+1]);
 412:           prgbg = YES;
 413:         }
 414:       }
 415:     }
 416:   }
 417: 
 418: }
 419:   
 420: 
 421: void SpManageWarningBox(string)
 422: char  *string;
 423: {
 424:   message.fatal=False;
 425:   message.error=True;
 426:   message.caller=SPMANAGEWARNINGBOX;
 427:   message.type=XmDIALOG_WARNING;
 428:   message.default_button=XmDIALOG_OK_BUTTON;
 429:   if((message.label=(char*)calloc((int)strlen(string)+2,CHAR_SIZE))==NULL)
 430:     SpBadMemAlloc("SpManageWarningBox 1");
 431:   (void)strcat(message.label,string);
 432:   SpManageMessage(message);
 433: 
 434: }
 435: 
 436: void  SpBadMemAlloc(string)
 437: char  *string;
 438: {
 439:   (void)fprintf(stderr,"ERROR: Memory allocation failed at %s! Exit & restart program!!",string);
 440:   message.fatal=True;
 441:   message.error=True;
 442:   message.caller=BADMEMALLOC;
 443:   message.type=XmDIALOG_ERROR;
 444:   message.default_button=XmDIALOG_OK_BUTTON;
 445:   message.label=(char*)calloc(200+(int)strlen(string),CHAR_SIZE);
 446:   (void)sprintf(message.label,"ERROR: Memory allocation failed at %s! Exit & restart program!!",string);
 447:   SpManageMessage(message);
 448: }
 449: 
 450: void SpSetSign(equ_widget,sign)
 451: EquationMenuData  equ_widget;
 452: int sign;
 453: {
 454:   Widget  *tmp_widget;
 455: 
 456:   if(sign==LESS_THAN)
 457:     tmp_widget=&equ_widget.pushButtonLessThan;
 458:   else if(sign==LESS_THAN_OR_EQUAL_TO)
 459:     tmp_widget=&equ_widget.pushButtonLessThanEqual;
 460:   else if(sign==GREATER_THAN)
 461:     tmp_widget=&equ_widget.pushButtonGreaterThan;
 462:   else if(sign==GREATER_THAN_OR_EQUAL_TO)
 463:     tmp_widget=&equ_widget.pushButtonGreaterThanEqual;
 464:   else
 465:     tmp_widget=&equ_widget.pushButtonEqual;
 466:   XtVaSetValues(equ_widget.EquationMenu,XmNmenuHistory,*tmp_widget,NULL);
 467: }
 468: 
 469: void SpSetEquationValues(equ)
 470: Equation  equ;
 471: {
 472:   Widget  *tmp_widget;
 473: 
 474: /* set text values */
 475:   if((charptr=(char*)calloc(200,CHAR_SIZE))==NULL)
 476:     SpBadMemAlloc("SpSetEquationValues 1");
 477:   (void)sprintf(charptr,"%.3f",equ.value1);
 478:   XmTextSetString(textFieldCullValue1,charptr);
 479:   (void)sprintf(charptr,"%.3f",equ.value2);
 480:   XmTextSetString(textFieldCullValue2,charptr);
 481:   free(charptr);
 482:   
 483: /* set signs */
 484:   SpSetSign(equationMenuCull1,equ.sign1);
 485:   SpSetSign(equationMenuCull2,equ.sign2);
 486: /* set join */
 487:   if(equ.join==ONLY)
 488:   {
 489:     tmp_widget=&pushButtonCullOnly;
 490:     XtSetSensitive(equationMenuCull2.EquationMenu,False);
 491:     XtSetSensitive(textFieldCullValue2,False);
 492:   }
 493:   else
 494:   {
 495:     tmp_widget=&pushButtonCullOr;
 496:     XtSetSensitive(equationMenuCull2.EquationMenu,True);
 497:     XtSetSensitive(textFieldCullValue2,True);
 498:   }
 499:   XtVaSetValues(optionMenuCullJoin,XmNmenuHistory,*tmp_widget,NULL);
 500: }
 501: 
 502: void SpUpdateReport(text_mes)
 503: char  *text_mes;
 504: {
 505:   XmTextPosition  last_pos;
 506: 
 507:   last_pos=XmTextGetLastPosition(textReport);
 508:   XmTextInsert(textReport,last_pos,text_mes);
 509:   last_pos=last_pos+(XmTextPosition)(int)strlen(text_mes);
 510:   XmTextShowPosition(textReport,last_pos);
 511:   if(report.saveflag)
 512:   {
 513:     fputs("\n",report.ptr);
 514:     fputs(text_mes,report.ptr);
 515:   }
 516: }
 517: 
 518: #define filesearch_string "ls -lAd1 %s|awk '{ if($1 ~ /^-/) { num=$NF; type=\"FILE\" }else if($1 ~ /^l/) { num=$(NF-2); type=\"LINK\" }else if($1 ~ /^d/) { num=$NF; type=\"DIR\" } nw=split(num,word,\"/\"); print type,word[nw] }'>%s\n"
 519: #define dirsearch_string "ls -lA1 %s|awk '{ if($1 ~ /^-/) { num=$NF; type=\"FILE\" }else if($1 ~ /^l/) { num=$(NF-2); type=\"LINK\" }else if($1 ~ /^d/) { num=$NF; type=\"DIR\" } nw=split(num,word,\"/\"); print type,word[nw] }'>%s\n"
 520: 
 521: 
 522: void SpUpdateOpen(widget_type)
 523: int widget_type;  
 524: {
 525:         char      *tmpfile=NULL, filetype[13], *filename;
 526:         FILE      *fptr;
 527:         int       numfiles=0,numdirs=0,alloc_file,alloc_dir,i;
 528:         XmString        *filelist,*dirlist,*selected_dir;
 529:   char    *textstring;
 530: 
 531:   tmpfile=tmpnam(tmpfile);
 532:   if(widget_type==FILE_FILTER) /* file filter was updated */
 533:   {
 534:     textstring = XmTextGetString(textOpenFilter);
 535:     if((int)strlen(textstring)==0)  /* no filename */
 536:     {
 537:       if((filename=(char*)calloc((int)strlen(current_path)+5,CHAR_SIZE))==NULL)
 538:         SpBadMemAlloc("SpUpdateOpen 1");
 539:       (void)sprintf(filename,"%s/*",current_path);
 540:     }
 541:     else if(textstring[0]=='/') /* full path used */
 542:     {
 543:       if((filename=(char*)calloc((int)strlen(textstring)+2,CHAR_SIZE))==NULL)
 544:         SpBadMemAlloc("SpUpdateOpen 2");
 545:       (void)strcpy(filename,textstring);
 546:       *strrchr(textstring,'/')= '\0';
 547:       if((charptr=(char*)realloc(current_path,((int)strlen(textstring)+2)*CHAR_SIZE))==NULL)
 548:         SpBadMemAlloc("SpUpdateOpen 3");
 549:       else
 550:         current_path=charptr;
 551:       (void)strcpy(current_path,textstring);
 552:       /* set current path */
 553:     }
 554:     else if (!strchr(textstring,'/')) /* only filename */
 555:     {
 556:       if((filename=(char*)calloc((int)strlen(current_path)+(int)strlen(textstring)+5,CHAR_SIZE))==NULL)
 557:         SpBadMemAlloc("SpUpdateOpen 4");
 558:       (void)sprintf(filename,"%s/%s",current_path,textstring);
 559:     }
 560:     else        /* relative path and filename */
 561:     {
 562:       if((filename=(char*)calloc((int)strlen(current_path)+(int)strlen(textstring)+5,CHAR_SIZE))==NULL)
 563:         SpBadMemAlloc("SpUpdateOpen 5");
 564:       (void)sprintf(filename,"%s/%s",current_path,textstring);
 565:       *strrchr(textstring,'/')= '\0';
 566:       if((charptr=(char*)realloc(current_path,((int)strlen(textstring)+2)*CHAR_SIZE))==NULL)
 567:         SpBadMemAlloc("SpUpdateOpen 6");
 568:       else
 569:         current_path=charptr;
 570:       (void)strcpy(current_path,textstring);
 571:     }
 572:     i=(int)strlen(filesearch_string)+(int)strlen(filename)+(int)strlen(tmpfile)+100;
 573:     if((charptr=(char*)calloc(i,CHAR_SIZE))==NULL)
 574:       SpBadMemAlloc("SpUpdateOpen 7");
 575:     else
 576:       (void)sprintf(charptr,filesearch_string,filename,tmpfile);
 577:     XtFree(textstring);
 578:   }
 579:   else /* directories was updates */
 580:   {
 581:     XtVaGetValues(listOpenDirectory,XmNselectedItems,&selected_dir,XmNselectedItemCount,&numdirs,NULL);
 582:     if(numdirs == 1)
 583:     {
 584:       if(!XmStringGetLtoR(selected_dir[0],XmFONTLIST_DEFAULT_TAG,&textstring))
 585:         {
 586:               message.error=True;
 587:               message.fatal=False;
 588:                                 message.caller=NONE;
 589:                                 message.type=XmDIALOG_WARNING;
 590:                                 message.default_button=XmDIALOG_OK_BUTTON;
 591:               if((message.label=(char*)calloc(200,CHAR_SIZE))==NULL)
 592:                       SpBadMemAlloc("SpUpdateOpen 7a");
 593:               else
 594:                       (void)sprintf(message.label,"Unable to convert selected_dir[0]");
 595:               SpManageMessage(message);
 596:         }
 597:                         if(strcmp(textstring,"..") == 0 && strcmp(current_path, "/") == 0) 
 598:                         {
 599:                         }
 600:       else if(!strcmp(textstring,"..")) /* move back a directory */
 601:                         {
 602:         *strrchr(current_path,'/')='\0';
 603:                                 if(strcmp(current_path, "") == 0)
 604:                                 {
 605:                                   (void)sprintf(current_path, "/");
 606:                                 }
 607:                         }
 608:       else  if(strcmp(textstring,"."))  /* new directory */
 609:       {
 610:         if((charptr=(char*)realloc(current_path,((int)strlen(current_path)+(int)strlen(textstring)+2)*CHAR_SIZE))==NULL)
 611:                                   SpBadMemAlloc("SpUpdateOpen 8");
 612:                           else
 613:                                   current_path=charptr;
 614:                                 if(strcmp(current_path, "/") == 0)
 615:                                 {
 616:             (void)sprintf(current_path,"/%s",textstring);
 617:                                 }
 618:                                 else
 619:                                 {
 620:                                   (void)sprintf(current_path,"%s/%s",current_path, textstring);
 621:                                 }
 622:       }
 623:       XtFree(textstring);
 624: 
 625:       if((charptr=(char*)calloc((int)strlen(dirsearch_string)+(int)strlen(current_path)+(int)strlen(tmpfile)+100,CHAR_SIZE))==NULL)
 626:         SpBadMemAlloc("SpUpdateOpen 9");
 627:       (void)sprintf(charptr,dirsearch_string,current_path,tmpfile);
 628: 
 629:       /* update filter field */
 630:       if((filename=(char*)calloc((int)strlen(current_path)+5,CHAR_SIZE))==NULL)
 631:         SpBadMemAlloc("SpUpdateOpen 10");
 632:                         if(strcmp(current_path, "/") == 0)
 633:                         {
 634:                           (void)sprintf(filename,"/*");
 635:                         }
 636:                         else
 637:                         {
 638:         (void)sprintf(filename,"%s/*",current_path);
 639:                         }
 640:       XmTextSetString(textOpenFilter,filename);
 641:       XmListDeselectAllItems(listOpenDirectory);
 642:     }
 643:     else
 644:                 {
 645:       SpManageWarningBox("Error opening new directory!");
 646:                 }
 647:   }
 648:         system(charptr);
 649:   free(charptr);
 650: 
 651:   /* get file and directory names  */
 652:   numdirs=0;
 653:   if((filelist=(XmString*)calloc((alloc_file=1000),sizeof(XmString**)))==NULL)
 654:       SpBadMemAlloc("SpUpdateOpen 10b");
 655:   if((dirlist=(XmString*)calloc((alloc_dir=1000),sizeof(XmString**)))==NULL)
 656:       SpBadMemAlloc("SpUpdateOpen 10c");
 657:         dirlist[numdirs++]=XmStringCreateLocalized(".");
 658:         dirlist[numdirs++]=XmStringCreateLocalized("..");
 659:         if((fptr=fopen(tmpfile,"r"))!=NULL)
 660:         {
 661:     if((charptr=(char*)realloc(filename,1000*CHAR_SIZE))==NULL)
 662:                   SpBadMemAlloc("SpUpdateOpen 11");
 663:           else
 664:                   filename=charptr;
 665:                 while(fscanf(fptr,"%s %s",filetype,filename)!=EOF)
 666:                 {
 667:                         if(!strcmp(filetype,"DIR"))     /* directory names */
 668:       {
 669:         if(numdirs>=alloc_dir)
 670:         {
 671:           if((charptr=(char*)realloc((char*)dirlist,(alloc_dir+=1000)*sizeof(XmString*)))==NULL)
 672:               SpBadMemAlloc("SpUpdateOpen 11b");
 673:           else
 674:             dirlist=(XmString*)charptr;
 675:         }
 676:                                 dirlist[numdirs++]=XmStringCreateLocalized(filename);
 677:       }
 678:                         else if(!strcmp(filetype,"FILE")) /* file names */
 679:       {
 680:         if(numfiles>=alloc_file)
 681:         {
 682:           if((charptr=(char*)realloc((char*)filelist,(alloc_file+=1000)*sizeof(XmString*)))==NULL)
 683:               SpBadMemAlloc("SpUpdateOpen 11c");
 684:           else
 685:             filelist=(XmString*)charptr;
 686:         }
 687:                                 filelist[numfiles++]=XmStringCreateLocalized(filename);
 688:       }
 689:       else if(!strcmp(filetype,"LINK")) /*  symbolic link name */
 690:       {
 691:         if((charptr=(char*)realloc((char*)filename,((int)strlen(filename)+25)*sizeof(XmString*)))==NULL)
 692:           SpBadMemAlloc("SpUpdateOpen 11d");
 693:         else
 694:           filename=charptr;
 695:         (void)strcat(filename," (sym-link)");
 696:         if(numfiles>=alloc_file)
 697:         {
 698:           if((charptr=(char*)realloc((char*)filelist,(alloc_file+=1000)*sizeof(XmString*)))==NULL)
 699:               SpBadMemAlloc("SpUpdateOpen 11e");
 700:           else
 701:             filelist=(XmString*)charptr;
 702:         }
 703:         filelist[numfiles++]=XmStringCreateLocalized(filename);
 704:       }
 705:                 }
 706:     free(filename);
 707:         }
 708: 
 709:   /* invalid directory */
 710:   if(numfiles <=0 && numdirs<=2)
 711:   {
 712:     if((charptr=(char*)calloc((int)strlen(filename)+50,CHAR_SIZE))==NULL)
 713:       SpBadMemAlloc("SpUpdateOpen 12");
 714:     (void)sprintf(charptr,"Invalid or empty filter: %s !",filename);
 715:     SpManageWarningBox(charptr);
 716:     free(charptr);
 717:   }
 718:         /* if files found - put them in file list */
 719:         else
 720:   {
 721:     if(numfiles>0)
 722:                   XtVaSetValues(listOpenFiles,XmNitemCount,numfiles,XmNvisibleItemCount,numfiles
 723:             ,XmNitems,filelist,NULL);
 724:           else    /* no files found  - clear list */
 725:       XmListDeleteAllItems(listOpenFiles);
 726: 
 727:           /* put directories in dir list */
 728:           XtVaSetValues(listOpenDirectory,XmNitemCount,numdirs,XmNvisibleItemCount,numdirs
 729:           ,XmNitems,dirlist,NULL);
 730:            /* clean up memory used and remove temporary file */
 731:           while(numdirs>0) XmStringFree(dirlist[--numdirs]);
 732:           while(numfiles>0) XmStringFree(filelist[--numfiles]);
 733:   }
 734:   free(dirlist);
 735:   free(filelist);
 736:         (void)remove(tmpfile);
 737: }
 738: 
 739: void CheckAffineTableOrder(affineptr)
 740: AffineTable *affineptr;
 741: {
 742:   int i, j, k, l, m, foundit;
 743:   float dep;
 744:   AffineHole tempaffinehole;
 745:   AffineCore  tempaffinecore;
 746:   void AffineTestPrint();
 747: 
 748:   /* is the affinetable in order?
 749:   */
 750:   /* by hole
 751:   */
 752:   doholeagain:
 753:   if(affineptr->numhole > 1) {
 754:     for(i=0; i<affineptr->numhole-1; ++i) {
 755:       if(strcmp(affineptr->hole[i].name, affineptr->hole[i+1].name) > 0) {
 756:         tempaffinehole = affineptr->hole[i];
 757:         affineptr->hole[i] = affineptr->hole[i+1];
 758:         affineptr->hole[i+1] = tempaffinehole;
 759:         goto doholeagain;
 760:       }
 761:     }
 762:   }
 763: 
 764:   /* by core
 765:   */
 766:   docoreagain:
 767:   for(i=0; i<affineptr->numhole-1; ++i) {
 768:     if(affineptr->hole[i].numcore > 1) {
 769:       for(j=0; j<affineptr->hole[i].numcore-1; ++j) {
 770:         if(affineptr->hole[i].core[j].name > affineptr->hole[i].core[j+1].name) {
 771:           tempaffinecore = affineptr->hole[i].core[j];
 772:           affineptr->hole[i].core[j] = affineptr->hole[i].core[j+1];
 773:           affineptr->hole[i].core[j+1] = tempaffinecore;
 774:           goto docoreagain;
 775:         }
 776:       }
 777:     }
 778:   }
 779: }
 780: 
 781: void CheckAffineTableMissingHole(affineptr, ds)
 782: AffineTable *affineptr;
 783: int ds;
 784: {
 785:   int i, j, k, foundit;
 786:   float dep;
 787:  
 788: 
 789:   /* are there missing holes in affinetable?
 790:   */
 791:   for(i=0; i<data[ds]->numholes; ++i) {
 792:     foundit = NO;
 793:     for(j=0; j<affineptr->numhole; ++j) {
 794:       if(strcmp(affineptr->hole[j].name, &data[ds]->holes[i]->name) == 0) {
 795:         foundit = YES;
 796:         break;
 797:       }
 798:     }
 799:     if(foundit == NO) {
 800:       j = 0;
 801:       while(strcmp(affineptr->hole[j].name, &data[ds]->holes[i]->name) <= 0 && j < affineptr->numhole) {
 802:         ++j;
 803:       }
 804:       ++affineptr->numhole;
 805:       for(k=affineptr->numhole-1; k > j; --k) {
 806:         affineptr->hole[k] = affineptr->hole[k-1];
 807:       }
 808:       strncpy(affineptr->hole[j].name, &data[ds]->holes[i]->name, 1);
 809:       affineptr->hole[j].numcore = data[ds]->holes[i]->numcores;
 810:       for(k=0; k<data[ds]->holes[i]->numcores; ++k) {
 811:         affineptr->hole[j].core[k].leg = data[ds]->leg;
 812:         affineptr->hole[j].core[k].site = data[ds]->site;
 813:         affineptr->hole[j].core[k].name = data[ds]->holes[i]->core[k]->num;
 814:         strncpy(affineptr->hole[j].core[k].type, &data[ds]->holes[i]->core[k]->value[0]->type, 1);
 815:         /*
 816:           dep = data[ds]->holes[i]->core[k]->value[0]->top_int + ((atoi(data[ds]->holes[i]->core[k]->value[0]->section) - 1) * 1.5);
 817:           affineptr->hole[j].core[k].mbsf = data[ds]->holes[i]->core[k]->value[0]->sb_depth - dep/100.0;
 818:         */
 819:         affineptr->hole[j].core[k].cum_depth_offset = 0.0;
 820:         affineptr->hole[j].core[k].did_offset = NO;
 821:       }
 822:     }
 823:   }
 824: }
 825: 
 826: 
 827: void CheckAffineTableMissingCore(affineptr, ds)
 828: AffineTable *affineptr;
 829: int ds;
 830: {
 831:   int i, j, k, m, nh;
 832:   float dep;
 833: 
 834:   /* within a hole are there missing cores in affinetable?
 835:   */ 
 836:   for(i=0; i<affineptr->numhole; ++i) {
 837:     k = 0;
 838:     j = 0;
 839:     nh = affineptr->hole[i].data_map_to_hole[ds];
 840:     if(nh >= 0 && nh < data[ds]->numholes) {
 841:       while(k < data[ds]->holes[nh]->numcores && nh >= 0) {
 842:         if(data[ds]->holes[nh]->core[k]->num == affineptr->hole[i].core[j].name) {
 843:           if(j+1 < affineptr->hole[i].numcore) {
 844:             ++j;
 845:           }
 846:           ++k;
 847:         }
 848:         else if(data[ds]->holes[nh]->core[k]->num > affineptr->hole[i].core[affineptr->hole[i].numcore-1].name) {
 849:           /*add to end*/
 850:           ++affineptr->hole[i].numcore;
 851:           j = affineptr->hole[i].numcore - 1;
 852:           affineptr->hole[i].core[j].leg = data[ds]->leg;
 853:           affineptr->hole[i].core[j].site = data[ds]->site;
 854:           affineptr->hole[i].core[j].name = data[ds]->holes[nh]->core[k]->num; 
 855:           strncpy(affineptr->hole[i].core[j].type, &data[ds]->holes[nh]->core[k]->value[0]->type, 1); 
 856:           /*
 857:             dep = data[ds]->holes[nh]->core[k]->value[0]->top_int + ((atoi(data[ds]->holes[nh]->core[k]->value[0]->section) - 1) * 1.5);
 858:             affineptr->hole[i].core[j].mbsf = data[ds]->holes[nh]->core[k]->value[0]->sb_depth - dep/100.0; 
 859:           */
 860:           if(j > 0) { 
 861:             affineptr->hole[i].core[j].cum_depth_offset = affineptr->hole[i].core[j-1].cum_depth_offset; 
 862:           } 
 863:           else { 
 864:             affineptr->hole[i].core[j].cum_depth_offset = 0.0;  
 865:           } 
 866:           affineptr->hole[i].core[j].did_offset = NO;
 867:           ++k;
 868:         }
 869:         else if(data[ds]->holes[nh]->core[k]->num > affineptr->hole[i].core[j].name) {
 870:           if(j+1 < affineptr->hole[i].numcore) {
 871:             ++j;
 872:           }
 873:         }
 874:         else if(data[ds]->holes[nh]->core[k]->num < affineptr->hole[i].core[j].name) {
 875:           /*insert new core into table*/
 876:           ++affineptr->hole[i].numcore;
 877:           for(m=affineptr->hole[i].numcore-1; m > j; --m) {
 878:             affineptr->hole[i].core[m] = affineptr->hole[i].core[m-1];
 879:           }
 880:           affineptr->hole[i].core[j].leg = data[ds]->leg;
 881:           affineptr->hole[i].core[j].site = data[ds]->site;
 882:           affineptr->hole[i].core[j].name = data[ds]->holes[nh]->core[k]->num;
 883:           strncpy(affineptr->hole[i].core[j].type, &data[ds]->holes[nh]->core[k]->value[0]->type, 1);
 884:           /*
 885:             dep = data[ds]->holes[nh]->core[k]->value[0]->top_int + ((atoi(data[ds]->holes[nh]->core[k]->value[0]->section) - 1) * 1.5);
 886:             affineptr->hole[i].core[j].mbsf = data[ds]->holes[nh]->core[k]->value[0]->sb_depth - dep/100.0;
 887:           */
 888:           if(j > 0) {
 889:             affineptr->hole[i].core[j].cum_depth_offset = affineptr->hole[i].core[j-1].cum_depth_offset;
 890:           }
 891:           else {
 892:             affineptr->hole[i].core[j].cum_depth_offset = 0.0;
 893:           }
 894:           affineptr->hole[i].core[j].did_offset = NO;
 895:           ++k;
 896:           if(j+1 < affineptr->hole[i].numcore) {
 897:             ++j;
 898:           }
 899:         }
 900:       }
 901:     }
 902:     else {
 903:     }
 904:   }
 905: }
 906: 
 907: void AffineTestPrint()
 908: {
 909:   int i, j;
 910:   
 911:   for(i=0; i<affinetable.numhole; ++i) {
 912:     for(j=0; j<affinetable.hole[i].numcore; ++j) {
 913:       printf("%d    %d   %s   %d   %s   %6.2f   %d\n",
 914:        affinetable.hole[i].core[j].leg, affinetable.hole[i].core[j].site, affinetable.hole[i].name, affinetable.hole[i].core[j].name,
 915:        affinetable.hole[i].core[j].type,  
 916:        affinetable.hole[i].core[j].cum_depth_offset, affinetable.hole[i].core[j].did_offset);
 917:     }
 918:   }
 919:   printf("\n\n");
 920: }
 921: 
 922: void MakeNewAffineTable(affineptr)
 923: AffineTable *affineptr;
 924: {
 925:   int i, j;
 926:   float top_dep;
 927: 
 928:   affineptr->numhole = data[dset]->numholes;
 929: 
 930:   for(i=0; i<data[dset]->numholes; ++i) {
 931:     strncpy(affineptr->hole[i].name, &data[dset]->holes[i]->name, 1);
 932:     affineptr->hole[i].numcore = data[dset]->holes[i]->numcores;
 933:     for(j=0; j<data[dset]->holes[i]->numcores; ++j) {
 934:       affineptr->hole[i].core[j].leg = data[dset]->leg;
 935:       affineptr->hole[i].core[j].site = data[dset]->site;
 936:       affineptr->hole[i].core[j].name = data[dset]->holes[i]->core[j]->num;
 937:       strncpy(affineptr->hole[i].core[j].type, &data[dset]->holes[i]->core[j]->value[0]->type, 1);
 938:       affineptr->hole[i].core[j].did_offset = NO;
 939:       affineptr->hole[i].core[j].cum_depth_offset = 0.0;
 940:       /*
 941:         top_dep = data[dset]->holes[i]->core[j]->value[0]->top_int + ((atoi(data[dset]->holes[i]->core[j]->value[0]->section) - 1) * 1.5);
 942:         affineptr->hole[i].core[j].mbsf = data[dset]->holes[i]->core[j]->value[0]->sb_depth - top_dep/100.0;
 943:       */
 944:     }
 945:   }
 946: }
 947: 
 948: void MapHoleToAffineTable(affineptr, ds)
 949: AffineTable *affineptr;
 950: int ds;
 951: {
 952:   int i, k;
 953: 
 954:   for(i=0; i<data[ds]->numholes; ++i) {
 955:     data[ds]->holes[i]->affine_map_to_hole = -1;
 956:   }
 957:   for(k=0; k<affineptr->numhole; ++k) {
 958:     affineptr->hole[k].data_map_to_hole[ds] = -1;
 959:   }
 960: 
 961:   for(i=0; i<data[ds]->numholes; ++i) {
 962:     for(k=0; k<affineptr->numhole; ++k) {
 963:       if(strncmp(affineptr->hole[k].name, &data[ds]->holes[i]->name, 1) == 0) {
 964:         data[ds]->holes[i]->affine_map_to_hole = k;
 965:         affineptr->hole[k].data_map_to_hole[ds] = i;
 966:         break;
 967:       }
 968:     }
 969:   }
 970: }
 971: 
 972: void MapCoreToAffineTable(affineptr, ds)
 973: AffineTable *affineptr;
 974: int ds;
 975: {
 976:   int i, j, k, nh;
 977: 
 978:   for(i=0; i<data[ds]->numholes; ++i) {
 979:     for(j=0; j<data[ds]->holes[i]->numcores; ++j) {
 980:       data[ds]->holes[i]->core[j]->affine_map_to_core = -1;
 981:       data[ds]->holes[i]->core[j]->cum_dep_offset = 0.0;
 982:       data[ds]->holes[i]->core[j]->did_offset = NO;
 983:     }
 984:   }
 985:   for(i=0; i<affineptr->numhole; ++i) {
 986:     for(k=0; k<affineptr->hole[i].numcore; ++k) {
 987:       affineptr->hole[i].core[k].data_map_to_core[ds] = -1;
 988:     }
 989:   }
 990: 
 991:   for(i=0; i<data[ds]->numholes; ++i) {
 992:     nh = data[ds]->holes[i]->affine_map_to_hole; 
 993:     if(nh >= 0) {
 994:       for(j=0; j<data[ds]->holes[i]->numcores; ++j) {
 995:         for(k=0; k<affineptr->hole[nh].numcore; ++k) {
 996:           if(data[ds]->holes[i]->core[j]->num == affineptr->hole[nh].core[k].name) {
 997:             data[ds]->holes[i]->core[j]->affine_map_to_core = k;
 998:             affineptr->hole[nh].core[k].data_map_to_core[ds] = j;
 999:             data[ds]->holes[i]->core[j]->cum_dep_offset = affineptr->hole[nh].core[k].cum_depth_offset;
1000:             data[ds]->holes[i]->core[j]->did_offset = affineptr->hole[nh].core[k].did_offset;
1001:             break;
1002:           }
1003:         }
1004:       }
1005:     }
1006:   }
1007: }
1008: 
1009: void SpliceUndoWarning(reason, cnt)
1010: int reason, cnt;
1011: {
1012: 
1013:   char *warn;
1014: 
1015:   if((warn=(char*)calloc(MAXBUF,CHAR_SIZE))==NULL)
1016:                         SpBadMemAlloc("SpliceUndoWarning");
1017:   if(reason == SPL_UNDO_MISSING_CORE) {
1018:     sprintf(warn, "Warning; missing %d core(s) above the core you want to remove.\n", cnt);
1019:     strcat(warn, "if you remove this core the missing core(s) will be removed from the splice table.\n");
1020:   }
1021:   else if(reason == SPL_UNDO_TAILING_CORE) {
1022:     sprintf(warn, "Warning; trailing %d core(s) below the core you want to remove.\n", cnt);
1023:     strcat(warn, "if you remove this core the trailing core(s) will be removed from the splice table.\n");
1024:   }
1025:   strcat(warn, "do you still want to remove this core?");
1026:   XmTextSetString(textSpliceUndo, warn);
1027:   free(warn);
1028: }
1029: 
1030: void CheckSpliceTableOffsets()
1031: {
1032:   int i, warn;
1033: 
1034:   warn = NO;
1035:   for(i=0; i<spcomp.numcores; ++i) {
1036:     if(fabs(spcomp.Depthoffset[i] - data[dset]->holes[spcomp.hole[i]]->core[spcomp.core[i]]->cum_dep_offset) >= 0.01) {
1037:       warn = YES;
1038:     }
1039:   }
1040: 
1041:   if(warn == YES) {
1042:     (void)sprintf(buffer,"Warning: the affine table and splice table depth offsets differ.");
1043:     SpManageWarningBox(buffer);
1044:   }
1045: }
1046: 
1047: int CheckCoreAndDepthOrder(d)
1048: int d;
1049: {
1050:   int i, j, k, k1, k2;
1051: 
1052:   /* check that, for each hole in current dataset, cores are in
1053:      desending order. 
1054:   */
1055:   for(i=0; i<data[d]->numholes; ++i) {
1056:     if(data[d]->holes[i]->numcores > 1) {
1057:       for(j=0; j<data[d]->holes[i]->numcores-1; ++j) {
1058:         if(data[d]->holes[i]->core[j]->num > data[d]->holes[i]->core[j+1]->num) {
1059:           (void)sprintf(buffer,"Error:  core %d in hole %c is not in descending order.", 
1060:                                   data[d]->holes[i]->core[j]->num, data[d]->holes[i]->name);
1061:           SpManageWarningBox(buffer);
1062:           return ERR;
1063:         }
1064:       }
1065:     }
1066:   }
1067:  
1068:   /* and within the core depths are in order.
1069:   */
1070:   for(i=0; i<data[d]->numholes; ++i) {
1071:     for(j=0; j<data[d]->holes[i]->numcores; ++j) {
1072:       if(data[d]->holes[i]->core[j]->numvalues > 1) {
1073:         k1 = 0;
1074:         k2 = 1;
1075:         while(k2 < data[d]->holes[i]->core[j]->numvalues) {
1076:           if(data[d]->holes[i]->core[j]->value[k1]->quality != GOOD) {
1077:             ++k1;
1078:             k2 = k1 + 1;
1079:           }
1080:           else if(data[d]->holes[i]->core[j]->value[k2]->quality != GOOD) {
1081:             ++k2;
1082:           }
1083:           else {
1084:             if(data[d]->holes[i]->core[j]->value[k1]->sb_depth > data[d]->holes[i]->core[j]->value[k2]->sb_depth) {
1085:              (void)sprintf(buffer,"Error: the depths in core %d, hole %c are not in descending order at\nsect. %s  %.1f(cm)  %.2f(mbsf)", 
1086:                                      data[d]->holes[i]->core[j]->num,data[d]->holes[i]->name,
1087:                                      data[d]->holes[i]->core[j]->value[k2]->section, data[d]->holes[i]->core[j]->value[k2]->top_int,
1088:                                      data[d]->holes[i]->core[j]->value[k2]->sb_depth);
1089:               SpManageWarningBox(buffer);
1090:               return ERR;
1091:             }
1092:             ++k1;
1093:             ++k2;
1094:           }
1095:         }
1096:       }
1097:     }
1098:   }
1099: 
1100:   /* section interval depth; bot not less than top
1101:   */
1102:   if(data[d]->in_format != ODPOTHER4 && data[d]->in_format != ODPOTHER5) {
1103:     for(i=0; i<data[d]->numholes; ++i) {
1104:       for(j=0; j<data[d]->holes[i]->numcores; ++j) {
1105:         for(k1=0; k1<data[d]->holes[i]->core[j]->numvalues; ++k1) {
1106:           if(data[d]->holes[i]->core[j]->value[k1]->quality != GOOD) {
1107:           }
1108:           else {
1109:             if((data[d]->holes[i]->core[j]->value[k1]->bot_int - data[d]->holes[i]->core[j]->value[k1]->top_int) < -0.05) {
1110:               (void)sprintf(buffer,"Error: the depths in core %d, hole %c are not in descending order at\nsect. %s  %.1f(cm)  %.1f(cm)  %.2f(mbsf)",
1111:                                      data[d]->holes[i]->core[j]->num,data[d]->holes[i]->name,
1112:                                      data[d]->holes[i]->core[j]->value[k1]->section, data[d]->holes[i]->core[j]->value[k1]->top_int,
1113:                                      data[d]->holes[i]->core[j]->value[k1]->bot_int, data[d]->holes[i]->core[j]->value[k1]->sb_depth);
1114:               SpManageWarningBox(buffer);
1115:               return ERR;
1116:             }
1117:           }
1118:         }
1119:       }
1120:     }
1121:   }
1122:   return GOOD;
1123: }
1124: 
1125: 
1126: int MakeSplice(ds, sp)
1127: int        ds;
1128: SpliceComp *sp;
1129: {
1130:   int i, j, k, sptable_cnt, sm, dum1, dum2, warn;
1131:   Core    *coreptr;
1132:   Value   *valueptr;
1133:   int FindValueInCoreToSplice();
1134: 
1135:   sptable_cnt = 0;
1136:   sp->numcores = 0;
1137:   warn = False;
1138: 
1139:   while(sptable_cnt < splicertable.data_cnt) {
1140: 
1141:     /* fine which hole
1142:     */
1143:     for(i=0;i<data[ds]->numholes && splicertable.data[sptable_cnt].hole!=data[ds]->holes[i]->name;i++);
1144: 
1145:     if(i<data[ds]->numholes)
1146:     {
1147: 
1148:       /* find correct core 
1149:       */
1150:       for(j=0;j<data[ds]->holes[i]->numcores && splicertable.data[sptable_cnt].core!=data[ds]->holes[i]->core[j]->num;j++);
1151:       if(j<data[ds]->holes[i]->numcores)
1152:       {
1153:         /* find first value for this core
1154:         */
1155:         coreptr=data[ds]->holes[i]->core[j];
1156:         for(k=0;k<coreptr->numvalues && coreptr->value[k]->sb_depth < splicertable.data[sptable_cnt].mbsf; k++);
1157: 
1158:         /* get "good" values until bottom of tied core
1159:         */
1160:         if(k<coreptr->numvalues)
1161:         {
1162:           sp->hole[sp->numcores]=i;
1163:           sp->core[sp->numcores]=j;
1164:           sp->Depthoffset[sp->numcores]=splicertable.data[sptable_cnt].mcd-splicertable.data[sptable_cnt].mbsf;
1165:           sp->numpercore[sp->numcores]=0;
1166:           sp->map_to_table[sp->numcores] = sptable_cnt;
1167:           if(sp->numcores > 0) {
1168:             if(strcmp(splicertable.data[sptable_cnt].type_of_splice, "tie") == 0) {
1169:               sp->splice_how[sp->numcores] = REAL;
1170:             }
1171:             else if(strcmp(splicertable.data[sptable_cnt].type_of_splice, "interp") == 0) {
1172:               sp->splice_how[sp->numcores] = INTERPOLATED;
1173:               sp->interpolated_spl_depth[sp->numcores] = splicertable.data[sptable_cnt].mcd;
1174: 
1175:               /* note that if find real value at 'depth' then splicearraynum comes back
1176:                  with indicy of real point. but if interpolated a point then splicearraynum
1177:                  is the indicy of next real point.
1178:               */
1179:               if(smooth.method == GAUSSIAN || smooth.method == OTHER) {
1180:                 sm == YES;
1181:               }
1182:               else {
1183:                 sm = NO;
1184:               }
1185:               (void)FindValueInCoreToSplice(ds, sm, sp->interpolated_spl_depth[sp->numcores],
1186:                                           sp->hole[sp->numcores], sp->core[sp->numcores],
1187:                                           &dum1, &dum2,
1188:                                           &sp->interpolated_value[sp->numcores], &sp->interpolated_sm_value[sp->numcores]);
1189:             }
1190:             else if(strcmp(splicertable.data[sptable_cnt].type_of_splice, "append") == 0) {
1191:               sp->splice_how[sp->numcores] = APPEND;
1192:             }
1193:             else {
1194:               sp->splice_how[sp->numcores] = NONE;
1195:             }
1196:           }
1197:           else {
1198:             sp->splice_how[sp->numcores] = NONE;
1199:           }
1200:           for(;k<coreptr->numvalues && coreptr->value[k]->sb_depth <= splicertable.data[sptable_cnt+1].mbsf; k++)
1201:           {
1202:             valueptr=coreptr->value[k];
1203:             if(valueptr->quality == GOOD)
1204:             {
1205:               sp->MapToOrig[sp->numcores][sp->numpercore[sp->numcores]] = k;
1206:               sp->sb_depth[sp->numcores][sp->numpercore[sp->numcores]] = valueptr->sb_depth+sp->Depthoffset[sp->numcores];
1207:               sp->var[sp->numcores][sp->numpercore[sp->numcores]] = *valueptr->data;
1208:               sp->numpercore[sp->numcores]++;
1209:             }
1210:           }
1211:           ++sp->numcores;
1212:         }
1213:         else {
1214:           warn=SPL_NO_CORE_VALUES;
1215:         }
1216:       }
1217:       else {
1218:         warn=SPL_MISSING_CORE;
1219:       }
1220: 
1221:       sptable_cnt+=2;
1222:     }
1223:     else
1224:     {
1225:       warn=SPL_MISSING_HOLE;
1226:       sptable_cnt+=2;
1227:     }
1228:   }
1229: }
1230: 
1231: void CullData(ds)
1232: int ds;
1233: {
1234:   int i, j, k, hashflag, err;
1235:   XmString xmstring;
1236:   int FindMinMax(), CheckHowManyCulled();
1237:   void SpCull(), SmoothData(), SetMinMax();
1238: 
1239:   for(i=0; i<data[ds]->numholes; ++i) {
1240: 
1241:     for(j=0; j<data[ds]->holes[i]->numcores; ++j) {
1242:       for(k=0; k<data[ds]->holes[i]->core[j]->numvalues; ++k) {
1243:         data[ds]->holes[i]->core[j]->value[k]->quality = GOOD;
1244:         if(fabs(data[ds]->holes[i]->core[j]->value[k]->sb_depth - BAD_CODE) <= ROUNDCHECK) {
1245:           data[ds]->holes[i]->core[j]->value[k]->quality =BAD_SB_DEPTH;
1246:         }
1247:       }
1248:     }
1249: 
1250:     if(cull.method==CULL_TABLE || cull.method == CULL_PARAMETER_AND_TABLE)
1251:     {
1252:            if(i==0 && hashflag!=CREATE_HASH)
1253:            {
1254:                    hashflag=CREATE_HASH;
1255:            }
1256:            else if(i+1==data[dset]->numholes)
1257:                    hashflag=DESTROY_HASH;
1258:            else
1259:                    hashflag=0;
1260:     }
1261:   }
1262:   SpCull(data[ds], data[ds]->type,cull,hashflag);
1263: 
1264:   /* if error culling restore data set by unculling
1265:   */
1266:   if((err = CheckHowManyCulled(ds)) > 0) {
1267:     for(i=0; i<data[ds]->numholes; ++i) {
1268:       for(j=0; j<data[ds]->holes[i]->numcores; ++j) {
1269:         for(k=0; k<data[ds]->holes[i]->core[j]->numvalues; ++k) {
1270:           data[ds]->holes[i]->core[j]->value[k]->quality = GOOD;
1271:           if(fabs(data[ds]->holes[i]->core[j]->value[k]->sb_depth - BAD_CODE) <= ROUNDCHECK) {
1272:             data[ds]->holes[i]->core[j]->value[k]->quality =BAD_SB_DEPTH;
1273:           }
1274:         }
1275:       }
1276:     }
1277:   }
1278: 
1279: 
1280:   if((err = FindMinMax(dset)) > 0) {
1281:     SpManageWarningBox("Warning; Could not find the min and max values of this data set. Using defaults.");
1282:     if(data[ds]->type == GRAPE) {
1283:       data[ds]->min = GRAPEMIN;
1284:       data[ds]->max = GRAPEMAX;
1285:     }
1286:     else if(data[ds]->type == PWAVE) {
1287:       data[ds]->min = PWAVEMIN;
1288:       data[ds]->max = PWAVEMAX;
1289:     }
1290:     else if(data[ds]->type == SUSCEPTIBILITY) {
1291:       data[ds]->min = SUSCMIN;
1292:       data[ds]->max = SUSCMAX;
1293:     }
1294:     else if(data[ds]->type == NATURALGAMMA) {
1295:       data[ds]->min = NGAMMAMIN;
1296:       data[ds]->max = NGAMMAMAX;
1297:     }
1298:     else if(data[ds]->type == REFLECTANCEMST) {
1299:       data[ds]->min = REFLECTMIN;
1300:       data[ds]->max = REFLECTMAX;
1301:     }
1302:     else if(data[ds]->type == OTHERTYPE) {
1303:       data[ds]->min = OTHERMIN;
1304:       data[ds]->max = OTHERMAX;
1305:     }
1306:     else {
1307:     } 
1308:   }
1309:   else {
1310:     data[ds]->min = minvar;
1311:     data[ds]->max = maxvar;
1312:   }
1313:   SetMinMax(ds);
1314: 
1315:   if(smooth.method!=NONE) {
1316:     SmoothData(ds);
1317:   }
1318: }
1319: 
1320: void SmoothData(ds)
1321: int ds;
1322: {
1323:   int i;
1324:   XmString xmstring;
1325:   void SpSmooth();
1326: 
1327:   xmstring = XmStringCreateSimple("Smoothing:");
1328:   XtVaSetValues(messageBoxWorking,XmNmessageString,xmstring,NULL);
1329:   XmStringFree(xmstring);
1330: 
1331:   for(i=0; i<data[ds]->numholes; ++i) {
1332:     if((charptr=(char*)calloc(100,CHAR_SIZE))==NULL)
1333:                                         SpBadMemAlloc("SmoothData 1");
1334:     (void)sprintf(charptr,"\nData : Smoothing data hole %c.",data[ds]->holes[i]->name);
1335:     SpUpdateReport(charptr);
1336:     free(charptr);
1337: 
1338:     if(i == 0) {
1339:       if(smooth.width_units == DEPTH && smooth.width < MINSMOOTHWIDTH * average_depth_step) {
1340:         if((charptr=(char*)calloc(200,CHAR_SIZE))==NULL)
1341:                                         SpBadMemAlloc("SmoothData 2");
1342:         (void)sprintf(charptr, "The width of the filter is too small. Please create a filter as wide or wider than %dcm.", 
1343:                                  MINSMOOTHWIDTH * average_depth_step);
1344:         SpManageWarningBox(charptr);
1345:         free(charptr);
1346:         smooth.method = NONE;
1347:       }
1348:       else if(smooth.width_units == POINTS && smooth.width < MINSMOOTHWIDTH) {
1349:         if((charptr=(char*)calloc(200,CHAR_SIZE))==NULL)
1350:                                         SpBadMemAlloc("SmoothData 3");
1351:         (void)sprintf(charptr, "The width of the filter is too small. Please create a filter as wide or wider than %d pts.", 
1352:                                  MINSMOOTHWIDTH);
1353:         SpManageWarningBox(charptr);
1354:         free(charptr);
1355:         smooth.method = NONE;
1356:       }
1357:     }
1358: 
1359:     if(smooth.method!=NONE) {
1360:       SpSmooth(data[ds]->holes[i]);
1361:     }
1362:   }
1363: }
1364: 
1365: void DecimateData(ds, decimate)
1366: int ds;
1367: int decimate;
1368: {
1369:   int i;
1370:   XmString xmstring;
1371:   void SpDecimate();
1372: 
1373:   CullData(ds);
1374: 
1375:   for(i=0; i<data[ds]->numholes; ++i) {
1376:     if((charptr=(char*)calloc(100,CHAR_SIZE))==NULL)
1377:                        SpBadMemAlloc("DecimateData 1");
1378:     (void)sprintf(charptr,"\nData : Decimating data hole %c.",data[ds]->holes[i]->name);
1379:     SpUpdateReport(charptr);
1380:     xmstring=XmStringCreateLocalized(&charptr[1]);
1381:     XtVaSetValues(messageBoxWorking,XmNmessageString,xmstring, NULL);
1382:     XmStringFree(xmstring);
1383:     free(charptr);
1384: 
1385:     SpDecimate(data[ds]->holes[i],decimate);
1386:     
1387:   }
1388: }
1389: 
1390: void DetrendData(ds, how)
1391: int ds;
1392: int how;
1393: {
1394:   int i, j, k;
1395:   float mean, sum, sum1, sum2, cnt, tbar, denom, xalpha, xbeta, fi;
1396: 
1397:   /* calculate mean
1398:   */
1399:   sum=0.0;
1400:   cnt = 0.0;
1401:   for(i=0; i<data[ds]->numholes; ++i) {
1402:     for(j=0; j<data[ds]->holes[i]->numcores; ++j) {
1403:       for(k=0; k<data[ds]->holes[i]->core[j]->numvalues; ++k) {
1404:         sum = sum + *data[ds]->holes[i]->core[j]->value[k]->data;
1405:         ++cnt;
1406:       }
1407:     }
1408:   }
1409:   mean = sum/cnt;
1410: 
1411: 
1412:   /* mean detrend
1413:   */
1414: 
1415:   if(how == MEAN_DETREND) {
1416: /*
1417:     xdetrend = x[i] - mean;
1418: */
1419:   }
1420: 
1421:   /* linear detrend
1422:   */
1423: 
1424:   else if(how == LINEAR_DETREND) {
1425:  
1426: /*
1427:         sum1=0.0;
1428:         sum2=0.0;
1429:         tbar = (cnt-1)/2.0;
1430:         denom = (cnt-1.0)*cnt*(cnt+1.0)/6.0;
1431:         do 250 i=1,nw
1432:           sum2=2*i-nw+1
1433:           sum2=(x(i)-vmean)*sum2
1434:   250     sum1=sum1+sum2
1435:         xalpha = sum1/denom;
1436:         xbeta = vmean-xalpha*tbar;
1437:         do 260 i=1,nw
1438:           fi = i-1;
1439:   260     x(i)=x(i)-fi*xalpha-xbeta;
1440: */
1441: 
1442:   }
1443: }
1444: 
1445: void SpUpdateFileSelection(how, who, ptrpath)
1446: int     how;
1447: int     who;
1448: char    *ptrpath;
1449: {
1450:         char            *tmpfile=NULL, *textstring, filetype[13], *filename;
1451:         int             numfiles=0,numdirs=0,alloc_file,alloc_dir,i;
1452:         XmString        *filelist, *dirlist, *selected_dir;
1453:         FILE            *fptr;
1454:   
1455:         if(how != FILE_FILTER && how != DIRECTORIES) {
1456:           return;
1457:         }
1458: 
1459:         tmpfile=tmpnam(tmpfile);
1460:         if(how==FILE_FILTER) /* file filter was updated */
1461:         {
1462:                 if(who == PREVIEWINPUT) {
1463:                   textstring = XmTextGetString(textFileSelectionFilterPreviewInput);
1464:                 }
1465:                 else if(who == SAVEDATA) {
1466:                   textstring = XmTextGetString(textFieldSaveDataFilter);
1467:                 }
1468:                 else {
1469:                   textstring = XmTextGetString(textFileSelectionFilter);
1470:                 }
1471:                 if((int)strlen(textstring)==0)  /* no filename */
1472:                 {
1473:                         if((filename=(char*)calloc((int)strlen(ptrpath)+5,CHAR_SIZE))==NULL)
1474:                                 SpBadMemAlloc("SpUpdateFileSelction 1");
1475:                         (void)sprintf(filename,"%s/*",ptrpath);
1476:                 }
1477:                 else if(textstring[0]=='/') /* full path used */
1478:                 {
1479:                         if((filename=(char*)calloc((int)strlen(textstring)+2,CHAR_SIZE))==NULL)
1480:                                 SpBadMemAlloc("SpUpdateFileSelction 2");
1481:                         (void)strcpy(filename,textstring);
1482:                         *strrchr(textstring,'/')= '\0';
1483:                         (void)strcpy(ptrpath,textstring);
1484:                         /* set current path */
1485:                 }
1486:                 else if (!strchr(textstring,'/'))       /* only filename */
1487:                 {
1488:                         if((filename=(char*)calloc((int)strlen(ptrpath)+(int)strlen(textstring)+5,CHAR_SIZE))==NULL)
1489:                                 SpBadMemAlloc("SpUpdateFileSelction 4");
1490:                         (void)sprintf(filename,"%s/%s",ptrpath,textstring);
1491:                 }
1492:                 else                            /* relative path and filename */
1493:                 {
1494:                         if((filename=(char*)calloc((int)strlen(ptrpath)+(int)strlen(textstring)+5,CHAR_SIZE))==NULL)
1495:                                 SpBadMemAlloc("SpUpdateFileSelction 5");
1496:                         (void)sprintf(filename,"%s/%s",ptrpath,textstring);
1497:                         *strrchr(textstring,'/')= '\0';
1498:                         (void)strcpy(ptrpath,textstring);
1499:                 }
1500:                 if(who == PREVIEWINPUT) {
1501:                   XtVaSetValues(textFileSelectionFilterPreviewInput, XmNvalue, filename, NULL);
1502:                 }
1503:                 else {
1504:                   XtVaSetValues(textFileSelectionFilter, XmNvalue, filename, NULL);
1505:                 }
1506:                 i=(int)strlen(filesearch_string)+(int)strlen(filename)+(int)strlen(tmpfile)+100;
1507:                 if((charptr=(char*)calloc(i,CHAR_SIZE))==NULL)
1508:                         SpBadMemAlloc("SpUpdateFileSelction 7");
1509:                 else
1510:                         (void)sprintf(charptr,filesearch_string,filename,tmpfile);
1511: 
1512:                 XtFree(textstring);
1513:         }
1514:         else if(how == DIRECTORIES) /* directories were updated */
1515:         {
1516:                 if(who == PREVIEWINPUT) {
1517:                   XtVaGetValues(listFileSelectionDirPreviewInput,XmNselectedItems,&selected_dir,XmNselectedItemCount,&numdirs,NULL);
1518:                 }
1519:                 else if(who == SAVEDATA) {
1520:                   XtVaGetValues(listSaveDataDirectory,XmNselectedItems,&selected_dir,XmNselectedItemCount,&numdirs,NULL);
1521:                 }
1522:                 else {
1523:                   XtVaGetValues(listFileSelectionDirectories,XmNselectedItems,&selected_dir,XmNselectedItemCount,&numdirs,NULL);
1524:                 }
1525:                 if(numdirs == 1)
1526:                 {
1527:                         if(!XmStringGetLtoR(selected_dir[0],XmFONTLIST_DEFAULT_TAG,&textstring))
1528:                         {
1529:                                 message.error=True;
1530:                                 message.fatal=False;
1531:                                 message.caller=NONE;
1532:                                 message.type=XmDIALOG_WARNING;
1533:                                 message.default_button=XmDIALOG_OK_BUTTON;
1534:                                 if((message.label=(char*)calloc(200,CHAR_SIZE))==NULL)
1535:                                         SpBadMemAlloc("SpUpdateFileSelction 7a");
1536:                                 else
1537:                                         (void)sprintf(message.label,"Unable to convert selected_dir[0]");
1538:                                 SpManageMessage(message);
1539:                         }
1540:                         if(strcmp(textstring,"..") == 0 && strcmp(ptrpath, "/") == 0)
1541:                         {
1542:                         }
1543:                         else if(!strcmp(textstring,".."))       /* move back a directory */
1544:                         {
1545:                                 *strrchr(ptrpath,'/')='\0';
1546:                                 if(strcmp(ptrpath, "") == 0)
1547:                                 {
1548:                                   (void)sprintf(ptrpath, "/");
1549:                                 }
1550:                         }
1551:                         else  if(strcmp(textstring,"."))        /* new directory */
1552:                         {
1553:                                 if(strcmp(ptrpath, "/") == 0)
1554:                                 {
1555:                                   (void)sprintf(ptrpath,"/%s",textstring);
1556:                                 }
1557:                                 else
1558:                                 {
1559:                                   (void)sprintf(ptrpath,"%s/%s",ptrpath, textstring);
1560:                                 }
1561:                         }
1562:                         XtFree(textstring);
1563:                         if((charptr=(char*)calloc((int)strlen(dirsearch_string)+(int)strlen(ptrpath)+(int)strlen(tmpfile)+100,CHAR_SIZE))==NULL)
1564:                                 SpBadMemAlloc("SpUpdateFileSelction 9");
1565:                         (void)sprintf(charptr,dirsearch_string,ptrpath, tmpfile);
1566: 
1567:                         /* update filter field */
1568:                         if((filename=(char*)calloc((int)strlen(ptrpath)+5,CHAR_SIZE))==NULL)
1569:                                 SpBadMemAlloc("SpUpdateFileSelction 10");
1570:                         if(strcmp(ptrpath, "/") == 0)
1571:                         {
1572:                           (void)sprintf(filename,"/*");
1573:                         }
1574:                         else
1575:                         {
1576:                           (void)sprintf(filename,"%s/*",ptrpath);
1577:                         }
1578:                         if(who == PREVIEWINPUT) {
1579:                           XmTextSetString(textFileSelectionFilterPreviewInput,filename);
1580:                           XmListDeselectAllItems(listFileSelectionDirPreviewInput);
1581:                         }
1582:                         else if(who == SAVEDATA) {
1583:                           XmTextSetString(textFieldSaveDataFilter,filename);
1584:                           XmListDeselectAllItems(listSaveDataDirectory);
1585:                         }
1586:                         else {
1587:                           XmTextSetString(textFileSelectionFilter,filename);
1588:                           XmListDeselectAllItems(listFileSelectionDirectories);
1589:                         }
1590:                 }
1591:                 else
1592:                 {
1593:                         SpManageWarningBox("Error opening new directory!");
1594:                 }
1595:         }
1596:         system(charptr);
1597:         free(charptr);
1598: 
1599:         /* get file and directory names for filter path */
1600:         numdirs=0;
1601:         if((filelist=(XmString*)calloc((alloc_file=1000),sizeof(XmString**)))==NULL)
1602:                         SpBadMemAlloc("SpUpdateFileSelction 10b");
1603:         if((dirlist=(XmString*)calloc((alloc_dir=1000),sizeof(XmString**)))==NULL)
1604:                         SpBadMemAlloc("SpUpdateFileSelction 10c");
1605:         dirlist[numdirs++]=XmStringCreateLocalized(".");
1606:         dirlist[numdirs++]=XmStringCreateLocalized("..");
1607:         if((fptr=fopen(tmpfile,"r"))!=NULL)
1608:         {
1609:                 if((charptr=(char*)realloc(filename,1000*CHAR_SIZE))==NULL)
1610:                         SpBadMemAlloc("SpUpdateFileSelction 11");
1611:                 else
1612:                         filename=charptr;
1613:                 while(fscanf(fptr,"%s %s",filetype,filename)!=EOF)
1614:                 {
1615:                         if(!strcmp(filetype,"DIR"))             /* directory names */
1616:                         {
1617:                                 if(numdirs>=alloc_dir)
1618:                                 {
1619:                                         if((charptr=(char*)realloc((char*)dirlist,(alloc_dir+=1000)*sizeof(XmString*)))==NULL)
1620:                                                         SpBadMemAlloc("SpUpdateFileSelction 11b");
1621:                                         else
1622:                                                 dirlist=(XmString*)charptr;
1623:                                 }
1624:                                 dirlist[numdirs++]=XmStringCreateLocalized(filename);
1625:                         }
1626:                         else if(!strcmp(filetype,"FILE"))       /* file names */
1627:                         {
1628:                                 if(numfiles>=alloc_file)
1629:                                 {
1630:                                         if((charptr=(char*)realloc((char*)filelist,(alloc_file+=1000)*sizeof(XmString*)))==NULL)
1631:                                                         SpBadMemAlloc("SpUpdateFileSelction 11c");
1632:                                         else
1633:                                                 filelist=(XmString*)charptr;
1634:                                 }
1635:                                 filelist[numfiles++]=XmStringCreateLocalized(filename);
1636:                         }
1637:                         else if(!strcmp(filetype,"LINK"))       /*  symbolic link name */
1638:                         {
1639:                                 if((charptr=(char*)realloc((char*)filename,((int)strlen(filename)+25)*sizeof(XmString*)))==NULL)
1640:                                         SpBadMemAlloc("SpUpdateFileSelction 11d");
1641:                                 else
1642:                                         filename=charptr;
1643:                                 (void)strcat(filename," (sym-link)");
1644:                                 if(numfiles>=alloc_file)
1645:                                 {
1646:                                         if((charptr=(char*)realloc((char*)filelist,(alloc_file+=1000)*sizeof(XmString*)))==NULL)
1647:                                                         SpBadMemAlloc("SpUpdateFileSelction 11e");
1648:                                         else
1649:                                                 filelist=(XmString*)charptr;
1650:                                 }
1651:                                 filelist[numfiles++]=XmStringCreateLocalized(filename);
1652:                         }
1653:                 }
1654:                 free(filename);
1655:         }
1656: 
1657:         /* invalid directory */
1658:         if(who == SAVEDATA <=0 && numdirs<=2)
1659:         {
1660:                 if((charptr=(char*)calloc((int)strlen(filename)+50,CHAR_SIZE))==NULL)
1661:                         SpBadMemAlloc("SpUpdateFileSelction 12");
1662:                 (void)sprintf(charptr,"Unable to do directory search.");
1663:                 SpManageWarningBox(charptr);
1664:                 free(charptr);
1665:         }
1666:         else if(numfiles <=0 && numdirs<=2)
1667:         {
1668:                 if((charptr=(char*)calloc((int)strlen(filename)+50,CHAR_SIZE))==NULL)
1669:                         SpBadMemAlloc("SpUpdateFileSelction 12");
1670:                 (void)sprintf(charptr,"Invalid or empty filter: %s !",filename);
1671:                 SpManageWarningBox(charptr);
1672:                 free(charptr);
1673:         }
1674: 
1675:         /* if files found - put them in file list */
1676:         if(who == PREVIEWINPUT) {
1677:           if(numfiles>0)
1678:             XtVaSetValues(listFileSelectionFilesPreviewInput,XmNitemCount,numfiles,XmNvisibleItemCount,numfiles
1679:                                             ,XmNitems,filelist,NULL);
1680:           else    /* no files found  - clear list */
1681:             XmListDeleteAllItems(listFileSelectionFilesPreviewInput);
1682: 
1683:           /* put directories in dir list */
1684:           XtVaSetValues(listFileSelectionDirPreviewInput,XmNitemCount,numdirs,XmNvisibleItemCount,numdirs
1685:                                         ,XmNitems,dirlist,NULL);
1686:         }
1687:         else if(who == SAVEDATA) {
1688:           /* put directories in dir list */
1689:           XtVaSetValues(listSaveDataDirectory,XmNitemCount,numdirs,XmNvisibleItemCount,numdirs
1690:                                         ,XmNitems,dirlist,NULL);
1691:         }
1692:         else {
1693:           if(numfiles>0)
1694:             XtVaSetValues(listFileSelectionFiles,XmNitemCount,numfiles,XmNvisibleItemCount,numfiles
1695:                                             ,XmNitems,filelist,NULL);
1696:           else    /* no files found  - clear list */
1697:             XmListDeleteAllItems(listFileSelectionFiles);
1698: 
1699:           /* put directories in dir list */
1700:           XtVaSetValues(listFileSelectionDirectories,XmNitemCount,numdirs,XmNvisibleItemCount,numdirs
1701:                                         ,XmNitems,dirlist,NULL);
1702:         }
1703: 
1704:         /* clean up memory used and remove temporary file */
1705:         while(numdirs>0) XmStringFree(dirlist[--numdirs]);
1706:         while(numfiles>0) XmStringFree(filelist[--numfiles]);
1707:        
1708:         free(dirlist);
1709:         free(filelist);
1710:         (void)remove(tmpfile);
1711: }
1712: 
1713: void RecalcDatumAges()
1714: {
1715:   int i, j, k, n, f, d, done;
1716:   float sedrate[MAXAGE], tempmcd[MAXAGE], tempage[MAXAGE], newage, diffage;
1717: 
1718:   /* make a temp array for all dataums that are tuned
1719:   */
1720:   n=0;
1721:   for(i=0; i<agemodel_cnt; ++i) {
1722:     if(agemodel[i].type == TIME) {
1723:       tempage[n] = agemodel[i].aveage;
1724:       tempmcd[n] = agemodel[i].avemcd;
1725:       ++n;
1726:     } 
1727:   }
1728: 
1729:   /* if no tuned datums then return original values to strat datums
1730:   */
1731:   if(n == 0) {
1732:     for(i=0; i<agemodel_cnt; ++i) {
1733:       if(agemodel[i].type != HANDPICK) {
1734:         f = agemodel[i].agemodel_maptostratfile;
1735:         d = agemodel[i].agemodel_maptodatum;
1736:         if(f >= 0 && d >= 0) {
1737:           agemodel[i].aveage = strat[f]->data[d].aveage;
1738:         }
1739:       }
1740:       else if(agemodel[i].type == HANDPICK) {
1741:      
1742:       }
1743:     }
1744:   }
1745: 
1746:   else if(agemodel_cnt == 1) {
1747:     return;
1748:   }
1749: 
1750:   /* calculate new strat ages
1751:   */
1752:   else {
1753:     /* calculate the sed rate using only the tuned datums
1754:     */
1755:     if(n > 1) {
1756:       for(i=1; i<n; ++i) {
1757:         sedrate[i-1] = (tempmcd[i] - tempmcd[i-1])/(tempage[i] - tempage[i-1]);
1758:       }
1759:       sedrate[n-1] = sedrate[n-2];
1760:     }
1761:     else {
1762:       sedrate[0] = agemodel_avesedrate;
1763:     }
1764: 
1765:     i = 0;
1766:     done = NO;
1767:     while(i<agemodel_cnt && done == NO) {
1768:       /* if the current datum is a strat datum
1769:       */
1770:       if(agemodel[i].type != TIME) {
1771: 
1772:         /* if the strat datum's mcd falls above first tuned datum or below last
1773:         */
1774:         if(agemodel[i].avemcd < tempmcd[0]) {
1775:           if(agemodel[i].type != HANDPICK) {
1776:             f = agemodel[i].agemodel_maptostratfile;
1777:             d = agemodel[i].agemodel_maptodatum;
1778:             if(f >= 0 && d >= 0) {
1779:               agemodel[i].aveage = strat[f]->data[d].aveage;
1780:             }
1781:             else {
1782: 
1783:             }
1784:           }
1785:           else if(agemodel[i].type == HANDPICK) {
1786: 
1787:           }
1788:           if(agemodel[i].aveage > tempage[0]) {
1789:             agemodel[i].aveage = tempage[0] - (tempmcd[0] - agemodel[i].avemcd)/sedrate[0];
1790:             for(j=i; j>0; --j) {
1791:               if(agemodel[j-1].aveage > agemodel[j].aveage) {
1792:                 agemodel[j-1].aveage = tempage[0] - (tempmcd[0] - agemodel[j-1].avemcd)/sedrate[0];
1793:               }
1794:             }
1795:           }
1796:         }
1797:         else if(agemodel[i].avemcd > tempmcd[n-1]) {
1798:           if(agemodel[i].type != HANDPICK) {
1799:             f = agemodel[i].agemodel_maptostratfile;
1800:             d = agemodel[i].agemodel_maptodatum;
1801:             if(f >= 0 && d >= 0) {
1802:               agemodel[i].aveage = strat[f]->data[d].aveage;
1803:             }
1804:             else {
1805: 
1806:             }
1807:           }
1808:           else if(agemodel[i].type == HANDPICK) {
1809: 
1810:           }
1811:           if(agemodel[i].aveage < tempage[n-1]) {
1812:             newage = tempage[n-1] + (agemodel[i].avemcd - tempmcd[n-1])/sedrate[n-1];
1813:             diffage = newage - agemodel[i].aveage;
1814:             agemodel[i].aveage = newage;
1815:           }
1816:           for(j=i; j<agemodel_cnt-1; ++j) {
1817:             if(agemodel[j+1].aveage < agemodel[j].aveage) {
1818:               agemodel[j+1].aveage = agemodel[j+1].aveage + diffage;
1819:             }
1820:             else {
1821:               if(agemodel[j+1].type != HANDPICK) {
1822:                 f = agemodel[j+1].agemodel_maptostratfile;
1823:                 d = agemodel[j+1].agemodel_maptodatum;
1824:                 if(f >= 0 && d >= 0) {
1825:                   agemodel[j+1].aveage = strat[f]->data[d].aveage;
1826:                 }
1827:               }
1828:               else if(agemodel[j+1].type == HANDPICK) {
1829:               }
1830:             }
1831:           }
1832:           done = YES;
1833:         }
1834: 
1835:         /* otherwise calculate(interpolate) a new tuned age for strat datum using tuned dates
1836:            from above and below
1837:         */
1838:         else {
1839:           for(j=0; j<n-1; ++j) {
1840:             if(agemodel[i].avemcd >= tempmcd[j] && agemodel[i].avemcd < tempmcd[j+1]) {
1841:               agemodel[i].aveage = tempage[j] + (agemodel[i].avemcd - tempmcd[j])/sedrate[j];
1842:             }
1843:           }
1844:         }
1845:       }
1846:       ++i;
1847:     }
1848:   }
1849: }
1850: 
1851: int VerifyFileReadable(file, kind, who)
1852: char *file;
1853: int kind;
1854: int who;
1855: {
1856:   int len;
1857:   char *checkfile, *tmpfile=NULL, *tmpinfo;
1858:   FILE *fptr;
1859: 
1860:   /* check that file can be openned
1861:   */
1862:   if(who != SAVEDATA) {
1863:     if(kind == OPEN) {
1864:       if((fptr=fopen(file, "r")) == NULL) {
1865:         return NO;
1866:       }
1867:       fclose(fptr);
1868:     }
1869:     else if(kind == SAVE) {
1870:       if((fptr=fopen(file, "w")) == NULL) {
1871:         return NO;
1872:       }
1873:       fclose(fptr);
1874:     }
1875:     else {
1876:       return NO;
1877:     }
1878:   }
1879: 
1880:   /* check that file is of a readable type
1881:   */
1882:   tmpfile=tmpnam(tmpfile);
1883:   if((tmpinfo=(char*)calloc((int)strlen(file)+(int)strlen(tmpfile)+50,CHAR_SIZE))==NULL) {
1884:                         SpBadMemAlloc("VerifyFileReadable1");
1885:   }
1886:   else {
1887:     (void)sprintf(tmpinfo,"ls -dF %s > %s", file, tmpfile);
1888:   }
1889:   system(tmpinfo);
1890:   XtFree(tmpinfo);
1891:   if(tmpinfo) {
1892:     tmpinfo = NULL;
1893:   }
1894:   
1895:   if((fptr=fopen(tmpfile,"r"))!=NULL) {
1896:     if((checkfile=(char*)calloc((int)strlen(file)+100,CHAR_SIZE))==NULL) {
1897:                         SpBadMemAlloc("VerifyFileReadable2");
1898:     }
1899:     (void)fscanf(fptr, "%s", checkfile);
1900:     fclose(fptr);
1901:     (void)remove(tmpfile);
1902:     len=strlen(checkfile);
1903:     if(len<1) {
1904:       XtFree(checkfile);
1905:       if(checkfile) {
1906:         checkfile = NULL;
1907:       }
1908:       return NO;
1909:     }
1910:     /* '/' indicates the file is a directory, '*' an executable and '@ a symbolic link
1911:     */
1912:     else if(who == OPENCULLTABLE &&
1913:             (strcmp(&checkfile[len-1], "/") == 0 || strcmp(&checkfile[len-1], "@") == 0)) {
1914:       XtFree(checkfile);
1915:       if(checkfile) {
1916:         checkfile = NULL;
1917:       }
1918:       return NO;
1919:     }
1920:     else if(who != OPENCULLTABLE && who != SAVEDATA &&
1921:             (strcmp(&checkfile[len-1], "/") == 0 || strcmp(&checkfile[len-1], "*") == 0 || strcmp(&checkfile[len-1], "@") == 0)) {
1922:       XtFree(checkfile);
1923:       if(checkfile) {
1924:         checkfile = NULL;
1925:       }
1926:       return NO;
1927:     }
1928:     else if(who == SAVEDATA && (strcmp(&checkfile[len-1], "/") != 0)) {
1929:       XtFree(checkfile);
1930:       if(checkfile) {
1931:         checkfile = NULL;
1932:       }
1933:       return NO;
1934:     }
1935:     else {
1936:       XtFree(checkfile);
1937:       if(checkfile) {
1938:         checkfile = NULL;
1939:       }
1940:       return YES;
1941:     }
1942:   }
1943:   else {
1944:     (void)remove(tmpfile);
1945:     return NO;
1946:   }
1947: }
1948: 
1949: int CheckHowManyCulled(ds)
1950: int ds;
1951: {
1952:   int i, j, k;
1953:   float  cullcnt, count, percent;
1954: 
1955:   for(i=0; i<data[ds]->numholes; ++i) {
1956:     for(j=0; j<data[ds]->holes[i]->numcores; ++j) {
1957:       for(k=0; k<data[ds]->holes[i]->core[j]->numvalues; ++k) {
1958:         if(data[ds]->holes[i]->core[j]->value[k]->quality != GOOD) {
1959:           ++cullcnt;
1960:           ++count;
1961:         }
1962:         else {
1963:           ++count;
1964:         }
1965:       }
1966:     }
1967:   }
1968: 
1969:   percent = (cullcnt/count)*100.0;
1970:   if(percent > WARNCULL) {
1971:     if(percent < TOOMANYCULLED) {
1972:       (void)sprintf(buffer,"Warning: %.1f percent of the data has been culled.", percent);
1973:       SpManageWarningBox(buffer);
1974:       message.error = False;
1975:       return 0;
1976:     }
1977:     else {
1978:       (void)sprintf(buffer,"Error: %.1f percent of the data has been culled. Restoring to unculled values.", percent);
1979:       SpManageWarningBox(buffer);
1980:       message.error = True;
1981:       return 1;
1982:     }
1983:   }
1984:   return 0;
1985: }
1986: 
1987: void WaitCursor(what)
1988: int what;
1989: {
1990: 
1991:   if(what == DEFINEWAITCURSOR) {
1992:     io_wait_cursor = XCreateFontCursor(XtDisplay(formSplicer), XC_watch);
1993:     if(io_wait_cursor != BadAlloc && io_wait_cursor != BadFont && io_wait_cursor != BadValue) {
1994:       XDefineCursor(XtDisplay(formSplicer), XtWindow(formSplicer), io_wait_cursor);
1995:       XFreeCursor(XtDisplay(formSplicer), io_wait_cursor);
1996:     }
1997: /*
1998:       XDefineCursor(XtDisplay(formTie), XtWindow(formTie), io_wait_cursor);
1999:       XDefineCursor(XtDisplay(formSplicing), XtWindow(formSplicing), io_wait_cursor);
2000:       XDefineCursor(XtDisplay(formSpliceToAge), XtWindow(formSpliceToAge), io_wait_cursor);
2001:       XDefineCursor(XtDisplay(formAgeDepth), XtWindow(formAgeDepth), io_wait_cursor);
2002: */
2003:   }
2004:   else if(what == FREEWAITCURSOR) {
2005:       XUndefineCursor(XtDisplay(formSplicer), XtWindow(formSplicer));
2006: /*
2007:       XUndefineCursor(XtDisplay(formTie), XtWindow(formTie));
2008:       XUndefineCursor(XtDisplay(formSplicing), XtWindow(formSplicing));
2009:       XUndefineCursor(XtDisplay(formSpliceToAge), XtWindow(formSpliceToAge));
2010:       XUndefineCursor(XtDisplay(formAgeDepth), XtWindow(formAgeDepth));
2011: */
2012:   }
2013: }
2014: 
2015: int CheckDepthInDescendingOrder(ds)
2016: int ds;
2017: {
2018:   int i, j, k, ndep, out, alloc, *map, howmany;
2019:   char *text, *line;
2020:   float    *pdep;
2021:   XmString xmstring;
2022:   XmTextPosition  pos;
2023: 
2024:   alloc = TOOMANYOUTOFORDER + 5;
2025:   if((text=(char*)calloc(300*alloc, CHAR_SIZE))==NULL)
2026:                 SpBadMemAlloc("CheckDepthInDescendingOrder");
2027:   if((line=(char*)calloc(300, CHAR_SIZE))==NULL)
2028:                 SpBadMemAlloc("CheckDepthInDescendingOrder");
2029: 
2030:   out = NO;
2031:   howmany = 0;
2032:   for(i=0; i<data[ds]->numholes; ++i) {
2033:     for(j=0; j<data[ds]->holes[i]->numcores; ++j) {
2034:       if(data[ds]->holes[i]->core[j]->numvalues < 2) {
2035:       }
2036:       else if(data[ds]->holes[i]->core[j]->numvalues == 2) {
2037: 
2038:       }
2039:       else {
2040:         if((pdep=(float*)calloc(data[ds]->holes[i]->core[j]->numvalues,sizeof(float))) == NULL)
2041:                         SpBadMemAlloc("CheckDepthInDescendingOrder");
2042:         if((map=(int*)calloc(data[ds]->holes[i]->core[j]->numvalues,sizeof(int))) == NULL)
2043:                         SpBadMemAlloc("CheckDepthInDescendingOrder");
2044:         ndep = 0;
2045:         for(k=0; k<data[ds]->holes[i]->core[j]->numvalues; ++k) {
2046:           if(data[ds]->holes[i]->core[j]->value[k]->quality & BAD_SB_DEPTH) {
2047:           }
2048:           else {
2049:             pdep[ndep] = data[ds]->holes[i]->core[j]->value[k]->sb_depth;
2050:             map[ndep] = k;
2051:             ++ndep;
2052:           }
2053:         }
2054: 
2055:         if(ndep >= 3) {
2056:           for(k=1; k<ndep-1; ++k) {
2057:             if(pdep[k] <= pdep[k+1] && pdep[k] >= pdep[k-1]) {
2058:             }
2059:             else {
2060:               ++howmany;
2061:               if(out == NO) {
2062:                 out = YES;
2063:                 (void)sprintf(text,"");
2064:                 (void)sprintf(text,"\n");
2065:                 (void)strcat(text, "The following depths are out of order\n\n");
2066:               }
2067:               if(howmany >= TOOMANYOUTOFORDER) {
2068:                 (void)sprintf(line, "There may be more; stopped checking after %d were found.\n",
2069:                               TOOMANYOUTOFORDER);
2070:                 (void)strcat(text, line);
2071:                 (void)sprintf(line, "Could there be a data type or format mismatch?\n");
2072:                 (void)strcat(text, line);
2073:                 break;
2074:               }
2075:               (void)sprintf(line,"site %d  hole %c  core %d  sect. %s top sect. depth  %.2f(cm)   %.2f(mbsf)\n",
2076:                                   data[ds]->site, data[ds]->holes[i]->name, data[ds]->holes[i]->core[j]->num,
2077:                                   data[ds]->holes[i]->core[j]->value[map[k]]->section,
2078:                                   data[ds]->holes[i]->core[j]->value[map[k]]->top_int,
2079:                                   data[ds]->holes[i]->core[j]->value[map[k]]->sb_depth);
2080:               (void)strncat(text, line, (int)strlen(line));
2081:             }
2082:           }
2083:         }
2084:         free(pdep);
2085:         pdep = NULL;
2086:         free(map);
2087:         map = NULL;
2088:         if(howmany >= TOOMANYOUTOFORDER) {
2089:           break;
2090:         }
2091:       }
2092:     }
2093:     if(howmany >= TOOMANYOUTOFORDER) {
2094:       break;
2095:     }
2096:   }
2097: 
2098:   if(out == YES) {
2099:     pos=XmTextGetLastPosition(textGeneric);
2100:     XmTextInsert(textGeneric,pos,text);
2101:     XmTextShowPosition(textGeneric,0);
2102:     XtManageChild(formGenericText);
2103:     XtPopup(XtParent(formGenericText),XtGrabNone);
2104:     xmstring=XmCvtCTToXmString("Out Of Order Core Depths");
2105:     XtVaSetValues(formGenericText,XmNdialogTitle,xmstring,NULL);
2106:     XmStringFree(xmstring);
2107:   }
2108: 
2109:   free(text);
2110:   text = NULL;
2111:   free(line);
2112:   line = NULL;
2113: 
2114:   return out;
2115: }
2116: 








































Html form generated by Xrefactory version 1.6.7 on Fri Sep 03 17:18:59 2004