//--------------------------------------------------------------------------------------------------
/// Get list of time step texts (dates)
//--------------------------------------------------------------------------------------------------
void RifEclipseOutputFileTools::timeSteps(ecl_file_type* ecl_file, std::vector<QDateTime>* timeSteps, std::vector<double>* daysSinceSimulationStart)
{
    if (!ecl_file) return;

    CVF_ASSERT(timeSteps && daysSinceSimulationStart);

    timeSteps->clear();
    daysSinceSimulationStart->clear();

    // Get the number of occurrences of the INTEHEAD keyword
    int numINTEHEAD = ecl_file_get_num_named_kw(ecl_file, INTEHEAD_KW);
    
    // Get the number of occurrences of the DOUBHEAD keyword
    int numDOUBHEAD = ecl_file_get_num_named_kw(ecl_file, DOUBHEAD_KW);

    std::vector<double> dayValues(numINTEHEAD, 0.0); // Init fraction to zero

    // Read out fraction of day if number of keywords are identical
    if (numINTEHEAD == numDOUBHEAD)
    {
        for (int i = 0; i < numDOUBHEAD; i++)
        {
            ecl_kw_type* kwDOUBHEAD = ecl_file_iget_named_kw(ecl_file, DOUBHEAD_KW, i);
            if (kwDOUBHEAD)
            {
                dayValues[i] = ecl_kw_iget_double(kwDOUBHEAD, DOUBHEAD_DAYS_INDEX);
            }
        }
    }

    std::set<QDateTime> existingTimesteps;

    for (int i = 0; i < numINTEHEAD; i++)
    {
        ecl_kw_type* kwINTEHEAD = ecl_file_iget_named_kw(ecl_file, INTEHEAD_KW, i);
        CVF_ASSERT(kwINTEHEAD);
        int day = 0;
        int month = 0;
        int year = 0;
        getDayMonthYear(kwINTEHEAD, &day, &month, &year);

        QDateTime reportDateTime = RiaQDateTimeTools::createUtcDateTime(QDate(year, month, day));
        CVF_ASSERT(reportDateTime.isValid());

        double dayValue = dayValues[i];
        double dayFraction = dayValue - cvf::Math::floor(dayValue);
        double milliseconds = dayFraction * 24.0 * 60.0 * 60.0 * 1000.0;

        reportDateTime = reportDateTime.addMSecs(milliseconds);
        if (existingTimesteps.insert(reportDateTime).second)
        {
            timeSteps->push_back(reportDateTime);
            daysSinceSimulationStart->push_back(dayValue);
        }
    }
}
示例#2
0
ecl_rsthead_type * ecl_rsthead_ialloc( const ecl_file_type * rst_file , int occurence) {
  if (ecl_file_get_num_named_kw( rst_file , INTEHEAD_KW) > occurence) {
    const ecl_kw_type * intehead_kw = ecl_file_iget_named_kw( rst_file , INTEHEAD_KW , occurence);

    ecl_rsthead_type * rsthead = util_malloc( sizeof * rsthead );

    {
      const int * data = (const int *) ecl_kw_get_void_ptr( intehead_kw );

      rsthead->day       = data[INTEHEAD_DAY_INDEX];
      rsthead->month     = data[INTEHEAD_MONTH_INDEX];
      rsthead->year      = data[INTEHEAD_YEAR_INDEX];
      rsthead->version   = data[INTEHEAD_IPROG_INDEX];
      rsthead->phase_sum = data[INTEHEAD_PHASE_INDEX];

      rsthead->nx        = data[INTEHEAD_NX_INDEX];
      rsthead->ny        = data[INTEHEAD_NY_INDEX];
      rsthead->nz        = data[INTEHEAD_NZ_INDEX];
      rsthead->nactive   = data[INTEHEAD_NACTIVE_INDEX];

      rsthead->nwells    = data[INTEHEAD_NWELLS_INDEX];
      rsthead->niwelz    = data[INTEHEAD_NIWELZ_INDEX];
      rsthead->nzwelz    = data[INTEHEAD_NZWELZ_INDEX];

      rsthead->nsconz    = data[INTEHEAD_NSCONZ_INDEX];
      rsthead->niconz    = data[INTEHEAD_NICONZ_INDEX];
      rsthead->ncwmax    = data[INTEHEAD_NCWMAX_INDEX];

      rsthead->nisegz    = data[INTEHEAD_NISEGZ_INDEX];
      rsthead->nsegmx    = data[INTEHEAD_NSEGMX_INDEX];
      rsthead->nswlmx    = data[INTEHEAD_NSWLMX_INDEX];
      rsthead->nrsegz    = data[INTEHEAD_NRSEGZ_INDEX];

      // The only derived quantity
      rsthead->sim_time  = rsthead_date( rsthead->day , rsthead->month , rsthead->year );
    }

    if (ecl_file_get_num_named_kw(rst_file, DOUBHEAD_KW) > occurence) {
        const ecl_kw_type * doubhead_kw = ecl_file_iget_named_kw( rst_file , DOUBHEAD_KW , occurence);
        rsthead->sim_days = ecl_kw_iget_double( doubhead_kw , DOUBHEAD_DAYS_INDEX );
    }

    if (ecl_file_get_num_named_kw(rst_file, LOGIHEAD_KW) > occurence) {
      const ecl_kw_type * logihead_kw = ecl_file_iget_named_kw( rst_file , LOGIHEAD_KW , occurence);
      rsthead->dualp    = ecl_kw_iget_bool( logihead_kw , LOGIHEAD_DUALP_INDEX);
    } else
      rsthead->dualp    = false;


    return rsthead;
  } else
    return NULL;
}
示例#3
0
int main(int argc , char ** argv) {
  const char * grid_file = argv[1];
  ecl_grid_type * ecl_grid = ecl_grid_alloc( grid_file );
  ecl_file_type * ecl_file = ecl_file_open( grid_file , 0);
  
  ecl_grid_test_lgr_consistency( ecl_grid );

  if (ecl_file_get_num_named_kw( ecl_file , COORD_KW ))
    test_assert_int_equal( ecl_file_get_num_named_kw( ecl_file , COORD_KW ) - 1, ecl_grid_get_num_lgr( ecl_grid ));
  
  ecl_grid_free( ecl_grid );
  ecl_file_close( ecl_file);
  exit(0);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifEclipseOutputFileTools::findKeywordsAndDataItemCounts(ecl_file_type* ecl_file, QStringList* keywords, std::vector<size_t>* keywordDataItemCounts)
{
    if (!ecl_file || !keywords || !keywordDataItemCounts) return;

    int numKeywords = ecl_file_get_num_distinct_kw(ecl_file);

    caf::ProgressInfo info(numKeywords, "Reading Keywords on file");

    for (int i = 0; i < numKeywords; i++)
    {
        const char* kw = ecl_file_iget_distinct_kw(ecl_file , i);
        int numKeywordOccurrences = ecl_file_get_num_named_kw(ecl_file, kw);
        bool validData = true;
        size_t fileResultValueCount = 0;
        for (int j = 0; j < numKeywordOccurrences; j++)
        {
            fileResultValueCount += ecl_file_iget_named_size(ecl_file, kw, j);

            ecl_type_enum dataType = ecl_file_iget_named_type(ecl_file, kw, j);
            if (dataType != ECL_DOUBLE_TYPE && dataType != ECL_FLOAT_TYPE && dataType != ECL_INT_TYPE )
            {
                validData = false;
                break;
            }
        }

        if (validData)
        {
            keywords->append(QString(kw));
            keywordDataItemCounts->push_back(fileResultValueCount);
        }

        info.setProgress(i);
    }
}
示例#5
0
void IntegrationTest::resultsForKeyword(const std::string keyword) {
    std::cout << "Comparing " << keyword << "...";
    keywordValidForComparing(keyword);
    const unsigned int occurrences1 = ecl_file_get_num_named_kw(ecl_file1, keyword.c_str());
    const unsigned int occurrences2 = ecl_file_get_num_named_kw(ecl_file2, keyword.c_str());
    if (occurrences1 != occurrences2) {
        OPM_THROW(std::runtime_error, "For keyword " << keyword << ":"
                << "\nKeyword occurrences in first file: "  << occurrences1
                << "\nKeyword occurrences in second file: " << occurrences2
                << "\nThe number of occurrences differ.");
    }
    initialOccurrenceCompare(keyword);
    for (unsigned int occurrence = 1; occurrence < occurrences1; ++occurrence) {
        occurrenceCompare(keyword, occurrence);
    }
    std::cout << "done." << std::endl;
}
void ECLFilesComparator::resultsForKeyword(const std::string keyword) {
    std::cout << "\nKeyword " << keyword << ":\n\n";
    const unsigned int occurrences1 = ecl_file_get_num_named_kw(ecl_file1, keyword.c_str());
    const unsigned int occurrences2 = ecl_file_get_num_named_kw(ecl_file2, keyword.c_str());
    if (occurrences1 != occurrences2) {
        std::cout << "For keyword " << keyword << ":\n";
        OPM_THROW(std::runtime_error, "Number of keyword occurrences are not equal.");
    }
    if (!keywordValidForComparing(keyword)) {
        return;
    }
    for (unsigned int occurence = 0; occurence < occurrences1; ++occurence) {
        deviationsForOccurence(keyword, occurence);
    }
    printResultsForKeyword(keyword);
    absDeviation.clear();
    relDeviation.clear();
}
//--------------------------------------------------------------------------------------------------
/// Get all values of a given static result as doubles
//--------------------------------------------------------------------------------------------------
bool RifReaderEclipseOutput::staticResult(const QString& result, PorosityModelResultType matrixOrFracture, std::vector<double>* values)
{
    CVF_ASSERT(values);
    CVF_ASSERT(m_ecl_file);

    std::vector<double> fileValues;

    size_t numOccurrences = ecl_file_get_num_named_kw(m_ecl_file, result.toAscii().data());
    size_t i;
    for (i = 0; i < numOccurrences; i++)
    {
        std::vector<double> partValues;
        RifEclipseOutputFileTools::keywordData(m_ecl_file, result, i, &partValues);
        fileValues.insert(fileValues.end(), partValues.begin(), partValues.end());
    }

    extractResultValuesBasedOnPorosityModel(matrixOrFracture, values, fileValues);

    return true;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
ecl_kw_type* RifEclipseOutputFileTools::createActnumFromPorv(ecl_file_type* ecl_file)
{
    std::string porv_kw("PORV");

    if (ecl_file_has_kw(ecl_file, porv_kw.data()))
    {
        ecl_file_view_type* fileView = ecl_file_get_global_view(ecl_file);

        int keywordCount = ecl_file_get_num_named_kw(ecl_file, porv_kw.data());
        for (int index = 0; index < keywordCount; index++)
        {
            ecl_kw_type* fileKeyword = ecl_file_view_iget_named_kw(fileView, porv_kw.data(), index);
            if (fileKeyword)
            {
                float porvLimit = 0.0f;

                return ecl_kw_alloc_actnum(fileKeyword, porvLimit);
            }
        }
    }

    return nullptr;
}
//--------------------------------------------------------------------------------------------------
/// Get result values for given time step
//--------------------------------------------------------------------------------------------------
bool RifEclipseUnifiedRestartFileAccess::results(const QString& resultName, size_t timeStep, size_t gridCount, std::vector<double>* values)
{
    if (!openFile())
    {
        return false;
    }

    size_t numOccurrences   = ecl_file_get_num_named_kw(m_ecl_file, resultName.toAscii().data());

    size_t startIndex       = timeStep * gridCount;
    CVF_ASSERT(startIndex + gridCount <= numOccurrences);

    size_t occurrenceIdx;
    for (occurrenceIdx = startIndex; occurrenceIdx < startIndex + gridCount; occurrenceIdx++)
    {
        std::vector<double> partValues;
        RifEclipseOutputFileTools::keywordData(m_ecl_file, resultName, occurrenceIdx, &partValues);

        values->insert(values->end(), partValues.begin(), partValues.end());
    }

    return true;
}
示例#10
0
static void ecl_sum_data_add_ecl_file(ecl_sum_data_type * data         , 
                                      time_t load_end , 
                                      int   report_step                , 
                                      const ecl_file_type   * ecl_file , 
                                      const ecl_smspec_type * smspec) {
  
  
  int num_ministep  = ecl_file_get_num_named_kw( ecl_file , PARAMS_KW);
  if (num_ministep > 0) {
    int ikw;

    for (ikw = 0; ikw < num_ministep; ikw++) {
      ecl_kw_type * ministep_kw = ecl_file_iget_named_kw( ecl_file , MINISTEP_KW , ikw);
      ecl_kw_type * params_kw   = ecl_file_iget_named_kw( ecl_file , PARAMS_KW   , ikw);

      {
        ecl_sum_tstep_type * tstep;
        int ministep_nr = ecl_kw_iget_int( ministep_kw , 0 );
        tstep = ecl_sum_tstep_alloc_from_file( report_step , 
                                               ministep_nr ,
                                               params_kw , 
                                               ecl_file_get_src_file( ecl_file ),
                                               smspec );
        
        if (tstep != NULL) {
          if (load_end == 0 || (ecl_sum_tstep_get_sim_time( tstep ) < load_end))
            ecl_sum_data_append_tstep__( data , ministep_nr , tstep );
          else 
            /* This tstep is in a time-period overlapping with data we
               already have; discard this. */
            ecl_sum_tstep_free( tstep );
        }
      }
    }
  }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
std::vector<int> RifEclipseUnifiedRestartFileAccess::reportNumbers()
{
    std::vector<int> reportNr;

    // Taken from well_info_add_UNRST_wells

    int num_blocks = ecl_file_get_num_named_kw(m_ecl_file, SEQNUM_KW);
    int block_nr;
    for (block_nr = 0; block_nr < num_blocks; block_nr++) {
        ecl_file_push_block(m_ecl_file);      // <-------------------------------------------------------
        {                                                                                               //
            ecl_file_subselect_block(m_ecl_file, SEQNUM_KW, block_nr);                                  //  Ensure that the status
            {                                                                                             //  is not changed as a side
                const ecl_kw_type * seqnum_kw = ecl_file_iget_named_kw(m_ecl_file, SEQNUM_KW, 0);          //  effect.
                int report_nr = ecl_kw_iget_int(seqnum_kw, 0);                                           //

                reportNr.push_back(report_nr);
            }                                                                                             //
        }                                                                                               //
        ecl_file_pop_block(m_ecl_file);       // <-------------------------------------------------------
    }

    return reportNr;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RifEclipseOutputFileTools::createReportStepsMetaData(std::vector<ecl_file_type*> ecl_files, std::vector<RifRestartReportStep>* reportSteps)
{
    if (!reportSteps) return;

    for (auto ecl_file : ecl_files)
    {
        if (!ecl_file) continue;

        int reportStepCount = ecl_file_get_num_named_kw(ecl_file, INTEHEAD_KW);
        for (int reportStepIndex = 0; reportStepIndex < reportStepCount; reportStepIndex++)
        {
            ecl_file_view_type* rst_view = ecl_file_get_global_view(ecl_file);
            if (!rst_view) continue;

            ecl_rsthead_type* restart_header = ecl_rsthead_alloc(rst_view, reportStepIndex);
            if (restart_header)
            {
                ecl_file_push_block(ecl_file);

                {
                    ecl_file_select_block(ecl_file, INTEHEAD_KW, reportStepIndex);

                    RifRestartReportStep reportStep;

                    // Set Date
                    {
                        QDateTime reportDateTime(QDate(restart_header->year, restart_header->month, restart_header->day));
                        reportStep.dateTime = reportDateTime;
                    }

                    // Find number of keywords withing this report step
                    int numKeywords = ecl_file_get_num_distinct_kw(ecl_file);
                    for (int iKey = 0; iKey < numKeywords; iKey++)
                    {
                        const char* kw = ecl_file_iget_distinct_kw(ecl_file, iKey);

                        int namedKeywordCount = ecl_file_get_num_named_kw(ecl_file, kw);
                        for (int iOcc = 0; iOcc < namedKeywordCount; iOcc++)
                        {
                            ecl_data_type dataType = ecl_file_iget_named_data_type(ecl_file, kw, iOcc);
                            ecl_type_enum dataTypeEmum = ecl_type_get_type(dataType);
                            if (dataTypeEmum != ECL_DOUBLE_TYPE && dataTypeEmum != ECL_FLOAT_TYPE && dataTypeEmum != ECL_INT_TYPE)
                            {
                                continue;
                            }

                            int itemCount = ecl_file_iget_named_size(ecl_file, kw, iOcc);
                            reportStep.m_keywords.appendKeyword(kw, itemCount, iOcc);
                        }
                    }

                    reportSteps->push_back(reportStep);
                }

                ecl_file_pop_block(ecl_file);

                ecl_rsthead_free(restart_header);
            }
        }
    }
}
示例#13
0
void RegressionTest::resultsForKeyword(const std::string keyword) {
    keywordValidForComparing(keyword);
    const unsigned int occurrences1 = ecl_file_get_num_named_kw(ecl_file1, keyword.c_str());
    const unsigned int occurrences2 = ecl_file_get_num_named_kw(ecl_file2, keyword.c_str());
    if (!onlyLastOccurrence && occurrences1 != occurrences2) {
        OPM_THROW(std::runtime_error, "For keyword " << keyword << ":"
                << "\nKeyword occurrences in first file: "  << occurrences1
                << "\nKeyword occurrences in second file: " << occurrences2
                << "\nThe number of occurrences differ.");
    }
    // Assuming keyword type is constant for every occurrence:
    const ecl_type_enum kw_type = ecl_file_iget_named_type(ecl_file1, keyword.c_str(), 0);
    switch(kw_type) {
        case ECL_DOUBLE_TYPE:
        case ECL_FLOAT_TYPE:
            std::cout << "Comparing " << keyword << "...";
            if (onlyLastOccurrence) {
                doubleComparisonForOccurrence(keyword, occurrences1 - 1, occurrences2 - 1);
            }
            else {
                for (unsigned int occurrence = 0; occurrence < occurrences1; ++occurrence) {
                    doubleComparisonForOccurrence(keyword, occurrence, occurrence);
                }
            }
            std::cout << "done." << std::endl;
            printResultsForKeyword(keyword);
            absDeviation.clear();
            relDeviation.clear();
            return;
        case ECL_INT_TYPE:
            std::cout << "Comparing " << keyword << "...";
            if (onlyLastOccurrence) {
                intComparisonForOccurrence(keyword, occurrences1 - 1, occurrences2 - 1);
            }
            else {
                for (unsigned int occurrence = 0; occurrence < occurrences1; ++occurrence) {
                    intComparisonForOccurrence(keyword, occurrence, occurrence);
                }
            }
            break;
        case ECL_CHAR_TYPE:
            std::cout << "Comparing " << keyword << "...";
            if (onlyLastOccurrence) {
                charComparisonForOccurrence(keyword, occurrences1 - 1, occurrences2 - 1);
            }
            else {
                for (unsigned int occurrence = 0; occurrence < occurrences1; ++occurrence) {
                    charComparisonForOccurrence(keyword, occurrence, occurrence);
                }
            }
            break;
        case ECL_BOOL_TYPE:
            std::cout << "Comparing " << keyword << "...";
            if (onlyLastOccurrence) {
                boolComparisonForOccurrence(keyword, occurrences1 - 1, occurrences2 - 1);
            }
            else {
                for (unsigned int occurrence = 0; occurrence < occurrences1; ++occurrence) {
                    boolComparisonForOccurrence(keyword, occurrence, occurrence);
                }
            }
            break;
        case ECL_MESS_TYPE:
            std::cout << "\nKeyword " << keyword << " is of type "
                << ecl_util_get_type_name(kw_type)
                << ", which is not supported in regression test." << "\n\n";
            return;
        default:
            std::cout << "\nKeyword " << keyword << "has undefined type." << std::endl;
            return;
    }
    std::cout << "done." << std::endl;
}
//--------------------------------------------------------------------------------------------------
/// Get list of time step texts (dates)
//--------------------------------------------------------------------------------------------------
void RifEclipseOutputFileTools::timeSteps(ecl_file_type* ecl_file, std::vector<QDateTime>* timeSteps, bool* detectedFractionOfDay )
{
    CVF_ASSERT(timeSteps);
    CVF_ASSERT(ecl_file);

    // Get the number of occurrences of the INTEHEAD keyword
    int numINTEHEAD = ecl_file_get_num_named_kw(ecl_file, INTEHEAD_KW);

    // Get the number of occurrences of the DOUBHEAD keyword
    int numDOUBHEAD = ecl_file_get_num_named_kw(ecl_file, DOUBHEAD_KW);

    CVF_ASSERT(numINTEHEAD == numDOUBHEAD);

    bool hasFractionOfDay = false;
    bool foundAllDayValues = false;
    const double delta = 0.001;

    // Find all days, and stop when the double value is lower than the previous
    QList<double> days;
    for (int i = 0; i < numDOUBHEAD; i++)
    {
        if (foundAllDayValues) continue;;

        ecl_kw_type* kwDOUBHEAD = ecl_file_iget_named_kw(ecl_file, DOUBHEAD_KW, i);
        if (kwDOUBHEAD)
        {
            double dayValue = ecl_kw_iget_double(kwDOUBHEAD, DOUBHEAD_DAYS_INDEX);
            double floorDayValue = cvf::Math::floor(dayValue);

            if (dayValue - floorDayValue > delta)
            {
                hasFractionOfDay = true;
            }

            days.push_back(dayValue);
        }
    }

    std::vector<QDateTime> timeStepsFound;

    if (hasFractionOfDay)
    {
        ecl_kw_type* kwINTEHEAD = ecl_file_iget_named_kw(ecl_file, INTEHEAD_KW, 0);
        if (kwINTEHEAD)
        {
            int day = 0;
            int month = 0;
            int year = 0;
            getDayMonthYear(kwINTEHEAD, &day, &month, &year);

            QDateTime simulationStart(QDate(year, month, day));
            for (int i = 0; i < days.size(); i++)
            {
                QDateTime reportDateTime(simulationStart);
                CVF_ASSERT(reportDateTime.isValid());

                double dayValue = days[i];
                double floorDayValue = cvf::Math::floor(dayValue);
                reportDateTime = reportDateTime.addDays(static_cast<int>(floorDayValue));

                double dayFraction = dayValue - floorDayValue;
                int seconds = static_cast<int>(dayFraction * 24.0 * 60.0 * 60.0);
                QTime time(0, 0);
                time = time.addSecs(seconds);

                reportDateTime.setTime(time);

                if (std::find(timeStepsFound.begin(), timeStepsFound.end(), reportDateTime) ==  timeStepsFound.end())
                {
                    timeStepsFound.push_back(reportDateTime);
                }
            }
        }
    }
    else
    {
        for (int i = 0; i < numINTEHEAD; i++)
        {
            ecl_kw_type* kwINTEHEAD = ecl_file_iget_named_kw(ecl_file, INTEHEAD_KW, i);
            if (kwINTEHEAD)
            {
                int day = 0;
                int month = 0;
                int year = 0;
                getDayMonthYear(kwINTEHEAD, &day, &month, &year);

                QDateTime reportDateTime(QDate(year, month, day));
                QTime time(0, 0);
                reportDateTime.setTime(time);

                CVF_ASSERT(reportDateTime.isValid());

                if (std::find(timeStepsFound.begin(), timeStepsFound.end(), reportDateTime) ==  timeStepsFound.end())
                {
                    timeStepsFound.push_back(reportDateTime);
                }
            }
        }
    }

    // Return time step info to caller
    *timeSteps = timeStepsFound;

    if (detectedFractionOfDay)
    {
        *detectedFractionOfDay = hasFractionOfDay;
    }
}
示例#15
0
文件: ecl_unpack.c 项目: akva2/ert
void unpack_file(const char * filename) {
  ecl_file_enum target_type = ECL_OTHER_FILE;
  ecl_file_enum file_type;
  bool fmt_file;
  file_type = ecl_util_get_file_type(filename , &fmt_file , NULL);
  if (file_type == ECL_UNIFIED_SUMMARY_FILE)
    target_type = ECL_SUMMARY_FILE;
  else if (file_type == ECL_UNIFIED_RESTART_FILE)
    target_type = ECL_RESTART_FILE;
  else 
    util_exit("Can only unpack unified ECLIPSE summary and restart files\n");
  
  if (target_type == ECL_SUMMARY_FILE) {
    printf("** Warning: when unpacking unified summary files it as ambigous - starting with 0001  -> \n");
  }
  {
    ecl_file_type * src_file = ecl_file_open( filename , 0 );
    int    size;
    int    offset;
    int    report_step = 0;
    int    block_index = 0;
    char * path; 
    char * base;
    msg_type * msg;
    util_alloc_file_components( filename , &path , &base , NULL);
    {
      char * label  = util_alloc_sprintf("Unpacking %s => ", filename);
      msg = msg_alloc( label , false);
      free( label );
    }
    msg_show(msg);

    if (target_type == ECL_SUMMARY_FILE) 
      size = ecl_file_get_num_named_kw( src_file , "SEQHDR" );
    else
      size = ecl_file_get_num_named_kw( src_file , "SEQNUM" );
    
    
    while (true) {
      if (block_index == size)
        break;

      if (target_type == ECL_SUMMARY_FILE) {
        ecl_file_select_block( src_file , SEQHDR_KW , block_index );
        report_step += 1;
        offset = 0;
      } else {
        ecl_kw_type * seqnum_kw;
        ecl_file_select_block( src_file , SEQNUM_KW , block_index );
        seqnum_kw = ecl_file_iget_named_kw( src_file , SEQNUM_KW , 0);
        report_step = ecl_kw_iget_int( seqnum_kw , 0);
        offset = 1;
      }

      /**
         Will unpack to cwd, even though the source files might be
         somewhere else. To unpack to the same directory as the source
         files, just send in @path as first argument when creating the
         target_file.
      */
      
      {
        char * target_file = ecl_util_alloc_filename( NULL , base , target_type , fmt_file , report_step);
        fortio_type * fortio_target = fortio_open_writer( target_file , fmt_file , ECL_ENDIAN_FLIP );
        msg_update(msg , target_file);
        ecl_file_fwrite_fortio( src_file , fortio_target , offset);
        
        fortio_fclose(fortio_target);
        free(target_file);
      }
      block_index++;
    } 
    ecl_file_close( src_file );
    util_safe_free(path);
    free(base);
    msg_free(msg , true);
  }
}