int ChebyModelSet_Insert(ChebyModelSet *cms, const ChebyModelSet *from) { int old_nseg = cms->nsegments; int iseg; cms->nsegments += from->nsegments; cms->segments = (ChebyModel *) realloc (cms->segments, cms->nsegments*sizeof(ChebyModel)); for (iseg=old_nseg; iseg < cms->nsegments ; iseg++) { ChebyModel_Init(&cms->segments[iseg], 0, 0); ChebyModel_Copy(&cms->segments[iseg], &from->segments[iseg-old_nseg]); } }
int ChebyModel_Read(ChebyModel *cm, FILE *f) { int done = 0; int first = 1; char line[1024], keyword[64], arg[64], junk[1024]; int nx=-1, ny=-1, ix=0, iy; int ichar, nread; cm->cheby.coeff=NULL; char str1[128]; char str2[128]; do { if (fgets(line, 1024, f)!=line) return -1; if (sscanf(line, "%s", keyword)!=1) continue; // skip blank lines if (sscanf(line, "%s %s", keyword, arg)!=2) return -2; if (line[0]=='#') continue; // skip comment lines // check first line if (first && (strcasecmp(keyword, "ChebyModel")||strcasecmp(arg, "BEGIN"))) return -3; // parse based on keyword if (!strcasecmp(keyword, "PSRNAME")) strcpy(cm->psrname, arg); else if (!strcasecmp(keyword, "SITENAME")) strcpy(cm->sitename, arg); else if (!strcasecmp(keyword, "TIME_RANGE")) { if (sscanf(line, "%*s %s %s", str1,str2)!=2) return -4; cm->mjd_start = (long double)parse_ld_cheby(str1); cm->mjd_end = (long double)parse_ld_cheby(str2); } else if (!strcasecmp(keyword, "FREQ_RANGE")) { if (sscanf(line, "%*s %s %s", str1, str2)!=2) return -5; cm->freq_start = (long double)parse_ld_cheby(str1); cm->freq_end = (long double)parse_ld_cheby(str2); } else if (!strcasecmp(keyword, "DISPERSION_CONSTANT")) { if (sscanf(arg, "%s", str1)!=1) return -6; cm->dispersion_constant = (long double)parse_ld_cheby(str1); } else if (!strcasecmp(keyword, "NCOEFF_TIME")) { if (sscanf(arg, "%d", &nx)!=1) return -7; } else if (!strcasecmp(keyword, "NCOEFF_FREQ")) { if (sscanf(arg, "%d", &ny)!=1) return -8; } else if (!strcasecmp(keyword, "COEFFS")) { if (cm->cheby.coeff==NULL) // first instance of COEFF keyword { if (nx < 0 && ny < 0) // oops, these should come first! return -8; ChebyModel_Init(cm, nx, ny); } if (ix >= nx) return -9; // too many coefficient lines!! sscanf(line, "%*s %n", &ichar); if (ny<4) // All on one line { for (iy=0; iy < cm->cheby.ny; iy++) { if (sscanf(line+ichar, "%s %n",str1, &nread)!=1) return -10; cm->cheby.coeff[iy*cm->cheby.nx+ix] = (long double)parse_ld_cheby(str1); ichar += nread; } } else // Code added by G. Hobbs for multiple lines in the predictor file { for (iy=0; iy < cm->cheby.ny; iy++) { if (sscanf(line+ichar, "%s %n",str1, &nread)!=1) return -10; cm->cheby.coeff[iy*cm->cheby.nx+ix] = (long double)parse_ld_cheby(str1); ichar += nread; if ((iy+1)%3==0) { if (sscanf(line+ichar, "%s", junk)==1) return -11; // excess stuff at end of line ichar = 0; if (fgets(line, 1024, f)!=line) return -1; } } } if (sscanf(line+ichar, "%s", junk)==1) return -11; // excess stuff at end of line ix++; } else if (!strcasecmp(keyword, "ChebyModel")) { if ((!first) && !strcasecmp(arg, "BEGIN")) return -12; else if (!strcasecmp(arg, "END")) { if (cm->cheby.coeff==NULL || ix!=nx) return -13; // haven't read enough coefficients yet!! else { Cheby2D_Construct_x_Derivative(&cm->frequency_cheby, &cm->cheby); done = 1; } } } else return -14; // unrecognized keyword!! first = 0; } while (!done); return 0; }