[Numpy-discussion] loop through values in a array and find maximum as looping

questions anon questions.anon at gmail.com
Wed Dec 7 00:26:09 EST 2011


thanks for all your responses. I think I have FINALLY worked it out with
all of your help.
I just assigned one array from one ncfile to "a" at the beginning of my
code and then ran the loop and it worked!!
sorry for all the questions but I learn so much playing and getting ideas
from others.
Thanks again. code below for anyone else that needs to do the same.

onefile=Dataset("E:/01/IDZ00026_T_SFC.nc", 'r+', 'NETCDF4')
oneTSFC=onefile.variables['T_SFC'][:]
a=oneTSFC[0]

for (path, dirs, files) in os.walk(MainFolder):
    for dir in dirs:
        print dir
    path=path+'/'
    for ncfile in files:
        if ncfile[-3:]=='.nc':
            ncfile=os.path.join(path,ncfile)
            ncfile=Dataset(ncfile, 'r+', 'NETCDF4')
            TSFC=ncfile.variables['T_SFC'][:]
            ncfile.close()
            for b in TSFC[:]:
                    N.maximum(a,b, out=a)
 print a




On Wed, Dec 7, 2011 at 4:11 PM, Derek Homeier <
derek at astro.physik.uni-goettingen.de> wrote:

> On 07.12.2011, at 5:54AM, questions anon wrote:
>
> > sorry the 'all_TSFC' is for my other check of maximum using concatenate
> and N.max, I know that works so I am comparing it to this method. The only
> reason I need another method is for memory error issues.
> > I like the code I have written so far as it makes sense to me. I can't
> get the extra examples I have been given to work and that is most likely
> because I don't understand them, these are the errors I get :
> >
> > Traceback (most recent call last):
> >   File "d:\plot_summarystats\test_plot_remove_memoryerror_max.py", line
> 46, in <module>
> >     N.maximum(a,TSFC,out=a)
> > ValueError: non-broadcastable output operand with shape (106,193)
> doesn't match the broadcast shape (721,106,193)
> >
> > and
> >
> OK, then it seems we did not indeed grasp the entire scope of the problem -
> since you have initialised a from the previous array TSFC (not from
> TSFC[0]?!), this can
> only mean the arrays read in come in different shapes? I don't quite
> understand how the
> previous version did not raise an error then; but if you only want the
> (106,193)-subarray
> you have indeed to keep the loop
>           for b in TSFC[:]:
>                N.maximum(a,b,out=a)
>
> But you would have to find some way to distinguish between ndim=2 and
> ndim=3 input,
> if really both can occur...
> >
> > Traceback (most recent call last):
> >   File "d:\plot_summarystats\test_plot_remove_memoryerror_max.py", line
> 45, in <module>
> >     if not instance(a, N.ndarray):
> > NameError: name 'instance' is not defined
> >
> Sorry, typing error (or devious auto-correct?) - this should be
> 'isinstance()'
>
> Cheers,
>                                                Derek
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111207/fa6c4b79/attachment.html>


More information about the NumPy-Discussion mailing list