00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <strings.h>
00012 #include <sys/types.h>
00013 #include <sys/stat.h>
00014 #include <unistd.h>
00015
00016 #include "jsoc_main.h"
00017 #include "fitsio.h"
00018 #include "drms_dsdsapi.h"
00019
00020 #define ARRLENGTH(ARR) (sizeof(ARR)/sizeof(ARR[0]))
00021 #define kNOTSPECIFIED "not specified"
00022
00023 char *module_name = "jpkbgn";
00024 char *cvsinfo_jpkbgn = "cvsinfo: $Header: /home/cvsuser/cvsroot/JSOC/proj/globalhs/apps/jpkbgn.c,v 1.13 2016/08/08 06:24:21 tplarson Exp $";
00025
00026 ModuleArgs_t module_args[] =
00027 {
00028 {ARG_STRING, "in", "", "input data records"},
00029 {ARG_STRING, "out", "", "output data series"},
00030 {ARG_STRING, "ffttmp", "", ""},
00031 {ARG_STRING, "segin" , kNOTSPECIFIED, "name of input segment if not using segment 0"},
00032 {ARG_STRING, "segout", kNOTSPECIFIED, "name of output segment if not using segment 0"},
00033 {ARG_STRING, "histlink", "HISTORY", "name of link to ancillary dataseries for processing metadata"},
00034
00035
00036 {ARG_INT, "VERB", "1", "option for level of verbosity: 0=only error and warning messages; >0=print messages outside of loop; >1=print messages inside loop; >2=debugging output", NULL},
00037
00038 {ARG_STRING, "LOGFILE", "stdout", ""},
00039 {ARG_INT, "READFFT", "0", "", ""},
00040 {ARG_STRING, "GAPFILE", "", "record or file containing window function"},
00041 {ARG_INT, "IPAR", "1", "", ""},
00042 {ARG_STRING, "PARAMFILE", "nofile", ""},
00043 {ARG_STRING, "PAR1FILE", "", ""},
00044 {ARG_STRING, "RESFILE", "result", ""},
00045 {ARG_STRING, "CROSSFILE", "", ""},
00046 {ARG_INT, "NDELTAL", "6", "", ""},
00047 {ARG_INT, "NOISETYPE", "1", "", ""},
00048 {ARG_STRING, "CROSSNFILE", "/dev/null", ""},
00049 {ARG_INT, "NNOIB", "1", "", ""},
00050
00051 {ARG_STRING, "NOIB", "40000 43000", ""},
00052
00053 {ARG_INT, "NNOIM", "0", "", ""},
00054 {ARG_STRING, "NOIM", " ", ""},
00055 {ARG_FLOAT, "CSUBM", "0.5", "", ""},
00056 {ARG_INT, "IFOLLOW", "1", "", ""},
00057 {ARG_FLOAT, "ANOISE", "0.0", "", ""},
00058 {ARG_FLOAT, "AOTHER", "0.0", "", ""},
00059
00060 {ARG_INT, "IODD", "1", "", ""},
00061 {ARG_INT, "NMIN", "0", "", ""},
00062 {ARG_INT, "NMAX", "50", "", ""},
00063 {ARG_FLOAT, "FMIN", "1000", "", ""},
00064 {ARG_FLOAT, "FMAX", "4000", "", ""},
00065 {ARG_INT, "IMFREQ", "0", "", ""},
00066 {ARG_INT, "ICASE", "3", "", ""},
00067 {ARG_INT, "NACOEFF", "5", "", ""},
00068 {ARG_INT, "NDT", "51840", "", ""},
00069
00070 {ARG_INT, "IDF", "1", "", ""},
00071 {ARG_INT, "NDF0", "20", "", ""},
00072 {ARG_INT, "NDF1", "50", "", ""},
00073 {ARG_FLOAT, "CDF", "5", "", ""},
00074 {ARG_INT, "NDM", "10", "", ""},
00075 {ARG_INT, "NDL", "6", "", ""},
00076 {ARG_FLOAT, "XSAFE", "10.0", "", ""},
00077 {ARG_FLOAT, "XSAFE1", "1.0", "", ""},
00078 {ARG_FLOAT, "DELMIN", "0.01", "", ""},
00079 {ARG_INT, "MAXCHI", "50", "", ""},
00080 {ARG_INT, "MAXGRAD", "30", "", ""},
00081 {ARG_INT, "MAXCOF", "4", "", ""},
00082 {ARG_INT, "IFIX", "0", "", ""},
00083 {ARG_INT, "IFIRST", "0", "", ""},
00084 {ARG_INT, "FLIPM", "0", "", ""},
00085 {ARG_INT, "NLOBES", "", "", ""},
00086 {ARG_INT, "NFOUR", "0", "", ""},
00087 {ARG_INT, "GONGFLAG", "0", "", ""},
00088 {ARG_INT, "PRINTFIT", "0", "", ""},
00089 {ARG_FLOAT, "CDETREND", "50.0", "", ""},
00090 {ARG_INT, "IFILL", "1", "", ""},
00091 {ARG_INT, "FDIFF", "0", "", ""},
00092 {ARG_INT, "IASYM", "0", "", ""},
00093 {ARG_INT, "ICT", "0", "", ""},
00094 {ARG_INT, "IWOOD", "0", "", ""},
00095 {ARG_FLOAT, "WOODB1", "0.0", "", ""},
00096 {ARG_FLOAT, "WOODB2", "0.0", "", ""},
00097 {ARG_INT, "CTX", "1.0", "", ""},
00098
00099 {ARG_END}
00100 };
00101
00102 #include "saveparm.c"
00103 #include "set_history.c"
00104
00105 void detrend(float *data, float *gaps, int length, float cdetrend);
00106 void fill_gaps(float *data, float *gaps, float *newgaps, int length);
00107
00108
00109 void sub24_(char *logfile,char *fftfile, float *rgaps, int *ipar, char *paramfile, char *par1file, char *resfile,
00110 char *crossfile, int *ndeltal, int *noisetype, char *crossnfile, int *nnoib, int *noib, int *nnoim, int *noim,
00111 float *csubm, int *ifol, float *anoise, float *aother, int *lin, int *ioddin, int *iasymin, int *ictin, float *ctxin,
00112 int *iwoodin, float *woodb1, float *woodb2, int *nmin, int *nmax, float *fmin, float *fmax,
00113 int *imfin, int *icasein, int *nain, int *idtin, int *ndtin, float *tsin, int *idf, int *ndf0, int *ndf1,
00114 float *cdf, int *ndmin, int *ndl,
00115 float *xsin, float *xs1in, float *delx, int *maxchi, int *maxgrad, int *maxcof, int *recin,
00116 int *nlobesin, int *nfourin, int *gongin, int *printflag,
00117 int, int, int, int, int, int, int);
00118
00119 void cffti_(int *, float *);
00120 void cfftb_(int *, float *, float *);
00121
00122
00123
00124
00125 int DoIt(void)
00126 {
00127 int status = DRMS_SUCCESS;
00128 int newstat = 0;
00129 int noib[10], noim[10];
00130
00131 int fstatus = 0;
00132 fitsfile *fitsptr;
00133 long fdims[1];
00134 long fpixel[]={1};
00135 int *anynul=0;
00136
00137 int zero=0;
00138 int i, j;
00139 float c,sum;
00140 char *ptr;
00141
00142 FILE *fileptr;
00143
00144 int readfft, savefft;
00145 int in_nsets;
00146
00147 int fdiff,flipm,m,navail,nread,nx1;
00148 float isign,*data;
00149 TIME epoch, step, start, stop;
00150 int gapsize, istart, istop;
00151 char *ffttmp, *fftfull, *infft;
00152 float cdetrend;
00153 char *syscall;
00154
00155 char *gapfile, *logfile, *paramfile, *par1file, *resfile, *crossfile, *crossnfile;
00156 int loglen, fftlen, paramlen, par1len, reslen, crosslen, crossnlen;
00157 int ipar, ndeltal, noisetype, nnoib, nnoim;
00158 char *noibstr, *noimstr;
00159 float csubm, anoise, aother, tsample, cdf, xsafe, xsafe1, delmin, fmin, fmax;
00160 int ifollow, lmode, iodd, nmin, nmax, imfreq, icase, nacoeff;
00161 int ndt, idf, ndf0, ndf1, ndm, ndl, maxchi, maxgrad, maxcof;
00162
00163 int ifix, ifirst, ifill;
00164 int nlobes,nfour,gongflag,printfit;
00165 int iasym, ict, iwood;
00166 float ctx;
00167 float woodb1, woodb2;
00168
00169 DRMS_Segment_t *segin = NULL;
00170 DRMS_Segment_t *segout = NULL;
00171 DRMS_Array_t *inarr = NULL;
00172 DRMS_Array_t *outarr = NULL;
00173 DRMS_Record_t *inrec = NULL;
00174 DRMS_Record_t *outrec = NULL;
00175 DRMS_Type_t usetype = DRMS_TYPE_FLOAT;
00176 DRMS_RecLifetime_t lifetime;
00177 long long histrecnum = -1;
00178 DRMS_Segment_t *gapseg = NULL;
00179 DRMS_Array_t *gaparr = NULL;
00180 DRMS_RecordSet_t *ffttmprecset = NULL;
00181 DRMS_Record_t *ffttmprec = NULL;
00182 DRMS_Segment_t *ffttmpseg = NULL;
00183 FILE *ffttmpfile = NULL;
00184 char tstartstr[100];
00185 char fftfile[DRMS_MAXPATHLEN+1];
00186 char dirstr[DRMS_MAXPATHLEN+1];
00187
00188 TIME tnow, UNIX_epoch = -220924792.000;
00189 double tstart, tstartsave, tstop, tstopsave, tstep;
00190
00191 int errbufstat=setvbuf(stderr, NULL, _IONBF, BUFSIZ);
00192 int outbufstat=setvbuf(stdout, NULL, _IONBF, BUFSIZ);
00193
00194
00195
00196
00197
00198 #ifdef __ia64
00199 int regsz = 4;
00200 #else
00201 int regsz = 1;
00202 #endif
00203
00204 float *rgaps, *igaps;
00205 float *gaps, *gaps1, *gaps2, *gaps3;
00206 float *help;
00207 float *datr, *dati, *data3;
00208 float *datarr;
00209
00210 ffttmp = (char *)cmdparams_save_str (&cmdparams, "ffttmp", &newstat);
00211 logfile = (char *)cmdparams_save_str (&cmdparams, "LOGFILE", &newstat);
00212 readfft = cmdparams_save_int (&cmdparams, "READFFT", &newstat);
00213
00214 gapfile = (char *)cmdparams_save_str (&cmdparams, "GAPFILE", &newstat);
00215 ipar = cmdparams_save_int (&cmdparams, "IPAR", &newstat);
00216 paramfile = (char *)cmdparams_save_str (&cmdparams, "PARAMFILE", &newstat);
00217 par1file = (char *)cmdparams_save_str (&cmdparams, "PAR1FILE", &newstat);
00218 resfile = (char *)cmdparams_save_str (&cmdparams, "RESFILE", &newstat);
00219 crossfile = (char *)cmdparams_save_str (&cmdparams, "CROSSFILE", &newstat);
00220 ndeltal = cmdparams_save_int (&cmdparams, "NDELTAL", &newstat);
00221 noisetype = cmdparams_save_int (&cmdparams, "NOISETYPE", &newstat);
00222 crossnfile = (char *)cmdparams_save_str (&cmdparams, "CROSSNFILE", &newstat);
00223 nnoib = cmdparams_save_int (&cmdparams, "NNOIB", &newstat);
00224 noibstr = (char *)cmdparams_save_str (&cmdparams, "NOIB", &newstat);
00225 nnoim = cmdparams_save_int (&cmdparams, "NNOIM", &newstat);
00226 noimstr = (char *)cmdparams_save_str (&cmdparams, "NOIM", &newstat);
00227 csubm = cmdparams_save_float (&cmdparams, "CSUBM", &newstat);
00228 ifollow = cmdparams_save_int (&cmdparams, "IFOLLOW", &newstat);
00229 anoise = cmdparams_save_float (&cmdparams, "ANOISE", &newstat);
00230 aother = cmdparams_save_float (&cmdparams, "AOTHER", &newstat);
00231
00232 iodd = cmdparams_save_int (&cmdparams, "IODD", &newstat);
00233 nmin = cmdparams_save_int (&cmdparams, "NMIN", &newstat);
00234 nmax = cmdparams_save_int (&cmdparams, "NMAX", &newstat);
00235 fmin = cmdparams_save_float (&cmdparams, "FMIN", &newstat);
00236 fmax = cmdparams_save_float (&cmdparams, "FMAX", &newstat);
00237 imfreq = cmdparams_save_int (&cmdparams, "IMFREQ", &newstat);
00238 icase = cmdparams_save_int (&cmdparams, "ICASE", &newstat);
00239 nacoeff = cmdparams_save_int (&cmdparams, "NACOEFF", &newstat);
00240 ndt = cmdparams_save_int (&cmdparams, "NDT", &newstat);
00241
00242 idf = cmdparams_save_int (&cmdparams, "IDF", &newstat);
00243 ndf0 = cmdparams_save_int (&cmdparams, "NDF0", &newstat);
00244 ndf1 = cmdparams_save_int (&cmdparams, "NDF1", &newstat);
00245 cdf = cmdparams_save_float (&cmdparams, "CDF", &newstat);
00246 ndm = cmdparams_save_int (&cmdparams, "NDM", &newstat);
00247 ndl = cmdparams_save_int (&cmdparams, "NDL", &newstat);
00248 xsafe = cmdparams_save_float (&cmdparams, "XSAFE", &newstat);
00249 xsafe1 = cmdparams_save_float (&cmdparams, "XSAFE1", &newstat);
00250 delmin = cmdparams_save_float (&cmdparams, "DELMIN", &newstat);
00251 maxchi = cmdparams_save_int (&cmdparams, "MAXCHI", &newstat);
00252 maxgrad = cmdparams_save_int (&cmdparams, "MAXGRAD", &newstat);
00253 maxcof = cmdparams_save_int (&cmdparams, "MAXCOF", &newstat);
00254 ifix = cmdparams_save_int (&cmdparams, "IFIX", &newstat);
00255 ifirst = cmdparams_save_int (&cmdparams, "IFIRST", &newstat);
00256 flipm = cmdparams_save_int (&cmdparams, "FLIPM", &newstat);
00257 nlobes = cmdparams_save_int (&cmdparams, "NLOBES", &newstat);
00258 nfour = cmdparams_save_int (&cmdparams, "NFOUR", &newstat);
00259 gongflag = cmdparams_save_int (&cmdparams, "GONGFLAG", &newstat);
00260 printfit = cmdparams_save_int (&cmdparams, "PRINTFIT", &newstat);
00261 cdetrend = cmdparams_save_float (&cmdparams, "CDETREND", &newstat);
00262 ifill = cmdparams_save_int (&cmdparams, "IFILL", &newstat);
00263 fdiff = cmdparams_save_int (&cmdparams, "FDIFF", &newstat);
00264 iasym = cmdparams_save_int (&cmdparams, "IASYM", &newstat);
00265 ict = cmdparams_save_int (&cmdparams, "ICT", &newstat);
00266 iwood = cmdparams_save_int (&cmdparams, "IWOOD", &newstat);
00267 woodb1 = cmdparams_save_float (&cmdparams, "WOODB1", &newstat);
00268 woodb2 = cmdparams_save_float (&cmdparams, "WOODB2", &newstat);
00269 ctx = cmdparams_save_int (&cmdparams, "CTX", &newstat);
00270
00271 char *inrecquery = (char *)cmdparams_save_str(&cmdparams, "in", &newstat);
00272 char *outseries = (char *)cmdparams_save_str(&cmdparams, "out", &newstat);
00273 char *segnamein = (char *)cmdparams_save_str(&cmdparams, "segin", &newstat);
00274 char *segnameout = (char *)cmdparams_save_str(&cmdparams, "segout", &newstat);
00275 int seginflag = strcmp(kNOTSPECIFIED, segnamein);
00276 int segoutflag = strcmp(kNOTSPECIFIED, segnameout);
00277 char *histlinkname = (char *)cmdparams_save_str(&cmdparams, "histlink", &newstat);
00278
00279 int verbflag = cmdparams_save_int(&cmdparams, "VERB", &newstat);
00280
00281
00282
00283
00284
00285
00286
00287
00288 if (newstat)
00289 {
00290 fprintf(stderr, "ERROR: problem with input arguments, status = %d, diagnosis follows\n", newstat);
00291 cpsave_decode_error(newstat);
00292 return 1;
00293 }
00294 else if (savestrlen != strlen(savestr))
00295 {
00296 fprintf(stderr, "ERROR: problem with savestr, savestrlen = %d, strlen(savestr) = %d\n", savestrlen, (int)strlen(savestr));
00297 return 1;
00298 }
00299
00300
00301 int histflag = strncasecmp("none", outseries, 4);
00302 if (histflag)
00303 {
00304 long long histrecnum;
00305 DRMS_Record_t *tempoutrec = drms_create_record(drms_env,
00306 outseries,
00307 DRMS_TRANSIENT,
00308 &status);
00309
00310 if (status != DRMS_SUCCESS)
00311 {
00312 fprintf(stderr,"ERROR: couldn't open a record in output dataseries %s, status = %d\n", outseries, status);
00313 return 1;
00314 }
00315
00316 DRMS_Link_t *histlink = hcon_lookup_lower(&tempoutrec->links, histlinkname);
00317 if (histlink != NULL)
00318 {
00319 histrecnum=set_history(histlink);
00320 if (histrecnum < 0)
00321 {
00322 fprintf(stderr, "ERROR: problem creating record in history dataseries for output dataseries %s\n", outseries);
00323 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00324 return 1;
00325 }
00326 printf("histrecnum=%ld\n",histrecnum);
00327 }
00328 else
00329 {
00330 fprintf(stderr,"ERROR: could not find history link in output dataseries %s\n", outseries);
00331 return 1;
00332 }
00333
00334 drms_close_record(tempoutrec, DRMS_FREE_RECORD);
00335 printf("module %s successful completion\n", cmdparams.argv[0]);
00336 return 0;
00337
00338 }
00339
00340
00341
00342
00343 if (strcmp(paramfile,"nofile") == 0)
00344 paramfile=par1file;
00345
00346 char *crosspath;
00347 char *dir, *sub, *hold;
00348 struct stat crosstat;
00349 if (fstatus=stat(crossfile, &crosstat))
00350 {
00351 DRMS_RecordSet_t *crossrecset = drms_open_records(drms_env, crossfile, &status);
00352 if (status != DRMS_SUCCESS || crossrecset == NULL)
00353 {
00354 fprintf(stderr,"ERROR: problem finding leakage matrix: file status = %d, DRMS status = %d\n",fstatus,status);
00355 return 1;
00356 }
00357 if (crossrecset->n == 0)
00358 {
00359 fprintf(stderr,"ERROR: crossfile recordset contains no records\n");
00360 return 1;
00361 }
00362 if (crossrecset->n > 1)
00363 {
00364 fprintf(stderr,"ERROR: crossfile recordset with more than 1 record not supported.\n");
00365 return 1;
00366 }
00367 DRMS_Segment_t *crosseg = drms_segment_lookupnum(crossrecset->records[0], 0);
00368 crosspath = (char *)malloc(DRMS_MAXPATHLEN+1);
00369 if (crosseg != NULL && crosspath != NULL)
00370 status=drms_record_directory(crossrecset->records[0],crosspath,0);
00371 if (crosseg == NULL || status != DRMS_SUCCESS || crosspath[0] == '\0')
00372 {
00373 fprintf(stderr, "ERROR: problem finding leakage matrix segment: status = %d\n", status);
00374 return 1;
00375 }
00376 dir=drms_getkey_string(crossrecset->records[0], "dirname", &status);
00377 sub=strtok(dir,"/");
00378 while ((hold=strtok(NULL,"/")) != NULL)
00379 sub=hold;
00380 strcat(crosspath,"/");
00381 strcat(crosspath,sub);
00382 strcat(crosspath,"/");
00383 if (verbflag)
00384 printf("crosspath = %s\n", crosspath);
00385 }
00386 else
00387 {
00388 crosspath=strdup(crossfile);
00389 }
00390
00391
00392 if (fits_open_file(&fitsptr, gapfile, READONLY, &fstatus))
00393 {
00394 DRMS_RecordSet_t *gaprecset = drms_open_records(drms_env, gapfile, &status);
00395 if (status != DRMS_SUCCESS || gaprecset == NULL)
00396 {
00397 fprintf(stderr,"ERROR: problem reading gaps: file status = %d, DRMS status = %d\n",fstatus,status);
00398 return 1;
00399 }
00400 if (gaprecset->n == 0)
00401 {
00402 fprintf(stderr,"ERROR: gapfile recordset contains no records\n");
00403 return 1;
00404 }
00405 if (gaprecset->n > 1)
00406 {
00407 fprintf(stderr,"ERROR: gapfile recordset with more than 1 record not yet supported.\n");
00408 return 1;
00409 }
00410 gapseg = drms_segment_lookupnum(gaprecset->records[0], 0);
00411 if (gapseg != NULL)
00412 gaparr = drms_segment_read(gapseg, DRMS_TYPE_FLOAT, &status);
00413 if (status != DRMS_SUCCESS || gaparr == NULL || gapseg == NULL)
00414 {
00415 fprintf(stderr, "ERROR: problem reading gaps segment: status = %d\n", status);
00416 return 1;
00417 }
00418 rgaps=(float *)(gaparr->data);
00419 gapsize=gaparr->axis[0];
00420 }
00421 else
00422 {
00423 fits_get_img_size(fitsptr, 1, fdims, &fstatus);
00424 gapsize=fdims[0];
00425 rgaps=(float *)(malloc(gapsize*sizeof(float)));
00426 fits_read_pix(fitsptr, TFLOAT, fpixel, gapsize, NULL, rgaps, anynul, &fstatus);
00427 fits_close_file(fitsptr, &fstatus);
00428 if (fstatus)
00429 {
00430 fits_report_error(stderr, fstatus);
00431 return 1;
00432 }
00433 }
00434
00435 if (verbflag)
00436 printf("gapfile read, gapsize = %d\n",gapsize);
00437
00438 igaps=(float *)(malloc(gapsize*sizeof(float)));
00439 gaps=(float *)(malloc(gapsize*sizeof(float)));
00440 gaps1=(float *)(malloc(gapsize*sizeof(float)));
00441 gaps2=(float *)(malloc(gapsize*sizeof(float)));
00442 datr=(float *)(malloc(gapsize*sizeof(float)));
00443 dati=(float *)(malloc(gapsize*sizeof(float)));
00444
00445
00446
00447 help=(float *)(malloc((4*ndt+30)*sizeof(float)));
00448 gaps3=(float *)(malloc(ndt*sizeof(float)));
00449 data3=(float *)(malloc(2*ndt*sizeof(float)));
00450
00451 isign = 1.0;
00452 if (flipm != 0)
00453 isign=-1.0;
00454 sum=0.0;
00455 for (i=0; i<gapsize; i++)
00456 {
00457 sum=sum+rgaps[i];
00458 igaps[i] = isign*rgaps[i];
00459 if (rgaps[i] == 0.0)
00460 gaps[i]=0.0;
00461 else
00462 gaps[i]=1.0;
00463 }
00464 if (verbflag)
00465 printf("%f\n",sum);
00466
00467 cffti_(&ndt, help);
00468
00469
00470 DRMS_RecordSet_t *inrecset = drms_open_records(drms_env, inrecquery, &status);
00471 if (status != DRMS_SUCCESS || inrecset == NULL)
00472 {
00473 fprintf(stderr,"ERROR: problem opening input recordset: status = %d\n",status);
00474 return 1;
00475 }
00476
00477 if (inrecset->n == 0)
00478 {
00479 fprintf(stderr,"ERROR: input recordset contains no records\n");
00480 return 1;
00481 }
00482
00483 if (inrecset->n > 1)
00484 {
00485 fprintf(stderr,"ERROR: nrecs > 1 not yet supported.\n");
00486 return 1;
00487 }
00488
00489
00490 inrec=inrecset->records[0];
00491 int itest;
00492 char *inchecklist[] = {"T_START", "T_STOP", "LMIN", "LMAX", "T_STEP"};
00493
00494 for (itest=0; itest < ARRLENGTH(inchecklist); itest++)
00495 {
00496 DRMS_Keyword_t *inkeytest = hcon_lookup_lower(&inrec->keywords, inchecklist[itest]);
00497 if (inkeytest == NULL)
00498 {
00499 fprintf(stderr, "ERROR: required input keyword %s is missing\n", inchecklist[itest]);
00500 drms_close_records(inrecset, DRMS_FREE_RECORD);
00501 return 1;
00502 }
00503 }
00504
00505 tstart=tstartsave=drms_getkey_time(inrec, "T_START", NULL);
00506 tstop =tstopsave =drms_getkey_time(inrec, "T_STOP", NULL);
00507 tstep=drms_getkey_float(inrec, "T_STEP", NULL);
00508
00509 navail=(tstop-tstart)/tstep;
00510 if (navail != gapsize)
00511 {
00512 fprintf(stderr, "ERROR: gaps seem incompatible with timeseries, gapsize=%d, navail=%d\n", gapsize, navail);
00513 return 0;
00514 }
00515
00516 nread=navail;
00517 tsample=tstep;
00518
00519 if ((ifirst+ndt) < navail)
00520 nread=ifirst+ndt;
00521
00522 if (verbflag)
00523 printf("%d %d\n",navail,nread);
00524
00525 int lmin=drms_getkey_int(inrec, "LMIN", NULL);
00526 int lmax=drms_getkey_int(inrec, "LMAX", NULL);
00527 if (lmin != lmax)
00528 {
00529 fprintf(stderr,"ERROR: lmin != lmax not yet supported.\n");
00530 return 0;
00531 }
00532 lmode=lmin;
00533
00534 char *tag = drms_getkey_string(inrec, "TAG", &status);
00535 if (status != DRMS_SUCCESS)
00536 tag=strdup("none");
00537 if (readfft == 0)
00538 {
00539 ffttmprec = drms_create_record(drms_env, ffttmp, DRMS_PERMANENT, &status);
00540
00541 if (status != DRMS_SUCCESS)
00542 {
00543 fprintf(stderr,"ERROR: couldn't create a record in ffttmp dataseries %s, status = %d\n", ffttmp, status);
00544 return 0;
00545 }
00546
00547 drms_copykeys(ffttmprec, inrec, 0, kDRMS_KeyClass_Explicit);
00548 drms_setkey_int(ffttmprec, "LMODE", lmode);
00549 drms_setkey_int(ffttmprec, "NDT", ndt);
00550 tnow = (double)time(NULL);
00551 tnow += UNIX_epoch;
00552 drms_setkey_time(ffttmprec, "DATE", tnow);
00553
00554 ffttmpseg = drms_segment_lookupnum(ffttmprec, 0);
00555
00556 sprintf(fftfile, "fft%d", lmode);
00557 ffttmpfile = drms_segment_fopen(ffttmpseg, fftfile, 0, &status);
00558 if (status != DRMS_SUCCESS)
00559 {
00560 fprintf(stderr,"ERROR: couldn't open a file to write ffttmp record, status = %d\n", status);
00561 return 0;
00562 }
00563
00564 drms_segment_filename(ffttmpseg,fftfile);
00565
00566 }
00567 else
00568 {
00569 char *ffttmpquery = malloc(100);
00570 sprint_time(tstartstr, tstart, "TAI", 0);
00571 sprintf(ffttmpquery, "%s[%s][%d][%d][%s]", ffttmp,tstartstr,lmode,ndt,tag);
00572
00573 ffttmprecset = drms_open_records(drms_env, ffttmpquery, &status);
00574 if (status != DRMS_SUCCESS || ffttmprecset == NULL || ffttmprecset->n == 0)
00575 {
00576 fprintf(stderr,"ERROR: couldn't open ffttmp record %s, status = %d\n", ffttmpquery, status);
00577 return 0;
00578 }
00579
00580 ffttmprec = ffttmprecset->records[0];
00581 ffttmpseg = drms_segment_lookupnum(ffttmprec, 0);
00582
00583 drms_segment_filename(ffttmpseg,fftfile);
00584
00585 }
00586
00587 if (verbflag)
00588 printf("READFFT=%d,fftfull=%s\n",readfft,fftfile);
00589
00590
00591
00592
00593 if (readfft == 0)
00594 {
00595 if (seginflag)
00596 segin = drms_segment_lookup(inrec, segnamein);
00597 else
00598 segin = drms_segment_lookupnum(inrec, 0);
00599 if (segin)
00600 inarr = drms_segment_read(segin, usetype, &status);
00601 if (status != DRMS_SUCCESS || inarr == NULL || segin == NULL)
00602 {
00603 fprintf(stderr, "problem reading input segment: status = %d\n", status);
00604 return 0;
00605 }
00606
00607 datarr=(float *)(inarr->data);
00608
00609 for (m=0; m<= lmode; m++)
00610 {
00611
00612
00613
00614 data=datarr + m*2*gapsize;
00615
00616 for (j=0; j<navail; j++)
00617 {
00618 if (isnan(data[2*j]))
00619 datr[j]=0.0;
00620 else
00621 datr[j]=rgaps[j]*data[2*j];
00622 if (isnan(data[2*j+1]))
00623 dati[j]=0.0;
00624 else
00625 dati[j]=igaps[j]*data[2*j+1];
00626 }
00627
00628 if (cdetrend != 0.0)
00629 {
00630 detrend(datr,gaps,navail,cdetrend);
00631 detrend(dati,gaps,navail,cdetrend);
00632 }
00633
00634 if (ifill == 0)
00635 {
00636 for (i=0; i<navail; i++)
00637 {
00638 gaps1[i]=gaps[i];
00639 }
00640 }
00641 else
00642 {
00643 fill_gaps(datr,gaps,gaps1,navail);
00644 if (m !=0 )
00645 {
00646 fill_gaps(dati,gaps,gaps1,navail);
00647 }
00648 }
00649
00650 if (fdiff!=0)
00651 {
00652 for (j=0; j<(navail-1); j++)
00653 {
00654 if ((gaps1[j]==1) & (gaps1[j+1]==1))
00655 {
00656 gaps2[j]=1.0;
00657 datr[j]=datr[j+1]-datr[j];
00658 dati[j]=dati[j+1]-dati[j];
00659 }
00660 else
00661 {
00662 gaps2[j]=0.0;
00663 datr[j]=0.0;
00664 dati[j]=0.0;
00665 }
00666 }
00667 gaps2[navail-1]=0.0;
00668 datr[navail-1]=0.0;
00669 dati[navail-1]=0.0;
00670 }
00671 else
00672 {
00673 for (j=0; j<navail; j++)
00674 {
00675 gaps2[j]=gaps1[j];
00676 }
00677 }
00678
00679
00680
00681 nx1=ndt;
00682 if (nread < (ifirst+ndt))
00683 nx1=nread-ifirst;
00684
00685 for (j=0; j<nx1; j++)
00686 {
00687 gaps3[j]=gaps2[j+ifirst];
00688 data3[2*j]=datr[j+ifirst];
00689 data3[2*j+1]=dati[j+ifirst];
00690 }
00691 for (j = nx1; j < ndt; j++)
00692 {
00693 gaps3[j] = 0;
00694 data3[2*j] = 0;
00695 data3[2*j+1] = 0;
00696 }
00697
00698 if ((ifix == 1) && (m == 0))
00699 {
00700 c = sqrt(2.0);
00701 for (j = 0; j < ndt; j++)
00702 {
00703 data3[2*j] *= c;
00704 data3[2*j+1] *= c;
00705 }
00706 }
00707
00708 cfftb_ (&ndt, data3, help);
00709
00710 fwrite ( (char*)data3, 8*ndt, 1, ffttmpfile);
00711
00712
00713 }
00714
00715
00716 fwrite ( (char*)data3, 8*ndt, 1, ffttmpfile);
00717 drms_segment_fclose(ffttmpfile);
00718 if (verbflag)
00719 printf("fft file written\n");
00720 }
00721 else
00722 {
00723
00724 for (j=0; j<navail; j++)
00725 {
00726 datr[j]=j;
00727 }
00728 if (cdetrend != 0.0)
00729 {
00730 detrend(datr,gaps,navail,cdetrend);
00731 }
00732 if (ifill == 0)
00733 {
00734 for (i=0; i<navail; i++)
00735 {
00736 gaps1[i]=gaps[i];
00737 }
00738 }
00739 else
00740 {
00741 fill_gaps(datr,gaps,gaps1,navail);
00742 }
00743 if (fdiff!=0)
00744 {
00745 for (j=0; j<(navail-1); j++)
00746 {
00747 if ((gaps1[j]==1) & (gaps1[j+1]==1))
00748 {
00749 gaps2[j]=1.0;
00750 }
00751 else
00752 {
00753 gaps2[j]=0.0;
00754 }
00755 }
00756 gaps2[navail-1]=0.0;
00757 }
00758 else
00759 {
00760 for (j=0; j<navail; j++)
00761 {
00762 gaps2[j]=gaps1[j];
00763 }
00764 }
00765 nx1=ndt;
00766 if (nread < (ifirst+ndt))
00767 nx1=nread-ifirst;
00768 for (j=0; j<nx1; j++)
00769 {
00770 gaps3[j]=gaps2[j+ifirst];
00771 }
00772 for (j = nx1; j < ndt; j++)
00773 {
00774 gaps3[j] = 0;
00775 }
00776 }
00777
00778 drms_free_array(inarr);
00779 if (inrecset)
00780 {
00781 drms_close_records(inrecset, DRMS_FREE_RECORD);
00782 }
00783 if (verbflag)
00784 printf("input recordset closed\n");
00785
00786 i = -1;
00787 for ( ptr = noibstr; (ptr = strtok(ptr, " ")) != NULL; ptr = NULL )
00788 noib[++i] = atoi(ptr);
00789
00790 i = -1;
00791 for ( ptr = noimstr; (ptr = strtok(ptr, " ")) != NULL; ptr = NULL )
00792 noim[++i] = atoi(ptr);
00793
00794 loglen = strlen(logfile);
00795 fftlen = strlen(fftfile);
00796 paramlen = strlen(paramfile);
00797 par1len = strlen(par1file);
00798 reslen = strlen(resfile);
00799 crosslen = strlen(crosspath);
00800 crossnlen = strlen(crossnfile);
00801 printf("crosspath=%s, crosslen=%d\n",crosspath,crosslen);
00802 printf("ndl = %d\n",ndl);
00803
00804 sub24_(logfile,fftfile, gaps3, &ipar,
00805 paramfile, par1file, resfile,
00806 crosspath, &ndeltal, &noisetype, crossnfile, &nnoib, noib, &nnoim, noim,
00807 &csubm, &ifollow, &anoise, &aother, &lmode, &iodd, &iasym, &ict, &ctx,
00808 &iwood, &woodb1, &woodb2, &nmin, &nmax, &fmin, &fmax,
00809 &imfreq, &icase, &nacoeff, &zero, &ndt, &tsample, &idf, &ndf0, &ndf1,
00810 &cdf, &ndm, &ndl,
00811 &xsafe, &xsafe1, &delmin, &maxchi, &maxgrad, &maxcof, ®sz,
00812 &nlobes, &nfour,
00813 &gongflag, &printfit,
00814 loglen, fftlen, paramlen, par1len, reslen, crosslen, crossnlen);
00815
00816 if (readfft == 0)
00817 drms_close_record(ffttmprec,DRMS_INSERT_RECORD);
00818 else
00819 drms_close_records(ffttmprecset,DRMS_FREE_RECORD);
00820
00821 printf("module %s successful completion\n", cmdparams.argv[0]);
00822 return 0;
00823 }
00824
00825
00826 void detrend(
00827 float *data,
00828 float *gaps,
00829 int length,
00830 float cdetrend
00831 )
00832 {
00833 #define chunksz 1440
00834 #define maxpols 50
00835
00836 extern float sdot_(int *, float *, int *, float *, int *);
00837 extern float saxpy_(int *, float *, float *, int *, float *, int *);
00838 extern float sscal_(int *, float *, float *, int *);
00839
00840 int i,j,k,l,n_good,ndegree;
00841 int goodlist[chunksz];
00842 float pols[maxpols][chunksz];
00843 float a,cg,rms2,sum,x[chunksz];
00844 float *help;
00845 int one=1;
00846
00847 for (i=0; i<length ; i=i+chunksz) {
00848 help=data+i;
00849 n_good=0;
00850 cg=0.0;
00851 for (j=i; j<i+chunksz; j++) {
00852 if (gaps[j] != 0) {
00853 goodlist[n_good]=j-i;
00854 cg=cg+j-i;
00855 n_good++;
00856 }
00857 }
00858 if (n_good != 0) {
00859 cg=cg/n_good;
00860 rms2=0.0;
00861 for (j=0; j<n_good; j++) {rms2=rms2+(goodlist[j]-cg)*(goodlist[j]-cg);}
00862 ndegree = floor((goodlist[n_good-1]-goodlist[0])/cdetrend)+2;
00863 for (k=0; k<n_good; k++) {
00864 x[k]=(goodlist[k]-cg)/sqrt(rms2);
00865 }
00866 for (k=0; k<n_good; k++) {pols[0][k]=1.0/sqrt(n_good);}
00867 for (k=0; k<n_good; k++) {pols[1][k]=x[k];}
00868 for (j=2; j<ndegree; j++) {
00869 for (k=0; k<n_good; k++) {
00870 pols[j][k]=(2.0*j)/j*x[k]*pols[j-1][k]-(j-1.0)/j*pols[j-2][k];
00871 }
00872 for (l=0; l<j; l++) {
00873 a=-sdot_(&n_good,&pols[j][0],&one,&pols[l][0],&one);
00874 saxpy_(&n_good,&a,&pols[l][0],&one,&pols[j][0],&one);
00875 }
00876 a=1.0/sqrt(sdot_(&n_good,&pols[j][0],&one,&pols[l][0],&one));
00877 sscal_(&n_good,&a,&pols[j][0],&one);
00878 }
00879 for (j=0; j<ndegree; j++) {
00880 sum=0.0;
00881 for (k=0; k<n_good; k++) {
00882 sum=sum+pols[j][k]*help[goodlist[k]];
00883 }
00884 for (k=0; k<n_good; k++) {
00885 help[goodlist[k]] -= sum*pols[j][k];
00886 }
00887 }
00888 }
00889
00890 }
00891
00892 }
00893
00894 void memcof(float *, int, int, float *, float *);
00895 void fixrts(float *, int);
00896 void predic(float *, int, float *, int, float *, int);
00897
00898 void fill_gaps(
00899 float *data,
00900 float *gaps,
00901 float *newgaps,
00902 int length
00903 )
00904 {
00905 #include "nr.h"
00906 #include "nrutil.h"
00907 #define chunksz 1440
00908 #define maxpoles 10
00909 #define maxgap 5
00910
00911 int i,j,k;
00912 int n_good, gap_start, gap_end, gap_size, type;
00913 float *help,good_points[chunksz];
00914 int goodlist[chunksz];
00915 int npoles;
00916 float pm,cof[maxpoles];
00917 float input[maxpoles];
00918 float f_predic[maxgap],b_predic[maxgap];
00919
00920 npoles=6;
00921 for (i=0; i<length ; i=i+chunksz) {
00922 help=data+i;
00923 n_good=0;
00924 for (j=i; j<i+chunksz; j++) {
00925 if (gaps[j] != 0) {
00926 goodlist[n_good]=j-i;
00927 good_points[n_good]=data[j];
00928 n_good++;
00929 newgaps[j]=gaps[j];
00930 }
00931 }
00932 if (n_good != 0) {
00933 memcof(good_points-1,n_good,npoles,&pm,cof-1);
00934 fixrts(cof-1,npoles);
00935 for (j=1; j<n_good; j++) {
00936 if ((goodlist[j]-goodlist[j-1]) > 1) {
00937 gap_start=goodlist[j-1]+1;
00938 gap_end=goodlist[j]-1;
00939 gap_size=gap_end-gap_start+1;
00940 if (gap_size <= maxgap) {
00941 type=0;
00942
00943
00944 if ((j >= npoles) && (goodlist[j-npoles] == goodlist[j-1]-npoles+1)) {
00945 for (k=0; k<npoles; k++) input[k]=help[gap_start-npoles+k];
00946 predic(input-1,npoles,cof-1,npoles,f_predic-1,gap_size);
00947 type +=1;
00948 }
00949
00950
00951 if ((j+npoles <= n_good) && (goodlist[j+npoles-1] == goodlist[j]+npoles-1)) {
00952 for (k=0; k<npoles; k++) input[k]=help[gap_end+npoles-k];
00953
00954 predic(input-1,npoles,cof-1,npoles,b_predic-1,gap_size);
00955 type +=2;
00956 }
00957 if (type == 3) {
00958
00959 for (k=0; k<gap_size; k++)
00960 help[gap_start+k] = (f_predic[k]+b_predic[gap_size-k-1])/2.;
00961 }
00962 if (type == 1) {
00963
00964 for (k=0; k<gap_size; k++)
00965 help[gap_start+k] = f_predic[k];
00966 }
00967 if (type == 2) {
00968
00969 for (k=0; k<gap_size; k++)
00970 help[gap_start+k] = b_predic[gap_size-k-1];
00971 }
00972 if (type !=0 ) {
00973 for (k=0; k<gap_size; k++)
00974 newgaps[gap_start+k+i]=1;
00975 }
00976 }
00977 }
00978 }
00979 }
00980 }
00981
00982 }