Exemple #1
0
int mathfunc() {
  double meanS, noise, meanG, maxS, minS, sum2, stdS, thr, X, minmax;
  int   pixel, pixel2, im, n1, n2, r, c, r1, c1, rG, cG, rS1, rS2, cS1, cS2;
  int   im_r1, im_r2, im_c1, im_c2, r0, c0, rad, d,
        Nfilter, NF2, N, skip_pix, pixelG;
  double fract;
  char msg[1024],pgm[1024];
  FILE  *fp;

  if (nbr_infiles<1 || input_sizes_differ){
    return FALSE;
  }


  if (nbr_outfiles != in_vec_len[0]) {
    sprintf(msg,"Math: You must supply as many output images as input images\n");
    ib_errmsg(msg);
    return FALSE;
  }


  /**************************/
  /* Create output images ***/
  /**************************/
  if (want_output(1))
    create_output_files(nbr_infiles*2, in_object[0]);
  else
    create_output_files(nbr_infiles, in_object[0]);

  /* How large a (mean) filter do we apply to find the ghosting level */
  if ((img_width < 128) || (img_height < 128))
    Nfilter = 5; /* for small matrices, default to a 5x5 filter */
  else
    Nfilter = 11;  /* else default to a 11x11 filter */
  if (nbr_params > 0) Nfilter = (int) in_params[0];
  NF2 = (int) (Nfilter/2);

  fract = 0.8;
  if (nbr_params > 1)
    fract = in_params[1]/100;

  strcpy(pgm,"SNR");
  if (nbr_strings > 0)
    strcpy(pgm,in_strings[0]);

  /**************************/
  /* Calculations ***********/
  /**************************/
  /* Get threshold for segmenting image */
  thr = threshold(in_data[0],img_height,img_width);

  /* Find image boundaries (radius) *********/
  find_object(in_data[0],thr,img_height,img_width,
    &r0,&c0,&im_r1,&im_c1,&im_r2,&im_c2,&rad);
  /* rad is the smallest radius; im_r1/c1/r2/c2 gives maximum extent of object */

  /* Find noise standard deviation **************/
  noise = find_noise(in_data[0],im_r1-3, im_c1-3, im_r2+3, im_c2+3);


  for (im = 0; im < nbr_infiles; im++){

    /* Apply a NxN mean filter to the image */
    filter(in_data[im],out_data[im],Nfilter,img_height,img_width,thr);

    /***********************************************/
    /* Calculate signal intensity and uniformity ***/
    /***********************************************/
    meanS = sum2 = n2 = 0;
    maxS = 0; minS = 1e6;
    for (r = 0; r < img_height; r++) {
      for (c = 0; c < img_width; c++) {
        pixel = r*img_width + c;
        X = in_data[im][pixel];
        d = (int) sqrt((double) ((r-r0)*(r-r0) + (c-c0)*(c-c0)));  /* distance from center */
        if (d <= rad*fract) {
            meanS += X;
            sum2  += (X*X);
            n2++;

            minmax = out_data[im][pixel];
            /* Find max/min filtered signal intensity */
            if (maxS < minmax) {maxS = minmax; rS1=r; cS1 = c;}
            if (minS > minmax) {minS = minmax; rS2=r; cS2 = c;}

        }
      }
    }
    meanS /= n2;
    stdS  = (float) sqrt((double)((sum2/n2) - (meanS*meanS)));


    /******************************************/
    /* Calculate ghost intensity **************/
    /******************************************/
    /* Assume ghosting is in horizontal direction   */
    /* Search +/- Nfilter columns beyond maximum extent of object */
    meanG = 0;
    cG = rG = pixelG = 0;
    skip_pix = (Nfilter > 3 ? Nfilter : 3);
    for (r = im_r1-1; r <= im_r2; r++) {
      /* Check to the left of the image */
      for (c = skip_pix; c < im_c1-skip_pix; c++) {
        pixel = r*img_width + c;
        X = out_data[im][pixel];
        if (X > meanG) {
          meanG = X;
          rG = r; cG = c; pixelG = pixel;
        }
      }
      /* Check to the right of the image */
      for (c = im_c2+skip_pix; c < img_width-skip_pix; c++) {
        pixel = r*img_width + c;
        X = out_data[im][pixel];
        if (X > meanG) {
          meanG = X;
          rG = r; cG = c; pixelG = pixel;
        }
      }
    }

    for (r = 0; r < img_height; r++) {
      for (c = 0; c < img_width; c++) {
        pixel = r*img_width + c;
        X = in_data[im][pixel];
        d = (int) sqrt((double) ((r-r0)*(r-r0) + (c-c0)*(c-c0)));  /* distance from center */
        if (!(d <= rad*fract)) {
          out_data[im][pixel] = 0;
        }
      }
    }
    out_data[im][pixelG] = meanG;




    printf("=========== %s: image %d ===================\n",pgm,im+1);
    printf("Signal, Noise, Ghosting (x100): %.6f, %.6f, %.6f\n",maxS*100,noise*100, meanG*100);
    printf("SNR: %.f (NEMA standard: %.f)\n",maxS/noise, maxS/noise*1.253);
    printf("Ghosting: %.2f%% of max signal (in %dx%dROI)\n",(meanG-noise)/maxS*100,Nfilter,Nfilter);
    printf("Maximum ghosting is in pixel %d, %d\n",cG,rG);
    printf("Percent Image Uniformity is%.2f%%\n",(1-(maxS-minS)/(maxS+minS))*100);
    printf("minS, maxS = %f and %f at (%d,%d),(%d,%d)\n",minS*100,maxS*100,cS2,rS2,cS1,rS1);
/*    printf("Image variation is %.f%%\n",stdS/meanS*100); */

    if ((fp = fopen("SNR_measurements.txt","a")) == NULL) {
      sprintf(msg,"Can't open file SNR_measurements.txt for printing results");
      ib_errmsg(msg);
      return FALSE;
    }
    fprintf(fp,"=========== %s: image %d ===================\n",pgm,im+1);
    fprintf(fp,"Signal, Noise, Ghosting (x100): %.6f, %.6f, %.6f\n",maxS*100,noise*100, meanG*100);
    fprintf(fp,"SNR: %.f (NEMA standard: %.f)\n",maxS/noise, maxS/noise*1.253);
    fprintf(fp,"Ghosting: %.2f%% of max signal (in %dx%dROI)\n",(meanG-noise)/maxS*100,Nfilter,Nfilter);
    fprintf(fp,"Maximum ghosting is in pixel %d, %d\n",cG,rG);
    fprintf(fp,"Percent Image Uniformity is%.2f%%\n",(1-(maxS-minS)/(maxS+minS))*100);
    fprintf(fp,"minS, maxS = %f and %f at (%d,%d),(%d,%d)\n",minS*100,maxS*100,cS2,rS2,cS1,rS1);
    fclose(fp);

  }  /* end image loop */

  return TRUE;
}
Exemple #2
0
void *pt_ecal(void *args)
{
  ecal_t arg = *(ecal_t *) args; 
  free(args);

  int i,j;
  int result;

  char ecal_id[250];
  // now we can unlock our things, since nothing else should use them

  fd_set thread_fdset;
  FD_ZERO(&thread_fdset);
  for (i=0;i<19;i++){
    if ((0x1<<i) & arg.crate_mask)
      FD_SET(rw_xl3_fd[i],&thread_fdset);
  }

  char comments[1000];
  memset(comments,'\0',1000);
  char command_buffer[1000];
  memset(command_buffer,'\0',1000);

  system("clear");
  pt_printsend("------------------------------------------\n");
  pt_printsend("Welcome to ECAL+!\n");
  pt_printsend("------------------------------------------\n");



  if (!arg.old_ecal){
    // once this is set we can no longer send other commands
    running_ecal = 1;
    sbc_lock = 0;
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask)
        xl3_lock[i] = 0;
    }



    get_new_id(ecal_id);


    pt_printsend("\nYou have selected the following slots:\n\n");
    for (i=0;i<19;i++){
      if ((0x1<<(i)) & arg.crate_mask){
        pt_printsend("crate %d: 0x%08x\n",i,arg.slot_mask[i]);
      }
    }
    pt_printsend("------------------------------------------\n");
    pt_printsend("Hit enter to start, or type quit if anything is incorrect\n");
    read_from_tut(comments);
    if (strncmp("quit",comments,4) == 0){
      pt_printsend("Exiting ECAL\n");
      running_ecal = 0;
      unthread_and_unlock(0,0x0,arg.thread_num); 
      return;
    }
    pt_printsend("------------------------------------------\n");

    time_t curtime = time(NULL);
    struct timeval moretime;
    gettimeofday(&moretime,0);
    struct tm *loctime = localtime(&curtime);
    char log_name[500] = {'\0'};  // random size, it's a pretty nice number though.

    strftime(log_name, 256, "ECAL_%Y_%m_%d_%H_%M_%S_", loctime);
    sprintf(log_name+strlen(log_name), "%d.log", (int)moretime.tv_usec);
    start_logging_to_file(log_name);

    sbc_lock = 1;
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask)
        xl3_lock[i] = 1;
    }



    pt_printsend("Creating ECAL document...\n");
    // post ecal doc
    post_ecal_doc(arg.crate_mask,arg.slot_mask,log_name,ecal_id,&thread_fdset);

    pt_printsend("Created! ECAL id: %s\n\n",ecal_id);
    pt_printsend("------------------------------------------\n");
    
    sbc_lock = 0;
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask)
        xl3_lock[i] = 0;
    }

    // ok we are set up, time to start

    // initial CRATE_INIT
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x -x -v",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // FEC_TEST
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"fec_test -c %d -s %04x -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = fec_test(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }

        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("-------------------------------------------\n");
      }
    }


    // BOARD_ID
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"board_id -c %d -s %04x",i,arg.slot_mask[i]);
          result = board_id(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }

        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("-------------------------------------------\n");
      }
    }


    // MTC_INIT
    do {
      sprintf(command_buffer,"mtc_init -x");
      result = mtc_init(command_buffer);
      if (result == -2 || result == -3){
        running_ecal = 0;
        unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
        return;
      }
    } while (result != 0);
    while (sbc_lock != 0){}
    pt_printsend("-------------------------------------------\n");

    

    // CGT_TEST

    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"cgt_test_1 -c %d -s %04x -p FFFFFFFF -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = cgt_test(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }

        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("-------------------------------------------\n");
      }
    }

    // MTC_INIT
    do {
      sprintf(command_buffer,"mtc_init -x");
      result = mtc_init(command_buffer);
      if (result == -2 || result == -3){
        running_ecal = 0;
        unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
        return;
      }
    } while (result != 0);
    while (sbc_lock != 0){}
    pt_printsend("-------------------------------------------\n");


    // CRATE_INIT with default values
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // CRATE_CBAL
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_cbal -c %d -s %04x -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = crate_cbal(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // CRATE_INIT with vbal values
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x -B",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("-------------------------------------------\n");
      }
    }


    // PED_RUN
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"ped_run -c %d -s %04x -b -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = ped_run(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("-------------------------------------------\n");
      }
    }

