⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gsegyfileaccessor.c

📁 segy 显示程序!希望能给正在做这部分朋友提供一部分资料
💻 C
📖 第 1 页 / 共 3 页
字号:
                    g_mutex_unlock (private->file_mutex);
#endif
                return FALSE;
            }
            if ((0 == private->sample_interval || private->sample_interval > G_MAXINT16) &&
                private->binary_header_size)
                g_segy_format_wizard_get_sample_interval_from_bin (private->format_wizard,
                                                                   private->binary_header,
                                                                   &private->sample_interval);
            if (0 == private->sample_interval) {
                if (file_error)
                    file_error->id = G_SEGY_FILE_ZERO_SAMPLE_INTERVAL;
                g_segy_file_accessor_scan_fraction_signal (self, -1, file_error);
#if defined LINUX || defined WIN32
                if (private->file_mutex)
                    g_mutex_unlock (private->file_mutex);
#endif
                return FALSE;
            }
            g_segy_format_wizard_decode_binary_header (private->format_wizard, private->binary_header, private->binary_header);
        }
        if (number_of_samples == 0 || number_of_samples > G_MAXINT16)
            number_of_samples = private->number_of_samples;

        g_segy_format_wizard_decode_sorting_fields (private->format_wizard, private->trace_buffer, private->sorting_data_buffer);
        g_array_append_vals (private->sorting_contents, private->sorting_data_buffer, 1);

#if defined LINUX || defined WIN32
        seek_pos = lseek (fd, number_of_samples * private->sample_size, SEEK_CUR);
        if (seek_pos == (guint64)LSEEK_ERR) {
            if (private->file_mutex)
                g_mutex_unlock (private->file_mutex);
            if (file_error) {
#else
        status = g_io_channel_seek_position (private->io_channel, number_of_samples * private->sample_size,
                                             G_SEEK_CUR, &file_error->gerror);
        if (status != G_IO_STATUS_NORMAL) {
            if (file_error && status != G_IO_STATUS_EOF) {
#endif
                file_error->id = G_SEGY_FILE_READ_ERROR;                  
                file_error->gerror = gerror;
                g_segy_file_accessor_scan_fraction_signal (self, -1, file_error);
            }
            break;
        }

        g_array_append_vals (private->trace_positions, &trace_position, 1);

        trace_position += private->trace_header_size + number_of_samples * private->sample_size;

        if (total_size != 0) {
            if ((trace_position - prev_trace_position) / (gfloat)total_size >= 0.01) {
                continue_scan = g_segy_file_accessor_scan_fraction_signal (self, trace_position / (gfloat)total_size, file_error);
                prev_trace_position = trace_position;
            }
        } else if (1 == (private->trace_positions->len % 1000))
            continue_scan = g_segy_file_accessor_scan_fraction_signal (self, private->trace_positions->len + 1.0, file_error);
        if (FALSE == continue_scan) {
#if defined LINUX || defined WIN32
            if (private->file_mutex)
                g_mutex_unlock (private->file_mutex);
#endif
            if (G_SEGY_FILE_NO_ERROR == file_error->id)
                file_error->id = G_SEGY_FILE_INTERRUPTED_BY_USER;
            break;
        }
    }

    if (G_SEGY_FILE_NO_ERROR == file_error->id) {
        g_segy_file_accessor_scan_fraction_signal (self, 1.0, file_error);
#ifdef DEBUG
        g_printf ("Number of traces read: %d\n", private->trace_positions->len);
        g_printf ("Number of samples in a trace: %"G_GINT16_FORMAT"\n", private->number_of_samples);
        g_printf ("Z value of the first sample: %"G_GINT16_FORMAT"\n", private->first_sample_value);
        g_printf ("Sample interval: %"G_GINT16_FORMAT"\n", private->sample_interval);
#endif
    }

    if (file_error)
        return (G_SEGY_FILE_NO_ERROR == file_error->id) ? TRUE : FALSE;
    else
        return continue_scan;
}

static void g_segy_file_accessor_setup (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    private->ebcdic_header_size = g_segy_format_wizard_get_ebcdic_header_size (private->format_wizard);
    private->ebcdic_header = NULL;
    if (private->ebcdic_header_size)
        private->ebcdic_header = (guint8*)g_malloc (private->ebcdic_header_size + 1);

    private->binary_header_size = g_segy_format_wizard_get_binary_header_size (private->format_wizard);
    if (private->binary_header_size)
        private->binary_header = (guint8*)g_malloc (private->binary_header_size);

    private->trace_header_size = g_segy_format_wizard_get_trace_header_size (private->format_wizard);
    private->sorting_element_size = g_segy_format_wizard_get_sorting_desc_size (private->format_wizard);
    private->sorting_contents = g_array_new (FALSE, FALSE, private->sorting_element_size);
    private->trace_positions = g_array_new (FALSE, FALSE, sizeof (guint64));
}

