コード例 #1
0
ファイル: xml_util.c プロジェクト: DavinSimmons/ASF_MapReady
long xml_get_long_attribute(xmlDoc *doc, char *format, ...)
{
  va_list ap;
  char str[4096];

  va_start(ap, format);
  vsnprintf(str, 4095, format, ap);
  va_end(ap);

  const char *val = xml_get_string_attribute(doc, str);
  if (val && strcmp(val, MAGIC_UNSET_STRING) != 0)
    return atol(val);
  else
    return MAGIC_UNSET_INT;
}
コード例 #2
0
ファイル: xml_util.c プロジェクト: DavinSimmons/ASF_MapReady
static void test_string_attr(xmlDoc *doc, char *key, const char *expected)
{
  const char *val = xml_get_string_attribute(doc, key);
  int passed;

  if (!expected) {
    passed = val==NULL;
  }
  else {
    passed = strcmp(val, expected)==0;
  }

  if (passed) {
    ++n_ok;
  }
  else {
    printf("WRONG!  for key: %s\n"
           "  Expected: %s\n"
           "       Got: %s\n",
           key, expected, val);
    ++n_bad;
  }
}
コード例 #3
0
ファイル: meta2radarsat.c プロジェクト: rudigens/ASF_MapReady
radarsat2_meta *read_radarsat2_meta_ext(const char *dataFile, int cal)
{
    int ii, numStateVectors, numDopplerEstimates;
    ymd_date imgStartDate, date;
    hms_time imgStartTime, time;
    julian_date julianDate;
    char timeStr[30], str[150];
    radarsat2_doppler_params *r2_doppler;

    radarsat2_meta *radarsat2 = radarsat2_meta_init();

    if (!fileExists(dataFile))
        asfPrintError("Metadata file (%s) does not exist!\n", dataFile);
    char *path = (char *) MALLOC(sizeof(char)*512);
    char *file = (char *) MALLOC(sizeof(char)*128);
    split_dir_and_file(dataFile, path, file);

    xmlDoc *doc = xmlReadFile(dataFile, NULL, 0);
    if (!doc)
        asfPrintError("Could not parse file %s\n", dataFile);

    strcpy(radarsat2->satellite, xml_get_string_value(doc,
            "product.sourceAttributes.satellite"));
    strcpy(radarsat2->sensor, xml_get_string_value(doc,
            "product.sourceAttributes.sensor"));
    strcpy(radarsat2->beamModeMnemonic, xml_get_string_value(doc,
            "product.sourceAttributes.beamModeMnemonic"));
    strcpy(radarsat2->acquisitionType, xml_get_string_value(doc,
            "product.sourceAttributes.radarParameters.acquisitionType"));
    strcpy(radarsat2->productType, xml_get_string_value(doc,
            "product.imageGenerationParameters.generalProcessingInformation."
            "productType"));
    strcpy(radarsat2->dataType, xml_get_string_value(doc,
            "product.imageAttributes.rasterAttributes.dataType"));
    strcpy(radarsat2->processingFacility, xml_get_string_value(doc,
            "product.imageGenerationParameters.generalProcessingInformation."
            "processingFacility"));
    strcpy(radarsat2->zeroDopplerAzimuthTime, xml_get_string_value(doc,
            "product.imageGenerationParameters.slantRangeToGroundRange."
            "zeroDopplerAzimuthTime"));
    strcpy(radarsat2->softwareVersion, xml_get_string_value(doc,
            "product.imageGenerationParameters.generalProcessingInformation."
            "softwareVersion"));
    radarsat2->bitsPerSample = xml_get_int_value(doc,
                               "product.productInfo.imageDataInfo.imageDataDepth");
    //radarsat2->absOrbit =
    //xml_get_int_value(doc, "product.productInfo.missionInfo.absOrbit");
    strcpy(radarsat2->passDirection, xml_get_string_value(doc,
            "product.sourceAttributes.orbitAndAttitude.orbitInformation."
            "passDirection"));
    // Number of bands needs to be derived from polarizations string
    int band_count = 0;
    char *attribute = (char *) MALLOC(sizeof(char)*128);
    char *fileName = (char *) MALLOC(sizeof(char)*512);
    strcpy(radarsat2->polarizations, xml_get_string_value(doc,
            "product.sourceAttributes.radarParameters.polarizations"));
    for (ii=0; ii<strlen(radarsat2->polarizations)-1; ii++)
        if (radarsat2->polarizations[ii] == ' ')
            radarsat2->polarizations[ii] = ',';
    if (strstr(radarsat2->polarizations, "HH"))
        band_count++;
    if (strstr(radarsat2->polarizations, "VV"))
        band_count++;
    if (strstr(radarsat2->polarizations, "HV"))
        band_count++;
    if (strstr(radarsat2->polarizations, "VH"))
        band_count++;
    radarsat2->band_count = band_count;
    strcpy(radarsat2->filename, "");
    strcpy(radarsat2->bands, "");
    // Park the filenames in the basename field of the metadata and replace
    // it with the directory name once we are done with importing the data
    for (ii=0; ii<band_count; ii++) {
        sprintf(str,
                "product.imageAttributes.fullResolutionImageData[%d].pole", ii);
        strcpy(attribute, xml_get_string_attribute(doc, str));
        sprintf(str, "product.imageAttributes.fullResolutionImageData[%d]", ii);
        strcpy(fileName, xml_get_string_value(doc, str));
        if (ii == 0) {
            sprintf(radarsat2->filename, "%s", fileName);
            sprintf(radarsat2->bands, "AMP-%s,PHASE-%s",
                    uc(attribute), uc(attribute));
        }
        else {
            strcat(radarsat2->filename, ",");
            strcat(radarsat2->filename, fileName);
            strcat(radarsat2->bands, ",");
            sprintf(str, "AMP-%s,PHASE-%s", uc(attribute), uc(attribute));
            strcat(radarsat2->bands, str);
        }
    }
    FREE(fileName);
    FREE(attribute);
    radarsat2->numberOfLines = xml_get_int_value(doc,
                               "product.imageAttributes.rasterAttributes.numberOfLines");
    radarsat2->numberOfSamplesPerLine = xml_get_int_value(doc,
                                        "product.imageAttributes.rasterAttributes.numberOfSamplesPerLine");
    radarsat2->sampledPixelSpacing = xml_get_double_value(doc,
                                     "product.imageAttributes.rasterAttributes.sampledPixelSpacing");
    radarsat2->sampledLineSpacing = xml_get_double_value(doc,
                                    "product.imageAttributes.rasterAttributes.sampledLineSpacing");
    radarsat2->semiMajorAxis = xml_get_double_value(doc,
                               "product.imageAttributes.geographicInformation."
                               "referenceEllipsoidParameters.semiMajorAxis");
    radarsat2->semiMinorAxis = xml_get_double_value(doc,
                               "product.imageAttributes.geographicInformation."
                               "referenceEllipsoidParameters.semiMinorAxis");
    strcpy(radarsat2->lineTimeOrdering, xml_get_string_value(doc,
            "product.imageAttributes.rasterAttributes.lineTimeOrdering"));
    strcpy(radarsat2->pixelTimeOrdering, xml_get_string_value(doc,
            "product.imageAttributes.rasterAttributes.pixelTimeOrdering"));
    strcpy(radarsat2->antennaPointing, xml_get_string_value(doc,
            "product.sourceAttributes.radarParameters.antennaPointing"));
    radarsat2->numberOfAzimuthLooks = xml_get_int_value(doc,
                                      "product.imageGenerationParameters.sarProcessingInformation."
                                      "numberOfAzimuthLooks");
    radarsat2->numberOfRangeLooks = xml_get_int_value(doc,
                                    "product.imageGenerationParameters.sarProcessingInformation."
                                    "numberOfRangeLooks");
    radarsat2->slantRangeNearEdge = xml_get_double_value(doc,
                                    "product.imageGenerationParameters.sarProcessingInformation."
                                    "slantRangeNearEdge");
    radarsat2->radarCenterFrequency = xml_get_double_value(doc,
                                      "product.sourceAttributes.radarParameters.radarCenterFrequency");
    radarsat2->pulseRepetitionFrequency = xml_get_double_value(doc,
                                          "product.sourceAttributes.radarParameters.pulseRepetitionFrequency");
    radarsat2->satelliteHeight = xml_get_double_value(doc,
                                 "product.imageGenerationParameters.sarProcessingInformation."
                                 "satelliteHeight");
    radarsat2->totalProcessedAzimuthBandwidth = xml_get_double_value(doc,
            "product.imageGenerationParameters.sarProcessingInformation."
            "totalProcessedAzimuthBandwidth");
    // chirp rate ???
    radarsat2->pulseLength = xml_get_double_value(doc,
                             "product.sourceAttributes.radarParameters.pulseLength");
    radarsat2->adcSamplingRate = xml_get_double_value(doc,
                                 "product.sourceAttributes.radarParameters.adcSamplingRate");
    // pitch, roll, yaw ???

    // read Doppler values
    radarsat2->doppler = meta_doppler_init();
    radarsat2->doppler->type = radarsat2_doppler;
    char *dopplerCentroidStr;
    dopplerCentroidStr = (char *) MALLOC(sizeof(char)*512);
    strcpy(dopplerCentroidStr, xml_get_string_value(doc,
            "product.imageGenerationParameters.dopplerCentroid."
            "dopplerCentroidCoefficients"));
    numDopplerEstimates = getNumParamsInString(dopplerCentroidStr);
    r2_doppler = radarsat2_doppler_init(numDopplerEstimates);
    radarsat2->doppler->r2 = r2_doppler;
    r2_doppler->ref_time_centroid = xml_get_double_value(doc,
                                    "product.imageGenerationParameters.dopplerCentroid."
                                    "dopplerCentroidReferenceTime");
    r2_doppler->ref_time_rate = xml_get_double_value(doc,
                                "product.imageGenerationParameters.dopplerRateValues."
                                "dopplerRateReferenceTime");
    char *dopplerRateStr;
    dopplerRateStr = (char *) MALLOC(sizeof(char)*512);
    strcpy(dopplerRateStr, xml_get_string_value(doc,
            "product.imageGenerationParameters.dopplerRateValues."
            "dopplerRateValuesCoefficients"));
    r2_doppler->time_first_sample = xml_get_double_value(doc,
                                    "product.imageGenerationParameters.slantRangeToGroundRange."
                                    "slantRangeTimeToFirstRangeSample");
    char *p, *q;
    p = dopplerCentroidStr;
    for (ii=0; ii<numDopplerEstimates; ii++) {
        if (ii == 0)
            q = p;
        else {
            if (strchr(p, ' ')) {
                q = strchr(p, ' ');
                q++;
            }
        }
        sscanf(q, "%lf", &r2_doppler->centroid[ii]);
        p = q;
    }
    FREE(dopplerCentroidStr);
    p = dopplerRateStr;
    for (ii=0; ii<numDopplerEstimates; ii++) {
        if (ii == 0)
            q = p;
        else {
            if (strchr(p, ' ')) {
                q = strchr(p, ' ');
                q++;
            }
        }
        sscanf(q, "%lf", &r2_doppler->rate[ii]);
        p = q;
    }
    FREE(dopplerRateStr);

    // read state vectors
    strcpy(radarsat2->zeroDopplerTimeFirstLine, xml_get_string_value(doc,
            "product.imageGenerationParameters.sarProcessingInformation."
            "zeroDopplerTimeFirstLine"));
    date_terrasar2date(radarsat2->zeroDopplerTimeFirstLine,
                       &imgStartDate, &imgStartTime);
    strcpy(radarsat2->zeroDopplerTimeLastLine, xml_get_string_value(doc,
            "product.imageGenerationParameters.sarProcessingInformation."
            "zeroDopplerTimeLastLine"));
    // Accommodate data stored in reverse time
    if (strcmp_case(radarsat2->lineTimeOrdering, "DECREASING") == 0)
        date_terrasar2date(radarsat2->zeroDopplerTimeLastLine,
                           &imgStartDate, &imgStartTime);

    // FIXME: determine numStateVector from data - count the entries
    //numStateVectors = xml_get_int_value(doc,
    //   "product.platform.orbit.orbitHeader.numStateVectors");
    numStateVectors = 5;
    radarsat2->state_vectors = meta_state_vectors_init(numStateVectors);
    radarsat2->state_vectors->year = imgStartDate.year;
    date_ymd2jd(&imgStartDate, &julianDate);
    radarsat2->state_vectors->julDay = julianDate.jd;
    radarsat2->state_vectors->second = date_hms2sec(&imgStartTime);
    for (ii=0; ii<numStateVectors; ii++) {
        sprintf(str, "product.sourceAttributes.orbitAndAttitude.orbitInformation."
                "stateVector[%d].timeStamp", ii);
        strcpy(timeStr, xml_get_string_value(doc, str));
        date_terrasar2date(timeStr, &date, &time);
        radarsat2->state_vectors->vecs[ii].time =
            time_difference(&date, &time, &imgStartDate, &imgStartTime);
        sprintf(str, "product.sourceAttributes.orbitAndAttitude.orbitInformation."
                "stateVector[%d].xPosition", ii);
        radarsat2->state_vectors->vecs[ii].vec.pos.x =
            xml_get_double_value(doc, str);
        sprintf(str, "product.sourceAttributes.orbitAndAttitude.orbitInformation."
                "stateVector[%d].yPosition", ii);
        radarsat2->state_vectors->vecs[ii].vec.pos.y =
            xml_get_double_value(doc, str);
        sprintf(str, "product.sourceAttributes.orbitAndAttitude.orbitInformation."
                "stateVector[%d].zPosition", ii);
        radarsat2->state_vectors->vecs[ii].vec.pos.z =
            xml_get_double_value(doc, str);
        sprintf(str, "product.sourceAttributes.orbitAndAttitude.orbitInformation."
                "stateVector[%d].xVelocity", ii);
        radarsat2->state_vectors->vecs[ii].vec.vel.x =
            xml_get_double_value(doc, str);
        sprintf(str, "product.sourceAttributes.orbitAndAttitude.orbitInformation."
                "stateVector[%d].yVelocity", ii);
        radarsat2->state_vectors->vecs[ii].vec.vel.y =
            xml_get_double_value(doc, str);
        sprintf(str, "product.sourceAttributes.orbitAndAttitude.orbitInformation."
                "stateVector[%d].zVelocity", ii);
        radarsat2->state_vectors->vecs[ii].vec.vel.z =
            xml_get_double_value(doc, str);
    }

    // read location information from the tie points
    ii = 0;
    int line=-99, pixel=-99, found=TRUE;
    while (found) {
        sprintf(str, "product.imageAttributes.geographicInformation."
                "geolocationGrid.imageTiePoint[%d].imageCoordinate.line", ii);
        line = (int) xml_get_double_value(doc, str);
        sprintf(str, "product.imageAttributes.geographicInformation."
                "geolocationGrid.imageTiePoint[%d].imageCoordinate.pixel", ii);
        pixel = (int) xml_get_double_value(doc, str);
        if (line < 0 || pixel < 0)
            found = FALSE;
        if (found) {
            if (line == 0 && pixel == 0) {
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "latitude", ii);
                radarsat2->sceneCornerCoord1Lat = xml_get_double_value(doc, str);
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "longitude", ii);
                radarsat2->sceneCornerCoord1Lon = xml_get_double_value(doc, str);
            }
            if (line == 0 && pixel == radarsat2->numberOfSamplesPerLine-1) {
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "latitude", ii);
                radarsat2->sceneCornerCoord2Lat = xml_get_double_value(doc, str);
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "longitude", ii);
                radarsat2->sceneCornerCoord2Lon = xml_get_double_value(doc, str);
            }
            if (line == radarsat2->numberOfLines-1 && pixel == 0) {
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "latitude", ii);
                radarsat2->sceneCornerCoord3Lat = xml_get_double_value(doc, str);
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "longitude", ii);
                radarsat2->sceneCornerCoord3Lon = xml_get_double_value(doc, str);
            }
            if (line == radarsat2->numberOfLines-1 &&
                    pixel == radarsat2->numberOfSamplesPerLine-1) {
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "latitude", ii);
                radarsat2->sceneCornerCoord4Lat = xml_get_double_value(doc, str);
                sprintf(str, "product.imageAttributes.geographicInformation."
                        "geolocationGrid.imageTiePoint[%d].geodeticCoordinate."
                        "longitude", ii);
                radarsat2->sceneCornerCoord4Lon = xml_get_double_value(doc, str);
            }
        }
        ii++;
    }

    // Read calibration information
    if (cal) {
        double sample_count = radarsat2->numberOfSamplesPerLine;
        attribute = (char *) MALLOC(sizeof(char)*128);
        fileName = (char *) MALLOC(sizeof(char)*512);
        for (ii=0; ii<3; ii++) {
            sprintf(str,
                    "product.imageAttributes.lookupTable[%d].incidenceAngleCorrection",
                    ii);
            strcpy(attribute, xml_get_string_attribute(doc, str));
            sprintf(str, "product.imageAttributes.lookupTable[%d]", ii);
            if (strlen(path) > 0)
                sprintf(fileName, "%s%s", path, xml_get_string_value(doc, str));
            else
                strcpy(fileName, xml_get_string_value(doc, str));
            if (strcmp_case(attribute, "Beta Nought") == 0)
                radarsat2->gains_beta = read_radarsat2_lut(fileName, sample_count);
            else if (strcmp_case(attribute, "Sigma Nought") == 0)
                radarsat2->gains_sigma = read_radarsat2_lut(fileName, sample_count);
            else if (strcmp_case(attribute, "Gamma") == 0)
                radarsat2->gains_gamma = read_radarsat2_lut(fileName, sample_count);
        }
        FREE(attribute);
        FREE(fileName);
    }

    xmlFreeDoc(doc);
    xmlCleanupParser();

    return radarsat2;
}