ncrcat is single-writer software: only one copy of it can be appending to the same
NetCDF file at any moment.
So the safe recipe is
do all the expensive, embarrassingly-parallel work first (extract_precip),
wait until every job has finished,
run one serial ncrcat on a sorted list of files.
Something like this:
bash
#!/usr/bin/env bash
set -euo pipefail
PATTERN='*_seed_*.eam.h2.000[34567]-*.nc'
###############################################################################
# 1. Run the heavy extraction in parallel
###############################################################################
find . -maxdepth 1 -type f -name "$PATTERN" -print0 |
sort -zV | # make sure input itself is in order
parallel -0 -j64 extract_precip {} # 64 workers
###############################################################################
# 2. Concatenate – must be done once, and in order
###############################################################################
# Assume extract_precip created files called <original>_pr.nc
find . -maxdepth 1 -type f -name '*_pr.nc' -print0 |
sort -zV | # chronological / numeric sort
xargs -0 ncrcat -O -h -o precip_all.nc # single ncrcat run
Why this works
• parallel is used only where tasks are independent (extract_precip writes its own output file).
• The sort -V (or sort -n / sort) guarantees that the file list handed to ncrcat
is in the correct chronological order, so the record dimension in the final file
will be correct.
• ncrcat is launched once, so there is no risk of multiple processes
clobbering each other.
Can I still use GNU parallel for the concatenation itself?
Only if you first break the job into independent chunks—e.g. produce temporary
files chunk_00.nc, chunk_01.nc, … in parallel and then do a final serial
ncrcat on those chunks. But that adds complexity and rarely gives a speed-up
because concatenation is I/O-bound, not CPU-bound.
Key GNU parallel options you might find useful
• -k, --keep-order When you pipe the output of parallel to another program and you
care about order.
• -0, --null Match the NUL-terminated stream that find … -print0 produces.
• sort -zV Version-aware sort, also NUL-terminated (-z).
In short:
Parallelise everything except the final ncrcat, and make sure the list you
hand to ncrcat is sorted.
User
Ok you are right. I shouldn't use parallel with ncrcat, from what I can tell. The issue with using ncrcat on its own is that there are many, many files.
Assistant
Right-now the bottle-neck is not “ncrcat can’t handle it”, it’s:
• the shell’s argument-length limit (∙ thousands of paths won’t fit on one command line), and
• the fact that only one process should be appending to a given NetCDF file at a time.
Below are three practical ways to get around those issues while still keeping everything in the right chronological order.
────────────────────────────────────────────────────────────────────────
A) Let ncrcat read a file-list (simplest, no parallelism)
────────────────────────────────────────────────────────────────────────
NCO understands the option ‑-fl_lst_in (shorthand ‑l).
So just hand it a plain-text list that you have already sorted.
bash
# Create a chronologically-sorted list, one filename per line
find . -maxdepth 1 -type f -name "*_seed_*.eam.h2.000[34567]-*.nc" \
-print0 | sort -zV | tr '\0' '\n' > filelist.txt
# Single ncrcat run; no “argument-list too long” problem
ncrcat -O -h --fl_lst_in filelist.txt -o precip_all.nc
Pro
– dead simple
Con
– uses only one CPU, but concatenation is usually I/O-bound anyway.
────────────────────────────────────────────────────────────────────────
B) Chunk the list and run several ncrcat’s in parallel
────────────────────────────────────────────────────────────────────────
Each ncrcat writes its own temporary output; after they finish you do one
last serial cat. This way no two processes ever touch the same file.
bash
PATTERN='*_seed_*.eam.h2.000[34567]-*.nc'
CHUNK=300 # files per sub-cat
JOBS=8 # concurrent ncrcat’s (tune to your disk/CPU)
# 1. Split the master list into groups of $CHUNK files
find . -name "$PATTERN" -print0 |
sort -zV |
parallel -0 -N "$CHUNK" -j "$JOBS" '
grp=$(printf "%03d" {#}) # {#} = job number
ncrcat -O -h -o tmp_${grp}.nc "$@"
'
# 2. Final serial cat, keeping the original order
ncrcat -O -h -o precip_all.nc $(ls -1v tmp_*.nc)
rm tmp_*.nc
Explanation of the GNU parallel flags
– -N CHUNK→giveexactlyCHUNK arguments to each job
– -j JOBS→runthatmanyjobssimultaneously–"@" → list of files received by the job
– {#} → job’s ordinal number (used to name the temp file)
────────────────────────────────────────────────────────────────────────
C) Same idea but with xargs (if you prefer POSIX tools)
────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────
Quick decision guide
────────────────────────────────────────────────────────────────────────
Need maximum simplicity & don’t care about a few extra minutes?
→ Method A.
Have a fast parallel file-system and want to squeeze more I/O?
→ Method B (or C) with chunking.
Either way, sorting before concatenation guarantees the record dimension
ends up in the right order, and only one process ever writes to any single
output, so nothing gets “screwed up”. Happy catting! 🐈