On Thu, 2010-08-26 at 09:07 -0500, P Kishor wrote:
> I have the following workflow (console output is shown as > ...) --
> 
>     my $pdl = readflex("$file");
> 
>     # Print basic info for the piddle
>     print "Info: " . $pdl->info . "\n";
>     > Info: PDL: Float D [271116,99]
> 
>     # Set the bad value for the piddle
>     my $bad = -9999;
>     print "Setting BAD to $bad... ";
>     $pdl->badvalue($bad);
>     print "and turning on the bad flag\n";
>     $pdl->badflag(1);
>     > Setting BAD to -9999... and turning on the bad flag
> 
>     # Print the dims of the piddle
>     my @dims = $pdl->dims;
>     print "Num of cells: " . $dims[0] . ", Num of years: " . $dims[1] . "\n";
>     Num of cells: 271116, Num of years: 99
> 
>     for my $year (0 .. 2) {
> 
>         # Get a ref to one year's worth of data
>         my $ypdl = $pdl->slice(":,$year");
>         print "Info for year $year: " . $ypdl->info . "\n";
>         > Info for year 0: PDL: Float D [271116,1]
> 
>         # Exchange the piddle dims to calculate the averages
>         print "Average for year $year: " . avg($ypdl) . "\n";
>         > Average for year 0: BAD
> 
>         # Print the sum of the piddle
>         print "Sum for year $year: " . sum($ypdl) . "\n";
>         > Sum for year 0: BAD
>     }
> }
> 
> I have the following questions --
> 
> 1. I actually have 100 years in the second dimension. I understood the
> $pdl->dims method to return the size of the dim. So, shouldn't it have
> returned 100 instead of 99. My understanding is that 99 is the highest
> value, but the number of values is 100. Did I understand wrong, or
> should I go back and check my data?

You should go back and check your data.  If $pdl->info says it only has
99 rows, then you only have 99 rows.

> 
> 2. Is my approach correct? I am looping over the second dimension, but
> is there a PDL-ish way for doing that?

I would say your approach is not correct.  My rule of thumb is: Unless
you're constrained by memory, the only reason to have a for-loop in PDL
is for looping over files.  If you're writing a for-loop over the
dimensions of a piddle, there's probably an easier, faster, cleaner way
to do it.  For example:

$year_avgs = $pdl->average; #99 elements
$year_sums = $pld->sumover; #99 elements

> 
> 3. The docs for avg say that it averages over the 1st dim. I actually
> want to avg over the second dim. Should I be doing something like
> 
>     my $yave = average $ypdl->xchg(0, 1);
> 
> Also, the same question applies for sum.

The docs for average say that it averages along the first dim.  avg does
the whole piddle.  sumover does the first dim, sum does the whole
piddle.  Short name: whole piddle; long name: first dim.  If you want
the avgs for each of your 99 years, then you want to take the average
along the first dim.
> 
> 4. The docs for avg (and for sum) say that the routines do handle BAD
> values. I understood that to mean that they would discard the BAD
> values in their calculation, not return a BAD value.

Correct, but they will take the BAD values into account when calculating
the average:

p pdl(BAD,BAD,BAD,1)->avg
0.25

If you had a ND piddle (N>1) with its badflag set, and did average, so
that your result was a (N-1)D piddle, that piddle would have its badflag
set.

> 
> 5. I really don't understand the difference between sum and sumover.

See my response to #3, and try it with a 2D piddle, like sequence(3,4).

> 
> Many thanks for putting up with these really basic steps.
> 
> My subsequent tasks would be to plot charts of the averages and sums
> by year, and create images of all the cells for every year. Again, I
> am hoping to discard the BAD values in doing so. What I really should
> be doing is creating piddles of the averages and sums, and then using
> PLplot to plot the charts. Suggestions?
> 

I think PGPLOT will ignore your bad values, I would imagine & hope (but
have not confirmed) that PLplot would do the same.

cheers,
Derek


_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to