#include #include #include "filter.h" #include "dates.h" /*******************************************************************************/ void export_sample_dates( FILE *fp, ExportData export, double xp[], double yp[], double smooth2[], double trend2[], double deriv2[], int np, FilterData filter ) { /*------------------------------------- * export data at same dates as * original samples *-------------------------------------*/ static char *format; int i, year, month, day, hour, minute, second; double p, f, h, t; format = export.format; for (i=0; i xfilt[nfilt-1]) { /* calculate derivative of polynomial */ if (filter.numpolyterms > 0 ) { p=filter.pm[filter.numpolyterms-1]; dp=0.0; for (j=filter.numpolyterms-2;j>=0; j--) { dp = dp*t0+p; p = p*t0+filter.pm[j]; } } else { dp=0; } fprintf (fp, format, dp); } else { d1 = interpolate(xfilt, deriv, nfilt, d2, t); fprintf (fp, format, d1); } } fprintf (fp, "\n"); i++; } free (s2); free (t2); free (d2); } /**********************************************************************************/ void export_user( char *datefile, FILE *fp, ExportData export, double xfilt[], double smooth[], double trend[], double deriv[], int nfilt, FilterData filter ) { char *format, s[256]; int i, year, month, day, hour, minute, second, j; double rinterval, dp; double p, f, h, t; double t0, t1, s1, d1, *s2, *t2, *d2; FILE *infile; infile = fopen (datefile, "r"); if ( infile == NULL) { fprintf (stderr, "Can't open %s.\n", datefile); return; } format = export.format; rinterval = filter.sampleinterval/365.0; i=0; s2 = (double *) calloc (nfilt, sizeof(double)); if (s2 == NULL) { fprintf (stderr, "Can't allocate memory to interpolate values."); return; } t2 = (double *) calloc (nfilt, sizeof(double)); if (t2 == NULL) { fprintf (stderr,"Can't allocate memory to interpolate values."); return; } d2 = (double *) calloc (nfilt, sizeof(double)); if (d2 == NULL) { fprintf (stderr, "Can't allocate memory to interpolate values."); return; } spline (xfilt, smooth, nfilt, s2); spline (xfilt, trend, nfilt, t2); spline (xfilt, deriv, nfilt, d2); while (fgets (s, 256, infile) != NULL) { sscanf (s, "%lf", &t); /*printf ("t = %f, rinterval = %f, i = %d\n", t, rinterval, i); */ if (export.cal_format) { GetCalendarDate (t, &year, &month, &day, &hour, &minute, &second); if (export.use_hour) { fprintf (fp, "%4d %2d %2d %2d",year, month, day, hour); } else { fprintf (fp, "%4d %2d %2d",year, month, day); } } else { fprintf (fp, "%13.8f",t); } t0 = t - filter.timezero; p = userfunc (filter.pm, t0, 0, filter.numpolyterms); f = userfunc (filter.pm, t0, filter.numharmonics, filter.numpolyterms); h = f-p; s1 = interpolate(xfilt, smooth, nfilt, s2, t); t1 = interpolate(xfilt, trend, nfilt, t2, t); if (export.func) fprintf (fp, format, f); if (export.poly) fprintf (fp, format, p); if (export.sm) fprintf (fp, format, f + s1); if (export.tr) fprintf (fp, format, p+t1); if (export.smcycle) fprintf (fp, format, h+s1-t1); if (export.harm) fprintf (fp, format, h); if (export.smres) fprintf (fp, format, s1); if (export.trres) fprintf (fp, format, t1); if (export.gr) { if (t < xfilt[0] || t > xfilt[nfilt-1]) { /* calculate derivative of polynomial */ if (filter.numpolyterms > 0 ) { p=filter.pm[filter.numpolyterms-1]; dp=0.0; for (j=filter.numpolyterms-2;j>=0; j--) { dp = dp*t0+p; p = p*t0+filter.pm[j]; } } else { dp=0; } fprintf (fp, format, dp); } else { d1 = interpolate(xfilt, deriv, nfilt, d2, t); fprintf (fp, format, d1); } } fprintf (fp, "\n"); i++; } free (s2); free (t2); free (d2); }