Skip to content

bioepic-data/ecosim-agent / .agents/skills/ameriflux-bif-timeseries-sorter

ameriflux-bif-timeseries-sorter

Verified

Sort AmeriFlux BASE-BADM BIF variables from Excel or CSV files into observation, management-event, static metadata, and plotting-ready target time-series tables. Use when Codex is asked to sort AmeriFlux BIF variables into time series; organize BADM/BIF variables by date; pivot AMF *_BIF_*.xlsx files by GROUP_ID; output total LAI, leaf mass per area, above-ground biomass, canopy height, fruit yield, or total yield time series; write MATLAB readers for BIF time-series plotting; or prepare dated AmeriFlux site observations and management records for EcoSIM workflows.

scripts/read_bif_target_timeseries.m

View on GitHub
function data = read_bif_target_timeseries(outputDir, makePlot)
%READ_BIF_TARGET_TIMESERIES Read AmeriFlux BIF target series for plotting.
%
% data = read_bif_target_timeseries(outputDir)
% data = read_bif_target_timeseries(outputDir, true)
%
% The input directory should be the output from sort_bif_timeseries.py, for
% example result/US-Ne3/bif_timeseries. The function reads:
%   target_timeseries/total_lai.csv
%   target_timeseries/leaf_mass_per_area.csv
%   target_timeseries/aboveground_biomass.csv
%   target_timeseries/canopy_height.csv
%   target_timeseries/fruit_yield.csv
%   target_timeseries/total_yield.csv
%
% Returned tables include Time as datetime and Value as double. Original BIF
% values, units, comments, and qualifiers are preserved in the table columns.

if nargin < 1 || isempty(outputDir)
    outputDir = fullfile("result", "US-Ne3", "bif_timeseries");
    makePlot= false;
end
if nargin < 2 || isempty(makePlot)
    makePlot = false;
end

targetDir = fullfile(outputDir, "target_timeseries");
series = [
    struct("name", "total_lai", "label", "Total LAI")
    struct("name", "leaf_mass_per_area", "label", "Leaf mass per area")
    struct("name", "aboveground_biomass", "label", "Above-ground biomass")
    struct("name", "canopy_height", "label", "Canopy height")
    struct("name", "fruit_yield", "label", "Fruit / grain yield")
    struct("name", "total_yield", "label", "Total crop production")
];

data = struct();
data.outputDir = string(outputDir);
data.targetDir = string(targetDir);
data.files = struct();

for i = 1:numel(series)
    name = series(i).name;
    filePath = fullfile(targetDir, name + ".csv");
    data.files.(name) = string(filePath);

    if ~isfile(filePath)
        warning("read_bif_target_timeseries:MissingFile", "Missing target time-series file: %s", filePath);
        data.(name) = table();
        continue
    end

    T = readtable(filePath, "TextType", "string");
    if ismember("TIMESTAMP", T.Properties.VariableNames)
        T.Time = parseBifTimestamp(T.TIMESTAMP);
    end
    if ismember("VALUE_NUMERIC", T.Properties.VariableNames)
        if isnumeric(T.VALUE_NUMERIC)
            T.Value = T.VALUE_NUMERIC;
        else
            T.Value = str2double(string(T.VALUE_NUMERIC));
        end
    end
    data.(name) = T;
end

if makePlot
    plotTargetSeries(data, series);
end
end

function t = parseBifTimestamp(values)
values = string(values);
t = NaT(numel(values), 1);
formats = ["yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyy"];

for i = 1:numel(values)
    value = strtrim(values(i));
    if strlength(value) == 0 || ismissing(value)
        continue
    end
    for j = 1:numel(formats)
        try
            parsed = datetime(value, "InputFormat", formats(j));
            if ~isnat(parsed)
                t(i) = parsed;
                break
            end
        catch
        end
    end
end
end

function plotTargetSeries(data, series)
figure;
tiledlayout(numel(series), 1, "TileSpacing", "compact");

for i = 1:numel(series)
    name = series(i).name;
    nexttile;
    if ~isfield(data, name) || isempty(data.(name)) || height(data.(name)) == 0
        title(series(i).label);
        ylabel("missing");
        continue
    end

    T = data.(name);
    if ~ismember("Time", T.Properties.VariableNames) || ~ismember("Value", T.Properties.VariableNames)
        title(series(i).label);
        ylabel("unreadable");
        continue
    end

    groupColumn = chooseGroupColumn(T);
    if groupColumn ~= ""
        groups = unique(string(T.(groupColumn)));
        hold on
        for g = 1:numel(groups)
            mask = string(T.(groupColumn)) == groups(g);
            plot(T.Time(mask), T.Value(mask), "o-", "DisplayName", groups(g));
        end
        hold off
        legend("Location", "best");
    else
        plot(T.Time, T.Value, "o-");
    end

    title(series(i).label);
    ylabel(valueLabel(T));
    grid on
end
xlabel("Time");
end

function groupColumn = chooseGroupColumn(T)
candidateColumns = ["AG_PROD_CROP_ORGAN", "AG_BIOMASS_CROP_ORGAN", "HEIGHTC_STATISTIC", "LAI_TYPE", "LMA_SPP"];
groupColumn = "";
for i = 1:numel(candidateColumns)
    column = candidateColumns(i);
    if ismember(column, T.Properties.VariableNames)
        values = string(T.(column));
        values = values(strlength(values) > 0 & ~ismissing(values));
        if numel(unique(values)) > 1
            groupColumn = column;
            return
        end
    end
end
end

function label = valueLabel(T)
label = "value";
if ismember("UNIT", T.Properties.VariableNames)
    units = string(T.UNIT);
    units = units(strlength(units) > 0 & ~ismissing(units));
    if ~isempty(units)
        label = "value (" + units(1) + ")";
    end
end
end
deprecation-and-migrationaddyosmani/agent-skillsManages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
performance-optimizationaddyosmani/agent-skillsOptimizes application performance across frontend, backend, queries, and databases. Use when performance requirements exist, when you suspect performance regressions, when Core Web Vitals or load times need improvement, when N+1 query patterns need fixing, or when profiling reveals bottlenecks.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
agent-cost-reportthedotmack/claude-memBelievable agent cost report for any period, default the last 7 full days PT, not counting today. Measured tokens from Claude Code transcripts priced at OpenRouter list prices (ESTIMATED), measured provider spend when a sanctioned source exists, note-taker cost separate, Timing-style HTML/PDF plus report.json, line-items.csv, evidence.json.Stars: 118kUpdated Claude CodeCodexVerifiedInstall