【问题标题】:My IDL loop only saves the last iteration - how can I fix it?我的 IDL 循环只保存最后一次迭代 - 我该如何修复它?
【发布时间】:2019-07-22 20:38:52
【问题描述】:

我正在尝试编写一个循环来处理整个月的每日数据。我的代码在一次手动执行 1 天时有效,但是当我尝试循环遍历每个月的每一天时,我的循环失败了。

基本上,我循环 n = 1,30 天来读取文件,将数据剪辑到我的研究区域,并将每一天的数据添加到我的数据数组中。我想要一个包含整个月数据的数组。

这些是我循环中的函数:

@mls_choosefile_co.pro --> 这只是给了我“ToOpen”文件路径+名称

@mls_readin_co.pro --> 这使用 file_id 读取 CO 数据并将其剪辑到我的研究区域。输出为 'co_sa_100',对于研究区域中 100 hpa 的 CO,一个 1x700 数组(长度随天而变,可能是 706、720、680...)

^^ 这两个都可以单独工作。

; make array of data for the whole month - CO at 100hpa
; to append each additional day to this on each loop iteration
    co_100_all = [ ] 
; make array to save how many data points per day. use -9 as a fill value to tell me when the loop has not gone over that day. 
    len = MAKE_ARRAY(1,ndays, VALUE = -9)

; the loop in question: 
    FOR n = 0,ndays DO BEGIN &$
        @mls_choosefile_co.pro &$
        file_id = H5F_OPEN(ToOpen) &$
        @mls_readin_co.pro &$
        len[n-1] = N_ELEMENTS(co_sa_100) &$
        co_100_all = [[co_100_all], [co_sa_100]] &$
    END 

我将“len”变量设置为测试。这将告诉我每天读取了多少数据点,每天有一个值(通常在 700 左右)。我已将 -9 设置为循环未运行的填充值。它应该是这样的: len = [702, 716, 706]。 但它只保存循环中的最后一个索引,我得到: len = [-9, -9, 706]。

提前致谢!

【问题讨论】:

    标签: idl-programming-language


    【解决方案1】:

    更新:已修复!

    我的问题是使用@program.pro 而不是program, input, output 格式调用函数。

    遍历n 天数,提取名为'tracer' 的化学物质的数据,并将每天的output 保存到saveoutputs 数组中。

    这是编辑后的代码,以防对任何人有帮助:

    FOR n = 0, ndays-1 DO BEGIN
        mls_choosefile, n, tracer, file_id
        mls_readin, file_id, tracer, output
        mls_len[n-1] = N_ELEMENTS(output)
        saveoutputs[n,*] = output
    END 
    

    其中mls_choosefilemls_readin 设置为IDL 过程。

    【讨论】:

      【解决方案2】:

      我的 IDL 代码也有类似的问题。我的变量 'm' 在数组中有 ~17700 个数据点,当我 >print,m 或 >plot m,a ...只有最后一个点被打印或绘制。

      所以我尝试的替代方法是使用“附加”将数据点保存在 .dat 文件中,然后绘制它。

      openu,1,'m.dat',/追加 打印f,1,m free_lun,1

      plot,m,a,yrange=1,6.2],xstyle=1,ystyle=1,xtitle='磁当地时间',ytitle='电压[伏特]',position=[0.06, 0.10, 0.97, 0.95]

      这对我有用。关键是使用'append'函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-15
        • 1970-01-01
        • 2021-06-23
        • 2016-04-23
        • 1970-01-01
        • 2021-05-11
        • 1970-01-01
        相关资源
        最近更新 更多