Example #1
0
void DoCrop(OptionsCls &options)
{
	  char name[MAX_PATH_LENGTH];
	  char destName[MAX_PATH_LENGTH];
	  int i;
	  Image loader;
	  Acq saver;
	  int mode = 0;
	  i = 0;
	  bool allocate = true;
	  int nameListLen;
	  char **nameList;
	  int maxMode=2;
    //@WARNING: >must< copy beadfind_post_0000 if it exists, or Analysis will >fail to complete< on PGM run crops
    // This is an inobvious bug due to the fact that the Image routine "ReadyToLoad" checks for >the next< acq file
    // or explog_final.txt, >or< beadfind_post.  If none of those three exist, we wait forever.
    // not all runs appear to have explog_final.txt copied around, so can't depend on that.
	  const char* defaultNameList[] = {"beadfind_pre_0000.dat", "beadfind_pre_0001.dat", "beadfind_pre_0002.dat", "beadfind_pre_0003.dat",
                                           "beadfind_post_0000.dat", "beadfind_post_0001.dat", "beadfind_post_0002.dat", "beadfind_post_0003.dat",
	                                   "prerun_0000.dat", "prerun_0001.dat", "prerun_0002.dat", "prerun_0003.dat"
	                                  };


	  // if requested...do not bother waiting for the files to show up
  if ( options.dont_retry )
    loader.SetTimeout ( 1,1 );

  if ( options.oneFile != NULL ) {
    nameList = &options.oneFile;
    nameListLen = 1;
    maxMode=1;
  } else {
    nameList = const_cast<char**> ( defaultNameList );
    nameListLen = sizeof ( defaultNameList ) /sizeof ( defaultNameList[0] );
  }

  // Create results folder
  umask ( 0 ); // make permissive permissions so its easy to delete.
  if ( mkdir ( options.destPath, 0777 ) ) {
    if ( errno == EEXIST ) {
      //already exists? well okay...
    } else {
      perror ( options.destPath );
      exit ( 1 );
    }
  }

  if(!options.skipCopy)
  {
  // Copy explog.txt file: all .txt files
  char cmd[1024];
  sprintf ( cmd, "cp -v %s/*.txt %s", options.expPath, options.destPath );
  if(system ( cmd ) != 0 )
	  printf("failed to copy txt files from src\n");
  // Copy lsrowimage.txt file
  const char *filesToMove[] = {
    "lsrowimage.dat",
    "gainimage.dat",
    "reimage.dat",
    "rmsimage.dat"
  };
  for(int iFile=0; iFile < 4; iFile++) {
    sprintf ( cmd, "cp -v %s/%s %s", options.expPath, filesToMove[iFile], options.destPath);
    if(system ( cmd ) == 1)
      fprintf (stdout, "No %s file found\n",filesToMove[iFile]);
  }
  }
  while ( mode < maxMode ) {
    if ( mode == 1 ) {
      sprintf ( name, "%s/acq_%04d.dat", options.expPath, i );
      sprintf ( destName, "%s/acq_%04d.dat", options.destPath, i );
    } else if ( mode == 0 ) {
      if ( i >= nameListLen ){
        mode++;
        i=0;
        continue;
      }
      sprintf ( name, "%s/%s", options.expPath, nameList[i] );
      sprintf ( destName, "%s/%s", options.destPath, nameList[i] );
    } else
      break;

    if ( loader.LoadRaw ( name, 0, allocate, false, false ) ) {
      allocate = false;
      const RawImage *raw = loader.GetImage();
      DetermineCropWidthAndHeight ( options.cropx, options.cropy, options.cropw, options.croph, raw->cols, raw->rows );
      struct timeval tv;
      double startT;
      double stopT;
      gettimeofday ( &tv, NULL );
      startT = ( double ) tv.tv_sec + ( ( double ) tv.tv_usec/1000000 );

      if(options.OverSample_skip || options.OverSample_combine)
    	  DoOverSample(options,loader);

      if(options.chipType)
    	  ChipIdDecoder::SetGlobalChipId(options.chipType);

      if(options.applyXtalkCorrection1){
			PairPixelXtalkCorrector xtalkCorrector;
			xtalkCorrector.Correct(loader.raw, options.applyXtalkCorrection1);
      }

      if(options.applyRowCorrection){
    	  printf("Applying row noise correction\n");
     		CorrNoiseCorrector rnc;
      		rnc.CorrectCorrNoise(loader.raw,options.applyColCorrection?3:1,true,true,false );
      }
      else if(options.applyColCorrection){
    	    printf("Applying column noise correction\n");
     		CorrNoiseCorrector rnc;
      		rnc.CorrectCorrNoise(loader.raw,0,true,true,false );
      }

      if(options.applyXtalkCorrection2){
			PairPixelXtalkCorrector xtalkCorrector;
			xtalkCorrector.Correct(loader.raw, options.applyXtalkCorrection2);
      }

      if(options.doColumnCorrectionTn)
	  {
		ComparatorNoiseCorrector cnc;
		cnc.CorrectComparatorNoiseThumbnail(loader.raw, NULL, 50,50, false);
	  } else if(options.doColumnCorrection){
		ComparatorNoiseCorrector cnc;
		bool beadfind = false;//((strstr(name,"beadfind_pre"))?true:false);
		cnc.CorrectComparatorNoise(loader.raw, NULL, false, options.cncType,beadfind );
	  }



      if(options.cmpFile){
    	  // subtract this file before continuing
    	  Image loader2;

    	  loader2.LoadRaw ( options.cmpFile, 0, allocate, 2 );

          if(options.OverSample_skip || options.OverSample_combine)
        	  DoOverSample(options,loader2);

          if(options.doColumnCorrectionTn)
    	  {
    		ComparatorNoiseCorrector cnc;
    		cnc.CorrectComparatorNoiseThumbnail(loader2.raw, NULL, 100,100, false);
    	  } else if(options.doColumnCorrection){
    		ComparatorNoiseCorrector cnc;
    		bool beadfind = ((strstr(options.cmpFile,"beadfind_pre"))?true:false);
    		cnc.CorrectComparatorNoise(loader2.raw, NULL, 0, options.cncType,beadfind );
    	  }



          {
            	short int *rawPtr = loader.raw->image;
              	short int *rawPtr2 = loader2.raw->image;
          	int frameStride=loader.raw->cols*loader.raw->rows;

          	for(int frame=0;frame<loader.raw->frames;frame++){
          		for(int idx=0;idx<frameStride;idx++){
          			*rawPtr -= *rawPtr2;
          			*rawPtr += 8192;
          			rawPtr++;
          			rawPtr2++;
          		}
          	}
          }
          printf("subtracted %s from the file\n",options.cmpFile);
      }

      if(options.subtractAverage)
      {
          // subtract off the mean trace
    	  SubtractOffMean(loader);
    	  printf("subtracted mean from trace\n");

      }

      // testing of lossy compression
      if(!options.pfc && ImageTransformer::PCATest[0]) {
        AdvComprTest(name,&loader,ImageTransformer::PCATest,true );
      }

      saver.SetData ( &loader );

      if(options.T0Test)
    	  saver.doT0Compression();

      if ( options.regBasedAcq && i == 0 ) {

        if ( options.excludeMask )
          saver.GenerateExcludeMaskRegions ( ( const char* ) options.excludeMaskFile );

        if ( options.useSeparatorT0File )
          saver.PopulateCroppedRegionalAcquisitionWindow ( ( const char* ) options.separatorIn, "t0Map.txt",
        		  options.cropx, options.cropy, options.cropw, options.croph, raw->timestamps[0] );
        else
          saver.ParseT0File ( ( const char* ) options.t0In, "t0Map.txt",
        		  options.cropx, options.cropy, options.cropw, options.croph, raw->timestamps[0] );

      }
      printf ( "Converting raw data %d %d frames: %d UncompFrames: %d\n", raw->cols, raw->rows, raw->frames, raw->uncompFrames );
      if ( options.doAscii ) {
        if ( !saver.WriteAscii ( destName, options.cropx, options.cropy, options.cropw, options.croph ) )
          break;
      } else {
        if ( options.vfc ) {
          if(options.pfc) {
            saver.WritePFV(destName, options.cropx, options.cropy, options.cropw, options.croph, options.PCAOpts);
          }
          else if ( options.regBasedAcq ) {
            if ( !saver.WriteFrameAveragedRegionBasedAcq ( destName, options.cropx, options.cropy, options.cropw, options.croph ) )
              break;
          } else if ( options.TimeBasedAcq ) {
            if ( !saver.WriteTimeBasedAcq ( destName, options.cropx, options.cropy, options.cropw, options.croph ) )
              break;
          } else {
            if ( !saver.WriteVFC ( destName, options.cropx, options.cropy, options.cropw, options.croph ) )
              break;
          }
        } else {
          if ( !saver.Write ( destName, options.cropx, options.cropy, options.cropw, options.croph ) )
            break;
        }
      }
      gettimeofday ( &tv, NULL );
      stopT = ( double ) tv.tv_sec + ( ( double ) tv.tv_usec/1000000 );
      printf ( "Converted: %s in %0.2lf sec\n", name,stopT - startT );
      fflush ( stdout );
      i++;
    } else {
      if ( ( mode == 0 && i >= 12 ) || ( mode ==1 ) ) {
        mode++;
        i = 0;
        allocate = true;
      } else
        i++;
    }
  }
}
Example #2
0
File: Crop.cpp Project: alecw/TS
int main(int argc, char *argv[])
{
	//int cropx = 624, cropy = 125, cropw = 100, croph = 100;
	int cropx = 0, cropy = 0, cropw = 0, croph = 0;
	char *expPath  = const_cast<char*>(".");
	char *destPath = const_cast<char*>("./converted");
	char *oneFile = NULL;
	int alternate_sampling=0;
	int doAscii = 0;
	int vfc = 1;
        int regBasedAcq = 0;
        int TimeBasedAcq = 0;
        int excludeMask = 0;
        int useSeparatorT0File = true;
        char* separatorIn = const_cast<char*>("./separator.summary.txt");
        char* t0In = const_cast<char*>("./T0file.txt");
        char* excludeMaskFile = "/opt/ion/config/exclusionMask_318.bin";
	int dont_retry = 0;
	if (argc == 1) {
	  usage(cropx, cropy, cropw, croph);
	}
	int argcc = 1;
	while (argcc < argc) {
		switch (argv[argcc][1]) {
			case 'a':
				doAscii = 1;
			break;
			
			case 'x':
				argcc++;
				cropx = atoi(argv[argcc]);
			break;

			case 'y':
				argcc++;
				cropy = atoi(argv[argcc]);
			break;

			case 'w':
				argcc++;
				cropw = atoi(argv[argcc]);
			break;

			case 'h':
				argcc++;
				// don't segfault if called with -h (user expects help)
				if (argcc >= argc) {
				  usage(cropx, cropy, cropw, croph);
				}
				croph = atoi(argv[argcc]);
			break;
		
			case 's':
				argcc++;
				expPath = argv[argcc];
			break;

			case 'f':
				argcc++;
				oneFile = argv[argcc];
			break;

			case 'z':
				dont_retry = 1;
			break;

			case 'c':
				vfc=1;
				cropx=0;
				cropy=0;
				cropw=0;
				croph=0;
			break;

			case 'n':
				vfc=0;
			break;

			case 'b':
				alternate_sampling=1;
			break;

			case 'v':
				fprintf (stdout, "%s", IonVersion::GetFullVersion("Crop").c_str());
				exit (0);
			break;
			case 'H':
			  usage(cropx, cropy, cropw, croph);
			  break;
			case 'd':
				argcc++;
				destPath = argv[argcc];
			break;
                        case 'r':
				regBasedAcq=1;
			break;
			case 'l':
				TimeBasedAcq=1;
			break;
		        case 't':
                                argcc++;
                                separatorIn = argv[argcc];	
                        break;
                        case 'e':
                                argcc++;
                                t0In = argv[argcc];	
                                useSeparatorT0File = false;
                        break;
                        case 'g':
                                argcc++; 
                                excludeMaskFile = argv[argcc];
                                excludeMask = 1;
                        break;
			default:
				argcc++;
				fprintf (stdout, "\n");
				
		}
		argcc++;
	}

	char name[MAX_PATH_LENGTH];
	char destName[MAX_PATH_LENGTH];
	int i;
	Image loader;
	Acq saver;
	int mode = 0;
	i = 0;
	bool allocate = true;
	char **nameList;
	char *defaultNameList[] = {"beadfind_post_0000.dat", "beadfind_post_0001.dat", "beadfind_post_0002.dat", "beadfind_post_0003.dat",
				"beadfind_pre_0000.dat", "beadfind_pre_0001.dat", "beadfind_pre_0002.dat", "beadfind_pre_0003.dat",
				"prerun_0000.dat", "prerun_0001.dat", "prerun_0002.dat", "prerun_0003.dat", "prerun_0004.dat"};
	int nameListLen;

	// Turn off this feature which sucks the life out of performance.
	PWR::PinnedWellReporter::Instance( false );
	
	// if requested...do not bother waiting for the files to show up
	if (dont_retry)
		loader.SetTimeout(1,1);

	if (oneFile != NULL)
	{
		nameList = &oneFile;
		nameListLen = 1;
		mode = 1;
	}
	else
	{
		nameList = defaultNameList;
		nameListLen = sizeof(defaultNameList)/sizeof(defaultNameList[0]);
	}

    // Create results folder
	umask (0);	// make permissive permissions so its easy to delete.
    if (mkdir (destPath, 0777))
    {
        if (errno == EEXIST) {
            //already exists? well okay...
        }
        else {
            perror (destPath);
            exit (1);
        }
    }
	
	// Copy explog.txt file: all .txt files
	char cmd[1024];
	sprintf (cmd, "cp -v %s/*.txt %s", expPath, destPath);
	assert(system(cmd) == 0);
	
	while (mode < 2) {
		if (mode == 0) {
			sprintf(name, "%s/acq_%04d.dat", expPath, i);
			sprintf(destName, "%s/acq_%04d.dat", destPath, i);
		} else if (mode == 1) {
			if(i >= nameListLen)
				break;
			sprintf(name, "%s/%s", expPath, nameList[i]);
			sprintf(destName, "%s/%s", destPath, nameList[i]);
		} else
			break;
		if (loader.LoadRaw(name, 0, allocate, false)) {
			allocate = false;
			const RawImage *raw = loader.GetImage();
                        DetermineCropWidthAndHeight(cropx, cropy, cropw, croph, raw->cols, raw->rows);
			struct timeval tv;
			double startT;
			double stopT;
			gettimeofday(&tv, NULL);
			startT = (double) tv.tv_sec + ((double) tv.tv_usec/1000000);

			saver.SetData(&loader);
                        if (regBasedAcq && i == 0) {

                            if (excludeMask)
                                saver.GenerateExcludeMaskRegions((const char*)excludeMaskFile);

                            if (useSeparatorT0File)
                                saver.PopulateCroppedRegionalAcquisitionWindow((const char*)separatorIn, "t0Map.txt", 
                                    cropx, cropy, cropw, croph, raw->timestamps[0]);
                            else 
                                saver.ParseT0File((const char*)t0In, "t0Map.txt", 
                                    cropx, cropy, cropw, croph, raw->timestamps[0]);
                            

                        }
			printf("Converting raw data %d %d frames: %d UncompFrames: %d\n", raw->cols, raw->rows, raw->frames, raw->uncompFrames);
			if (doAscii) {
				if (!saver.WriteAscii(destName, cropx, cropy, cropw, croph))
					break;
			}
			else {
				if(vfc)
				{
                                            if (regBasedAcq) {
                                                if (!saver.WriteFrameAveragedRegionBasedAcq(destName, cropx, cropy, cropw, croph)) 
                                                    break;                                                            
                                            }
                                            else if (TimeBasedAcq) { 
                                                if (!saver.WriteTimeBasedAcq(destName, cropx, cropy, cropw, croph))   
                                                    break;                                                            
                                            }
                                            else { 
                                                if (!saver.WriteVFC(destName, cropx, cropy, cropw, croph))            
                                                    break;                                                            
                                            }                                                                         
                                } 
                                else                                                                                  
                                {       
                                        if (!saver.Write(destName, cropx, cropy, cropw, croph))                       
                                                break;                                                                
                                }                                         		
                        }
			gettimeofday(&tv, NULL);
			stopT = (double) tv.tv_sec + ((double) tv.tv_usec/1000000);
			printf("Converted: %s in %0.2lf sec\n", name,stopT - startT);
			fflush (stdout);
			i++;
		} else {
			if ((mode == 1 && i >= 12) || (mode == 0)) {
				mode++;
				i = 0;
				allocate = true;
			} else
				i++;
		}
	}
}