//    pt_printsend("Time to do the cable business\n");
//    pt_printsend("ECL output --> EXT PED (long cable)\n");
//    pt_printsend("TTL input --> Global trigger)\n");
//    pt_printsend("Hit enter when ready\n");

//    read_from_tut(comments);

    // MTC_INIT
    do {
      sprintf(command_buffer,"mtc_init -x");
      result = mtc_init(command_buffer);
      if (result == -2 || result == -3){
        running_ecal = 0;
        unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
        return;
      }
    } while (result != 0);
    while (sbc_lock != 0){}
    pt_printsend("-------------------------------------------\n");


    // CRATE_INIT with default + vbal values
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x -B",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // SET_TTOT
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"set_ttot -c %d -s %04x -t 420 -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = set_ttot(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // CRATE_INIT with default + vbal + tdisc values
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x -B -D",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // GET_TTOT
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"get_ttot -c %d -s %04x -t 400 -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = get_ttot(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


//    pt_printsend("Time to remove cables\n");
//    pt_printsend("Hit enter when ready\n");

//    read_from_tut(comments);


    // CRATE_INIT with default + vbal + tdisc values
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x -B -D",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // DISC_CHECK
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"disc_check -c %d -s %04x -n 500000 -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = disc_check(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // MTC_INIT
    do {
      sprintf(command_buffer,"mtc_init -x");
      result = mtc_init(command_buffer);
      if (result == -2 || result == -3){
        running_ecal = 0;
        unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
        return;
      }
    } while (result != 0);
    while (sbc_lock != 0){}
    pt_printsend("-------------------------------------------\n");


    // CRATE_INIT with default + vbal + tdisc values
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x -B -D",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // CMOS_M_GTVALID
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"cmos_m_gtvalid -c %d -s %04x -g 410 -n -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = cmos_m_gtvalid(command_buffer); 
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // MTC_INIT
    do {
      sprintf(command_buffer,"mtc_init -x");
      result = mtc_init(command_buffer);
      if (result == -2 || result == -3){
        running_ecal = 0;
        unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
        return;
      }
    } while (result != 0);
    while (sbc_lock != 0){}
    pt_printsend("-------------------------------------------\n");


    // CRATE_INIT with default + vbal + tdisc + tcmos values
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"crate_init -c %d -s %04x -B -D -C",i,arg.slot_mask[i]);
          result = crate_init(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }


    // ZDISC
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        do {
          sprintf(command_buffer,"zdisc -c %d -s %04x -o 0 -r 100 -d -E %s",i,arg.slot_mask[i],ecal_id);
          result = zdisc(command_buffer);
          if (result == -2 || result == -3){
            running_ecal = 0;
            unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
            return;
          }
        } while (result != 0);
        while (xl3_lock[i] != 0){}
        pt_printsend("------------------------------------------\n");
      }
    }

    pt_printsend("-------------------------------------------\n");
    pt_printsend("ECAL finished.\n");

    sbc_lock = 1;
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask)
        xl3_lock[i] = 1;
    }



  }else{
    sprintf(ecal_id,"%s",arg.ecal_id);
  }


  if (arg.update_hwdb){
    pt_printsend("Now updating FEC database with test results\n");

    // get the ecal document with the configuration
    char get_db_address[500];
    sprintf(get_db_address,"%s/%s/%s",DB_SERVER,DB_BASE_NAME,ecal_id);
    pouch_request *ecaldoc_response = pr_init();
    pr_set_method(ecaldoc_response, GET);
    pr_set_url(ecaldoc_response, get_db_address);
    pr_do(ecaldoc_response);
    if (ecaldoc_response->httpresponse != 200){
      pt_printsend("Unable to connect to database. error code %d\n",(int)ecaldoc_response->httpresponse);
      running_ecal = 0;
      unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
      return;
    }
    JsonNode *ecalconfig_doc = json_decode(ecaldoc_response->resp.data);

    // get all the ecal test results for all crates/slots
    sprintf(get_db_address,"%s/%s/%s/get_ecal?startkey=\"%s\"&endkey=\"%s\"",DB_SERVER,DB_BASE_NAME,DB_VIEWDOC,ecal_id,ecal_id);
    pouch_request *ecal_response = pr_init();
    pr_set_method(ecal_response, GET);
    pr_set_url(ecal_response, get_db_address);
    pr_do(ecal_response);
    if (ecal_response->httpresponse != 200){
      pt_printsend("Unable to connect to database. error code %d\n",(int)ecal_response->httpresponse);
      running_ecal = 0;
      unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
      return;
    }
    JsonNode *ecalfull_doc = json_decode(ecal_response->resp.data);
    JsonNode *ecal_rows = json_find_member(ecalfull_doc,"rows");
    int total_rows = json_get_num_mems(ecal_rows); 
    if (total_rows == 0){
      pt_printsend("No documents for this ECAL yet! (id %s)\n",ecal_id);
      running_ecal = 0;
      unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
      return;
    }

    // loop over crates/slots, create a fec document for each
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        for (j=0;j<16;j++){
          if ((0x1<<j) & arg.slot_mask[i]){
            printf("crate %d slot %d\n",i,j);

            // lets generate the fec document
            JsonNode *doc;
            create_fec_db_doc(i,j,&doc,ecalconfig_doc,&thread_fdset);

            int k;
            for (k=0;k<total_rows;k++){
              JsonNode *ecalone_row = json_find_element(ecal_rows,k);
              JsonNode *test_doc = json_find_member(ecalone_row,"value");
              JsonNode *config = json_find_member(test_doc,"config");
              if ((json_get_number(json_find_member(config,"crate_id")) == i) && (json_get_number(json_find_member(config,"slot")) == j)){
                if (strcmp(json_get_string(json_find_member(test_doc,"type")),"find_noise") != 0){
                  printf("test type is %s\n",json_get_string(json_find_member(test_doc,"type")));
                  add_ecal_test_results(doc,test_doc);
                }
              }
            }

            post_fec_db_doc(i,j,doc);

            json_delete(doc); // only delete the head node
          }
        }
      }
    }

    json_delete(ecalfull_doc);
    pr_free(ecal_response);
    json_delete(ecalconfig_doc);
    pr_free(ecaldoc_response);
  }

  if (arg.noise_run){
    // re lock everything down
    sbc_lock = 0;
    for (i=0;i<19;i++)
      if ((0x1<<i) & arg.crate_mask)
        xl3_lock[i] = 0;

    // FIND_NOISE
    int some_crate = 0;
    do{
      sprintf(command_buffer,"find_noise -c %05x -d -E %s ",arg.crate_mask,ecal_id);
      for (i=0;i<19;i++){
        if ((0x1<<i) & arg.crate_mask){
          some_crate = i;
        }
        sprintf(command_buffer+strlen(command_buffer),"-%02d %04x ",i,arg.slot_mask[i]);
      }
      result = find_noise(command_buffer);
      if (result == -2 || result == -3){
        printf("result was %d\n",result);
        running_ecal = 0;
        unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
        return;
      }
    } while (result != 0);
    while (xl3_lock[some_crate] != 0){}
    pt_printsend("------------------------------------------\n");
  }



  // now update again with noise run stuff
  if (arg.update_hwdb){

    // re lock everything down
    sbc_lock = 0;
    for (i=0;i<19;i++)
      if ((0x1<<i) & arg.crate_mask)
        xl3_lock[i] = 0;

    pt_printsend("Updating hw db with find_noise results\n");

    // get the find noise test results
    char get_db_address[500];
    sprintf(get_db_address,"%s/%s/%s/get_ecal?startkey=\"%s\"&endkey=\"%s\"",DB_SERVER,DB_BASE_NAME,DB_VIEWDOC,ecal_id,ecal_id);
    pouch_request *ecal_response = pr_init();
    pr_set_method(ecal_response, GET);
    pr_set_url(ecal_response, get_db_address);
    pr_do(ecal_response);
    if (ecal_response->httpresponse != 200){
      pt_printsend("Unable to connect to database. error code %d\n",(int)ecal_response->httpresponse);
      running_ecal = 0;
      unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
      return;
    }
    JsonNode *ecalfull_doc = json_decode(ecal_response->resp.data);
    JsonNode *ecal_rows = json_find_member(ecalfull_doc,"rows");
    int total_rows = json_get_num_mems(ecal_rows); 
    if (total_rows == 0){
      pt_printsend("No documents for this ECAL yet! (id %s)\n",ecal_id);
      running_ecal = 0;
      unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
      return;
    }

    // loop over crates/slots
    for (i=0;i<19;i++){
      if ((0x1<<i) & arg.crate_mask){
        for (j=0;j<16;j++){
          if ((0x1<<j) & arg.slot_mask[i]){
printf("crate %d slot %d\n",i,j);
            // get the current fec document
            sprintf(get_db_address,"%s/%s/%s/get_fec?startkey=[%d,%d,\"\"]&endkey=[%d,%d]&descending=true",FECDB_SERVER,FECDB_BASE_NAME,FECDB_VIEWDOC,i,j+1,i,j);
            pouch_request *fec_response = pr_init();
            pr_set_method(fec_response, GET);
            pr_set_url(fec_response, get_db_address);
            pr_do(fec_response);
            if (fec_response->httpresponse != 200){
              pt_printsend("Unable to connect to database. error code %d\n",(int)fec_response->httpresponse);
              unthread_and_unlock(1,arg.crate_mask,arg.thread_num);
              return;
            }
            JsonNode *fecfull_doc = json_decode(fec_response->resp.data);
            JsonNode *fec_rows = json_find_member(fecfull_doc,"rows");
            int total_rows2 = json_get_num_mems(fec_rows); 
            if (total_rows2 == 0){
              pt_printsend("No FEC documents for this crate/card yet! (crate %d card %d)\n",i,j);
              unthread_and_unlock(1,arg.crate_mask,arg.thread_num);
              return;
            }
            JsonNode *fecone_row = json_find_element(fec_rows,0);
            JsonNode *fec_doc = json_find_member(fecone_row,"value");

            // now find the noise run document for this crate/slot and add it to the fec document
            int found_it = 0;
            int k;
            for (k=0;k<total_rows;k++){
              JsonNode *ecalone_row = json_find_element(ecal_rows,k);
              JsonNode *test_doc = json_find_member(ecalone_row,"value");
              JsonNode *config = json_find_member(test_doc,"config");
              if ((json_get_number(json_find_member(config,"crate_id")) == i) && (json_get_number(json_find_member(config,"slot")) == j)){
                if (strcmp(json_get_string(json_find_member(test_doc,"type")),"find_noise") == 0){
                  found_it = 1;
                  printf("test type is %s\n",json_get_string(json_find_member(test_doc,"type")));
                  add_ecal_test_results(fec_doc,test_doc);
                  break;
                }
              }
            }
            if (found_it == 0){
              pt_printsend("Couldn't find noise run results!\n");
            }else{
              // push the updated fec document
              update_fec_db_doc(fec_doc);
            }
            json_delete(fecfull_doc);
            pr_free(fec_response);
          }
        }
      }
    }

    json_delete(ecalfull_doc);
    pr_free(ecal_response);
  }


  running_ecal = 0;
  unthread_and_unlock(1,arg.crate_mask,arg.thread_num); 
}