0

我很难写出这个问题。我有多个 CTD 数据文件(包含海洋温度值和深度的文件)。我将它们绘制在一个图形上,以查看温度如何随深度变化。我现在想做的是绘制平均温度(在所有文件中)与深度的平均分布(仅一行)。因此,就像来自多个数据文件的每个变量的逐行平均值一样。

我的数据是 cnv 格式,它只是一列温度值和另一列深度值。每个数据集没有相同数量的深度和温度值(即不同的行数)。

这就是我的代码看起来只是为每个文件分配的样子,我附上了它产生的数字:

from seabird.cnv import fCNV
import numpy as np
import matplotlib.pyplot as plt
from seabird.cnv import fCNV
import glob

filenames = sorted(glob.glob('dSBE19plus*.cnv')) #load multiple files
filenames = filenames[0:8]

fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
for f in filenames:
    print(f)


    data = fCNV(f)
    # Assign variable names
    depth = data['prdM']
    temp  = data['tv290C']
    salt  = data['PSAL']
    fluo  = data['flECO-AFL']
    turbidity = data['turbWETntu0']


    ax1.plot(temp,depth)

    # Draw x label
    ax1.set_xlabel('Temperature (C)')
    ax1.xaxis.set_label_position('top') # this moves the label to the top
    ax1.xaxis.set_ticks_position('top') # this moves the ticks to the top
    # Draw y label
    ax1.set_ylim([0, 100])
    ax1.set_ylabel('Depth (m)')
    ax1.set_ylim(ax1.get_ylim()[::-1]) 
    ax1.set_xlim([15, 26])

fig1.savefig('ctd_plot.png')

绘制的每个 CTD 数据集的图

我希望我的问题是有道理的。

非常感谢

4

1 回答 1

0

您可以组合多个 CTD 数据文件,根据深度(或在您的情况下为压力“prDM”)分箱,并对按箱分组的每个参数进行平均。

我不知道如何在 Python 中执行此操作,但这里有一个用于合并 CTD 数据的 R 函数:

library("tidyverse")

binCTD.mean <- function(.data, # data.frame to be binned
                        .binvar, # variable name to bin over (usually depth or pressure)
                        .breaks, # breaks as in cut() (either single value giving the number of equally distributed bins or a vector of cut points)
                        .binwidth = NA # alternatively to .breaks the binwidth can be set (overwrites .breaks)
) {
    # calculate .breaks from .binwidth if provided:
    if (!is.na(.binwidth)) { 
        .breaks <- seq(0, ## starting from the water surface makes sense?!
                       ceiling(max(.data[, .binvar])), # to highest depth (rounded up)
                       by = .binwidth) # in intervals of given binwidth
    }

    # new parameter "bins", cut according to given breaks (or binwidth)
    .data$bins <- cut(x = .data[, .binvar], breaks = .breaks)

    # return new data frame with averaged parameters, grouped by bins
    .data %>% # I LOVE the pipe operator %>% !! 
        group_by(bins) %>% # dplyr function to group data
        summarise_all(mean, na.rm = TRUE) # dplyr function to apply functions on all parameters in the data frame (actually a tibble, which is kind of the better data.frame)
}

您还可以在GitHub 上找到用于执行 CTD 分箱的 R 代码以及一个解释性示例。要阅读 SBE CTD .cnv 文件,您可以使用此 R 函数(使用在不同德国研究船上收集的 SBE CTD .cnv 文件进行测试)GitHub

干杯,马尔科

于 2018-08-20T20:31:09.650 回答