如何在python上从多个CTD数据文件(多个数据文件中相同变量的行平均值)中找到平均CTD配置文件

时间:2017-11-14 13:56:51

标签: python mean multiple-files rowwise

我在尝试写这个问题时遇到了困难。我有多个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')

Figure of each CTD data set plotted

我希望我的问题有道理。

非常感谢

1 个答案:

答案 0 :(得分: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

干杯, 马可