static int y_Xb_at_lag (char *spec, arma_info *ainfo, int narmax, int lag) { char chunk[32]; int i, nt; int err = 0; if (narmax == 0) { sprintf(chunk, "y_%d", lag); return add_to_spec(spec, chunk); } nt = ainfo->ifc + narmax; sprintf(chunk, "(y_%d-", lag); if (nt > 1) { strcat(chunk, "("); } if (ainfo->ifc) { strcat(chunk, "_b0"); } err = add_to_spec(spec, chunk); for (i=0; i<narmax && !err; i++) { if (ainfo->ifc || i > 0) { err += add_to_spec(spec, "+"); } sprintf(chunk, "_b%d*x%d_%d", i+1, i+1, lag); err += add_to_spec(spec, chunk); } if (nt > 1) { err += add_to_spec(spec, "))"); } else { err += add_to_spec(spec, ")"); } return err; }
static int arma_get_nls_model (MODEL *amod, arma_info *ainfo, int narmax, const double *coeff, DATASET *dset, PRN *prn) { gretlopt nlsopt = OPT_A; char fnstr[MAXLINE]; char term[32]; nlspec *spec; double *parms = NULL; char **pnames = NULL; double *b0 = NULL, *by1 = NULL; int nparam, lag; int i, j, k, err = 0; spec = nlspec_new(NLS, dset); if (spec == NULL) { return E_ALLOC; } if (arma_least_squares(ainfo)) { /* respect verbose option */ if (prn != NULL) { nlsopt |= OPT_V; } } else { #if AINIT_DEBUG nlsopt |= OPT_V; #else /* don't bother with standard errors */ nlsopt |= OPT_C; #endif } nlspec_set_t1_t2(spec, 0, ainfo->T - 1); nparam = ainfo->ifc + ainfo->np + ainfo->P + ainfo->nexo; if (ainfo->misslist != NULL) { nparam += ainfo->misslist[0]; } parms = malloc(nparam * sizeof *parms); if (parms == NULL) { err = E_ALLOC; goto bailout; } pnames = strings_array_new_with_length(nparam, VNAMELEN); if (pnames == NULL) { err = E_ALLOC; goto bailout; } /* make names for the parameters; construct the param list; and do some rudimentary fall-back initialization */ for (i=0; i<nparam; i++) { parms[i] = 0.0; } k = 0; if (ainfo->ifc) { if (coeff != NULL) { parms[k] = coeff[k]; } else { parms[k] = gretl_mean(0, dset->n - 1, dset->Z[1]); } b0 = &parms[k]; strcpy(pnames[k++], "_b0"); } for (i=0; i<ainfo->p; i++) { if (AR_included(ainfo, i)) { if (by1 == NULL) { by1 = &parms[k]; if (coeff == NULL) { parms[k] = 0.1; } } if (coeff != NULL) { parms[k] = coeff[k]; } sprintf(pnames[k++], "_phi%d", i+1); } } for (i=0; i<ainfo->P; i++) { if (by1 == NULL) { by1 = &parms[k]; if (coeff == NULL) { parms[k] = 0.1; } } if (coeff != NULL) { parms[k] = coeff[k]; } sprintf(pnames[k++], "_Phi%d", i+1); } for (i=0; i<ainfo->nexo; i++) { if (coeff != NULL) { parms[k] = coeff[k]; } sprintf(pnames[k++], "_b%d", i+1); } if (ainfo->misslist != NULL) { for (i=1; i<=ainfo->misslist[0]; i++) { j = ainfo->misslist[i]; parms[k] = dset->Z[1][j]; sprintf(pnames[k++], "_c%d", i); } } /* construct NLS specification string */ strcpy(fnstr, "y="); if (ainfo->ifc) { strcat(fnstr, "_b0"); } else { strcat(fnstr, "0"); } for (i=0; i<ainfo->p && !err; i++) { if (AR_included(ainfo, i)) { lag = i + 1; sprintf(term, "+_phi%d*", lag); err = add_to_spec(fnstr, term); if (!err) { err = y_Xb_at_lag(fnstr, ainfo, narmax, lag); } } } for (j=0; j<ainfo->P && !err; j++) { sprintf(term, "+_Phi%d*", j+1); strcat(fnstr, term); lag = (j + 1) * ainfo->pd; y_Xb_at_lag(fnstr, ainfo, narmax, lag); for (i=0; i<ainfo->p; i++) { if (AR_included(ainfo, i)) { sprintf(term, "-_phi%d*_Phi%d*", i+1, j+1); err = add_to_spec(fnstr, term); if (!err) { lag = (j+1) * ainfo->pd + (i+1); y_Xb_at_lag(fnstr, ainfo, narmax, lag); } } } } for (i=0; i<ainfo->nexo && !err; i++) { sprintf(term, "+_b%d*x%d", i+1, i+1); err = add_to_spec(fnstr, term); } if (!err && ainfo->misslist != NULL) { for (i=1; i<=ainfo->misslist[0]; i++) { sprintf(term, "+_c%d*d%d", i, i); err = add_to_spec(fnstr, term); } } if (!err) { if (coeff == NULL) { nls_kickstart(amod, dset, b0, by1); } #if AINIT_DEBUG fprintf(stderr, "initting using NLS spec:\n %s\n", fnstr); for (i=0; i<nparam; i++) { fprintf(stderr, "initial NLS b[%d] = %g (%s)\n", i, parms[i], pnames[i]); } #endif err = nlspec_set_regression_function(spec, fnstr, dset); } if (!err) { set_auxiliary_scalars(); err = aux_nlspec_add_param_list(spec, nparam, parms, pnames); if (!err) { *amod = model_from_nlspec(spec, dset, nlsopt, prn); err = amod->errcode; #if AINIT_DEBUG if (!err) { printmodel(amod, dset, OPT_NONE, prn); } #endif } unset_auxiliary_scalars(); } bailout: nlspec_destroy(spec); free(parms); strings_array_free(pnames, nparam); return err; }
bool FitsInput::read_fits_header (void) { std::string fits_header (HEADER_SIZE, 0); // we read whole header at once if (fread (&fits_header[0], 1, HEADER_SIZE, m_fd) != HEADER_SIZE) { if (feof (m_fd)) error ("Hit end of file unexpectedly"); else error ("read error"); return false; // Read failed } for (int i = 0; i < CARDS_PER_HEADER; ++i) { std::string card (CARD_SIZE, 0); // reading card number i memcpy (&card[0], &fits_header[i*CARD_SIZE], CARD_SIZE); std::string keyname, value; fits_pvt::unpack_card (card, keyname, value); // END means that this is end of the FITS header // we can now add to the ImageSpec COMMENT, HISTORY and HIERARCH keys if (keyname == "END") { // removing white spaces that we use to separate lines of comments // from the end ot the string m_comment = m_comment.substr (0, m_comment.size() - m_sep.size ()); m_history = m_history.substr (0, m_history.size() - m_sep.size ()); m_hierarch = m_hierarch.substr (0, m_hierarch.size() - m_sep.size ()); add_to_spec ("Comment", m_comment); add_to_spec ("History", m_history); add_to_spec ("Hierarch", m_hierarch); return true; } if (keyname == "SIMPLE" || keyname == "XTENSION") continue; // setting up some important fields // m_bitpix - format of the data (eg. bpp) // m_naxes - number of axes // width, height and depth of the image if (keyname == "BITPIX") { m_bitpix = atoi (&card[10]); continue; } if (keyname == "NAXIS") { m_naxes = atoi (&card[10]); continue; } if (keyname == "NAXIS1") { m_spec.width = atoi (&card[10]); m_spec.full_width = m_spec.width; continue; } if (keyname == "NAXIS2") { m_spec.height = atoi (&card[10]); m_spec.full_height = m_spec.height; continue; } // ignoring other axis if (keyname.substr (0,5) == "NAXIS") { continue; } if (keyname == "ORIENTAT") { add_to_spec ("Orientation", value); continue; } if (keyname == "DATE") { add_to_spec ("DateTime", convert_date (value)); continue; } if (keyname == "COMMENT") { m_comment += (value + m_sep); continue; } if (keyname == "HISTORY") { m_history += (value + m_sep); continue; } if (keyname == "HIERARCH") { m_hierarch += (value + m_sep); continue; } add_to_spec (pystring::capitalize(keyname), value); } // if we didn't found END keyword in current header, we read next one return read_fits_header (); }