GSEGYFileAccessor* g_segy_file_accessor_new (GIOChannel *io_channel, GSEGYFormatWizard *format_wizard) {
    return G_SEGY_FILE_ACCESSOR (g_object_new (G_SEGY_TYPE_FILE_ACCESSOR, "io_channel", io_channel,
                                                                          "format_wizard", format_wizard, NULL));
}

gboolean g_segy_file_accessor_scan_file (GSEGYFileAccessor *self, GSEGYFileError *file_error) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    if (private->trace_positions->len != 0)
        return FALSE;

    return g_segy_file_accessor_initial_read (self, file_error);
}

guint32 g_segy_file_accessor_get_number_of_traces (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return private->trace_positions->len;
}

guint16 g_segy_file_accessor_get_sample_interval (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return private->sample_interval;
}

guint16 g_segy_file_accessor_get_number_of_samples (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return private->number_of_samples;
}

GCompareDataFunc g_segy_file_accessor_get_compare_func_for_sorting_id (GSEGYFileAccessor *self, gint16 id,
                                                                       gboolean ascending, guint16 *sorting_data_offset) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    if (id <= 0)
        return NULL;

    *sorting_data_offset = g_segy_format_wizard_get_sorting_field_offset (private->format_wizard, id);
    return g_segy_format_wizard_get_compare_func_for_sorting_field (private->format_wizard, id, ascending);
}

GArray *g_segy_file_accessor_get_sorting_contents (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return private->sorting_contents;
}

guint16 g_segy_file_accessor_get_sorting_desc_size (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return g_segy_format_wizard_get_sorting_desc_size (private->format_wizard);
}

GSEGYFormatWizard* g_segy_file_accessor_get_format_wizard (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return private->format_wizard;
}

guint8* g_segy_file_accessor_get_binary_header (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return private->binary_header;
}

guint8* g_segy_file_accessor_get_ebcdic_header (GSEGYFileAccessor *self) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    return private->ebcdic_header;
}

guint8* g_segy_file_accessor_get_trace_header (GSEGYFileAccessor *self, guint32 trace_index,
                                                guint8 *buffer, guint32 buffer_len) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    if (private->trace_positions->len <= trace_index)
        return NULL;

    guint8 *gbuffer = buffer;

    if (NULL == gbuffer || 0 == buffer_len) {
        gbuffer = (guint8*)g_malloc (private->trace_header_size);
        if (NULL == gbuffer)
            return NULL;
        buffer_len = private->trace_header_size;
    }

    GIOStatus status;
    GError *gerror = NULL;
    gsize bytes_read = 0, bytes_to_read;
    guint64 seek_pos = 0;
    gint fd;

    fd = g_io_channel_unix_get_fd (private->io_channel);

#if defined LINUX || defined WIN32
    if (private->file_mutex)
        g_mutex_lock (private->file_mutex);

    seek_pos = lseek (fd, g_array_index (private->trace_positions, guint64, trace_index), SEEK_SET);
    if (seek_pos == (guint64)LSEEK_ERR) {
        if (private->file_mutex)
            g_mutex_unlock (private->file_mutex);
#else
    status = g_io_channel_seek_position (private->io_channel,
                                         g_array_index (private->trace_positions, guint64, trace_index),
                                         G_SEEK_SET, &gerror);
    if (status != G_IO_STATUS_NORMAL) {
        if (gerror)
            g_error_free (gerror);
#endif
        return NULL;
    }

    bytes_to_read = buffer_len <= private->trace_header_size ? buffer_len : private->trace_header_size;

#if defined LINUX || defined WIN32
    bytes_read = read (fd, gbuffer, bytes_to_read);
    if (private->file_mutex)
        g_mutex_unlock (private->file_mutex);
    if (bytes_read != bytes_to_read) {
#else
    status = g_io_channel_read_chars (private->io_channel, gbuffer, bytes_to_read,
                                      &bytes_to_read, &gerror);
    if (status != G_IO_STATUS_NORMAL || bytes_read != private->trace_header_size) {
        if (gerror)
            g_error_free (gerror);
#endif
        return NULL;
    }

    g_segy_format_wizard_decode_trace_header (private->format_wizard, gbuffer, gbuffer);

    return gbuffer;
}

gfloat* g_segy_file_accessor_get_data (GSEGYFileAccessor *self, guint32 *traces_indices, guint32 traces_num,
                                       guint32 *slow_num, guint32 *fast_num, gboolean double_buffer,
                                       GSEGYFileError *file_error) {
    GSEGYFileAccessorPrivate *private = G_SEGY_FILE_ACCESSOR_GET_PRIVATE (self);

    if (file_error) {
        file_error->gerror = NULL;
        file_error->id = G_SEGY_FILE_NO_ERROR;
    }

    if (slow_num)
        *slow_num = 0;
    if (fast_num)
        *fast_num = 0;

    GIOStatus status;
    guint16 number_of_samples;
    gfloat *data = NULL;
    GError *gerror = NULL;
    gsize bytes_read = 0, bytes_to_read = 0, samples_to_read = 0;
    guint64 seek_pos = 0;
    gint fd;
    guint32 i, j, trace_num = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -