Пример #1
0
static void get_uavsar_line(ReadUavsarClientInfo *info, meta_parameters *meta,
                            int row, float *buf)
{
    // wrapper for get_float_line() that multilooks if needed
    int j,ns=meta->general->sample_count;
    if (info->ml) {
        assert(meta->sar);
        int k,nlooks = meta->sar->azimuth_look_count;
        row *= nlooks;

        // we fudged the line count in the metadata for the
        // viewer (which is displaying a multilooked image), we must
        // put the correct value back for the reader
        int lc = meta->general->line_count;
        meta->general->line_count = g_saved_line_count;

        // FIXME: figure a nice way to avoid allocating every time we read a line
        get_float_line(info->fp, meta, row, buf);
        float *tmp = MALLOC(sizeof(float)*meta->general->sample_count);
        for (k=1; k<nlooks; ++k) {
            get_float_line(info->fp, meta, row+k, tmp);
            for (j=0; j<ns; ++j)
                ieee_big32(tmp[j]);
            for (j=0; j<ns; ++j)
                buf[j] += tmp[j];
        }
        for (j=0; j<ns; ++j)
            buf[j] /= nlooks;
        free(tmp);

        // restore fudged value
        meta->general->line_count = lc;
    }
    else {
        // no multilooking case
        if (info->is_complex) {
            complexFloat *cf_buf = MALLOC(sizeof(complexFloat)*ns);
            get_complexFloat_line(info->fp, meta, row, cf_buf);
            for (j=0; j<ns; ++j) {
                ieee_big32(cf_buf[j].real);
                ieee_big32(cf_buf[j].imag);
                buf[j] = hypot(cf_buf[j].real, cf_buf[j].imag);
                //buf[j] = atan2_check(cf_buf[j].imag, cf_buf[j].real);
            }
            FREE(cf_buf);
        }
        else {
            get_float_line(info->fp, meta, row, buf);
            for (j=0; j<ns; ++j)
                ieee_big32(buf[j]);
        }
    }
}
Пример #2
0
void calc_minmax_polsarpro(const char *inFile, double *min, double *max)
{
  int ii,jj;
  
  *min = 999999;
  *max = -999999;
  
  char *enviName = (char *) MALLOC(sizeof(char)*(strlen(inFile) + 10));
  sprintf(enviName, "%s.hdr", inFile);
  envi_header *envi = read_envi(enviName);
  meta_parameters *meta = envi2meta(envi);
  float *data = MALLOC(sizeof(float) * meta->general->sample_count);
  
  FILE *fp = FOPEN(inFile, "rb");
  asfPrintStatus("\nCalculating min and max ...\n");
  for (ii=0; ii<meta->general->line_count; ++ii) {
    asfPercentMeter(((double)ii/(double)meta->general->line_count));
    get_float_line(fp, meta, ii, data);
    
    for (jj=0; jj<meta->general->sample_count; ++jj) {
      ieee_big32(data[jj]);
      if (data[jj] < *min) *min = data[jj];
      if (data[jj] > *max) *max = data[jj];
    }
  }
  asfPercentMeter(1.0);
  FCLOSE(fp);
  FREE(data);
  meta_free(meta);
  FREE(envi);
  FREE(enviName);
}
Пример #3
0
/*Read_line: reads given line of the given input file,
padding with zeros out to destLen if needed.*/
void read_line(fetchRec *g,int y,float *destBuf,int destLen)
{
    int x;
    if (y>=g->inDDR->nl)
    {   /*We were asked for a line past the end of the image:*/
        for (x=0; x<destLen; x++)
            destBuf[x]=backgroundFill;
        return;/*Return a line of all background*/
    }
    /*Otherwise, the line is in bounds.*/
    /*Check for complex data.*/
    if (g->inDDR->dtype==DTYPE_COMPLEX)
    {   /*Complex data has to be handled specially*/
        int maxInX=g->inDDR->ns;
        unsigned int bytesPerLine=2*sizeof(float)*maxInX;
        float *inputBuffer=(float *)MALLOC(bytesPerLine);
        FSEEK64(g->inFile,(long long) y*bytesPerLine,0);
        ASF_FREAD(inputBuffer,1,bytesPerLine,g->inFile);

        if (g->getRealCpx) /*Fetch Real fields from complex data.*/
            for (x=0; x<maxInX; x++) {
                float f=inputBuffer[x*2];
                ieee_big32(f);
                destBuf[x]=f;
            }
        else /*Fetch Imaginary Fields from complex data.*/
            for (x=0; x<maxInX; x++) {
                float f=inputBuffer[x*2+1];
                ieee_big32(f);
                destBuf[x]=f;
            }
        FREE(inputBuffer);
    }
    else /*Non-complex data is much easier to handle*/
        getFloatLine_mb(g->inFile,g->inDDR,y,g->inBand,destBuf);

    /*Pad to the end of the buffer with the background color*/
    for (x=g->inDDR->ns; x<destLen; x++)
        destBuf[x]=backgroundFill;

}
Пример #4
0
/*******************************************************************************
 * Get x number of lines of data (any data type) and fill a pre-allocated array
 * with it. The data is assumed to be in big endian format and will be converted
 * to the native machine's format. The line_number argument is the zero-indexed
 * line number to get. The dest argument must be a pointer to existing memory.
 * Returns the amount of samples successfully read & converted. */
int get_data_lines(FILE *file, meta_parameters *meta,
       int line_number, int num_lines_to_get,
       int sample_number, int num_samples_to_get,
       void *dest, int dest_data_type)
{
  int ii;               /* Sample index.  */
  int samples_gotten=0; /* Number of samples retrieved */
  int line_samples_gotten;
  size_t sample_size;   /* Sample size in bytes.  */
  void *temp_buffer;    /* Buffer for unconverted data.  */
  int sample_count = meta->general->sample_count;
  int line_count = meta->general->line_count;
  int band_count = meta->general->band_count;
  int data_type    = meta->general->data_type;
  int num_lines_left = line_count * band_count - line_number;
  int num_samples_left = sample_count - sample_number;
  long long offset;

  // Check whether data conversion is possible
  if ((data_type>=COMPLEX_BYTE) && (dest_data_type<=REAL64))
    asfPrintError("\nget_data_lines: Cannot put complex data"
      " into a simple data buffer. Exiting.\n\n");
  if ((data_type<=REAL64) && (dest_data_type>=COMPLEX_BYTE))
    asfPrintError("\nget_data_lines: Cannot put simple data"
      " into a complex data buffer. Exiting.\n\n");
  // Make sure not to go outside the image
  if (line_number > (line_count * band_count))
    asfPrintError("\nget_data_lines: Cannot read line %d "
      "in a file of %d lines. Exiting.\n",
      line_number, line_count*band_count);
  if (sample_number < 0 || sample_number > meta->general->sample_count)
    asfPrintError("\nget_data_lines: Cannot read sample %d "
      "in a file of %d lines. Exiting.\n",
      sample_number, sample_count);
  if (num_lines_to_get > num_lines_left)
    asfPrintError("\nget_data_lines: Cannot read %d lines. "
      "Only %d lines left in file. Exiting.\n",
      num_lines_to_get, num_lines_left);
  if (num_samples_to_get > num_samples_left)
    asfPrintError("\nget_data_lines: Cannot read %d samples. "
      "Only %d samples left in file. Exiting.\n",
      num_samples_to_get, num_samples_left);

  /* Determine sample size.  */
  sample_size = data_type2sample_size(data_type);

  temp_buffer = MALLOC( sample_size * num_lines_to_get * num_samples_to_get);


  // Scan to the beginning of the line sample.
  for (ii=0; ii<num_lines_to_get; ii++) {
    offset = (long long)sample_size *
        ((long long)sample_count * ((long long)line_number + (long long)ii) + (long long)sample_number);
    if (offset<0) {
        asfPrintError("File offset overflow error ...file is too large to read.\n"
                      "offset = %ld (sample_size * (sample_count * (line_number + ii) + sample_number)\n"
                      "sample_size = %d\n"
                      "sample_count = %d\n"
                      "line_number = %d\n"
                      "ii = %d\n"
                      "sample_number = %d\n",
                      offset, sample_size, sample_count, line_number, ii, sample_number);
    }
    FSEEK64(file, offset, SEEK_SET);
    line_samples_gotten = FREAD(temp_buffer+ii*num_samples_to_get*sample_size,
        sample_size, num_samples_to_get, file);
    samples_gotten += line_samples_gotten;
  }

  /* Fill in destination array.  */
  switch (data_type) {
    case REAL32:
      for ( ii=0; ii<samples_gotten; ii++ ) {
        ieee_big32(((float*)temp_buffer)[ii]);
        switch (dest_data_type) {
         case ASF_BYTE:((unsigned char*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case INTEGER16:((short int*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case INTEGER32:((int*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case REAL32:((float*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case REAL64:((double*)dest)[ii] = ((float*)temp_buffer)[ii];break;
        }
      }
      break;
    case COMPLEX_REAL32:
      for ( ii=0; ii<samples_gotten*2; ii++ ) {
        ieee_big32(((float*)temp_buffer)[ii]);
        switch (dest_data_type) {
         case COMPLEX_BYTE:((unsigned char*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER16:((short int*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER32:((int*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case COMPLEX_REAL32:((float*)dest)[ii] = ((float*)temp_buffer)[ii];break;
         case COMPLEX_REAL64:((double*)dest)[ii] = ((float*)temp_buffer)[ii];break;
        }
      }
      break;
    case ASF_BYTE:
      for ( ii=0; ii<samples_gotten; ii++ ) {
        switch (dest_data_type) {
         case ASF_BYTE:((unsigned char*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case INTEGER16:((short int*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case INTEGER32:((int*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case REAL32:((float*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case REAL64:((double*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
        }
      }
     break;
    case INTEGER16:
      for ( ii=0; ii<samples_gotten; ii++ ) {
        big16(((short int *)temp_buffer)[ii]);
        switch (dest_data_type) {
         case ASF_BYTE:((unsigned char*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case INTEGER16:((short int*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case INTEGER32:((int*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case REAL32:((float*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case REAL64:((double*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
        }
      }
      break;
    case INTEGER32:
      for ( ii=0; ii<samples_gotten; ii++ ) {
        big32(((int *)temp_buffer)[ii]);
        switch (dest_data_type) {
         case ASF_BYTE:((unsigned char*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case INTEGER16:((short int*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case INTEGER32:((int*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case REAL32:((float*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case REAL64:((double*)dest)[ii] = ((int*)temp_buffer)[ii];break;
        }
      }
      break;
    case REAL64:
      for ( ii=0; ii<samples_gotten; ii++ ) {
        ieee_big64(((double*)temp_buffer)[ii]);
        switch (dest_data_type) {
         case ASF_BYTE:((unsigned char*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case INTEGER16:((short int*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case INTEGER32:((int*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case REAL32:((float*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case REAL64:((double*)dest)[ii] = ((double*)temp_buffer)[ii];break;
        }
      }
    case COMPLEX_BYTE:
      for ( ii=0; ii<samples_gotten*2; ii++ ) {
        switch (dest_data_type) {
         case COMPLEX_BYTE:((unsigned char*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER16:((short int*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER32:((int*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case COMPLEX_REAL32:((float*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
         case COMPLEX_REAL64:((double*)dest)[ii] = ((unsigned char*)temp_buffer)[ii];break;
        }
      }
      break;
    case COMPLEX_INTEGER16:
      for ( ii=0; ii<samples_gotten*2; ii++ ) {
        big16(((short int *)temp_buffer)[ii]);
         switch (dest_data_type) {
         case COMPLEX_BYTE:((unsigned char*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER16:((short int*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER32:((int*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case COMPLEX_REAL32:((float*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
         case COMPLEX_REAL64:((double*)dest)[ii] = ((short int*)temp_buffer)[ii];break;
        }
      }
      break;
    case COMPLEX_INTEGER32:
      for ( ii=0; ii<samples_gotten*2; ii++ ) {
        big32(((int *)temp_buffer)[ii]);
        switch (dest_data_type) {
         case COMPLEX_BYTE:((unsigned char*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER16:((short int*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER32:((int*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case COMPLEX_REAL32:((float*)dest)[ii] = ((int*)temp_buffer)[ii];break;
         case COMPLEX_REAL64:((double*)dest)[ii] = ((int*)temp_buffer)[ii];break;
        }
      }
      break;
    case COMPLEX_REAL64:
      for ( ii=0; ii<samples_gotten*2; ii++ ) {
        ieee_big64(((double*)temp_buffer)[ii]);
        switch (dest_data_type) {
         case COMPLEX_BYTE:((unsigned char*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER16:((short int*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case COMPLEX_INTEGER32:((int*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case COMPLEX_REAL32:((float*)dest)[ii] = ((double*)temp_buffer)[ii];break;
         case COMPLEX_REAL64:((double*)dest)[ii] = ((double*)temp_buffer)[ii];break;
        }
      }
  }

  FREE(temp_buffer);
  return samples_gotten;
}
Пример #5
0
/*******************************************************************************
 * Write x number of lines of any data type to file in the data format specified
 * by the meta structure. It is always written in big endian format. Returns the
 * amount of samples successfully converted & written. Will not write more lines
 * than specified in the supplied meta struct. */
static int put_data_lines(FILE *file, meta_parameters *meta, int band_number,
                          int line_number_in_band, int num_lines_to_put,
                          const void *source, int source_data_type)
{
  int ii;               /* Sample index.                       */
  int samples_put;      /* Number of samples written           */
  size_t sample_size;   /* Sample size in bytes.               */
  void *out_buffer;     /* Buffer of converted data to write.  */
  int sample_count       = meta->general->sample_count;
  int data_type          = meta->general->data_type;
  int num_samples_to_put = num_lines_to_put * sample_count;
  int line_number        = meta->general->line_count * band_number +
                               line_number_in_band;

  if ((source_data_type>=COMPLEX_BYTE) && (data_type<=REAL64)) {
    printf("\nput_data_lines: Cannot put complex data into a simple data file. Exiting.\n\n");
    exit(EXIT_FAILURE);
  }
  if ((source_data_type<=REAL64) && (data_type>=COMPLEX_BYTE)) {
    printf("\nput_data_lines: Cannot put simple data into a complex data file. Exiting.\n\n");
    exit(EXIT_FAILURE);
  }

  // Write out all optical data as byte image.
  // They don't have a larger dynamic range than that.
  if (meta->optical)
    data_type = ASF_BYTE;

  /* Determine sample size.  */
  sample_size = data_type2sample_size(data_type);

  /* Make sure not to make file bigger than meta says it should be */
  if (line_number > meta->general->line_count * meta->general->band_count) {
    printf("\nput_data_lines: Cannot write line %d of band %d in a\n"
           "file that should be %d lines. Exiting.\n",
           line_number, band_number,
           meta->general->line_count * meta->general->band_count);
    exit(EXIT_FAILURE);
  }
  if ((line_number+num_lines_to_put) >
      meta->general->line_count * meta->general->band_count)
  {
    num_samples_to_put = (meta->general->line_count - line_number)
                          * sample_count;
  }

  FSEEK64(file, (long long)sample_size*sample_count*line_number, SEEK_SET);
  out_buffer = MALLOC( sample_size * sample_count * num_lines_to_put );

  /* Fill in destination array.  */
  switch (data_type) {
    case REAL32:
      for ( ii=0; ii<num_samples_to_put; ii++ ) {
        switch (source_data_type) {
         case ASF_BYTE:((float*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case INTEGER16:((float*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case INTEGER32:((float*)out_buffer)[ii] = ((int*)source)[ii];break;
         case REAL32:((float*)out_buffer)[ii] = ((float*)source)[ii];break;
         case REAL64:((float*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        ieee_big32( ((float*)out_buffer)[ii] );
      }
      break;
    case COMPLEX_REAL32:
      for ( ii=0; ii<num_samples_to_put*2; ii++ ) {
        switch (source_data_type) {
         case COMPLEX_BYTE:((float*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case COMPLEX_INTEGER16:((float*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case COMPLEX_INTEGER32:((float*)out_buffer)[ii] = ((int*)source)[ii];break;
         case COMPLEX_REAL32:((float*)out_buffer)[ii] = ((float*)source)[ii];break;
         case COMPLEX_REAL64:((float*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        ieee_big32( ((float*)out_buffer)[ii] );
      }
      break;
    case ASF_BYTE:
      for ( ii=0; ii<num_samples_to_put; ii++ ) {
        switch (source_data_type) {
         case ASF_BYTE:((unsigned char*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case INTEGER16:((unsigned char*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case INTEGER32:((unsigned char*)out_buffer)[ii] = ((int*)source)[ii];break;
         case REAL32:((unsigned char*)out_buffer)[ii] = ((float*)source)[ii];break;
         case REAL64:((unsigned char*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
      }
      break;
    case INTEGER16:
      for ( ii=0; ii<num_samples_to_put; ii++ ) {
        switch (source_data_type) {
         case ASF_BYTE:((short int*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case INTEGER16:((short int*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case INTEGER32:((short int*)out_buffer)[ii] = ((int*)source)[ii];break;
         case REAL32:((short int*)out_buffer)[ii] = ((float*)source)[ii];break;
         case REAL64:((short int*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        big16( ((short int*)out_buffer)[ii] );
      }
      break;
    case INTEGER32:
      for ( ii=0; ii<num_samples_to_put; ii++ ) {
        switch (source_data_type) {
         case ASF_BYTE:((int*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case INTEGER16:((int*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case INTEGER32:((int*)out_buffer)[ii] = ((int*)source)[ii];break;
         case REAL32:((int*)out_buffer)[ii] = ((float*)source)[ii];break;
         case REAL64:((int*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        big32( ((int*)out_buffer)[ii] );
      }
      break;
    case REAL64:
      for ( ii=0; ii<num_samples_to_put; ii++ ) {
        switch (source_data_type) {
         case ASF_BYTE:((double*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case INTEGER16:((double*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case INTEGER32:((double*)out_buffer)[ii] = ((int*)source)[ii];break;
         case REAL32:((double*)out_buffer)[ii] = ((float*)source)[ii];break;
         case REAL64:((double*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        ieee_big64( ((double*)out_buffer)[ii] );
      }
      break;
    case COMPLEX_BYTE:
      for ( ii=0; ii<num_samples_to_put*2; ii++ )
        switch (source_data_type) {
         case COMPLEX_BYTE:((unsigned char*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case COMPLEX_INTEGER16:((unsigned char*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case COMPLEX_INTEGER32:((unsigned char*)out_buffer)[ii] = ((int*)source)[ii];break;
         case COMPLEX_REAL32:((unsigned char*)out_buffer)[ii] = ((float*)source)[ii];break;
         case COMPLEX_REAL64:((unsigned char*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
     break;
    case COMPLEX_INTEGER16:
      for ( ii=0; ii<num_samples_to_put*2; ii++ ) {
        switch (source_data_type) {
         case COMPLEX_BYTE:((short int*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case COMPLEX_INTEGER16:((short int*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case COMPLEX_INTEGER32:((short int*)out_buffer)[ii] = ((int*)source)[ii];break;
         case COMPLEX_REAL32:((short int*)out_buffer)[ii] = ((float*)source)[ii];break;
         case COMPLEX_REAL64:((short int*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        big16( ((short int*)out_buffer)[ii] );
      }
      break;
    case COMPLEX_INTEGER32:
      for ( ii=0; ii<num_samples_to_put*2; ii++ ) {
        switch (source_data_type) {
         case COMPLEX_BYTE:((int*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case COMPLEX_INTEGER16:((int*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case COMPLEX_INTEGER32:((int*)out_buffer)[ii] = ((int*)source)[ii];break;
         case COMPLEX_REAL32:((int*)out_buffer)[ii] = ((float*)source)[ii];break;
         case COMPLEX_REAL64:((int*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        big32( ((int*)out_buffer)[ii] );
      }
      break;
    case COMPLEX_REAL64:
      for ( ii=0; ii<num_samples_to_put*2; ii++ ) {
        switch (source_data_type) {
         case COMPLEX_BYTE:((double*)out_buffer)[ii] = ((unsigned char*)source)[ii];break;
         case COMPLEX_INTEGER16:((double*)out_buffer)[ii] = ((short int*)source)[ii];break;
         case COMPLEX_INTEGER32:((double*)out_buffer)[ii] = ((int*)source)[ii];break;
         case COMPLEX_REAL32:((double*)out_buffer)[ii] = ((float*)source)[ii];break;
         case COMPLEX_REAL64:((double*)out_buffer)[ii] = ((double*)source)[ii];break;
        }
        ieee_big64( ((double*)out_buffer)[ii] );
      }
      break;
  }
  samples_put = FWRITE(out_buffer, sample_size, num_samples_to_put, file);
  FREE(out_buffer);

  if ( samples_put != num_samples_to_put ) {
    printf("put_data_lines: failed to write the correct number of samples\n");
  }

  return samples_put;
}
Пример #6
0
int update_state_vectors(char *outBaseName, char *file)
{
    int ret = FALSE;
    char *odrFile = NULL;
    meta_parameters *meta = meta_read(outBaseName);

    const char *sensor = meta->general->sensor;
    if (strcmp_case(sensor, "E1") == 0 ||
            strcmp_case(sensor, "ERS1") == 0)
    {
        sensor = "ERS-1";
    }
    if (strcmp_case(sensor, "E2") == 0 ||
            strcmp_case(sensor, "ERS2") == 0)
    {
        sensor = "ERS-2";
    }

    if (strcmp_case(sensor, "ERS-1") != 0 &&
            strcmp_case(sensor, "ERS-2") != 0)
    {
        // not ERS data
        meta_free(meta);
        return ret;
    }

    asfPrintStatus("\nChecking for %s precision state vectors...\n",
                   sensor);

    if (!file || strlen(file) == 0) {
        file = get_arclist_from_settings(sensor);
    }
    if (!file || strlen(file) == 0) {
        return ret;
    }

    if (is_dir(file)) {
        char *arclist = MALLOC(sizeof(char)*(strlen(file) + 128));
        if (file[strlen(file)-1] == DIR_SEPARATOR)
            sprintf(arclist, "%sarclist", file);
        else
            sprintf(arclist, "%s%carclist", file, DIR_SEPARATOR);
        if (isArcList(sensor, arclist))
            odrFile = findInArcList(meta->general->acquisition_date, arclist);
        else
            asfPrintError("No arclist file found in %s\n", file);
        FREE(arclist);
    }
    else if (isArcList(sensor, file))
        odrFile = findInArcList(meta->general->acquisition_date, file);
    else if (!fileExists(file))
        asfPrintError("Not found: %s\n", file);
    else
        // assume file is just a regular ODR file
        odrFile = STRDUP(file);

    if (!odrFile || !fileExists(odrFile))
        asfPrintError("Precision state vector file (%s) does not exist!\n",
                      odrFile ? odrFile : "null");

    asfPrintStatus("Refining %s orbits using ODR file: %s\n",
                   sensor, odrFile);

    FILE *fpIn = FOPEN(odrFile, "rb");

    // Check for new version of ODR file: xODR
    char spec[5];
    FREAD(&spec, 1, 4, fpIn);
    spec[4] = '\0';
    if (strcmp_case(spec, "xODR") != 0)
        asfPrintError("Unsupported precision state vector file (%s) type\n",
                      odrFile);

    // Determine image center time for reference
    julian_date jd;
    jd.year = meta->state_vectors->year;
    jd.jd = meta->state_vectors->julDay;
    double ref_secs = meta->state_vectors->second;
    double ref_time = date2seconds(&jd, ref_secs);
    int stVec_count = meta->state_vectors->vector_count;
    ref_time += meta->state_vectors->vecs[stVec_count/2].time;

    // Initialize structure for updated state vector structure
    meta_state_vectors *prcVec = meta_state_vectors_init(9);
    int year = meta->state_vectors->year;
    prcVec->year = year;
    int julDay = meta->state_vectors->julDay;
    prcVec->julDay = julDay;
    double second = meta->state_vectors->second;
    prcVec->second = second;
    prcVec->vector_count = 9;

    // Read header information
    char satellite[9];
    int begin, repeat_cycle, arc, nRecords, version;
    FREAD(&satellite, 1, 8, fpIn);
    satellite[8] = '\0';
    FREAD(&begin, 1, 4, fpIn);
    ieee_big32(begin);
    FREAD(&repeat_cycle, 1, 4, fpIn);
    ieee_big32(repeat_cycle);
    FREAD(&arc, 1, 4, fpIn);
    ieee_big32(arc);
    FREAD(&nRecords, 1, 4, fpIn);
    ieee_big32(nRecords);
    FREAD(&version, 1, 4, fpIn);
    ieee_big32(version);

    // Read state vectors
    doris_prc_polar *stVec =
        (doris_prc_polar *) MALLOC(sizeof(doris_prc_polar)*nRecords);
    int ii, kk, closest = 0, time, nLat, nLon, nHeight;
    double diff = DAY2SEC*100;
    for (ii=0; ii<nRecords; ii++) {
        FREAD(&time, 1, 4, fpIn);
        ieee_big32(time);
        FREAD(&nLat, 1, 4, fpIn);
        ieee_big32(nLat);
        FREAD(&nLon, 1, 4, fpIn);
        ieee_big32(nLon);
        FREAD(&nHeight, 1, 4, fpIn);
        ieee_big32(nHeight);
        stVec[ii].time = time;
        stVec[ii].lat = (double)nLat/10000000.0;
        stVec[ii].lon = (double)nLon/10000000.0;
        stVec[ii].height = (double)nHeight/1000.0;
        if (fabs((double)time - ref_time) < diff) {
            closest = ii;
            diff = fabs(time - ref_time);
        }
    }
    FCLOSE(fpIn);
    if (closest > 0) {
        asfPrintStatus("Updating orbit information with precision state vectors.\n\n");
        ret = TRUE;

        // Generating state vectors in earth-fixed coordinates
        doris_prc_polar polarVec;
        doris_prc_cartesian cartVec, velOne, velTwo;
        ref_time = date2seconds(&jd, ref_secs);
        for (ii=closest-4; ii<=closest+4; ii++) {
            geocentric_latlon(stVec[ii], &polarVec);
            polar2cartesian(polarVec, &cartVec);

            // Approximate state vector velocity according to getorb FAQs
            // (http://www.deos.tudelft.nl/ers/precorbs/faq.shtml#004001):
            // XYZvel(t) = XYZpos(t + 0.5sec) - XYZpos(t - 0.5sec)
            // Approximating velocities is fine since we will use an interpolation
            // scheme later that does not require these.
            interpolate_prc_vectors(stVec, cartVec.time-0.5, closest-4, closest+4,
                                    &velOne);
            interpolate_prc_vectors(stVec, cartVec.time+0.5, closest-4, closest+4,
                                    &velTwo);

            // Fill in the orbit information into metadata state vector structure
            kk = ii - closest + 4;
            prcVec->vecs[kk].time = cartVec.time - ref_time;
            prcVec->vecs[kk].vec.pos.x = cartVec.x;
            prcVec->vecs[kk].vec.pos.y = cartVec.y;
            prcVec->vecs[kk].vec.pos.z = cartVec.z;
            prcVec->vecs[kk].vec.vel.x = velTwo.x - velOne.x;
            prcVec->vecs[kk].vec.vel.y = velTwo.y - velOne.y;
            prcVec->vecs[kk].vec.vel.z = velTwo.z - velOne.z;
        }
        FREE(meta->state_vectors);
        meta->state_vectors = prcVec;
        meta_write(meta, outBaseName);
        meta_free(meta);
        FREE(stVec);
    }
    else
        asfPrintStatus("\nCould not update orbit information with precision state"
                       " vectors\nOrbit information not available in orbit file "
                       "(%s).\n\n", odrFile);

    return ret;
}
Пример #7
0
int main(int argc, char **argv)
{
  FILE *fpIn, *fpOut, *fpInList, *fpOutList, *fpXml;
  meta_parameters *meta;
  extern int currArg; /* from cla.h in asf.h... initialized to 1 */
  logflag = 0;
  
  // Parse command line args
  while (currArg < (argc-2)) {
    char *key=argv[currArg++];
    if (strmatch(key,"-log")) {
      sprintf(logFile, "%s", argv[currArg]);
      logflag = 1;
    }
    else {
      printf("\n   ***Invalid option:  %s\n\n",
	     argv[currArg-1]);
      usage(argv[0]);
    }
  }
  if ((argc-currArg) < 2) {
    printf("Insufficient arguments.\n");
    usage(argv[0]);
  }
  
  asfSplashScreen(argc, argv);
  
  char *listInFile = (char *) MALLOC(sizeof(char)*(strlen(argv[1])+1));
  strcpy(listInFile, argv[1]);
  char *outFile = (char *) MALLOC(sizeof(char)*(strlen(argv[2])+1));
  strcpy(outFile, argv[2]);
  
  // Setup file names
  char outDirName[512], outFileName[512];
  split_dir_and_file(outFile, outDirName, outFileName);
  char *tmpDir = (char *) MALLOC(sizeof(char)*512);
  sprintf(tmpDir, "%smeasures-", outDirName);
  char *tsdir = time_stamp_dir();
  strcat(tmpDir, tsdir);
  FREE(tsdir);
  create_clean_dir(tmpDir);
  char *isoStr = iso_date();

  // Read header information
  char inFile[512], imgFile[768], metaFile[768];
  char listOutFile[768], citation[50], start[30], end[30], first[30];
  char header[120], baseName[512], dirName[512], ext[5];
  float x_pix, y_pix, x_map_ll, y_map_ll, x_map_ur, y_map_ur, inc, cat;
  double lat, lon, height, x, y, z;
  int ii, kk, nFiles=0, num = 1, sample_count, line_count;
  image_data_type_t image_data_type;
  sprintf(listOutFile, "%s%crgps.xml", tmpDir, DIR_SEPARATOR);

  // Preparing map projection information
  project_parameters_t pps;
  projection_type_t proj_type;
  datum_type_t datum;
  spheroid_type_t spheroid;
  read_proj_file("polar_stereographic_north_ssmi.proj", 
     &pps, &proj_type, &datum, &spheroid);
  pps.ps.false_easting = 0.0;
  pps.ps.false_northing = 0.0;
  meta_projection *proj = meta_projection_init();
  proj->type = proj_type;
  proj->datum = HUGHES_DATUM;
  proj->spheroid = HUGHES_SPHEROID;
  proj->param = pps;
  strcpy(proj->units, "meters");
  proj->hem = 'N';
  spheroid_axes_lengths(spheroid, &proj->re_major, &proj->re_minor);
  FREE(proj);

  // Set up supplemental file names: water mask, lat/lon, x/y grids
  char maskFile[768], latFile[768], lonFile[768], xFile[768], yFile[768]; 
  sprintf(maskFile, "%s%cwater_mask.img", tmpDir, DIR_SEPARATOR);
  sprintf(latFile, "%s%clatitude.img", tmpDir, DIR_SEPARATOR);
  sprintf(lonFile, "%s%clongitude.img", tmpDir, DIR_SEPARATOR);
  sprintf(xFile, "%s%cxgrid.img", tmpDir, DIR_SEPARATOR);
  sprintf(yFile, "%s%cygrid.img", tmpDir, DIR_SEPARATOR);

  // Generating output XML file
  fpInList = FOPEN(listInFile, "r");
  fpOutList = FOPEN(listOutFile, "w");
  fprintf(fpOutList, "<netcdf>\n");
  fprintf(fpOutList, "  <data>\n");
  fprintf(fpOutList, "    <latitude>%s</latitude>\n", latFile);
  fprintf(fpOutList, "    <longitude>%s</longitude>\n", lonFile);
  fprintf(fpOutList, "    <xgrid>%s</xgrid>\n", xFile);
  fprintf(fpOutList, "    <ygrid>%s</ygrid>\n", yFile);
  fprintf(fpOutList, "    <mask>%s</mask>\n", maskFile);
  
  julian_date jdStart, jdEnd, jdRef;
  hms_time hms;
  hms.hour = 0;
  hms.min = 0;
  hms.sec = 0.0;

  asfPrintStatus("Working through the file list:\n");
  int myrFlag=FALSE, divFlag=FALSE, vrtFlag=FALSE, shrFlag=FALSE;
  int firstYear, firstDay, startYear, startDay, endYear, endDay;
  double westBoundLon, eastBoundLon, northBoundLat, southBoundLat;
  double minLat=90.0, maxLat=-90.0, minLon=180.0, maxLon=-180.0;

  while (fgets(inFile, 512, fpInList)) {

    chomp(inFile);
    char inDirName[512], inFileName[512];
    split_dir_and_file(inFile, inDirName, inFileName);

    // Preparing map projection information
    project_parameters_t pps;
    projection_type_t proj_type;
    datum_type_t datum;
    spheroid_type_t spheroid;
    read_proj_file("polar_stereographic_north_ssmi.proj", 
       &pps, &proj_type, &datum, &spheroid);
    pps.ps.false_easting = 0.0;
    pps.ps.false_northing = 0.0;
    meta_projection *proj = meta_projection_init();
    proj->type = proj_type;
    proj->datum = HUGHES_DATUM;
    proj->spheroid = HUGHES_SPHEROID;
    proj->param = pps;
    strcpy(proj->units, "meters");
    proj->hem = 'N';
    spheroid_axes_lengths(spheroid, &proj->re_major, &proj->re_minor);

    // Sort out dates
    startYear = subInt(inFileName, 0, 4);
    startDay = subInt(inFileName, 4, 3);
    endYear = subInt(inFileName, 8, 4);
    endDay = subInt(inFileName, 12, 3);
    if (nFiles == 0) {
      firstYear = startYear;
      firstDay = startDay;
    }
    sprintf(citation, "%d%03d to %d%03d", startYear, startDay, endYear, endDay);
    rgps2iso_date(startYear, (double) startDay, start);
    rgps2iso_date(endYear, (double) endDay, end);
    rgps2iso_date(firstYear, (double) firstDay, first);
    
    // Read header information
    FILE *fpIn = FOPEN(inFile, "r");
    fgets(header, 100, fpIn);
    sscanf(header, "%f %f %f %f %f %f", &x_pix, &y_pix, &x_map_ll, &y_map_ll, 
      &x_map_ur, &y_map_ur);
    fgets(header, 100, fpIn);
    int params = sscanf(header, "%f %f %d %d", 
      &inc, &cat, &sample_count, &line_count);
    if (params == 3) {
      sscanf(header, "%f %d %d", &cat, &sample_count, &line_count);
      inc = 0;
    }
    else if (params == 2) {
      sscanf(header, "%d %d", &sample_count, &line_count);
      inc = 0;
      cat = 1;
    }
    num = (int) cat;
    if (num > 1)
      asfPrintError("Multiband imagery (%s) not supported for netCDF "
        "generation!\n", inFile);

    /*  
    printf("x_pix: %f, y_pix: %f\n", x_pix, y_pix);
    printf("x_map_ll: %f, y_map_ll: %f\n", x_map_ll, y_map_ll);
    printf("x_map_ur: %f, y_map_ur: %f\n", x_map_ur, y_map_ur);
    printf("sample_count: %d, line_count: %d\n\n", sample_count, line_count);
    */
      
    // Check extension
    split_base_and_ext(inFileName, 1, '.', baseName, ext);
    asfPrintStatus("Processing %s ...\n", inFileName);
    sprintf(imgFile, "%s%c%s_%s.img", tmpDir, DIR_SEPARATOR, baseName, &ext[1]);
    sprintf(metaFile, "%s%c%s_%s.meta", tmpDir, DIR_SEPARATOR, baseName, 
      &ext[1]);
    
    jdRef.year = firstYear;
    jdRef.jd = 1;
    jdStart.year = startYear;
    jdStart.jd = startDay;
    jdEnd.year = endYear;
    jdEnd.jd = endDay;
    double startSec = date2sec(&jdStart, &hms) - date2sec(&jdRef, &hms);
    double endSec = date2sec(&jdEnd, &hms) - date2sec(&jdRef, &hms);
    if (strcmp_case(ext, ".MYR") == 0) {
      fprintf(fpOutList, "    <multiyear_ice_fraction start=\"%.0f\" end=\"%.0f"
        "\">%s</multiyear_ice_fraction>\n", startSec, endSec, imgFile);
      image_data_type = MULTIYEAR_ICE_FRACTION;
      myrFlag = TRUE;
    }
    else if (strcmp_case(ext, ".DIV") == 0) {
      fprintf(fpOutList, "    <divergence start=\"%.0f\" end=\"%.0f\">%s"
        "</divergence>\n", startSec, endSec, imgFile);
      image_data_type = DIVERGENCE;
      divFlag = TRUE;
    }
    else if (strcmp_case(ext, ".VRT") == 0) {
      fprintf(fpOutList, "    <vorticity start=\"%.0f\" end=\"%.0f\">%s"
        "</vorticity>\n", startSec, endSec, imgFile);
      image_data_type = VORTICITY;
      vrtFlag = TRUE;
    }
    else if (strcmp_case(ext, ".SHR") == 0) {
      fprintf(fpOutList, "    <shear start=\"%.0f\" end=\"%.0f\">%s</shear>", 
        startSec, endSec, imgFile);
      image_data_type = SHEAR;
      shrFlag = TRUE;
    }

    // Generate basic metadata
    meta = raw_init();
    meta->general->line_count = line_count;
    meta->general->sample_count = sample_count;
    meta->general->band_count = 1;
    meta->general->data_type = REAL32;
    meta->general->image_data_type = image_data_type;
    strcpy(meta->general->basename, inFile);
    meta->general->x_pixel_size = x_pix*1000.0;
    meta->general->y_pixel_size = y_pix*1000.0;
    meta->general->start_line = 0;
    meta->general->start_sample = 0;
    meta->general->no_data = MAGIC_UNSET_DOUBLE;
    strcpy(meta->general->sensor, "RGPS MEaSUREs");
    char *tmp = image_data_type2str(meta->general->image_data_type);
    sprintf(meta->general->bands, "%s", lc(tmp));
    FREE(tmp);
    sprintf(meta->general->acquisition_date, "%s", baseName);
    
    // Sort out map projection
    proj->startX = x_map_ll*1000.0;
    proj->startY = y_map_ur*1000.0;
    proj->perX = x_pix*1000.0;
    proj->perY = -y_pix*1000.0;
    meta->projection = proj;
    meta_write(meta, metaFile);
    strcpy(meta->general->bands, "water mask");
    sprintf(metaFile, "%s%cwater_mask.meta", tmpDir, DIR_SEPARATOR);
    meta_write(meta, metaFile);  
    sprintf(metaFile, "%s%c%s_%s.meta", tmpDir, DIR_SEPARATOR, baseName, 
      &ext[1]);
    
    float *floatBuf = (float *) MALLOC(sizeof(float)*sample_count);

    // Write gridded data to ASF internal format
    fpOut = FOPEN(imgFile, "wb");
    for (ii=0; ii<line_count; ii++) {
      for (kk=0; kk<sample_count; kk++) {
	      ASF_FREAD(&floatBuf[kk], sizeof(float), 1, fpIn);
	      ieee_big32(floatBuf[kk]);
        if (floatBuf[kk] > 10000000000.0 || 
          FLOAT_EQUIVALENT(floatBuf[kk], 10000000000.0))
          floatBuf[kk] = MAGIC_UNSET_DOUBLE;
      }
      put_float_line(fpOut, meta, line_count-ii-1, floatBuf);
    }
    FCLOSE(fpOut);
    FREE(floatBuf);
    
    double lat1, lon1, lat2, lon2, lat3, lon3, lat4, lon4;
    proj_to_latlon(proj, x_map_ll*1000.0, y_map_ll*1000.0, 0.0, 
      &lat1, &lon1, &height);
    proj_to_latlon(proj, x_map_ll*1000.0, y_map_ur*1000.0, 0.0, 
      &lat2, &lon2, &height);
    proj_to_latlon(proj, x_map_ur*1000.0, y_map_ur*1000.0, 0.0, 
      &lat3, &lon3, &height);
    proj_to_latlon(proj, x_map_ur*1000.0, y_map_ll*1000.0, 0.0, 
      &lat4, &lon4, &height);
    westBoundLon = minValue(lon1*R2D, lon2*R2D, lon3*R2D, lon4*R2D);
    eastBoundLon = maxValue(lon1*R2D, lon2*R2D, lon3*R2D, lon4*R2D);
    northBoundLat = maxValue(lat1*R2D, lat2*R2D, lat3*R2D, lat4*R2D);
    southBoundLat = minValue(lat1*R2D, lat2*R2D, lat3*R2D, lat4*R2D);
    if (westBoundLon < minLon)
      minLon = westBoundLon;
    if (eastBoundLon > maxLon)
      maxLon = eastBoundLon;
    if (southBoundLat < minLat)
      minLat = southBoundLat;
    if (northBoundLat > maxLat)
      maxLat = northBoundLat;

    meta_free(meta);
    nFiles++;
  }
  FCLOSE(fpInList);
  
  fprintf(fpOutList, "  </data>\n");
  fprintf(fpOutList, "  <metadata>\n");
  fprintf(fpOutList, "    <time>\n");
  fprintf(fpOutList, "      <axis type=\"string\" definition=\"name of axis\">T"
    "</axis>\n");
  fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
    "descriptive name\">serial date</long_name>\n");
  fprintf(fpOutList, "      <references type=\"string\" definition=\"reference "
    "of the value\">start time of 3-day average</references>\n");
  fprintf(fpOutList, "      <standard_name type=\"string\" definition=\"name "
    "used to identify the physical quantity\">time</standard_name>\n");
  fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
    "dimensional quantity\">seconds since %d-01-01T00:00:00Z</units>\n",
    firstYear);
  fprintf(fpOutList, "      <bounds type=\"string\" definition=\"variable "
    "containing data range\">time_bounds</bounds>\n");
  fprintf(fpOutList, "      <FillValue type=\"double\" definition=\"default "
    "value\">0</FillValue>\n");
  fprintf(fpOutList, "    </time>\n");
  fprintf(fpOutList, "    <time_bounds>\n");
  fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
    "descriptive name\">serial date</long_name>\n");
  fprintf(fpOutList, "      <references type=\"string\" definition=\"reference "
    "of the value\">start and end time of 3-day average</references>\n");
  fprintf(fpOutList, "      <standard_name type=\"string\" definition=\"name "
    "used to identify the physical quantity\">time</standard_name>\n");
  fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
    "dimensional quantity\">seconds since %d-01-01T00:00:00Z</units>\n",
    firstYear);
  fprintf(fpOutList, "      <FillValue type=\"double\" definition=\"default "
    "value\">0</FillValue>\n");
  fprintf(fpOutList, "    </time_bounds>\n");
  fprintf(fpOutList, "    <latitude>\n");
  fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
    "descriptive name\">latitude</long_name>\n");
  fprintf(fpOutList, "      <standard_name type=\"string\" definition=\"name "
    "used to identify the physical quantity\">latitude</standard_name>\n");
  fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
    "dimensional quantity\">degrees_north</units>\n");
  fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
    "value\">-999</FillValue>\n");
  fprintf(fpOutList, "      <valid_min type=\"float\" definition=\"minimum "
    "valid value\">-90.0</valid_min>\n");
  fprintf(fpOutList, "      <valid_max type=\"float\" definition=\"minimum "
    "valid value\">90.0</valid_max>\n");
  fprintf(fpOutList, "    </latitude>\n");
  fprintf(fpOutList, "    <longitude>\n");
  fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
    "descriptive name\">longitude</long_name>\n");
  fprintf(fpOutList, "      <standard_name type=\"string\" definition=\"name "
    "used to identify the physical quantity\">longitude</standard_name>\n");
  fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
    "dimensional quantity\">degrees_east</units>\n");
  fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
    "value\">-999</FillValue>\n");
  fprintf(fpOutList, "      <valid_min type=\"float\" definition=\"minimum "
    "valid value\">-180.0</valid_min>\n");
  fprintf(fpOutList, "      <valid_max type=\"float\" definition=\"minimum "
    "valid value\">180.0</valid_max>\n");
  fprintf(fpOutList, "    </longitude>\n");
  fprintf(fpOutList, "    <xgrid>\n");
  fprintf(fpOutList, "      <axis type=\"string\" definition=\"name of axis\">X"
    "</axis>\n");
  fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
    "descriptive name\">projection_grid_x_center</long_name>\n");
  fprintf(fpOutList, "      <standard_name type=\"string\" definition=\"name "
    "used to identify the physical quantity\">projection_x_coordinate"
    "</standard_name>\n");
  fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
    "dimensional quantity\">meters</units>\n");
  fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
    "value\">NaN</FillValue>\n");
  fprintf(fpOutList, "    </xgrid>\n");
  fprintf(fpOutList, "    <ygrid>\n");
  fprintf(fpOutList, "      <axis type=\"string\" definition=\"name of axis\">Y"
    "</axis>\n");
  fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
    "descriptive name\">projection_grid_y_center</long_name>\n");
  fprintf(fpOutList, "      <standard_name type=\"string\" definition=\"name "
    "used to identify the physical quantity\">projection_y_coordinate"
    "</standard_name>\n");
  fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
    "dimensional quantity\">meters</units>\n");
  fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
    "value\">NaN</FillValue>\n");
  fprintf(fpOutList, "    </ygrid>\n");
  fprintf(fpOutList, "    <Polar_Stereographic>\n");
  fprintf(fpOutList, "      <grid_mapping_name>polar_stereographic"
    "</grid_mapping_name>\n");
  fprintf(fpOutList, "      <straight_vertical_longitude_from_pole>%.1f"
    "</straight_vertical_longitude_from_pole>\n", pps.ps.slon);
  fprintf(fpOutList, "      <longitude_of_central_meridian>90.0"
    "</longitude_of_central_meridian>\n");
  fprintf(fpOutList, "      <standard_parallel>%.1f</standard_parallel>\n", 
    pps.ps.slat);
  fprintf(fpOutList, "      <false_easting>%.1f</false_easting>\n", 
    pps.ps.false_easting);
  fprintf(fpOutList, "      <false_northing>%.1f</false_northing>\n",
    pps.ps.false_northing);
  fprintf(fpOutList, "      <projection_x_coordinate>xgrid"
    "</projection_x_coordinate>\n");
  fprintf(fpOutList, "      <projection_y_coordinate>ygrid"
    "</projection_y_coordinate>\n");
  fprintf(fpOutList, "      <units>meters</units>\n");
  fprintf(fpOutList, "    </Polar_Stereographic>\n");
  fprintf(fpOutList, "    <mask>\n");
  fprintf(fpOutList, "      <coordinates type=\"string\" definition=\""
    "coordinate reference\">ygrid xgrid</coordinates>\n");
  fprintf(fpOutList, "      <grid_mapping type=\"string\" definition=\"\">"
    "Polar_Stereographic</grid_mapping>\n");
  fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
    "descriptive name\">projection_grid_y_center</long_name>\n");
  fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
    "dimensional quantity\">1</units>\n");
  fprintf(fpOutList, "      <units_description type=\"string\" definition=\""
    "descriptive information about dimensionless quantity\">unitless"
    "</units_description>\n");
  fprintf(fpOutList, "      <FillValue type=\"int\" definition=\"default "
    "value\">0</FillValue>\n");
  fprintf(fpOutList, "    </mask>\n");
  if (myrFlag) {
    fprintf(fpOutList, "    <multiyear_ice_fraction>\n");
    fprintf(fpOutList, "      <cell_methods type=\"string\" definition=\""
      "characteristic of a field that is represented by cell values\">area: "
      "multiyear ice fraction value</cell_methods>\n");
    fprintf(fpOutList, "      <coordinates type=\"string\" definition=\""
      "coordinate reference\">ygrid xgrid</coordinates>\n");
    fprintf(fpOutList, "      <grid_mapping type=\"string\" definition=\"\">"
      "Polar_Stereographic</grid_mapping>\n");
    fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
      "descriptive name\">RGPS MEaSUREs multiyear ice fraction</long_name>\n");
    fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
      "dimensional quantity\">1</units>\n");
    fprintf(fpOutList, "      <units_description type=\"string\" definition=\""
      "descriptive information about dimensionless quantity\">unitless"
      "</units_description>\n");
    fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
      "value\">NaN</FillValue>\n");
    fprintf(fpOutList, "    </multiyear_ice_fraction>\n");
  }
  if (divFlag) {
    fprintf(fpOutList, "    <divergence>\n");
    fprintf(fpOutList, "      <cell_methods type=\"string\" definition=\""
      "characteristic of a field that is represented by cell values\">area: "
      "divergence value</cell_methods>\n");
    fprintf(fpOutList, "      <coordinates type=\"string\" definition=\""
      "coordinate reference\">ygrid xgrid</coordinates>\n");
    fprintf(fpOutList, "      <grid_mapping type=\"string\" definition=\"\">"
      "Polar_Stereographic</grid_mapping>\n");
    fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
      "descriptive name\">RGPS MEaSUREs divergence</long_name>\n");
    fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
      "dimensional quantity\">1</units>\n");
    fprintf(fpOutList, "      <units_description type=\"string\" definition=\""
      "descriptive information about dimensionless quantity\">unitless"
      "</units_description>\n");
    fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
      "value\">NaN</FillValue>\n");
    fprintf(fpOutList, "    </divergence>\n");
  }
  if (vrtFlag) {
    fprintf(fpOutList, "    <vorticity>\n");
    fprintf(fpOutList, "      <cell_methods type=\"string\" definition=\""
      "characteristic of a field that is represented by cell values\">area: "
      "vorticity value</cell_methods>\n");
    fprintf(fpOutList, "      <coordinates type=\"string\" definition=\""
      "coordinate reference\">ygrid xgrid</coordinates>\n");
    fprintf(fpOutList, "      <grid_mapping type=\"string\" definition=\"\">"
      "Polar_Stereographic</grid_mapping>\n");
    fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
      "descriptive name\">RGPS MEaSUREs vorticity</long_name>\n");
    fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
      "dimensional quantity\">1</units>\n");
    fprintf(fpOutList, "      <units_description type=\"string\" definition=\""
      "descriptive information about dimensionless quantity\">unitless"
      "</units_description>\n");
    fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
      "value\">NaN</FillValue>\n");
    fprintf(fpOutList, "    </vorticity>\n");
  }
  if (shrFlag) {
    fprintf(fpOutList, "    <shear>\n");
    fprintf(fpOutList, "      <cell_methods type=\"string\" definition=\""
      "characteristic of a field that is represented by cell values\">area: "
      "shear value</cell_methods>\n");
    fprintf(fpOutList, "      <coordinates type=\"string\" definition=\""
      "coordinate reference\">ygrid xgrid</coordinates>\n");
    fprintf(fpOutList, "      <grid_mapping type=\"string\" definition=\"\">"
      "Polar_Stereographic</grid_mapping>\n");
    fprintf(fpOutList, "      <long_name type=\"string\" definition=\"long "
      "descriptive name\">RGPS MEaSUREs shear</long_name>\n");
    fprintf(fpOutList, "      <units type=\"string\" definition=\"unit of "
      "dimensional quantity\">1</units>\n");
    fprintf(fpOutList, "      <units_description type=\"string\" definition=\""
      "descriptive information about dimensionless quantity\">unitless"
      "</units_description>\n");
    fprintf(fpOutList, "      <FillValue type=\"float\" definition=\"default "
      "value\">NaN</FillValue>\n");
    fprintf(fpOutList, "    </shear>\n");
  }
  fprintf(fpOutList, "  </metadata>\n");
  fprintf(fpOutList, "  <parameter>\n");
  if (myrFlag)
    fprintf(fpOutList, "    <multiyear_ice_fraction type=\"float\"/>\n");
  if (divFlag)
    fprintf(fpOutList, "    <divergence type=\"float\"/>\n");
  if (vrtFlag)
    fprintf(fpOutList, "    <vorticity type=\"float\"/>\n");
  if (shrFlag)
    fprintf(fpOutList, "    <shear type=\"float\"/>\n");
  fprintf(fpOutList, "  </parameter>\n");
  
  char startStr[15], endStr[15];
  jdStart.year = firstYear;
  jdStart.jd = firstDay;
  jdEnd.year = endYear;
  jdEnd.jd = endDay;
  jd2date(&jdStart, startStr);
  jd2date(&jdEnd, endStr);
  if (firstYear != endYear || firstDay != endDay)
    sprintf(citation, "%s to %s", startStr, endStr);
  else
    sprintf(citation, "%s", startStr);
  fprintf(fpOutList, "  <root>\n");
  fprintf(fpOutList, "    <Conventions>CF-1.6</Conventions>\n");
  fprintf(fpOutList, "    <institution>Alaska Satellite Facility</institution>\n");
  fprintf(fpOutList, "    <title>Kwok, Ron. 2008. MEaSUREs Small-Scale Kinematics"
    " of Arctic Ocean Sea Ice, Version 01, %s. Jet Propulsion Laboratory "
    "Pasadena, CA USA and Alaska Satellite Facility Fairbanks, AK USA. "
    "Digital media.</title>\n", citation);
  fprintf(fpOutList, "    <source>Products derived from RADARSAT-1 SWB imagery at "
    "100 m resolution</source>\n");
  fprintf(fpOutList, "    <comment>Imagery the products are derived from: Copyright "
    "Canadian Space Agency (1996 to 2008)</comment>\n");
  fprintf(fpOutList, "    <reference>Documentation available at: www.asf.alaska.edu"
    "</reference>\n");
  fprintf(fpOutList, "    <history>%s: netCDF file created.</history>\n", isoStr);
  fprintf(fpOutList, "  </root>\n");
  fprintf(fpOutList, "</netcdf>\n");
  FCLOSE(fpOutList);

  // Generate supplemental files: water mask, lat/lon, x/y grids
  asfPrintStatus("Generating supplemental files ...\n");
  float *floatBuf = (float *) MALLOC(sizeof(float)*sample_count);
  float *maskBuf = (float *) MALLOC(sizeof(float)*sample_count);
  float *latBuf = (float *) MALLOC(sizeof(float)*sample_count);
  float *lonBuf = (float *) MALLOC(sizeof(float)*sample_count);
  float *xBuf = (float *) MALLOC(sizeof(float)*sample_count);
  float *yBuf = (float *) MALLOC(sizeof(float)*sample_count);
  meta = meta_read(metaFile);
  
  fpIn = FOPEN(inFile, "r");
  fgets(header, 100, fpIn);
  sscanf(header, "%f %f %f %f %f %f", &x_pix, &y_pix, &x_map_ll, &y_map_ll, 
    &x_map_ur, &y_map_ur);
  fgets(header, 100, fpIn);
  sscanf(header, "%d %d", &sample_count, &line_count);
  
  FILE *fpMask = FOPEN(maskFile, "wb");
  FILE *fpLat = FOPEN(latFile, "wb");
  FILE *fpLon = FOPEN(lonFile, "wb");
  FILE *fpXgrid = FOPEN(xFile, "wb");
  FILE *fpYgrid = FOPEN(yFile, "wb");
  for (ii=0; ii<line_count; ii++) {
    for (kk=0; kk<sample_count; kk++) {
      ASF_FREAD(&floatBuf[kk], sizeof(float), 1, fpIn);
      ieee_big32(floatBuf[kk]);
    }
    for (kk=0; kk<sample_count; kk++) {
      meta_get_latLon(meta, line_count-ii-1, kk, 0.0, &lat, &lon);
      latlon_to_proj(meta->projection, 'R', lat*D2R, lon*D2R, 0.0, &x, &y, &z);
      latBuf[kk] = lat;
      lonBuf[kk] = lon;
      xBuf[kk] = x;
      yBuf[kk] = y;
      if (floatBuf[kk] < 10000000000.0) {
        maskBuf[kk] = 1.0;
      }
      else if (floatBuf[kk] > 10000000000.0) {
        maskBuf[kk] = 1.0;
      }
      else {
        maskBuf[kk] = 0.0;
      }
    }
    put_float_line(fpMask, meta, line_count-ii-1, maskBuf);
    put_float_line(fpLat, meta, line_count-ii-1, latBuf);
    put_float_line(fpLon, meta, line_count-ii-1, lonBuf);
    put_float_line(fpXgrid, meta, line_count-ii-1, xBuf);
    put_float_line(fpYgrid, meta, line_count-ii-1, yBuf);
  }
  FCLOSE(fpIn);
  FCLOSE(fpMask);
  FCLOSE(fpLat);
  FCLOSE(fpLon);
  FREE(floatBuf);
  FREE(maskBuf);
  FREE(latBuf);
  FREE(lonBuf);
  FREE(xBuf);
  FREE(yBuf);
  meta_write(meta, latFile);
  meta_write(meta, lonFile);
  meta_write(meta, xFile);
  meta_write(meta, yFile);

  // Write ISO meatadata for netCDF
  asfPrintStatus("Generating metadata for netCDF file ...\n");

  char *ncXmlBase = get_basename(outFile);
  char *ncXmlFile = appendExt(outFile, ".xml");
  fpXml = FOPEN(ncXmlFile, "w");
  fprintf(fpXml, "<rgps>\n");
  fprintf(fpXml, "  <granule>%s</granule>\n", ncXmlBase);
  fprintf(fpXml, "  <metadata_creation>%s</metadata_creation>\n", isoStr);
  fprintf(fpXml, "  <metadata>\n");
  fprintf(fpXml, "    <product>\n");
  fprintf(fpXml, "      <file type=\"string\" definition=\"name of product "
    "file\">%s.nc</file>\n", ncXmlBase);
  if (divFlag && vrtFlag && shrFlag)
    fprintf(fpXml, "      <type type=\"string\" definition=\"product type\">"
    "divergence, vorticity, shear</type>\n");
  else if (myrFlag)
    fprintf(fpXml, "      <type type=\"string\" definition=\"product type\">"
    "multiyear ice fraction</type>\n");
  fprintf(fpXml, "      <format type=\"string\" definition=\"name of the data "
    "format\">netCDF</format>\n");

  fpInList = FOPEN(listInFile, "r");
  while (fgets(inFile, 512, fpInList)) {
    chomp(inFile);
    split_dir_and_file(inFile, dirName, baseName);
    fprintf(fpXml, "      <source type=\"string\" definition=\"name of the data"
    " source\">%s</source>\n", baseName);
  }
  FCLOSE(fpInList);

  fprintf(fpXml, "      <cell_size_x type=\"double\" definition=\"cell size "
    "in x direction\" units=\"m\">%.2f</cell_size_x>\n", x_pix*1000.0);
  fprintf(fpXml, "      <cell_size_y type=\"double\" definition=\"cell size "
    "in y direction\" units=\"m\">%.2f</cell_size_y>\n", y_pix*1000.0);
  fprintf(fpXml, "      <map_x_lower_left type=\"double\" definition=\"x "
    "coordinate of lower left corner\" units=\"m\">%.6f</map_x_lower_left>\n",
    x_map_ll*1000.0);
  fprintf(fpXml, "      <map_y_lower_left type=\"double\" definition=\"y "
    "coordinate of lower left corner\" units=\"m\">%.6f</map_y_lower_left>\n",
    y_map_ll*1000.0);
  fprintf(fpXml, "      <map_x_upper_right type=\"double\" definition=\"x "
    "coordinate of upper right corner\" units=\"m\">%.6f</map_x_upper_right>"
    "\n", x_map_ur*1000.0);
  fprintf(fpXml, "      <map_y_upper_right type=\"double\" definition=\"y "
    "coordinate of upper right corner\" units=\"m\">%.6f</map_y_upper_right>"
    "\n", y_map_ur*1000.0);
  fprintf(fpXml, "      <cell_dimension_x type=\"int\" definition=\"cell "
    "dimension in x direction\">%d</cell_dimension_x>\n", 
    sample_count);
  fprintf(fpXml, "      <cell_dimension_y type=\"int\" definition=\"cell "
    "dimension in y direction\">%d</cell_dimension_y>\n",
      line_count);
  fprintf(fpXml, "      <projection_string type=\"string\" definition=\"map "
    "projection information as well known text\">%s</projection_string>\n", 
  meta2esri_proj(meta, NULL));
  fprintf(fpXml, "    </product>\n");
  fprintf(fpXml, "  </metadata>\n");
  fprintf(fpXml, "  <extent>\n");
  fprintf(fpXml, "    <product>\n");
  fprintf(fpXml, "      <westBoundLongitude>%.5f</westBoundLongitude>\n",
    minLon);
  fprintf(fpXml, "      <eastBoundLongitude>%.5f</eastBoundLongitude>\n",
    maxLon);
  fprintf(fpXml, "      <northBoundLatitude>%.5f</northBoundLatitude>\n",
    maxLat);
  fprintf(fpXml, "      <southBoundLatitude>%.5f</southBoundLatitude>\n",
    minLat);
  fprintf(fpXml, "      <start_datetime>%s</start_datetime>\n", first);
  fprintf(fpXml, "      <end_datetime>%s</end_datetime>\n", end);
  fprintf(fpXml, "    </product>\n");
  fprintf(fpXml, "  </extent>\n");
  fprintf(fpXml, "</rgps>\n");
  FCLOSE(fpXml);
  FREE(ncXmlBase);
  FREE(ncXmlFile);
  meta_free(meta);

  // Export to netCDF
  asfPrintStatus("Exporting to netCDF file ...\n");
  export_netcdf_xml(listOutFile, outFile);

  // Clean up
  remove_dir(tmpDir);
  FREE(tmpDir);
  FREE(outFile);
  FREE(listInFile);
  FREE(isoStr);

  return 0;
}
Пример #8
0
static void get_uavsar_lines(ReadUavsarClientInfo *info, meta_parameters *meta,
                             int row, int n, float *buf)
{
    // wrapper for get_float_line() that multilooks if needed
    int j,ns=meta->general->sample_count;
    if (info->ml) {
        assert(meta->sar);
        int i,k;
        int nlooks = meta->sar->azimuth_look_count;
        row *= nlooks;

        // we fudged the line count in the metadata for the
        // viewer (which is displaying a multilooked image), we must
        // put the correct value back for the reader
        int lc = meta->general->line_count;
        meta->general->line_count = g_saved_line_count;

        float *tmp = MALLOC(sizeof(float)*ns);
        for (i=0; i<n; ++i) {
            float *this_row = buf + i*ns;
            get_float_line(info->fp, meta, row+i*nlooks, this_row);
            for (j=0; j<ns; ++j)
                ieee_big32(this_row[j]);
            int n_read = nlooks;
            for (k=1; k<nlooks; ++k) {
                if (row+n*nlooks+k >= meta->general->line_count) {
                    --n_read;
                } else {
                    get_float_line(info->fp, meta, row+i*nlooks+k, tmp);
                    for (j=0; j<ns; ++j)
                        ieee_big32(tmp[j]);
                    for (j=0; j<ns; ++j)
                        this_row[j] += tmp[j];
                }
            }
            for (j=0; j<meta->general->sample_count; ++j)
                this_row[j] /= n_read;
        }
        free(tmp);

        // restore the fudged value
        meta->general->line_count = lc;
    }
    else {
        // no multilooking case
        if (info->is_complex) {
            complexFloat *cf_buf = MALLOC(sizeof(complexFloat)*ns*n);
            get_complexFloat_lines(info->fp, meta, row, n, cf_buf);
            for (j=0; j<n*ns; ++j) {
                ieee_big32(cf_buf[j].real);
                ieee_big32(cf_buf[j].imag);
                buf[j] = hypot(cf_buf[j].real, cf_buf[j].imag);
                //buf[j] = atan2_check(cf_buf[j].imag, cf_buf[j].real);
            }
            FREE(cf_buf);
        }
        else {
            get_float_lines(info->fp, meta, row, n, buf);
            for (j=0; j<n*ns; ++j)
                ieee_big32(buf[j]);
        }
    }
}