# HELP! What's wrong here ??? I don't get it!

Tim Roberts timr at probo.com
Thu Nov 8 06:46:52 CET 2001

christian werner <chr_werner at gmx.de> wrote:
>
>If I call the function with type = t or type = r I get the right results...
>If I call the function with type = p I get this wired result below:
>
>hour 1: takes first line, substracts 0s'   = OK!
>           a = 1.31 / pa = 0 / a2 = 1.31 / new pa = 1.31
>
>hour 2: a has the same value as in previous timestep, since the function
>           assigns pa = a in the loop I get '0' for the a2 array
>           a = 1.31 (again!!! - why?) / pa = 0 / a2 = 1.31 / new pa = 1.31
>
>hour 3: like hour 2
>hour 3: like hour 2
>etc.

Well, we aren't seeing the exact code here to be sure, but it's interesting
that the behavior you describe would occur if that very last "ln += 1" on
line 23 was actually at a different tabbing level, and thus rendered
outside the loop.  Have you checked spaces vs. tabs to make sure you're
consistent?

>12   elif (type == 'p'):
>13       for hour in range(1,25,1):
>14           line = linecache.getline(file, ln)             # read line
>15           a = array(map(float, string.split(line)))   # map line to 1d
>array
>16           a = reshape(a, (x,y))                          # reshape to 2d
>array
>17           a2 = a - pa                                       # a2 =
>corrected array
>18           pa = a                                              # this
>input array a will be
>19                                                                  # used
>in the next iteration as pa
>20
>21           a3[(hour-1)] = a[:,::-1]                   # reverse fortran
>order off array and
>22                                                              # put into
>3d array (day)
>23           ln += 1
>24       linecache.clearcache()

It seems dangerous to be incrementing two indices.  Why not write it like
this:

elif (type == 'p'):
for hour in range(24):
line = linecache.getline(file, ln+hour)
a = array(map(float, string.split(line)))
a = reshape(a, (x,y))
a2 = a - pa
pa = a
a3[(hour)] = a[:,::-1]

linecache.clearcache()
--
- Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.