[SciPy-User] Remove the need for the loop

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Mar 22 21:19:54 EDT 2010


On Mon, Mar 22, 2010 at 9:05 PM, Martin De Kauwe <mdekauwe at gmail.com> wrote:
> 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]

this should work if rows, cols and gswp_snow are all 1d (not clear from snippet)

snowfall[month-1,(180-(rows-1)),cols-1] += gswp_snow

otherwise, I think we need an example from which the dimensions/shapes
can be inferred

Josef

> Many Thanks
> Martin
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>



More information about the SciPy-User mailing list