void Run()
    {
        const RangeMultiD param_range({ args.range_x(), args.range_y() });
        Range m_H_range;
        const auto& limits = ReadInterpolatedLimits(args.input(), m_H_range, args.units());
        const auto model_reader = ModelReaderFactory::Make(args.model_file(), args.model_file_version());
        const auto model = ModelFactory::Make(args.model(), model_reader);
        std::vector<double> x_list, y_list, z_list_pred;
        std::vector<std::vector<double>> z_list(limits.size());

        for(const auto& desc : model->GetAvailablePoints()) {
            if(!param_range.Contains(desc.point)) continue;
            const double th_predicted = std::max(desc.GetProcessBR_CS(args.process()), 0.0);
            const double m_H = desc.GetMass(Particle::H);
            if(!m_H_range.Contains(m_H) || !th_predicted) continue;
            x_list.push_back(desc.point.at(0));
            y_list.push_back(desc.point.at(1));
            z_list_pred.push_back(th_predicted);
            for(size_t n = 0; n < limits.size(); ++n) {
                const double r = limits.at(n)->Eval(m_H) / th_predicted;
                z_list.at(n).push_back(r);
            }
        }

        const auto ref_hist = model_reader->GetReferenceHistogram();
        auto output_file = root_ext::CreateRootFile(args.output());
        for(size_t n = 0; n < limits.size(); ++n)
            CreateOutput(x_list, y_list, z_list.at(n), output_file, all_limit_quantile_names.at(n), ref_hist,
                         1.0, 0.05, 1.0);
        CreateOutput(x_list, y_list, z_list_pred, output_file, "predicted_CS_BR", ref_hist);

        std::cout << "File '" << args.output() << "' successfully created.\n";
    }
REFERENCE_TIME CTimeStretchFilter::DrainBuffers(IMediaSample* pSample, REFERENCE_TIME rtNewStart)
{
  Log("TS - DrainBuffers - rtNewStart: %6.3f", rtNewStart / 10000000.0);

  uint unprocessedSamplesBefore = numUnprocessedSamples();
  uint zeros = flushEx() - 32; // Magic 32 to keep the SoundTouch's output in sync
  uint unprocessedSamplesAfter = numUnprocessedSamples();

  UINT32 outFramesAfter = numSamples();
  UINT32 totalSamples = zeros + unprocessedSamplesBefore;
  UINT32 totalProcessedSamples = totalSamples - unprocessedSamplesAfter;

  Log("TS - DrainBuffers - unprocessedSamplesBefore: %u zeros: %u unprocessedSamplesAfter: %u outFramesAfter: %u duration %6.3f",
    unprocessedSamplesBefore, zeros, unprocessedSamplesAfter, outFramesAfter, (double)unprocessedSamplesBefore * (double) UNITS / (double) m_pOutputFormat->Format.nSamplesPerSec);

  REFERENCE_TIME rtAHwTime = 0;
  REFERENCE_TIME rtRCTime = 0;
  REFERENCE_TIME estimatedExtraSampleDuration = (((int)zeros - (int)unprocessedSamplesAfter) * UNITS) / m_pOutputFormat->Format.nSamplesPerSec;

  double bias = m_pClock->GetBias();
  double adjustment = m_pClock->Adjustment();

  m_pClock->GetHWTime(&rtRCTime, &rtAHwTime);
  double AVMult = m_pClock->SuggestedAudioMultiplier(rtAHwTime, rtRCTime, bias, adjustment);
  setTempoInternal(AVMult, 1.0);

  CreateOutput(totalProcessedSamples, outFramesAfter, bias, adjustment, AVMult, true);
    
  // Empty SoundTouch's buffers
  clear();

  pSample->SetDiscontinuity(false);

  return estimatedExtraSampleDuration;
}
   ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef filename)
   {
      //llvm::errs() << __FUNCTION__ << ":" << filename << "\n";
      CreateOutput(CI);
      JSONFormatter* fmt = new JSONFormatter(output_);

      fmt->BeginSourceFile();
      return new ASTVistor(CI, output_, fmt);
   }
Example #4
0
HRESULT CTimeStretchFilter::CheckSample(IMediaSample* pSample)
{
  if (!pSample)
    return S_OK;

  AM_MEDIA_TYPE *pmt = NULL;
  bool bFormatChanged = false;
  
  HRESULT hr = S_OK;

  if (SUCCEEDED(pSample->GetMediaType(&pmt)) && pmt)
    bFormatChanged = !FormatsEqual((WAVEFORMATEXTENSIBLE*)pmt->pbFormat, m_pInputFormat);

  if (bFormatChanged)
  {
    uint unprocessedSamplesBefore = numUnprocessedSamples();
     uint zeros = flushEx();

    uint unprocessedSamplesAfter = numUnprocessedSamples();
    UINT32 outFramesAfter = numSamples();
    
    UINT32 totalSamples = zeros + unprocessedSamplesBefore;
    UINT32 totalProcessedSamples = totalSamples - unprocessedSamplesAfter;
    //double bias = (double)totalProcessedSamples / (double)outFramesAfter;

    REFERENCE_TIME estimatedSampleDuration = totalProcessedSamples * UNITS / m_pOutputFormat->Format.nSamplesPerSec;

    double bias = m_pClock->GetBias();
    double adjustment = m_pClock->Adjustment();
    double AVMult = m_pClock->SuggestedAudioMultiplier(estimatedSampleDuration, bias, adjustment);
    setTempoInternal(AVMult, 1.0);

    CreateOutput(totalProcessedSamples, outFramesAfter, bias, adjustment, AVMult, true);
    
    // Empty SoundTouch's buffers
    clear();

    // Apply format change
    ChannelOrder chOrder;
    hr = NegotiateFormat((WAVEFORMATEXTENSIBLE*)pmt->pbFormat, 1, &chOrder);
    pSample->SetDiscontinuity(false);

    if (FAILED(hr))
    {
      DeleteMediaType(pmt);
      Log("CTimeStretchFilter::CheckFormat failed to change format: 0x%08x", hr);
      return hr;
    }
    else
    {
      m_chOrder = chOrder;
      return S_FALSE; // format changed
    }
  }

  return S_OK;
}
   virtual bool BeginSourceFileAction(CompilerInstance& CI, StringRef filename)
   {
      llvm::errs() << __FUNCTION__ << ":" << filename << "\n";

      CreateOutput(CI);
      if (output_) {
         *output_ << "{\n";
         // Set to consumer because it could be created before this function invoked.
         if (consumer_) consumer_->SetRawOStream(output_);
         return true;
      }

      return false;
   }
