~javier-lopez/ubuntu/quantal/cdo/sru-1023329

« back to all changes in this revision

Viewing changes to src/field.c

  • Committer: Bazaar Package Importer
  • Author(s): Alastair McKinstry
  • Date: 2010-09-22 15:58:09 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100922155809-u1d3vlmlqj02uxjt
Tags: 1.4.6.dfsg.1-1
New upstream release. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include "cdo.h"
19
19
#include "cdo_int.h"
20
 
#include "cdi.h"
 
20
#include <cdi.h>
21
21
/* RQ */
22
22
#include "nth_element.h"
23
23
/* QR */
42
42
  return FDIV(x, y);
43
43
}
44
44
 
45
 
double _FROOT_(double x, double missval1)
46
 
{
47
 
  return FROOT(x);
 
45
double _FPOW_(double x, double y, double missval1, double missval2)
 
46
{
 
47
  return FPOW(x, y);
 
48
}
 
49
 
 
50
double _FSQRT_(double x, double missval1)
 
51
{
 
52
  return FSQRT(x);
48
53
}
49
54
 
50
55
 
53
58
  double rval = 0;
54
59
 
55
60
  if      ( function == func_min  )  rval = fldmin(field);
56
 
  else if ( function == func_max  )  rval = fldmax(field);  
57
 
  else if ( function == func_sum  )  rval = fldsum(field);  
58
 
  else if ( function == func_mean )  rval = fldmean(field);  
59
 
  else if ( function == func_avg  )  rval = fldavg(field);  
60
 
  else if ( function == func_std  )  rval = fldstd(field);  
 
61
  else if ( function == func_max  )  rval = fldmax(field);
 
62
  else if ( function == func_sum  )  rval = fldsum(field);
 
63
  else if ( function == func_mean )  rval = fldmean(field);
 
64
  else if ( function == func_avg  )  rval = fldavg(field);
 
65
  else if ( function == func_std  )  rval = fldstd(field);
61
66
  else if ( function == func_var  )  rval = fldvar(field);
62
67
  else cdoAbort("function %d not implemented!", function);
63
68
 
327
332
    }
328
333
    */
329
334
 
330
 
  ravg = ROOT(DIV(rsum, rsumw));
 
335
  ravg = SQRT(DIV(rsum, rsumw));
331
336
 
332
337
  if ( DBL_IS_EQUAL(ravg, missval1) ) rnmiss++;
333
338
 
381
386
    }
382
387
    */
383
388
 
384
 
  ravg = ROOT(DIV(rsum, rsumw));
 
389
  ravg = SQRT(DIV(rsum, rsumw));
385
390
 
386
391
  if ( DBL_IS_EQUAL(ravg, missval1) ) rnmiss++;
387
392
 
392
397
/* RQ */
393
398
double fldpctl(field_t field, int p)
394
399
{
395
 
  static const char func[] = "fldpctl";
396
 
  
 
400
  static const char *func = "fldpctl";
 
401
 
397
402
  long   len     = field.size;
398
403
  int    nmiss   = field.nmiss;
399
404
  double missval = field.missval;
400
405
  double *array  = field.ptr;
401
406
  double *array2;
402
 
        
 
407
 
403
408
  long i, j;
404
409
  double pctl = missval;
405
 
  
 
410
 
406
411
  if ( len - nmiss > 0 )
407
412
    {
408
413
      if ( nmiss > 0 )
409
414
        {
410
415
          array2 = (double *) malloc((len - nmiss)*sizeof(double));
411
 
          
 
416
 
412
417
          for ( i = 0, j = 0; i < len; i++ ) 
413
418
            if ( !DBL_IS_EQUAL(array[i], missval) )
414
419
              array2[j++] = array[i];
415
420
 
416
421
          pctl = nth_element(array2, j, (int)ceil(j*(p/100.0))-1);
417
 
          
418
 
          free(array2);   
 
422
 
 
423
          free(array2);
419
424
        }
420
425
      else
421
426
        {
426
431
  return pctl;
427
432
}
428
433
/* QR */
 
434
 
 
435
/*  field_t UTILITIES */
 
436
/*  update the number non missing values */
 
437
void fldunm(field_t *field)
 
438
{
 
439
  long i;
 
440
 
 
441
  field->nmiss = 0;
 
442
  for ( i = 0; i < field->size; i++ )
 
443
    if ( DBL_IS_EQUAL(field->ptr[i], field->missval) ) field->nmiss++;
 
444
}
 
445
 
 
446
/*  check for non missval values */
 
447
int fldhvs(field_t *fieldPtr, int nlevels)
 
448
{
 
449
  int level;
 
450
  field_t field;
 
451
 
 
452
  for ( level = 0; level < nlevels; level++)
 
453
    {
 
454
      field = fieldPtr[level];
 
455
      if ( field.nmiss != field.size )
 
456
        return TRUE;
 
457
    }
 
458
  return FALSE;
 
459
}