Creates one CSD or LFP file with downsampled data per cell type

def create_downsampled_data(params):
    '''
    Creates one CSD or LFP file with downsampled data per cell type
    '''
    maxsamples = 1

    for data_type in ['LFP','CSD']:

        if RANK == 0:
            if not os.path.isdir(os.path.join(params.savefolder, 'populations', 'subsamples')):
                os.mkdir((os.path.join(params.savefolder,'populations','subsamples')))
        COMM.Barrier()

        try:
            assert(ana_params.scaling <= params.recordSingleContribFrac)
        except AssertionError as ae:
            raise ae, 'scaling parameter must be less than simulation recordSingleContribFrac'
        
        samples = int(1. / ana_params.scaling)
        if samples > maxsamples:
            samples = maxsamples
        
        COUNTER = 0
        for j, layer in enumerate(params.y_in_Y): # loop over layers
            for k, pop in enumerate(layer): # loop over populations
                for i, y in enumerate(pop): # loop over cell types
                    if COUNTER % SIZE == RANK:
                        # Load data
                        fname = os.path.join(params.savefolder, 'populations', '%s_%ss.h5' \
                                             % (y, data_type))
                        f = h5py.File(fname)
                        print 'Load %s' % str(f.filename)
                        raw_data = f['data'].value
                        srate = f['srate'].value
                        f.close()

                        ## shuffle data
                        #np.random.shuffle(raw_data)

                        # sample size
                        N = int(params.N_y[np.array(params.y) == y]*ana_params.scaling)
                        try:
                            assert(N <= raw_data.shape[0])
                        except AssertionError as ae:
                            raise ae, 'shape mismatch with sample size'


                        for sample in range(samples): # loop over samples

                            # slice data
                            data = raw_data[sample*N:(sample+1)*N]

                            # create cell resolved file
                            fname = os.path.join(params.savefolder,'populations','subsamples',
                                                 '%s_%ss_%i_%i.h5' \
                                                 % (y, data_type, ana_params.scaling*100, sample))
                            f = h5py.File(fname, 'w')
                            print 'Write %s' % str(f.filename)
                            f['data'] = data
                            f['srate'] = srate
                            f.close()

                            # create cell type resolved file
                            fname = os.path.join(params.savefolder,'populations','subsamples',
                                                 '%s_population_%s_%i_%i.h5' \
                                                 % (y,data_type,ana_params.scaling*100,sample))
                            f = h5py.File(fname, 'w')
                            print 'Write %s' % str(f.filename)
                            f['data'] = data.sum(axis=0)
                            f['srate'] = srate
                            f.close()

                    COUNTER += 1

        COMM.Barrier()
        f = h5py.File(os.path.join(params.savefolder,'populations', '%s_%ss.h5' % (y,data_type)), 'r')
        datashape = f['data'].shape
        f.close()

        COUNTER = 0
        for sample in range(samples): # loop over samples
            if COUNTER % SIZE == RANK:
                # initialize full sum signal
                data_full = np.zeros(datashape[1:])
                for j,layer in enumerate(params.y_in_Y): # loop over layers
                    for k,pop in enumerate(layer): # loop over populations

                        # initialize population resolved sum signal
                        data_Y = np.zeros(datashape[1:])

                        for i,y in enumerate(pop): # loop over cell types

                            # Load data
                            fname = os.path.join(params.savefolder, 'populations',
                                                 'subsamples', '%s_population_%s_%i_%i.h5' \
                                                 % (y, data_type, ana_params.scaling*100,
                                                    sample))
                            f = h5py.File(fname, 'r')    
                            # Update population sum:
                            data_Y += f['data'].value
                            srate = f['srate'].value
                            f.close()

                        # write population sum
                        fname = os.path.join(params.savefolder,'populations','subsamples',
                                             '%s_population_%s_%i_%i.h5' \
                                             % (params.Y[2*j+k], data_type,
                                                ana_params.scaling*100, sample))
                        f = h5py.File(fname,'w')
                        print 'Write %s' % str(f.filename)
                        f['data'] = data_Y
                        f['srate'] = srate
                        f.close() 

                        # update full sum
                        data_full += data_Y

                # write sum
                fname = os.path.join(params.savefolder,'populations','subsamples',
                                     '%ssum_%i_%i.h5' % (data_type,ana_params.scaling*100,sample))
                f = h5py.File(fname,'w')
                print 'Write %s' % str(f.filename)
                f['data'] = data_full
                f['srate'] = srate
                f.close()

            COUNTER += 1

        COMM.Barrier()