Example #6
0
void DAssemTranslationUnit(AstTranslationUnit transUnit)
{
	AstNode p = transUnit->extDecls;

	IRFile = CreateOutput(Input.filename, ".uil");

	while (p)
	{
		if (p->kind == NK_Function)
		{
			DAssemFunction((AstFunction)p);
		}
		p = p->next;
	}

	fclose(IRFile);
}
Example #7
0
void DumpTranslationUnit(AstTranslationUnit transUnit)
{
	AstNode p;

	ASTFile = CreateOutput(Input.filename, ".ast");

	p = transUnit->extDecls;
	while (p)
	{
		if (p->kind == NK_Function)
		{
			DumpFunction((AstFunction)p);
		}
		p = p->next;
	}
	fclose(ASTFile);
}
Example #8
0
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {

    long num_pixels;
    const int  *dim_array;
    double *input_image, *output_image;

    if (nrhs != 1 || nlhs != 1) {
        mexErrMsgTxt("Usage: [a2fFlux] = fnFastFlux(a2fDistanceMap)");
        return;
    }

    num_pixels = long(mxGetNumberOfElements(prhs[0]));
    dim_array = mxGetDimensions(prhs[0]);
    input_image = (double*)mxGetData(prhs[0]);

    plhs[0] = mxCreateNumericArray(2, dim_array, mxDOUBLE_CLASS, mxREAL);
    output_image = (double*)mxGetPr(plhs[0]);

    CreateOutput(input_image, dim_array,output_image);
}
Example #9
0
void ReadFromFile(FILE* input, FILE* output, List* words, FILE* dictionary, FILE* stopwords){
    char* word=calloc(100,sizeof(char));
    char* empty;
    int word_counter=0;
    int char_counter=0;


//CREATE WORDS LIST    
    while(fscanf(input,"%s",word)>0){ 
        Node* node = NewNode(word, words->end_, words->end_);
        InsertBack(words, word);  
        word_counter++;
        char_counter = countWordCharacter(word) + char_counter;
    }


    FindMinorDistance(words, dictionary, stopwords, word_counter);

    CreateOutput(output, word_counter, char_counter, words);


}
Example #10
0
/*
===============
WriteLump
===============
*/
void WriteLump (int type, int compression)
{
	int		size;
	
	if (!outputcreated)
		CreateOutput ();

//
// dword align the size
//
	while ((int)lump_p&3)
		*lump_p++ = 0;

	size = lump_p - lumpbuffer;
	if (size > MAXLUMP)
		Error ("Lump size exceeded %d, memory corrupted!",MAXLUMP);

//
// write the grabbed lump to the wadfile
//
	AddLump (lumpname,lumpbuffer,size,type, compression);
}
Example #11
0
/* Entry Points */
void mexFunction( int nlhs, mxArray *plhs[], 
				 int nrhs, const mxArray *prhs[] ) {


  long number_of_dims,  num_voxels;
  const int  *dim_array;  
  unsigned short *input_volume;
  unsigned short *select_array;
  unsigned short *output_volume;
  long select_length;

  /* Check for proper number of input and output arguments. */    
  if (nrhs != 2 || nlhs != 1) {
    mexErrMsgTxt("Usage: [a3iVolume] = fnSelectLabels(a3iLabeledVolume, aiLabelList)");
	return;
  } 

  /* Check data type of labeled volume argument. */
  if (!(mxIsUint16(prhs[0])) || !(mxIsUint16(prhs[1]))) {
    mexErrMsgTxt("Input volume must be of type UINT16. Label List must be of type UINT 16");
	return;
  }
    
  /* Get the number of num_voxels in the mask argument. */
  num_voxels = mxGetNumberOfElements(prhs[0]);
  number_of_dims = mxGetNumberOfDimensions(prhs[0]);
 
   dim_array = mxGetDimensions(prhs[0]);
   input_volume = (unsigned short *)mxGetData(prhs[0]);
   select_array = (unsigned short *)mxGetData(prhs[1]);
   select_length = mxGetNumberOfElements(prhs[1]);

   plhs[0] = mxCreateNumericArray(number_of_dims, dim_array, mxUINT16_CLASS, mxREAL);
   output_volume = (unsigned short*)mxGetPr(plhs[0]);
 
   CreateOutput(input_volume, num_voxels,select_array,select_length,output_volume);
}
Example #12
0
/******************************************************************************
METHOD:  cfmask

PURPOSE:  the main routine for fmask in C

RETURN VALUE:
Type = int
Value           Description
-----           -----------
ERROR           An error occurred during processing of the cfmask
SUCCESS         Processing was successful

PROJECT:  Land Satellites Data System Science Research and Development (LSRD)
at the USGS EROS

HISTORY:
Date        Programmer       Reason
--------    ---------------  -------------------------------------
3/15/2013   Song Guo         Original Development
5/14/2013   Song Guo         Added in Polar Stereographic support
7/17/2013   Song Guo         Added in Map info 
8/15/2013   Song Guo         Modified to use TOA reflectance file 
                             as input instead of metadata file

NOTES: type ./cfmask --help for information to run the code
******************************************************************************/
int main (int argc, char *argv[])
{
    char errstr[MAX_STR_LEN];           /* error string */
    char lndth_name[MAX_STR_LEN];       /* ledaps Brightness Temperature file */
    char fmask_name[MAX_STR_LEN];       /* output fmask binary file name */
    char fmask_header[MAX_STR_LEN];     /* output fmask binary file header */
    char fmask_hdf_name[MAX_STR_LEN];   /* output fmask HDF file name */
    char fmask_hdf_hdr[MAX_STR_LEN];    /* output fmask HDF file header */
    char *lndcal_name = NULL;           /* input lndcal data filename */
    char directory[MAX_STR_LEN];        /* input/output data directory */
    char extension[MAX_STR_LEN];        /* input TOA file extension */
    int ib;                             /* band counters */
    char sds_names[NBAND_REFL_MAX][MAX_STR_LEN]; /* array of image SDS names */
    Input_t *input = NULL;              /* input data and meta data */
    char  scene_name[MAX_STR_LEN];      /* input data scene name */
    char *hdf_grid_name = "Grid";  /* name of the grid for HDF-EOS */
    unsigned char **cloud_mask;    /* cloud pixel mask */
    unsigned char **shadow_mask;   /* shadow pixel mask */
    unsigned char **snow_mask;     /* snow pixel mask */
    unsigned char **water_mask;    /* water pixel mask */
    int status;                    /* return value from function call */
    FILE *fd = NULL;               /* file pointer */
    float ptm;                     /* percent of clear-sky pixels */
    float t_templ = 0.0;     /* percentile of low background temperature */
    float t_temph = 0.0;     /* percentile of high background temperature */
    int out_sds_types[NUM_OUT_SDS];     /* array of image SDS types */
    char *sds_name="fmask_band";        /* Fmask hdf SDS name */
    Output_t *output = NULL;            /* output structure and metadata */
    bool verbose;            /* verbose flag for printing messages */
    bool write_binary;       /* should we write raw binary output? */
    bool no_hdf_output;      /* should we don't write HDF4 output file? */
    int cldpix = 2;          /* Default buffer for cloud pixel dilate */
    int sdpix = 2;           /* Default buffer for shadow pixel dilate */
    float cloud_prob;        /* Default cloud probability */
    Space_def_t space_def;   /* spatial definition information */
    float sun_azi_temp = 0.0;/* Keep the original sun azimuth angle */
  
    time_t now;
    time(&now);
    printf("CFmask start_time=%s\n",ctime(&now));

    /* Read the command-line arguments, including the name of the input
       Landsat TOA reflectance product and the DEM */
    status = get_args (argc, argv, &lndcal_name, &cloud_prob, &cldpix,
                       &sdpix, &write_binary, &no_hdf_output, &verbose);
    if (status != SUCCESS)
    { 
        sprintf (errstr, "calling get_args");
        ERROR (errstr, "main");
    }

    split_filename(lndcal_name, directory, scene_name, extension);
    if (verbose)
        printf("directory, scene_name, extension=%s,%s,%s\n", 
            directory, scene_name, extension);
    sprintf(lndcal_name, "%slndcal.%s.hdf", directory, scene_name);
    sprintf(lndth_name, "%slndth.%s.hdf", directory, scene_name);
    sprintf(fmask_name, "%sfmask.%s.img", directory, scene_name);
    sprintf(fmask_header, "%sfmask.%s.img.hdr", directory, scene_name);
    sprintf(fmask_hdf_name, "%sfmask.%s.hdf", directory, scene_name);
    if (verbose)
    {
        printf("lndcal_name, lndth_name = %s, %s\n", lndcal_name, lndth_name); 
        printf("fmask_name, fmask_header, fmask_hdf_name = %s, %s, %s\n", 
                fmask_name, fmask_header, fmask_hdf_name); 
    }

    /* Open input file, read metadata, and set up buffers */
    input = OpenInput(lndth_name, lndcal_name);
    if (input == NULL)
    {
        sprintf (errstr, "opening the input files: %s & %s", lndth_name,
                 lndcal_name);
        ERROR (errstr, "main");
    }

    /* Get the projection and spatial information from the input TOA
       reflectance product */
    status = get_space_def_hdf(&space_def, lndcal_name, hdf_grid_name);
    if (status != SUCCESS)
    {
        sprintf(errstr, "Reading spatial metadata from the HDF file: %s", 
               lndcal_name);
        ERROR (errstr, "main");
    }
    input->meta.zone = space_def.zone;

    if (verbose)
    {
        /* Print some info to show how the input metadata works */
        printf ("DEBUG: Number of input TOA bands: %d\n", input->nband);
        printf ("DEBUG: Number of input thermal bands: %d\n", 1);
        printf ("DEBUG: Number of input lines: %d\n", input->size.l);
        printf ("DEBUG: Number of input samples: %d\n", input->size.s);
        printf ("DEBUG: Number of input TOA lines: %d\n", input->toa_size.l);
        printf ("DEBUG: Number of input TOA samples: %d\n", input->toa_size.s);
        printf ("DEBUG: Provider is %s\n", input->meta.provider);
        printf ("DEBUG: Satellite is %s\n", input->meta.sat);
        printf ("DEBUG: Instrument is %s\n", input->meta.inst);
        printf ("DEBUG: ACQUISITION_DATE.DOY is %d\n", input->meta.acq_date.doy);
        printf ("DEBUG: WRS system is %s\n", input->meta.wrs_sys);
        printf ("DEBUG: Path is %d\n", input->meta.path);
        printf ("DEBUG: Row is %d\n", input->meta.row);
        printf ("DEBUG: Fill value is %d\n", input->meta.fill);
        for (ib = 0; ib < input->nband; ib++)
        {
            printf ("DEBUG: Band %d-->\n", ib);
            printf ("DEBUG:   SDS name is %s\n", input->sds[ib].name);
            printf ("DEBUG:   SDS rank: %d\n", input->sds[ib].rank);
            printf ("DEBUG:   band satu_value_ref: %d\n", 
                   input->meta.satu_value_ref[ib]);
            printf ("DEBUG:   band satu_value_max: %d\n", 
                   input->meta.satu_value_max[ib]);
            printf ("DEBUG:   band gains: %f, band biases: %f\n", 
                    input->meta.gain[ib], input->meta.bias[ib]);      
        }
        printf ("DEBUG: Thermal Band -->\n");
        printf ("DEBUG:   SDS name is %s\n", input->therm_sds.name);
        printf ("DEBUG:   SDS rank: %d\n", input->therm_sds.rank);
        printf ("DEBUG:   therm_satu_value_ref: %d\n", 
                   input->meta.therm_satu_value_ref);
        printf ("DEBUG:   therm_satu_value_max: %d\n", 
                   input->meta.therm_satu_value_max);
        printf ("DEBUG:   therm_gain: %f, therm_bias: %f\n", 
                input->meta.gain_th, input->meta.bias_th);

        printf("DEBUG: ROW is %d\n", input->meta.row);
        printf("DEBUG: PATH is %d\n", input->meta.path);
        printf("DEBUG: SUN AZIMUTH is %f\n", input->meta.sun_az);
        printf("DEBUG: SUN ZENITH is %f\n", input->meta.sun_zen);
        printf("DEBUG: Projection Zone is %d\n", input->meta.zone);

        printf("DEBUG: unit_ref is %s\n", input->meta.unit_ref);
        printf("DEBUG: valid_range_ref is %f & %f\n", 
           input->meta.valid_range_ref[0], 
           input->meta.valid_range_ref[1]);
    }

    /* If the scene is an ascending polar scene (flipped upside down), then
       the solar azimuth needs to be adjusted by 180 degrees.  The scene in
       this case would be north down and the solar azimuth is based on north
       being up clock-wise direction. Flip the south to be up will not change 
       the actual sun location, with the below relations, the solar azimuth
       angle will need add in 180.0 for correct sun location */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
    {
        /* Keep the original solar azimuth angle */
        sun_azi_temp = input->meta.sun_az;
        input->meta.sun_az += 180.0;
        if ((input->meta.sun_az - 360.0) > MINSIGMA)
            input->meta.sun_az -= 360.0;
        if (verbose)
            printf ("  Polar or ascending scene.  Readjusting solar azimuth by "
                "180 degrees.\n  New value: %f degrees\n", input->meta.sun_az);
    }

    /* Copy the SDS names and QA SDS names from the input structure for the
       output structure, since we are simply duplicating the input */
    for (ib = 0; ib < input->nband; ib++)
        strcpy (&sds_names[ib][0], input->sds[ib].name);

    /* Dynamic allocate the 2d mask memory */
    cloud_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    shadow_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    snow_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    water_mask = (unsigned char **)allocate_2d_array(input->size.l, 
                 input->size.s, sizeof(unsigned char)); 
    if (cloud_mask == NULL  || shadow_mask == NULL || snow_mask == NULL
        || water_mask == NULL)
    {
        sprintf (errstr, "Allocating mask memory");
        ERROR (errstr, "main");
    }

    /* Build the potential cloud, shadow, snow, water mask */
    status = potential_cloud_shadow_snow_mask(input, cloud_prob, &ptm,
             &t_templ, &t_temph, cloud_mask, shadow_mask, snow_mask, 
             water_mask, verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing potential_cloud_shadow_snow_mask");
        ERROR (errstr, "main");
    }

    printf("Pcloud done, starting cloud/shadow match\n");


    /* Build the final cloud shadow based on geometry matching and
       combine the final cloud, shadow, snow, water masks into fmask */
    status = object_cloud_shadow_match(input, ptm, t_templ, t_temph,
             cldpix, sdpix, cloud_mask, shadow_mask, snow_mask, water_mask,
             verbose);
    if (status != SUCCESS)
    {
        sprintf (errstr, "processing object_cloud_and_shadow_match");
        ERROR (errstr, "main");
    }    

    status = free_2d_array((void **)shadow_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }
    status = free_2d_array((void **)snow_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }
    status = free_2d_array((void **)water_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }

    /* Reassign solar azimuth angle for output purpose if south up north 
       down scene is involved */
    if (input->meta.ul_corner.is_fill &&
        input->meta.lr_corner.is_fill &&
        (input->meta.ul_corner.lat - input->meta.lr_corner.lat) < MINSIGMA)
        input->meta.sun_az = sun_azi_temp;
   
    if (write_binary)
    {
        /* Create an ENVI header file for the binary fmask */
        status = write_envi_hdr(fmask_header, BINARY_FILE, input, &space_def);
        if (status != SUCCESS)
        {
            sprintf(errstr, "Creating ENVI header for binary fmask");
            ERROR (errstr, "main");
        }

        /* Open the mask file for writing */
        fd = fopen(fmask_name, "w"); 
        if (fd == NULL)
        {
            sprintf(errstr, "Opening report file: %s", fmask_name);
            ERROR (errstr, "main");
        }

        /* Write out the mask file */
        status = fwrite(&cloud_mask[0][0], sizeof(unsigned char),
            input->size.l * input->size.s, fd);
        if (status != input->size.l * input->size.s)
        {
            sprintf(errstr, "Writing to %s", fmask_name);
            ERROR (errstr, "main");
        }

        /* Close the mask file */
        status = fclose(fd);
        if (status)
        {
            sprintf(errstr, "Closing file %s", fmask_name);
            ERROR (errstr, "main");
        }
    }

    if (!no_hdf_output)
    {
        /* Create and open fmask HDF output file */
        if (!CreateOutput(fmask_hdf_name))
        {
            sprintf(errstr, "Creating HDF fmask output file");
            ERROR (errstr, "main");
        }
        output = OpenOutput (fmask_hdf_name, sds_name, &input->size);
        if (output == NULL)
        {
            sprintf (errstr, "opening output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }

        if (!PutOutput(output, cloud_mask))
        {
            sprintf (errstr, "Writing output fmask in HDF files\n");
            ERROR (errstr, "main");
        }

        if (!PutMetadata(output, input))
        {
            sprintf (errstr, "Writing output fmask metadata in HDF files\n");
            ERROR (errstr, "main");
        }

        /* Close the output file and free the structure */
        if (!CloseOutput (output))
        {
            sprintf (errstr, "closing output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }
        if (!FreeOutput (output))
        {
            sprintf (errstr, "freeing output file - %s", fmask_hdf_name);
            ERROR(errstr, "main");
        }

        /* Write the spatial information, after the file has been closed */
        out_sds_types[0] = DFNT_UINT8;
        status = put_space_def_hdf (&space_def, fmask_hdf_name, NUM_OUT_SDS, 
            out_sds_names, out_sds_types, hdf_grid_name);
        if (status != SUCCESS)
        {
            sprintf("Putting spatial metadata to the HDF file: "
            "%s", lndcal_name);
            ERROR (errstr, "main");
        }

        /* Write CFmask HDF header to add in envi map info */
        sprintf (fmask_hdf_hdr, "%s.hdr", fmask_hdf_name);
        status = write_envi_hdr (fmask_hdf_hdr, HDF_FILE, input, &space_def);
        if (status != SUCCESS)
        {
            sprintf(errstr, "Error writing the ENVI header for CFmask HDF hdr");
            ERROR (errstr, "main");
        }
    }

    /* Free the final output cloud_mask */
    status = free_2d_array((void **)cloud_mask);
    if (status != SUCCESS)
    {
        sprintf (errstr, "Freeing mask memory");
        ERROR (errstr, "main");
    }

    /* Close the input file and free the structure */
    CloseInput (input);
    FreeInput (input);

    free(lndcal_name);
    printf ("Processing complete.\n");
    time(&now);
    printf("CFmask end_time=%s\n",ctime(&now));
    return (SUCCESS);
}
Example #13
0
// Processing
DWORD CTimeStretchFilter::ThreadProc()
{
  Log("CTimeStretchFilter::timestretch thread - starting up - thread ID: %d", m_ThreadId);
  
  SetThreadName(0, "TimeStretchFilter");

  AudioSinkCommand command;
  CComPtr<IMediaSample> sample;

  while (true)
  {
    m_csResources.Unlock();
    HRESULT hr = GetNextSampleOrCommand(&command, &sample.p, INFINITE, &m_hSampleEvents, &m_dwSampleWaitObjects);
    m_csResources.Lock();

    if (hr == MPAR_S_THREAD_STOPPING)
    {
      Log("CTimeStretchFilter::timestretch thread - closing down - thread ID: %d", m_ThreadId);
      SetEvent(m_hCurrentSampleReleased);
      CloseThread();
      m_csResources.Unlock();
      return 0;
    }
    else
    {
      if (command == ASC_Flush)
      {
      	Log("CTimeStretchFilter::timestretch thread - flushing");
        m_rtInSampleTime = m_rtNextIncomingSampleTime = 0;

        if (m_pNextOutSample)
          m_pNextOutSample.Release();

        flush();

        sample.Release();
        SetEvent(m_hCurrentSampleReleased);
      }
      else if (command == ASC_Pause || command == ASC_Resume)
        continue;
      else if (sample)
      {
        BYTE *pMediaBuffer = NULL;
        long size = sample->GetActualDataLength();

        if (sample->IsDiscontinuity() == S_OK)
        {
          sample->SetDiscontinuity(false);
          m_bDiscontinuity = true;
        }

        if (CheckSample(sample) == S_FALSE)
        {
          DeleteMediaType(m_pMediaType);
          sample->GetMediaType(&m_pMediaType);
        }

        CheckStreamContinuity(sample);
        m_nSampleNum++;

        hr = sample->GetPointer(&pMediaBuffer);

        if ((hr == S_OK) && m_pMemAllocator)
        {
          uint unprocessedSamplesBefore = numUnprocessedSamples();
          uint unprocessedSamplesAfter = 0;

          UINT32 nFrames = size / m_pOutputFormat->Format.nBlockAlign;
          REFERENCE_TIME estimatedSampleDuration = nFrames * UNITS / m_pOutputFormat->Format.nSamplesPerSec;

          double bias = m_pClock->GetBias();
          double adjustment = m_pClock->Adjustment();
          double AVMult = m_pClock->SuggestedAudioMultiplier(estimatedSampleDuration, bias, adjustment);
          setTempoInternal(AVMult, 1.0); // this should be the same as previous line, but in future we want to get rid of the 2nd parameter

          // Process the sample 
          putSamplesInternal((const short*)pMediaBuffer, size / m_pOutputFormat->Format.nBlockAlign);
          unprocessedSamplesAfter = numUnprocessedSamples();

          UINT32 nInFrames = (size / m_pOutputFormat->Format.nBlockAlign) - unprocessedSamplesAfter + unprocessedSamplesBefore;
          UINT32 nOutFrames = numSamples();
          
          CreateOutput(nInFrames, nOutFrames, bias, adjustment, AVMult, false);
        }
      }
    }
  }
}
Example #14
0
void OcrNet::Train(cv::Mat &image, std::map< int, std::set<ImageRegion> > &image_lines, std::string &char_set)
{
	std::map<int, std::set<ImageRegion> >::iterator image_lines_it;
	std::set<ImageRegion> regions;
	std::set<ImageRegion>::iterator regions_it;
	unsigned int char_count = 0, char_length = 0, repeat = 2, index = 0;
	struct fann_train_data **data;
	fann_type **inputs, **outputs;

	char_length = char_set.length();
	data = new struct fann_train_data* [char_length];

	for(uint j=0; j < char_length; j++)
	{
		data[j] = new struct fann_train_data;
		data[j]->num_data = repeat;
		data[j]->num_input = Ann[j]->num_input;
		data[j]->num_output = Ann[j]->num_output;
		inputs = new fann_type*[data[j]->num_data];
		outputs = new fann_type*[data[j]->num_data];
		data[j]->input = inputs;
		data[j]->output = outputs;

		for(uint i = 0; i < repeat; i++, char_count = 0)
		{
			for(image_lines_it = image_lines.begin();
					char_count < char_length && image_lines_it != image_lines.end();
					image_lines_it++, char_count++)
			{
				regions = (std::set<ImageRegion>) image_lines_it->second;

				for(regions_it = regions.begin();
						char_count < char_length && regions_it != regions.end();
						regions_it++, char_count++)
				{
					index = i * char_length + char_count;
					inputs[index] = new fann_type[data->num_input];
					outputs[index] = new fann_type[data->num_output];
					CreateInput(inputs[index], data->num_input, image, *regions_it);
					CreateOutput(outputs[index], data->num_output, char_count);

					std::cout << "Repeat Index: " << i << ", index: " << index << std::endl;
				}
			}
		}

		fann_randomize_weights(Ann[j], -0.001, 0.0);
		//	fann_init_weights(Ann, data);
		fann_train_on_data(Ann[j], &data[j], MaxEpochs, 10, Error);

		for(unsigned int i=0; i < data[j]->num_data; i++)
		{
			delete [] inputs[i];
			delete [] outputs[i];
		}
	}

	delete data;
	delete [] inputs;
	delete [] outputs;
}
Example #15
0
int main (int argc, const char **argv) 
/* 
!C******************************************************************************

!Description: 'main' is the main function for the 'resamp' program.
 
!Input Parameters:
 argc           number of run-time (command line) arguments
 argv           list of run-time argument values

!Output Parameters:
 (returns)      status:
                  'EXIT_SUCCESS' = okay
		  'EXIT_FAILURE' = fatal error

!Team Unique Header:

 ! Design Notes:
   1. See 'USAGE' in 'parser.h' for information on how to use the program.
   2. An error status is returned when:
       a. there is a problem getting runtime parameters
       b. there is a bad input image, geolocation or kernel file
       c. unable to set up the input grid, scan data structure, 
          or the output space
       d. unable to generate kernel
       f. unable to free memory for data structures
       g. unable to set up intermediate patches data structure
       h. there is an error reading geolocation for a scan
       i. there is an error mapping a scan
       j. there is an error generating geolocation for a scan
       k. there is an error copying a scan
       l. there is an error extending the scan
       m. there is an error reading input data for a scan
       n. there is an error resampling a scan
       o. there is an error writting patches to disk
       p. there is an error closing input image, geolocation, kernel or 
          output files
       q. there is an error untouching patches
       r. there is an error creating output image file
       s. there is an error opening output file
       t. there is an error unscrambling the output file
       u. there is an error writing metadata.
   3. Errors are handled with the 'LOG_ERROR' macro.

!END****************************************************************************
*/
{
  int i,j,k;
  int curr_sds, curr_band;        /* current SDS and current band in SDS */
  char tmp_sds_name[MAX_STR_LEN];
  char errstr[M_MSG_LEN+1];              /* error string for OpenInput */
  char sdsname[256];              /* SDS name without '/'s */
  char msg[M_MSG_LEN+1];
  Param_t *param = NULL;
  Param_t *param_save = NULL;
  Geoloc_t *geoloc = NULL;
  Kernel_t *kernel = NULL;
  Input_t *input = NULL;
  Scan_t *scan = NULL;
  Space_t *output_space = NULL;
  Patches_t *patches = NULL;
  int iscan, kscan;
  int il, nl;
  Output_t *output = NULL;
  Img_coord_double_t img;
  Geo_coord_t geo;
  FILE_ID *MasterGeoMem;    /* Output GeoTiff file */
  FILE *rbfile = NULL;       /* Output Raw Binary file */
  char HDF_File[1024], CharThisPid[256], FinalFileName[1024];
  Output_t output_mem;       /* Contains output HDF file */
  int32 exec_resamp, ThisPid; 
  char filename[1024];       /* name of raw binary file to be written to */
  time_t startdate, enddate;  /* start and end date struct */
  bool file_created;         /* was the current HDF file created? */

  /* Initialize the log file */
  InitLogHandler();

  /* Print the MRTSwath header */
  LogInfomsg(
     "*******************************************************************"
     "***********\n");
  sprintf(msg, "%s (%s)\n", RESAMPLER_NAME, RESAMPLER_VERSION);
  LogInfomsg(msg);
  startdate = time(NULL);
  sprintf(msg, "Start Time:  %s", ctime(&startdate));
  LogInfomsg(msg);
  LogInfomsg(
  "------------------------------------------------------------------\n");

  /* Get runtime parameters */
  if (NeedHelp(argc, argv))
    exit(EXIT_SUCCESS);

  param_save = GetParam(argc, argv);
  if (param_save == (Param_t *)NULL)
    LOG_ERROR("getting runtime parameters", "main");

  /* Print out the user-specified processing information */
  PrintParam(param_save);

  /* Loop through all the SDSs */
  for (curr_sds = 0; curr_sds < param_save->num_input_sds; curr_sds++)
  {
    /* Loop through all the bands in the current SDS */
    for (curr_band = 0; curr_band < param_save->input_sds_nbands[curr_sds];
         curr_band++)
    {
      /* Is this band one that should be processed? */
      if (!param_save->input_sds_bands[curr_sds][curr_band])
        continue;

      /* Assume the HDF file does not need to be created */
      file_created = false;

      /* Get a copy of the user parameters */
      param = CopyParam(param_save);
      if (param == (Param_t *)NULL)
        LOG_ERROR("copying runtime parameters", "main");

      /* Create the input_sds_name which is "SDSname, band" */
      if (param->input_sds_nbands[curr_sds] == 1)
      {
        /* 2D product so the band number is not needed */
        sprintf(tmp_sds_name, "%s", param->input_sds_name_list[curr_sds]);
      }
      else
      {
        /* 3D product so the band number is required */
        sprintf(tmp_sds_name, "%s, %d", param->input_sds_name_list[curr_sds],
          curr_band);
      }

      param->input_sds_name = strdup (tmp_sds_name);
      if (param->input_sds_name == NULL)
        LOG_ERROR("error creating input SDS band name", "main");
      sprintf(msg, "\nProcessing %s ...\n", param->input_sds_name);
      LogInfomsg(msg);

      /* Update the system to process the current SDS and band */
      if (!update_sds_info(curr_sds, param))
        LOG_ERROR("error updating SDS information", "main");

      /* Open input file for the specified SDS and band */
      input = OpenInput(param->input_file_name, param->input_sds_name, 
        param->iband, param->rank[curr_sds], param->dim[curr_sds], errstr);
      if (input == (Input_t *)NULL) {
        /* This is an invalid SDS for our processing so skip to the next
           SDS. We will only process SDSs that are at the 1km, 500m, or
           250m resolution (i.e. a factor of 1, 2, or 4 compared to the
           1km geolocation lat/long data). We also only process CHAR8,
           INT8, UINT8, INT16, and UINT16 data types. */
        LOG_WARNING(errstr, "main");
        LOG_WARNING("not processing SDS/band", "main");
        break;
      }

      /* Setup kernel */
      kernel = GenKernel(param->kernel_type);
      if (kernel == (Kernel_t *)NULL)
        LOG_ERROR("generating kernel", "main");

      /* Open geoloc file */
      geoloc = OpenGeolocSwath(param->geoloc_file_name);
      if (geoloc == (Geoloc_t *)NULL)
        LOG_ERROR("bad geolocation file", "main");

      /* Setup input scan */
      scan = SetupScan(geoloc, input, kernel);
      if (scan == (Scan_t *)NULL)
        LOG_ERROR("setting up scan data structure", "main");

      /* Set up the output space, using the current pixel size and
         number of lines and samples based on the current SDS (pixel size
         and number of lines and samples are the same for all the bands
         in the SDS) */
      param->output_space_def.img_size.l = param->output_img_size[curr_sds].l;
      param->output_space_def.img_size.s = param->output_img_size[curr_sds].s;
      param->output_space_def.pixel_size = param->output_pixel_size[curr_sds];
      sprintf(msg, "  output lines/samples: %d %d\n",
        param->output_space_def.img_size.l, param->output_space_def.img_size.s);
      LogInfomsg(msg);
      if (param->output_space_def.proj_num == PROJ_GEO)
      {
        sprintf(msg, "  output pixel size: %.4f\n",
          param->output_space_def.pixel_size * DEG);
	LogInfomsg(msg);
      }
      else
      {
        sprintf(msg, "  output pixel size: %.4f\n",
          param->output_space_def.pixel_size);
	LogInfomsg(msg);
      }
      LogInfomsg("  output data type: ");
      switch (param->output_data_type)
      {
          case DFNT_CHAR8:
              LogInfomsg("CHAR8\n");
              break;
          case DFNT_UINT8:
              LogInfomsg("UINT8\n");
              break;
          case DFNT_INT8:
              LogInfomsg("INT8\n");
              break;
          case DFNT_UINT16:
              LogInfomsg("UINT16\n");
              break;
          case DFNT_INT16:
              LogInfomsg("INT16\n");
              break;
          case DFNT_UINT32:
              LogInfomsg("UINT32\n");
              break;
          case DFNT_INT32:
              LogInfomsg("INT32\n");
              break;
          default:
              LogInfomsg("same as input\n");
              break;
      }

      output_space = SetupSpace(&param->output_space_def);
      if (output_space == (Space_t *)NULL) 
        LOG_ERROR("setting up output space", "main");

      /* Compute and print out the corners */
      img.is_fill = false;
      img.l = img.s = 0.0;
      if (!FromSpace(output_space, &img, &geo)) 
      {
        LOG_WARNING("unable to compute upper left corner", "main");
      }
      else
      {
        sprintf(msg,
          "  output upper left corner: lat %13.8f  long %13.8f\n", 
          (DEG * geo.lat), (DEG * geo.lon));
	LogInfomsg(msg);
      }

      img.is_fill = false;
      img.l = output_space->def.img_size.l - 1; 
      img.s = output_space->def.img_size.s - 1;
      if (!FromSpace(output_space, &img, &geo)) 
      {
        LOG_WARNING("unable to compute lower right corner", "main");
      }
      else
      {
        sprintf(msg,
          "  output lower right corner: lat %13.8f  long %13.8f\n", 
          (DEG * geo.lat), (DEG * geo.lon));
        LogInfomsg(msg);
      }

      /* If output data type not specified, then set to input data type
         (changes from SDS to SDS) */
      if (param->output_data_type == -1) 
        param->output_data_type = input->sds.type;

      /* Save the data type of this SDS for output to the metadata */
      param_save->output_dt_arr[curr_sds] = param->output_data_type;

      /* Setup intermediate patches. Setup as the input data type. Then we
         will convert to the output data type later. */
      patches = SetupPatches(&param->output_space_def.img_size, 
        param->patches_file_name, input->sds.type, input->fill_value, input->factor, input->offset);
      if (patches == (Patches_t *)NULL) 
        LOG_ERROR("setting up intermediate patches data structure","main");

      if (param->input_space_type != SWATH_SPACE)
        LOG_ERROR("input space type is not SWATH", "main");

      sprintf(msg, "  input lines/samples: %d %d\n", input->size.l,
        input->size.s);
      LogInfomsg(msg);
      sprintf(msg, "  input scale factor: %g\n", input->factor);
      LogInfomsg(msg);
      sprintf(msg, "  input offset: %lg\n", input->offset);
      LogInfomsg(msg);
      switch (input->ires)
      {
        case 1:
          LogInfomsg("  input resolution: 1 km\n");
          break;
        case 2:
          LogInfomsg("  input resolution: 500 m\n");
          break;
        case 4:
          LogInfomsg("  input resolution: 250 m\n");
          break;
      }
      LogInfomsg("  %% complete: 0%");

      /* For each input scan */
      kscan = 0;
      for (iscan = 0; iscan < geoloc->nscan; iscan++)
      {
        /* Update status? */
        if (100 * iscan / geoloc->nscan > kscan)
        {
          kscan = 100 * iscan / geoloc->nscan;
          if (kscan % 10 == 0)
          {
            sprintf(msg, " %d%%", kscan);
            LogInfomsg(msg);
	  }
        }

        /* Read the geolocation data for the scan and map to output space */
        if (!GetGeolocSwath(geoloc, output_space, iscan)) 
          LOG_ERROR("reading geolocation for a scan", "main");

        /* Map scan to input resolution */
        if (!MapScanSwath(scan, geoloc)) 
          LOG_ERROR("mapping a scan (swath)", "main");

        /* Extend the scan */
        if (!ExtendScan(scan)) LOG_ERROR("extending the scan", "main");

        /* Read input scan data into extended scan */
        il = iscan * input->scan_size.l;
        nl = input->scan_size.l;
        if (il + nl > input->size.l)
          nl = input->size.l - il;

        if (!GetScanInput(scan, input, il, nl))
          LOG_ERROR("reading input data for a scan", "main");

        /* Resample all of the points in the extended scan */
        if (!ProcessScan(scan, kernel, patches, nl, param->kernel_type))
          LOG_ERROR("resampling a scan", "main");

        /* Toss patches that were not touched */
        if (!TossPatches(patches, param->output_data_type))
          LOG_ERROR("writting patches to disk", "main");

      } /* End loop for each input scan */

      /* Finish the status message */
      LogInfomsg(" 100%\n");

      /* Save the background fill value from the patches data structure for
         output to the metadata */
      param_save->fill_value[curr_sds] = patches->fill_value;

      /* If output is raw binary, then we need the patches information
         so write the header before deleting the patches info */
      if (param->output_file_format == RB_FMT)
      { /* Output is raw binary */
        /* Create the raw binary header file */
        if (!WriteHeaderFile (param, patches))
          LOG_ERROR("writing raw binary header file", "main");
      }

      /* Done with scan and kernel strutures */
      if (!FreeScan(scan))
        LOG_ERROR("freeing scan structure", "main");
      if (!FreeKernel(kernel))
        LOG_ERROR("freeing kernel structure", "main");

      /* Close geolocation file */
      if (!CloseGeoloc(geoloc))
         LOG_ERROR("closing geolocation file", "main");

      /* Close input file */
      if (!CloseInput(input)) LOG_ERROR("closing input file", "main");

      /* Free the output space structure */
      if (!FreeSpace(output_space)) 
        LOG_ERROR("freeing output space structure", "main");

      /* Write remaining patches in memory to disk */
      if (!UntouchPatches(patches)) 
        LOG_ERROR("untouching patches", "main");
      if (!TossPatches(patches, param->output_data_type))
        LOG_ERROR("writting remaining patches to disk", "main");
      if (!FreePatchesInMem(patches))
        LOG_ERROR("freeing patches data structure in memory", "main");

      /* Output format can be HDF, GeoTiff, raw binary, or both HDF and
         GeoTiff */
      if (param->output_file_format == HDF_FMT ||
          param->output_file_format == BOTH)
      { /* Output is HDF */
        /* Create output file. If the output has multiple resolutions, then
           the resolution value will be used as an extension to the basename.
           Otherwise no resolution extension will be used. */
        if (param->multires)
        {
          if (param->output_space_def.proj_num != PROJ_GEO)
            /* Output the pixel size with only two decimal places, since
               the pixel size will be in meters */
            sprintf(HDF_File, "%s_%dm.hdf", param->output_file_name,
              (int) param->output_pixel_size[curr_sds]);
          else
            /* Output the pixel size with four decimal places, since the
               pixel size will be in degrees (need to convert from radians) */
            sprintf(HDF_File, "%s_%.04fd.hdf", param->output_file_name,
              param->output_pixel_size[curr_sds] * DEG);
        }
        else
        {
          sprintf(HDF_File, "%s.hdf", param->output_file_name);
        }

        /* Does the HDF file need to be created? */
        if (param->create_output[curr_sds])
        {
          /* Create the output HDF file */
          if (!CreateOutput(HDF_File))
            LOG_ERROR("creating output image file", "main");
          file_created = true;

          /* Loop through the rest of the SDSs and unmark the ones of the same
             resolution, since they will be output to this same HDF file
             and therefore do not need to be recreated. */
          for (k = curr_sds; k < param_save->num_input_sds; k++)
          {
            if (param->output_pixel_size[k] ==
                param->output_pixel_size[curr_sds])
              param_save->create_output[k] = false;
          }
        }

        /* Open output file */
        output = OutputFile(HDF_File, param->output_sds_name,
          param->output_data_type, &param->output_space_def);
        if (output == (Output_t *)NULL)
          LOG_ERROR("opening output HDF file", "main");
      }

      if (param->output_file_format == GEOTIFF_FMT ||
          param->output_file_format == BOTH)
      { /* Output is geotiff */
        /* Attach the SDS name to the output file name */
        if (param->output_file_format == GEOTIFF_FMT)
        {
          output = &output_mem;
          output->size.l = param->output_space_def.img_size.l;
          output->size.s = param->output_space_def.img_size.s;
          output->open   = true;
        }

        /* Open and initialize the GeoTiff File */
        MasterGeoMem = Open_GEOTIFF(param);
        if( ! MasterGeoMem ) {
           LOG_ERROR("allocating GeoTiff file id structure", "main");
        } else if( MasterGeoMem->error ) {
           LOG_ERROR(MasterGeoMem->error_msg, "main");
        }

/* Remove due to clash of tiff and hdf header files.
 *        if (!OpenGeoTIFFFile (param, &MasterGeoMem))
 *         LOG_ERROR("opening and initializing GeoTiff file", "main");
 */
      }

      if (param->output_file_format == RB_FMT)
      { /* Output is raw binary */
        output = &output_mem;
        output->size.l = param->output_space_def.img_size.l;
        output->size.s = param->output_space_def.img_size.s;
        output->open   = true;

        /* Get the size of the data type */
        switch (param->output_data_type)
        {
          case DFNT_INT8:
          case DFNT_UINT8:
            /* one byte in size */
            output->output_dt_size = 1;
            break;

          case DFNT_INT16:
          case DFNT_UINT16:
            /* two bytes in size */
            output->output_dt_size = 2;
            break;

          case DFNT_INT32:
          case DFNT_UINT32:
          case DFNT_FLOAT32:
            /* four bytes in size */
            output->output_dt_size = 4;
            break;
        }

        /* Copy the SDS name and remove any '/'s in the SDSname */
        j = 0;
        for (i = 0; i < (int)strlen(param->output_sds_name); i++)
        {
          if (param->output_sds_name[i] != '/' &&
              param->output_sds_name[i] != '\\')
          {
            sdsname[j++] = param->output_sds_name[i];
          }
          sdsname[j] = '\0';
        }

        /* Remove any spaces (from the SDS name) from the name */
        k = 0;
        while (sdsname[k])
        {
          if (isspace(sdsname[k]))
            sdsname[k] = '_';
          k++;
        }

        /* Add the SDS band name and dat extension to the filename */
        sprintf(filename, "%s_%s.dat", param->output_file_name,
          sdsname);

        rbfile = fopen(filename, "wb");
        if (rbfile == NULL)
          LOG_ERROR("opening output raw binary file", "main");
      }

      /* Read patches (in input data type) and write to output file (in
         output data type). If NN kernel, then fill any holes left from the
         resampling process. */
      if (!UnscramblePatches(patches, output, param->output_file_format,
          MasterGeoMem, rbfile, param->output_data_type, param->kernel_type))
        LOG_ERROR("unscrambling the output file", "main");

      /* Done with the patches */
      if (!FreePatches(patches))
        LOG_ERROR("freeing patches", "main");

      /* Close output HDF file */
      if (param->output_file_format == HDF_FMT ||
          param->output_file_format == BOTH)
      {
        if (!CloseOutput(output))
          LOG_ERROR("closing output file", "main");

        /* If not appending, write metadata to output HDF file */
        if (file_created)
        {
          if (!WriteMeta(output->file_name, &param->output_space_def)) 
            LOG_ERROR("writing metadata", "main");
        }
      }

      /* Close output GeoTiff file */
      if (param->output_file_format == GEOTIFF_FMT ||
          param->output_file_format == BOTH)
      {
        Close_GEOTIFF( MasterGeoMem );
        /* CloseGeoTIFFFile(&MasterGeoMem); */
        output->open = false;
      }

      /* Close output raw binary file */
      if (param->output_file_format == RB_FMT)
      {
        fclose(rbfile);
        output->open = false;
      }

      /* Free remaining memory */
      if (!FreeGeoloc(geoloc)) 
        LOG_ERROR("freeing geoloc file stucture", "main");
      if (!FreeInput(input)) 
        LOG_ERROR("freeing input file stucture", "main");
      if (param->output_file_format == HDF_FMT ||
          param->output_file_format == BOTH) {
        if (!FreeOutput(output)) 
          LOG_ERROR("freeing output file stucture", "main");
      }

      /* Get rid of patches file */
      ThisPid = getpid();
      sprintf(CharThisPid,"%d",(int)ThisPid);
      strcpy(FinalFileName,param->patches_file_name);
      strcat(FinalFileName,CharThisPid);

      exec_resamp = remove(FinalFileName);
      if(exec_resamp == -1)
      {
        LOG_ERROR("Something bad happened deleting patches file", "main");
      }  

      /* Free the parameter structure */
      if (!FreeParam(param)) 
        LOG_ERROR("freeing user parameter structure", "main");
    } /* loop through bands in the current SDS */
  } /* loop through SDSs */

  /* If output format is HDF then append the metadata, for all resolutions */
  if (param_save->output_file_format == HDF_FMT ||
      param_save->output_file_format == BOTH)
  {
    /* Initialize the create_output structure again for all the SDSs. This
       will be used to determine if the metadata needs to be appended. */
    for (curr_sds = 0; curr_sds < param_save->num_input_sds; curr_sds++)
      param_save->create_output[curr_sds] = true;

    /* Loop through all the SDSs */
    for (curr_sds = 0; curr_sds < param_save->num_input_sds; curr_sds++)
    {
      /* Do we need to append the metadata or has it already been done for
         the current SDSs HDF file? */
      if (!param_save->create_output[curr_sds])
        continue;

      /* Determine the name of the output HDF file */
      if (param_save->multires)
      {
        if (param_save->output_space_def.proj_num != PROJ_GEO)
          /* Output the pixel size with only two decimal places, since
             the pixel size will be in meters */
          sprintf(HDF_File, "%s_%dm.hdf", param_save->output_file_name,
            (int) param_save->output_pixel_size[curr_sds]);
        else
          /* Output the pixel size with four decimal places, since the
             pixel size will be in degrees (need to convert from radians) */
          sprintf(HDF_File, "%s_%.04fd.hdf", param_save->output_file_name,
            param_save->output_pixel_size[curr_sds] * DEG);
      }
      else
      {
        sprintf(HDF_File, "%s.hdf", param_save->output_file_name);
      }

      /* Append the metadata for this HDF file, only for the SDSs of the
         current resolution */
      if (!AppendMetadata(param_save, HDF_File, param_save->input_file_name,
        curr_sds)) 
      {
          /* NOTE: We won't flag this as an error, since in some cases the
             resolution file may not exist.  For example, if a MOD02HKM is
             specified and the Latitude or Longitude data is specified, then
             that data is at a different resolution (1000m) than the rest of
             the image SDS data (500m).  The software will think that there
             should be a 1000m product, however the Latitude and Longitude
             data didn't actually get processed .... since it is float data.
             So, AppendMetadata will flag an error since that HDF file will
             not really exist. */
/*        LOG_ERROR("appending metadata to the output HDF file","main"); */
      }

      /* Loop through the rest of the SDSs and unmark the ones of the same
         resolution, since they will be output to this same HDF file
         and therefore do not need to be recreated. */
      for (k = curr_sds; k < param_save->num_input_sds; k++)
      {
        if (param_save->output_pixel_size[k] ==
            param_save->output_pixel_size[curr_sds])
          param_save->create_output[k] = false;
      }
    }
  }

  /* Free the saved parameter structure */
  if (!FreeParam(param_save))
    LOG_ERROR("freeing saved user parameter structure", "main");

  /* Stop timer and print elapsed time */
  enddate = time(NULL);
  sprintf(msg, "\nEnd Time:  %s", ctime(&enddate));
  LogInfomsg(msg);
  LogInfomsg("Finished processing!\n");
  LogInfomsg( 
    "*********************************************************************"
    "*********\n");

  /* Close the log file */
  CloseLogHandler();

  /* All done */
  exit (EXIT_SUCCESS);
}
// Processing
DWORD CTimeStretchFilter::ThreadProc()
{
  Log("CTimeStretchFilter::timestretch thread - starting up - thread ID: %d", m_ThreadId);
  
  SetThreadName(0, "TimeStretchFilter");

  AudioSinkCommand command;
  CComPtr<IMediaSample> sample;

  while (true)
  {
    m_csResources.Unlock();
    HRESULT hr = GetNextSampleOrCommand(&command, &sample.p, INFINITE, &m_hSampleEvents, &m_dwSampleWaitObjects);
    m_csResources.Lock();

    if (hr == MPAR_S_THREAD_STOPPING)
    {
      Log("CTimeStretchFilter::timestretch thread - closing down - thread ID: %d", m_ThreadId);
      SetEvent(m_hCurrentSampleReleased);
      CloseThread();
      m_csResources.Unlock();
      return 0;
    }
    else
    {
      if (command == ASC_Flush)
      {
      	Log("CTimeStretchFilter::timestretch thread - flushing");
        m_rtInSampleTime = m_rtNextIncomingSampleTime = 0;
        m_rtLastOuputStart = m_rtLastOuputEnd = -1;
        if (m_pNextOutSample)
          m_pNextOutSample.Release();

        flush();
        m_pClock->Flush();
        sample.Release();
        SetEvent(m_hCurrentSampleReleased);
      }
      else if (command == ASC_Pause || command == ASC_Resume)
        continue;
      else if (sample)
      {
        BYTE *pMediaBuffer = NULL;
        long size = sample->GetActualDataLength();

        if (sample->IsDiscontinuity() == S_OK)
        {
          sample->SetDiscontinuity(false);
          m_bDiscontinuity = true;
        }

        REFERENCE_TIME rtDrained = 0;

        if (CheckSample(sample, &rtDrained) == S_FALSE)
        {
          DeleteMediaType(m_pMediaType);
          sample->GetMediaType(&m_pMediaType);
        }

        CheckStreamContinuity(sample, rtDrained);

        m_nSampleNum++;

        hr = sample->GetPointer(&pMediaBuffer);

        if ((hr == S_OK) && m_pMemAllocator)
        {
          REFERENCE_TIME rtStart = 0;
          REFERENCE_TIME rtAdjustedStart = 0;
          REFERENCE_TIME rtEnd = 0;
          REFERENCE_TIME rtAdjustedEnd = 0;
          REFERENCE_TIME rtAHwTime = 0;
          REFERENCE_TIME rtRCTime = 0;

          m_pClock->GetHWTime(&rtRCTime, &rtAHwTime);

          sample->GetTime(&rtStart, &rtEnd);
          REFERENCE_TIME sampleDuration = rtEnd - rtStart;

          uint unprocessedSamplesBefore = numUnprocessedSamples();
          uint unprocessedSamplesAfter = 0;

          UINT32 nFrames = size / m_pOutputFormat->Format.nBlockAlign;

          double bias = m_pClock->GetBias();
          double adjustment = m_pClock->Adjustment();
          double AVMult = m_pClock->SuggestedAudioMultiplier(rtAHwTime, rtRCTime, bias, adjustment);
          setTempoInternal(AVMult, 1.0);

          if (m_rtLastOuputEnd == -1)
            m_rtLastOuputEnd = rtStart / AVMult - 1;

          m_rtLastOuputStart = m_rtLastOuputEnd + 1;

          // Process the sample 
          putSamplesInternal((const short*)pMediaBuffer, size / m_pOutputFormat->Format.nBlockAlign);

          unprocessedSamplesAfter = numUnprocessedSamples();

          UINT32 nInFrames = (size / m_pOutputFormat->Format.nBlockAlign) - unprocessedSamplesAfter + unprocessedSamplesBefore;
          UINT32 nOutFrames = numSamples();
          
          // TODO: Soundtouch can provide less samples than asked (but never more) so a cummulative error is possible.  This will not happen over the course of a long TV stint, but could be solved for correctness
          // m_rtLastOuputEnd += (nOutFrames + unprocessedSamplesAfter - unprocessedSamplesBefore) * UNITS / m_pOutputFormat->Format.nSamplesPerSec;

          //rtStart = m_rtInSampleTime;
          rtEnd = rtStart + sampleDuration;
          rtAdjustedStart = m_rtLastOuputEnd +1;
          rtAdjustedEnd = rtAdjustedStart + sampleDuration / AVMult;

          m_rtLastOuputEnd += sampleDuration / AVMult;

          CreateOutput(nInFrames, nOutFrames, bias, adjustment, AVMult, false);


          m_pClock->AddSample(rtStart, rtAdjustedStart, rtEnd, rtAdjustedEnd);
        }
      }
    }
  }
}
Example #17
0
int main (int argc, const char **argv) {
  Param_t *param;
  Input_t *input;
  Input_t *input_th;
  Lut_t *lut;
  Output_t *output;
  int i,odometer_flag=0;

  /* Space info */
  Space_def_t space_def;
  Space_t *space;
  char *grid_name = "Grid";
  char *sds_names[NSDS];
  int sds_types[NSDS];

  printf ("\nRunning lndcsm ...\n");
  for (i=1; i<argc; i++)if ( !strcmp(argv[i],"-o") )odometer_flag=1;
  param = GetParam(argc, argv);
  if (param == (Param_t *)NULL) ERROR("getting runtime parameters", "main");

  /* Open input file */

  input = OpenInput(param->input_file_name);
  if (input == (Input_t *)NULL) ERROR("bad input file", "main");

  /* Get space definition from HDF */

  if (!GetSpaceDefHDF(&space_def, param->input_file_name, grid_name))
    ERROR("getting definition from file", "main");

  /* Setup Space */

  space = SetupSpace(&space_def);
  if (space == (Space_t *)NULL) ERROR("setting up space", "main");

  /* Open input thermal file */
  if ( param->therm_flag )
    {
    input_th = OpenInput(param->input_therm_file_name);
    if (input_th == (Input_t *)NULL) ERROR("bad thermal input file", "main");

  if ( input_th->size.s != input->size.s || input_th->size.l != input->size.l )
     ERROR("mismatch size on inputs (reflective vs thermal)", "main");
    }

  /* Get Lookup table */
  lut = GetLut(param->lut_file_name, input->nband, &input->meta);
  if (lut == (Lut_t *)NULL) ERROR("bad lut file", "main");
  
  /* Create and open output file */

  if (!CreateOutput(param->output_file_name))
    ERROR("creating output file", "main");

  output = OpenOutput( param->output_file_name, &input->size );
  if (output == (Output_t *)NULL) ERROR("opening output file", "main");

  /******************************************************************/
  /*                         call snow mask                         */
  /******************************************************************/

  if ( !CloudMask( 
                  input 
                , input_th
                , output
                , lut
                , param
                , odometer_flag
		  ) )
     ERROR("In Cloud/Snow Mask Computation","main");

  if (!PutMetadata(output, &input->meta,lut, param) )
    ERROR("writing the metadata", "main");


  if (!CloseInput(input)) ERROR("closing input file", "main");

  if (!CloseOutput(output)) ERROR("closing output file", "main");

  sds_names[0] = output->sds_csm[0].name;
  sds_types[0] = output->sds_csm[0].type;

  if (!PutSpaceDefHDF(&space_def, param->output_file_name, 1, 
                      sds_names, sds_types, grid_name))
    ERROR("putting space metadata in HDF file", "main");

  if (!FreeInput(input)) 
    ERROR("freeing input file stucture", "main");

  if (!FreeLut(lut)) 
    ERROR("freeing lut file stucture", "main");

  if (!FreeOutput(output)) 
    ERROR("freeing output file stucture", "main");

  /* All done */

  printf ("lndcsm complete.\n");

  return (EXIT_SUCCESS);
}