coadd example: coadd_pseudo3d_pipe

This example shows how to use nmrglue to coadd a number of collections of 2D NMRPipe files which constitute a pseudo-3D data set. The two (or more) psuedo-3D data sets are assumed to be in directories named run*.dir with subdirectories named *.fid containing a test.fid file. The directory coadded_data.dir is created with the same subdirectory structure containing test.fid files containing data created by coadding each pseudo-3D.

The data used in this example is available for download.

[source code]

#! /usr/bin/env python

import nmrglue as ng
import numpy as np
import glob
import os.path

# create a list of directories to coadd
dlist = glob.glob("run*.dir")
dlist.sort()

# create a list of 2D files in the first directory
flist = glob.glob(os.path.join(dlist[0], "*.fid", "test.fid"))
flist.sort()

# loop over the files
for base_fname in flist:
    
    # initilize the new data
    dic, data = ng.pipe.read(base_fname)
    coadd_data = np.zeros_like(data)
    coadd_dic = dict(dic)
    
    # loop over the pseudo-3D directories
    for d in dlist:
        
        # the file names is found by replace the directory name
        f = base_fname.replace(dlist[0], d, 1)    
        print "Coadding file:", f
        dic, data = ng.pipe.read(f)
        coadd_data += data / len(dlist)

    # write out the file
    of = base_fname.replace(dlist[0], "coadded_data.dir", 1)
    print "Writing out:", of
    ng.pipe.write(of, coadd_dic, coadd_data, overwrite=True)