[SciPy-User] Remove the need for the loop

Martin De Kauwe mdekauwe at gmail.com
Mon Mar 22 21:05:28 EDT 2010


Hi,

I would like to transfer the contents of a 1D array to a 2D array based on
two arrays listing the relevant locations (the arrays rows and cols).
Ideally I would like to solve my problem without the need for the current
for loop that it quite slow due to the array size. Any help doing this more
elegantly would be much appreciated.

What I currently have...

for fname in glob.glob(os.path.join(path, 'Snowf_P3_*.nc')):
    year = int(fname.split('/')[9].split('_')[2].split('.')[0][0:4])
    if year >= year_jules_run_starts and year <= year_jules_run_ends:
        month = int(fname.split('/')[9].split('_')[2].split('.')[0][4:6])
        f = Dataset(fname, 'r')
        snow = f.variables['Snowf']
        landpts = len(f.dimensions['land'])

        # sum monthly vals
        gswp_snow = (snow[:] * 1000.0 * (3.0 * 60.0 * 60.0) /
1000.0).sum(axis=0)
        f.close()

        # fill up the holding array, this is the bit where I would like to
avoid the for loop
        for point in xrange(numpts):
            snowfall[month-1,(180-(rows[point]-1)),cols[point]-1] +=
gswp_snow[point]
 Many Thanks
Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20100323/d541c571/attachment.html>


More information about the SciPy-User mailing list