Example #1
0
File: preader.c Project: CKehl/nn-c
static point* reader_getpoint(reader* r)
{
    char buf[BUFSIZE];
    char seps[] = " ,;\t";
    char* token;
    point* p = &r->p;

    if (r->f == NULL)
        return NULL;

    while (1) {
        if (fgets(buf, BUFSIZE, r->f) == NULL) {
            if (r->f != stdin)
                if (fclose(r->f) != 0)
                    nn_quit("%s: %s\n", r->fname, strerror(errno));
            r->f = NULL;

            return NULL;
        }

        if (buf[0] == '#')
            continue;
        if ((token = strtok(buf, seps)) == NULL)
            continue;
        if (!str2double(token, &p->x))
            continue;
        if ((token = strtok(NULL, seps)) == NULL)
            continue;
        if (!str2double(token, &p->y))
            continue;
        r->n++;

        return p;
    }
}
Example #2
0
string_map GSQAligner::get_aligned_contigs(const double& min_score, const double& min_coverage, const string& all_contig_file, const string& hit_contig_file, const string& alignment_file){
	ifstream old_contig_fs(all_contig_file.c_str());
	ifstream alignment_fs(alignment_file.c_str());
	ofstream new_contig_fs(hit_contig_file.c_str());
	string line;
	vector<string> contig_list;
	logger->debug("Finding the aligned contigs");
	num_matches = 0;
	output_string = "<B>Contig\tStrand\tQuery\tScore\tLength\tCov\tG/P/C</B>\n";
	output_string = "<B>" + string_format("%-15s %-8s %-30s %-10s %-15s %-15s %-15s","Contig","Strand","Query","Score","Length","Coverage","G/P/C") + "</B>\n";
	output_string += "----------------------------------------------------------------------------------------------------------\n";
	while (getline(alignment_fs, line)) {
		if (line.substr(0,5) == "MATCH"){
			vector<string> tokens;
			tokenize(line, tokens, "\t");
			string contig_id = tokens[1].substr(0, tokens[1].length()-1);
			contig_list.push_back(contig_id);
			string contig = tokens[1].substr(0,tokens[1].length()-1);
			string strand = tokens[1].substr(tokens[1].length()-1,1);
			string query = tokens[2];
			string score = tokens[3];
			string length = tokens[4];
			string cov = tokens[5];
			string type = tokens[6];
			output_string += string_format("%-15s %-8s %-30s %-10s %-15s %-15s %-15s",contig.c_str(),strand.c_str(),query.c_str(),score.c_str(),length.c_str(),cov.c_str(),type.c_str()) + "\n";
			num_matches++;
			output_string += "\n";
			if (type == "P" || type == "C"){
				if (str2double(cov) > min_coverage && str2double(score) > min_score)
					aligned_query_list[query] = contig_id;
			}
		}
	}
	output_string += "\nLength: cumulative length of scored exons\nCov G/P/C: coverage of contig (G) or cDNA (C) or protein (P), whichever is highest";
	alignment_fs.close();

	while (getline(old_contig_fs, line)) {
		if (line.substr(0,1) == ">"){
			vector<string> tokens;
			tokenize(line, tokens, " ");
			string contig_id = tokens[0].substr(1, tokens[0].length()-1);
			if (std::find(contig_list.begin(), contig_list.end(), contig_id)!=contig_list.end()){
				new_contig_fs << line << endl;
				getline(old_contig_fs, line);
				new_contig_fs << line << endl;
			}
		}
	}
	old_contig_fs.close();
	new_contig_fs.close();
	return aligned_query_list;
}
Example #3
0
void PopsDat::parse( const std::string& _Line )
{
	std::vector<std::string> parts = chopstr( _Line, TOKEN_WHITESPACE.c_str() );
	if( parts.size() != 5 ) throw ParseException("POPS: Atom set initial line contains the wrong number of elements");
	if( 
		0 != str2int( parts[0], expectedAtomTypeCount ) ||
		0 != str2double( parts[1], b12 ) ||
		0 != str2double( parts[2], b13 ) ||
		0 != str2double( parts[3], b14 ) ||
		0 != str2double( parts[4], bOther )
		) throw ParseException("POPS: Invalid value in atom set initial line");
	return;
}
Example #4
0
void MainWindow::on_push_run_clicked()
{
    ui->push_run->setEnabled(true);

    //Run MonteCarlo Simulation
    ui->timeBar->setMaximum(ui->t_max->text().toDouble()/Simu->dt);
    Simu->simuMonteCarlo(ui->t_max->text().toDouble()/Simu->dt+1);

    //Rune SIVIA
    //Loading parameters
    QScriptEngine e;
    double _box[4][2];
    _box[0][0] = (ui->x_min->text()).toDouble();
    _box[0][1] = (ui->x_max->text()).toDouble();
    _box[1][0] = (ui->y_min->text()).toDouble();
    _box[1][1] = (ui->y_max->text()).toDouble();
    _box[2][0] = str2double(ui->th_min->text());
    _box[2][1] = str2double(ui->th_max->text());
    _box[3][0] = (ui->t_min->text()).toDouble();
    _box[3][1] = (ui->t_max->text()).toDouble();

    if(sivia!=NULL) delete sivia;

    IntervalVector box(4, _box);
    //cout << box;

    QTime t;
    QString info;


    t.start();
    sivia = new Sivia(*R,ui->epsilonBox->value(), box);
    double tsiv = t.elapsed()/1000.0;

    info += "Elapsed time: " + QString::number(tsiv) + " s\n";

    info += "\nInitial box:\n\tx:\t[" + QString::number(_box[0][0]) + "," + QString::number(_box[0][1]) + "]\n";
    info += "\ty:\t[" + QString::number(_box[1][0]) + "," + QString::number(_box[1][1]) + "]\n";
    info += "\ttheta:\t[" + QString::number(_box[2][0]) + "," + QString::number(_box[2][1]) + "]\n";
    info += "\tt:\t[" + QString::number(_box[3][0]) + "," + QString::number(_box[3][1]) + "]\n";
    info += "\tepsilon: " + QString::number(ui->epsilonBox->value()) + "\n";

    info += "\nNumber of boxes: " + QString::number(sivia->Sout.size()+sivia->Sp.size());
    info += "\nUnsafe boxes: " + QString::number(sivia->Sp.size());

    QMessageBox::information(this,"Sivia report",info);

    drawAll();
}
Example #5
0
	void RotLibConvert_Dunbrack_BBInd::readDefinition( StringBuilder& sb, RotamerLibrary& _RotLib ) const
	{
		ASSERT( sb.size() >= 67, ParseException, "String too short to parse!!");

		StringBuilder resname(3);
		resname.setTo( sb, 0, 3 );
		sb.TruncateLeftBy(18);

		int pdbCount = sb.parseInt(0,7);
		if( pdbCount == 0 )
			return;
		double probability = sb.parseDouble(7,8);
		sb.TruncateLeftBy(37);
		
		std::vector<double> importChis;
		std::vector<std::string> parts = sb.tokenise();
		ASSERT( (parts.size() % 2 == 0), ParseException, "Unexpected element count");

		for( size_t i = 0; i < parts.size(); i+=2 )
		{
			double chi;
			ASSERT( 0 == str2double( parts[i], chi ), ParseException, "Error parsing chi def");
			importChis.push_back(Maths::DegToRad(chi));
		}

		ASSERT( importChis.size() != 0, ParseException, "No chis found!!");
		_RotLib.addRotamer( resname.toString(), importChis, ConstantProbability( probability ) );
	}
owner_file_t * loading_owner_file_solution(const int *num_solutions_r, 	const int *numobj_r, const char *path_file_name){
	FILE * solution_file;	
	char *line;
	char *line_splited;
	int sol;
	owner_file_t * solutions_aux;

	line = Malloc(char, MAX_LINE_SOLUTION_FILE);

	//Alocating Solution
	solutions_aux = allocate_file_t(num_solutions_r, numobj_r);

	//Reading file and set values of objective
	sol = -1;
	solution_file = open_file(path_file_name, fREAD);
	//Removing first line that is collumn
	fgets(line,MAX_LINE_SOLUTION_FILE,solution_file);
	while ( fgets(line,MAX_LINE_SOLUTION_FILE,solution_file) != NULL){
		sol = sol + 1;
		//Obtaing index collumn
		line_splited = strtok (line,"\t");
		strcpy(solutions_aux[sol].file_name, line_splited);
		//Setting number of objectives		
		for (int ob = 0; ob < *numobj_r; ob++){
			line_splited = strtok (NULL,"\t");
			trim(line_splited);
			solutions_aux[sol].obj_values[ob] = str2double(line_splited);
		}					
	}	
	fclose(solution_file);

	free(line);

	return solutions_aux;
}
Example #7
0
void read_constraint_mod(Driver* driver, string line, char tag){//w/grb/
    double lb = LB, ub = UB;
    strvec toks;
    split(&line, &toks, DELIM);
    int ntoks = toks.size();
    if (ntoks < 1) throw runtime_error(line);
    string tok0 = toks[0];
    if ((ntoks) > 1){
            lb = str2double(toks[1]);
            ub = lb;
    }
    if ((ntoks > 2)) ub = str2double(toks[2]);
    strvec reacs=driver->model->get_reactions();
    switch(tag){
        case PLUSTAG:
            read_sto_mod(driver, tok0, lb, ub);
            break;
        case MINUSTAG:
            driver->model->del_element(tok0.substr(1));
            break;
        case ALLTAG:{
            for(int i=0;i<reacs.size();i++){
                string Rn=reacs[i];
                Reaction* reac=driver->model->get_reac(tok0);
                reac->set_bounds(lb, ub);
            }
            break;
        }
//        case CTAG:
//            if (driver->thermo)
//                ((ThermoLP*)driver->lp)->setConcentration(tok0, lb, ub);
//            break;
//        case DG0TAG:
//            if (driver->thermo)
//                ((ThermoLP*)driver->lp)->setDeltaG0(tok0.substr(1), lb, ub);
//            break;
//        case DGTAG:
//            tok0 = DELTAG_TAG + tok0.substr(1);
        default:
            if(find(reacs.begin(),reacs.end(),tok0)==reacs.end())
                throw runtime_error(string("no column ") + tok0);
            else{
                 Reaction* reac=driver->model->get_reac(tok0);
                 reac->set_bounds(lb, ub);
            }
      }
}
Example #8
0
ParameterError str2udouble(double *val, const char *str)
{
  ParameterError result = str2double(val, str);
  if(result != PARAM_OK)
    return result;
  if(*val < 0)
    return PARAM_NEGATIVE_NUMERIC;

  return PARAM_OK;
}
Example #9
0
float CVariant::asFloat(float fallback) const
{
  switch (m_type)
  {
    case VariantTypeDouble:
      return (float)m_data.dvalue;
    case VariantTypeInteger:
      return (float)m_data.integer;
    case VariantTypeUnsignedInteger:
      return (float)m_data.unsignedinteger;
    case VariantTypeString:
      return (float)str2double(*m_data.string, fallback);
    case VariantTypeWideString:
      return (float)str2double(*m_data.wstring, fallback);
    default:
      return fallback;
  }
  
  return fallback;
}
Example #10
0
void PopsAtomDBType::parse( const std::string& _Line )
{
	std::vector<std::string> parts = chopstr( _Line, TOKEN_WHITESPACE.c_str() );
	if( parts.size() != 6 ) throw ParseException("POPS: Atom set initial line contains the wrong number of elements");
	if( 0 != parts[0].compare("ATOM") ) THROW( CodeException, "Only atom lines should ever be fed in here!" );

	atomName = parts[1];
	resName = parts[2];

	int endCol;
	if( 
		0 != str2double( parts[3], radius ) ||
		0 != str2double( parts[4], param ) ||
		0 != str2int( parts[5], endCol )
		) throw ParseException("POPS: Invalid value in ATOM line");

	hydrophilic = (endCol != 0);

	return;
}
Example #11
0
bool read_record(Driver* driver, string record){
    if (record == CONTINUE)
        return true;
    if (isallspace(record))
        return false;
    istringstream is(record, ios::in);
    string line;
    int i = 0;
    do{
        ++i;
        line = line.substr(0, line.find(COMMENT));
        if (!line.empty() && !isallspace(line)){
            char tag = line[0];
            string line2 = line.substr(1);
            try{
                switch(tag){
                    case OBJTAG:
                        if (driver->target.empty())
                            read_objective(driver, line2);
                        break;
                    case ARGTAG:
                        if (driver->arg.empty())
                            driver->set_args(line2);
                        break;
                    //case EXTTAG:
                    //    driver->set_externals(line2, true);//externalise the list provided in line2
                    //    break;
                    //case INTTAG:
                    //    driver->set_externals(line2, false);//internalise the list provided in line2
                    //    break;
                    case EXTTAG://w/ simply set externals with '\'
                        driver->set_externals_gfa(line2);
                        break;
                    case VMAXTAG:
                        driver->lp->setVMax(str2double(line2));
                        break;
//                    case TTAG:
//                        if (driver->thermo) ((ThermoLP*)driver->lp)->setTemperature(T0 + str2double(line2));
//                        break;
                    default:
                        try{
                            read_constraint(driver, line, tag);
                        }
                        catch(exception &e){;}
                }
            }
            catch(exception &e) {
                throw runtime_error(string("error in line ") + num2str(i) + ": " + e.what());
            }
        }
    }
    while(getline(is, line, NEWLINE));
    return true;
}
Example #12
0
void read_constraint(Driver* driver, string line, char tag){
    double lb = LB, ub = UB;
    strvec toks;
    split(&line, &toks, DELIM);
    int ntoks = toks.size();
    if (ntoks < 1) throw runtime_error(line);
    string tok0 = toks[0];
    if ((ntoks) > 1){
            lb = str2double(toks[1]);
            ub = lb;
    }
    if ((ntoks > 2)) ub = str2double(toks[2]);

    switch(tag){
        case PLUSTAG:
            read_sto(driver, tok0, lb, ub);
            break;
        case MINUSTAG:
            driver->lp->delRow(tok0.substr(1));
            break;
        case ALLTAG:{
            strvec cols = driver->lp->getColNames();
            for (strvec::iterator it = cols.begin(); it != cols.end(); ++it)
                driver->lp->setColBnds(*it, lb, ub);
            break;
        }
//        case CTAG:
//            if (driver->thermo)
//                ((ThermoLP*)driver->lp)->setConcentration(tok0, lb, ub);
//            break;
//        case DG0TAG:
//            if (driver->thermo)
//                ((ThermoLP*)driver->lp)->setDeltaG0(tok0.substr(1), lb, ub);
//            break;
//        case DGTAG:
//            tok0 = DELTAG_TAG + tok0.substr(1);
        default:
            if (driver->lp->hasCol(tok0))
                driver->lp->setColBnds(tok0, lb, ub);
  }
}
Example #13
0
ParameterError str2udouble(double *valp, const char *str, long max)
{
  double value;
  ParameterError result = str2double(&value, str, max);
  if(result != PARAM_OK)
    return result;
  if(value < 0)
    return PARAM_NEGATIVE_NUMERIC;

  *valp = value;
  return PARAM_OK;
}
Example #14
0
//------------------------------------------------------------------------------
void GeoDat::dodaj_rek (string ln, string sep, bool nadpisz)
{
    int sepS=sep.size();
    if (sepS==0 || ln.size()==0) return;
    unsigned int licz = 0;
    unsigned int poz1 = 0;
    unsigned int poz2 = ln.find(sep,poz1);
    wektor3d tmp1, tmp2;
    tmp1.x = str2double(ln.substr(poz1,poz2));
    while (poz2<ln.size() && (poz2+sepS)<ln.size() && licz < 5)
    {
        poz1 = poz2+sepS;
        poz2 = ln.find(sep,poz1);
        switch(licz)
        {
        case(0): tmp1.y = str2double(ln.substr(poz1,poz2-poz1));break;
        case(1): tmp1.z = str2double(ln.substr(poz1,poz2-poz1));break;
        case(2): tmp2.x = str2double(ln.substr(poz1,poz2-poz1));break;
        case(3): tmp2.y = str2double(ln.substr(poz1,poz2-poz1));break;
        case(4): tmp2.z = str2double(ln.substr(poz1,poz2-poz1));break;
        }
        licz++;
    }
    dodaj_rek(tmp1,tmp2,nadpisz);
}
Example #15
0
int pop_led_hdf5(hid_t input,state_vector *sv){
    int i,count,iy;
    char tmp_c[200],date[200];
    double t[200],t0,t_tmp;
    unsigned short tmp_i[200];
    double x[600], v[600];
    
    hdf5_read(tmp_i,input,"/","","Number of State Vectors",'i');
    count = tmp_i[0];
    
    hdf5_read(tmp_c,input,"/","","Reference UTC",'c');
    cat_nums(date,tmp_c);
    str_date2JD(tmp_c,date);
    t0 = str2double(tmp_c);
    date[4]='\0';
    iy = (int)str2double(date);
    hdf5_read(t,input,"/","","State Vectors Times",'d');
    hdf5_read(x,input,"/","","ECEF Satellite Position",'d');
    hdf5_read(v,input,"/","","ECEF Satellite Velocity",'d');
    //fprintf(stderr,"%.15f\n",x[3]);
    
    for (i=0;i<count;i++){
        t_tmp = t[i]/86400.0+t0;
        sv[i].yr = iy;
        sv[i].jd = (int)(t_tmp - trunc(t_tmp/1000.0)*1000.0);
        sv[i].sec = (t_tmp - trunc(t_tmp))*86400.0;
        sv[i].x = (double)x[i*3];
        sv[i].y = (double)x[i*3+1];
        sv[i].z = (double)x[i*3+2];
        sv[i].vx = (double)v[i*3];
        sv[i].vy = (double)v[i*3+1];
        sv[i].vz = (double)v[i*3+2];
        //fprintf(stderr,"%d %d %.3f %.6f %.6f %.6f %.8f %.8f %.8f \n",sv[i].yr,sv[i].jd,sv[i].sec,x[i*3],x[i*3+1],x[i*3+2],v[i*3],v[i*3+1],v[i*3+2]);
    }

    printf("%d Lines Written for Orbit...\n",count);

    return(count);
}
Example #16
0
int TextMatrix::convert(SimpleMatrix* out) const {
  const int nc = ncol();
  const int nr = nrow();

  double d = 0;
  SimpleMatrix& m = *out;
  m.resize(nr, nc);
  for (int i = 0; i < nr; ++i) {
    for (int j = 0; j < nc; ++j) {
      if (str2double(mat[i][j], &d)) {
        m[i][j] = d;
      } else {
        m[i][j] = NAN;
      }
    }
  }

  m.setRowName(rowName);
  m.setColName(colName);

  return 0;
}
/* ****************************************************************** */
int ReadCommandLine(int nargs, char **args) {

  // Returns 1 if input file is specified and 0 otherwise.

  int options;  // Counters used in getting flags
  int ShowHelp=0; // ShowHelp = 1 if help option flag is selected
  int option_index;
  char line[MAXLINE];
  char param[MAXLINE];
  float temp;

  static struct option long_options [] =
    {
      {"quiet", no_argument,            NULL, 'a'},
      {"ordered", no_argument,          NULL, 'b'},
      {"out", required_argument,        NULL, 'c'},
      {"pairs", no_argument,            NULL, 'd'},
      {"T", required_argument,          NULL, 'e'},
      {"dangles", required_argument,    NULL, 'f'},
      {"material", required_argument,   NULL, 'g'},
      {"help", no_argument,             NULL, 'h'},
      {"timeonly", no_argument,         NULL, 'i'},
      {"listonly", no_argument,         NULL, 'w'},
      {"debug", no_argument,            NULL, 'v'},
      {"echo", no_argument,             NULL, 'j'},
      {"mfe", no_argument,              NULL, 'k'},
      {"cutoff", required_argument,     NULL, 'l'},
      {"progress", no_argument,         NULL, 'm'},
      {"degenerate",no_argument,        NULL, 'n'},
      {"sodium", required_argument,   NULL, 'o'},
      {"magnesium", required_argument,   NULL, 'p'},
      {"longhelixsalt", no_argument,    NULL, 'q'},
      {"validate",no_argument,NULL,'r'},
      {"defect",no_argument,NULL,'s'},
      {0, 0, 0, 0}
    };


  NUPACK_VALIDATE=0;
  // Get the option flags
  while (1)
    {

      /* getopt_long stores the option index here. */
      option_index = 0;
      options = getopt_long_only (nargs, args, 
				  "abcde:f:g:h:ijkl:mno:p:qrs", 
                                  long_options, 
				  &option_index);
		
      // Detect the end of the options.
      if (options == -1)
        break;

      switch (options) {
      case 'a':
        globalArgs.quiet = 1;
        break;

      case 'b':
        globalArgs.permsOn = 1;
        break;

      case 'c':
        strcpy( line, optarg);
        if( sscanf(line, "%d", &(globalArgs.out)) != 1) {
          printf("Invalid out value\n");
          exit(1);
        }
        break;

      case 'd':
        globalArgs.dopairs = 1;
        break;

      case 'e':
        strcpy( line, optarg);
        if( sscanf(line, "%f", &(temp)) != 1) {
          printf("Invalid T value\n");
          exit(1);
        }
        globalArgs.T = (long double) temp;
        break;

      case 'f':
        strcpy( line, optarg);
        if( sscanf(line, "%s", param) != 1) {
          printf("Invalid parameters value\n");
          exit(1);
        }
        if (isdigit(param[0])) {
          globalArgs.dangles = atoi(param);
        }
        else {
          if (!strcmp(param,"none")) {
            globalArgs.dangles = 0;
          }
          else if (!strcmp(param,"some")) {
            globalArgs.dangles = 1;
          }
          else if (!strcmp(param,"all")) {
            globalArgs.dangles = 2;
          }
          else {
            printf("Invalid dangles value\n");
            exit(1);
          }
        }
        break;

      case 'g':
        strcpy( line, optarg);
        if( sscanf(line, "%s", param) != 1) {
          printf("Invalid parameters value\n");
          exit(1);
        }
        else if( !strcmp(param, "dna") || !strcmp(param, "dna1998")) {
          globalArgs.parameters = DNA;

        } else if( !strcmp(param, "rna") || !strcmp(param, "rna1995") ) {
          globalArgs.parameters = RNA;
        } else if( !strcmp(param, "rna37") || !strcmp(param, "rna1999") ) {
          if(strcmp(PARAM_FILE,"rna37") == 0) {
            printf("Parameter specification using rna37 has been deprecated. Please use rna1999 instead\n");
          }
          globalArgs.parameters = RNA37;
        } else {
          globalArgs.parameters = USE_SPECIFIED_PARAMETERS_FILE;
          strcpy( PARAM_FILE, param);
        }
        break;

      case 'h':
        ShowHelp = 1;
        break;

      case 'i':
        globalArgs.timeonly = 1;
        break;

      case 'w':
        globalArgs.listonly = 1;
        break;

      case 'v':
        globalArgs.debug = 1;
        break;

      case 'j':
        globalArgs.echo = 1;
        break;

      case 'k':
        globalArgs.mfe = 1;
        break;

      case 'l':
        strcpy( line, optarg);
        if( sscanf(line, "%f", &(temp)) != 1) {
          printf("Invalid cutoff value\n");
          exit(1);
        }
        globalArgs.cutoff = (long double) temp;
        break;

      case 'm':
        globalArgs.progress = 1;
        break;

      case 'n':
        globalArgs.onlyOneMFE = 0;
        break;

      case 'o':
	strcpy( line, optarg);
	globalArgs.sodiumconc = str2double(line);
	break;
	
      case 'p':
	strcpy( line, optarg);
	globalArgs.magnesiumconc = str2double(line);
	break;
	
      case 'q':
	globalArgs.uselongsalt = 1;
	break;

      case '?':
        // getopt_long already printed an error message.
        break;

      case 'r':
        NUPACK_VALIDATE=1;
        globalArgs.permsOn = 1;
        globalArgs.cutoff = 0.0;
        
        break;

      case 's':
        globalArgs.dodefect = 1;
        break;
      default:
        abort ();
      }
    }
      


  if (ShowHelp) {
    DisplayHelpComplexes();
    exit(1);
  }


  // Check salt inputs to make sure we're ok
  if ((globalArgs.sodiumconc != 1.0 || globalArgs.magnesiumconc != 0.0) && globalArgs.parameters != DNA) {
    printf("WARNING: No salt corrections availabe for RNA.  Using 1 M Na and 0 M Mg.\n");
    globalArgs.sodiumconc = 1.0;
    globalArgs.magnesiumconc = 0.0;
  }

  if (globalArgs.sodiumconc  <= 0.0) {
    printf("ERROR: In valid sodium concentration.  Must have [Na+] > 0.\n");
    exit(1);
  }

  if (globalArgs.magnesiumconc  < 0.0) {
    printf("ERROR: In valid magnesium concentration.  Must have [Mg2+] >= 0.\n");
    exit(1);
  }

  if (globalArgs.sodiumconc < 0.05 || globalArgs.sodiumconc > 1.1) {
    printf("WARNING: Salt correction only verified for 0.05 M < [Na+] < 1.1 M.\n");
    printf("         [Na+] = %g M may give erroneous results.\n",(float) globalArgs.sodiumconc);
  }

  if (globalArgs.magnesiumconc > 0.2) {
    printf("WARNING: Salt correction only verified for [Mg2+] <= 0.2 M.\n");
    printf("         [Mg2+] = %g M may give erroneous results.\n",(float) globalArgs.magnesiumconc);
  }
  // The range of validity of the magnesium correction is unknown

  if (globalArgs.uselongsalt && globalArgs.magnesiumconc > 0.0) {
    printf("WARNING: No magnesium correction parameters are available for the long\n");
    printf("         helix mode of salt correction.  Using [Mg2+] = 0.\n");
    globalArgs.magnesiumconc = 0.0;
  }
  

  // Get the the input file
  if (optind == nargs) { // There's no input from the user
    return 0;
  }
  else {
    strcpy(globalArgs.inputFilePrefix,args[optind]);
  }

  return 1;

}
Example #18
0
void OFDMletters2bits(const emlrtStack *sp, const char_T str[80], real_T f[560])
{
  int32_T bits_size_idx_1;
  char_T bits_data[1280];
  int32_T i2;
  int32_T firstcol;
  int32_T j;
  boolean_T exitg1;
  boolean_T p;
  int32_T i;
  boolean_T exitg2;
  char_T b_bits_data[1280];
  const mxArray *y;
  const mxArray *m4;
  emlrtStack st;
  st.prev = sp;
  st.tls = sp->tls;

  /*  Encode a string of ASCII text into bits(1,0) */
  st.site = &hf_emlrtRSI;
  bits_size_idx_1 = 16;
  for (i2 = 0; i2 < 1280; i2++) {
    bits_data[i2] = '0';
  }

  for (firstcol = 0; firstcol < 80; firstcol++) {
    for (j = 0; j < 16; j++) {
      if (((uint8_T)str[firstcol] & 1 << j) != 0) {
        bits_data[firstcol + 80 * (15 - j)] = '1';
      }
    }
  }

  firstcol = 16;
  j = 1;
  exitg1 = FALSE;
  while ((exitg1 == FALSE) && (j <= 15)) {
    p = FALSE;
    i = 0;
    exitg2 = FALSE;
    while ((exitg2 == FALSE) && (i <= 79)) {
      if (bits_data[i + 80 * (j - 1)] != '0') {
        p = TRUE;
        exitg2 = TRUE;
      } else {
        i++;
      }
    }

    if (p) {
      firstcol = j;
      exitg1 = TRUE;
    } else {
      j++;
    }
  }

  if (firstcol > 1) {
    for (j = firstcol; j < 17; j++) {
      for (i = 0; i < 80; i++) {
        bits_data[i + 80 * (j - firstcol)] = bits_data[i + 80 * (j - 1)];
      }
    }

    j = 17 - firstcol;
    for (i2 = 0; i2 < j; i2++) {
      memcpy(&b_bits_data[80 * i2], &bits_data[80 * i2], 80U * sizeof(char_T));
    }

    bits_size_idx_1 = 17 - firstcol;
    j = 17 - firstcol;
    for (i2 = 0; i2 < j; i2++) {
      memcpy(&bits_data[80 * i2], &b_bits_data[80 * i2], 80U * sizeof(char_T));
    }
  }

  for (firstcol = 0; firstcol < 80; firstcol++) {
    for (i = 0; i < 7; i++) {
      i2 = 1 + i;
      emlrtDynamicBoundsCheckFastR2012b(i2, 1, bits_size_idx_1, &c_emlrtBCI, sp);
      y = NULL;
      m4 = emlrtCreateString1(bits_data[firstcol + 80 * i]);
      emlrtAssign(&y, m4);
      st.site = &er_emlrtRSI;
      f[firstcol + 80 * i] = emlrt_marshallIn(&st, str2double(&st, y,
        &b_emlrtMCI), "str2double");
      emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
    }

    emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
  }
}
Example #19
0
void b_OFDMletters2bits(const emlrtStack *sp, real_T f[560])
{
  int32_T bits_size_idx_1;
  char_T bits_data[1280];
  int32_T i14;
  int32_T firstcol;
  static const char_T cv93[80] = { 'L', 'i', 'v', 'e', ' ', 'l', 'o', 'n', 'g',
    ' ', 'a', 'n', 'd', ' ', 'p', 'r', 'o', 's', 'p', 'e', 'r', ',', ' ', 'f',
    'r', 'o', 'm', ' ', 't', 'h', 'e', ' ', 'C', 'o', 'm', 'm', 'u', 'n', 'i',
    'c', 'a', 't', 'i', 'o', 'n', 's', ' ', 'S', 'y', 's', 't', 'e', 'm', ' ',
    'T', 'o', 'o', 'l', 'b', 'o', 'x', ' ', 'T', 'e', 'a', 'm', ' ', 'a', 't',
    ' ', 'M', 'a', 't', 'h', 'W', 'o', 'r', 'k', 's', '!' };

  uint16_T dk;
  int32_T j;
  boolean_T exitg1;
  boolean_T p;
  int32_T i;
  boolean_T exitg2;
  char_T b_bits_data[1280];
  const mxArray *y;
  const mxArray *m12;
  emlrtStack st;
  st.prev = sp;
  st.tls = sp->tls;

  /*  Encode a string of ASCII text into bits(1,0) */
  st.site = &hf_emlrtRSI;
  bits_size_idx_1 = 16;
  for (i14 = 0; i14 < 1280; i14++) {
    bits_data[i14] = '0';
  }

  for (firstcol = 0; firstcol < 80; firstcol++) {
    dk = (uint16_T)cv93[firstcol];
    for (j = 0; j < 16; j++) {
      if ((dk & 1 << j) != 0) {
        bits_data[firstcol + 80 * (15 - j)] = '1';
      }
    }
  }

  firstcol = 16;
  j = 1;
  exitg1 = FALSE;
  while ((exitg1 == FALSE) && (j <= 15)) {
    p = FALSE;
    i = 0;
    exitg2 = FALSE;
    while ((exitg2 == FALSE) && (i <= 79)) {
      if (bits_data[i + 80 * (j - 1)] != '0') {
        p = TRUE;
        exitg2 = TRUE;
      } else {
        i++;
      }
    }

    if (p) {
      firstcol = j;
      exitg1 = TRUE;
    } else {
      j++;
    }
  }

  if (firstcol > 1) {
    for (j = firstcol; j < 17; j++) {
      for (i = 0; i < 80; i++) {
        bits_data[i + 80 * (j - firstcol)] = bits_data[i + 80 * (j - 1)];
      }
    }

    j = 17 - firstcol;
    for (i14 = 0; i14 < j; i14++) {
      memcpy(&b_bits_data[80 * i14], &bits_data[80 * i14], 80U * sizeof(char_T));
    }

    bits_size_idx_1 = 17 - firstcol;
    j = 17 - firstcol;
    for (i14 = 0; i14 < j; i14++) {
      memcpy(&bits_data[80 * i14], &b_bits_data[80 * i14], 80U * sizeof(char_T));
    }
  }

  for (firstcol = 0; firstcol < 80; firstcol++) {
    for (i = 0; i < 7; i++) {
      i14 = 1 + i;
      emlrtDynamicBoundsCheckFastR2012b(i14, 1, bits_size_idx_1, &c_emlrtBCI, sp);
      y = NULL;
      m12 = emlrtCreateString1(bits_data[firstcol + 80 * i]);
      emlrtAssign(&y, m12);
      st.site = &er_emlrtRSI;
      f[firstcol + 80 * i] = emlrt_marshallIn(&st, str2double(&st, y,
        &b_emlrtMCI), "str2double");
      emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
    }

    emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
  }
}
Example #20
0
static void parse_commandline(int argc, char* argv[], specs * s)
{
    int i;

    if (argc < 2)
        usage();

    i = 1;
    while (i < argc) {
        if (argv[i][0] != '-')
            usage();

        switch (argv[i][1]) {
        case 'c':
            i++;
            s->square = 0;
            s->invariant = 1;
            break;
        case 'i':
            i++;
            if (i >= argc)
                quit("no file name found after -i\n");
            s->fin = argv[i];
            i++;
            break;
        case 'l':
            i++;
            s->linear = 1;
            break;
        case 'n':
            i++;
            s->fout = NULL;
            s->generate_points = 1;
            if (i >= argc)
                quit("no grid dimensions found after -n\n");
            if (sscanf(argv[i], "%dx%d", &s->nx, &s->ny) != 2)
                quit("could not read grid dimensions after \"-n\"\n");
#if defined(NN_SERIAL)
            if (s->nx <= 0 || s->ny <= 0)
#else
            if (s->nx <= 0 || s->nx > NMAX || s->ny <= 0 || s->ny > NMAX)
#endif
                quit("invalid size for output grid\n");
            i++;
            break;
        case 'o':
            i++;
            if (i >= argc)
                quit("no file name found after -o\n");
            s->fout = argv[i];
            i++;
            break;
        case 's':
            i++;
            s->square = 1;
            s->invariant = 0;
            break;
        case 'x':
            if (argv[i][2] == 0) {
                i++;
                if (i >= argc)
                    quit("no xmin value found after -x\n");
                s->xmin = str2double(argv[i], "-x");
                i++;
                if (i >= argc)
                    quit("no xmax value found after -x\n");
                s->xmax = str2double(argv[i], "-x");
                i++;
            } else if (strcmp(argv[i], "-xmin") == 0) {
                i++;
                if (i >= argc)
                    quit("no value found after -xmin\n");
                s->xmin = str2double(argv[i], "-xmin");
                i++;
            } else if (strcmp(argv[i], "-xmax") == 0) {
                i++;
                if (i >= argc)
                    quit("no value found after -xmax\n");
                s->xmax = str2double(argv[i], "-xmax");
                i++;
            } else
                usage();
            break;
        case 'y':
            if (argv[i][2] == 0) {
                i++;
                if (i >= argc)
                    quit("no ymin value found after -y\n");
                s->ymin = str2double(argv[i], "-y");
                i++;
                if (i >= argc)
                    quit("no ymax value found after -y\n");
                s->ymax = str2double(argv[i], "-y");
                i++;
            } else if (strcmp(argv[i], "-ymin") == 0) {
                i++;
                if (i >= argc)
                    quit("no value found after -ymin\n");
                s->ymin = str2double(argv[i], "-ymin");
                i++;
            } else if (strcmp(argv[i], "-ymax") == 0) {
                i++;
                if (i >= argc)
                    quit("no value found after -ymax\n");
                s->ymax = str2double(argv[i], "-ymax");
                i++;
            } else
                usage();
            break;
        case 'v':
            i++;
            nn_verbose = 1;
            break;
        case 'z':
            i++;
            if (i >= argc)
                quit("no zoom value found after -z\n");
            s->zoom = str2double(argv[i], "-z");
            i++;
            break;
        case 'D':
            i++;
            s->thin = 1;
            if (argc > i && argv[i][0] != '-') {
                if (sscanf(argv[i], "%dx%d", &s->nxd, &s->nyd) != 2)
                    quit("could not read grid dimensions after \"-D\"\n");
                if (s->nxd <= 0 || s->nyd <= 0)
                    quit("invalid value for nx = %d or ny = %d after -D option\n", s->nxd, s->nyd);
#if !defined(NN_SERIAL)
                if (s->nxd > NMAX || s->nyd > NMAX)
                    quit("too big value after -D option (expected < %d)\n", NMAX);
#endif
                i++;
            }
            break;
        case 'L':
            i++;
            s->thin = 2;
            if (i >= argc)
                quit("no value found after -L\n");
            s->rmax = str2double(argv[i], "-L");
            i++;
            break;
        case 'N':
            i++;
            s->nointerp = 1;
            break;
        case 'P': {
            char delim[] = "=";
            char prmstr[STRBUFSIZE] = "";
            char* token;

            i++;
            if (i >= argc)
                quit("no input found after -P\n");

            if (strlen(argv[i]) >= STRBUFSIZE)
                quit("could not interpret \"%s\" after -P option\n", argv[i]);

            strcpy(prmstr, argv[i]);
            token = strtok(prmstr, delim);
            if (token == NULL)
                quit("could not interpret \"%s\" after -P option\n", argv[i]);

            if (strcmp(token, "alg") == 0) {
                token = strtok(NULL, delim);
                if (token == NULL)
                    quit("could not interpret \"%s\" after -P option\n", argv[i]);

                if (strcmp(token, "nn") == 0) {
                    nn_rule = SIBSON;
                    s->linear = 0;
                } else if (strcmp(token, "ns") == 0) {
                    nn_rule = NON_SIBSONIAN;
                    s->linear = 0;
                } else if (strcmp(token, "l") == 0) {
                    s->linear = 1;
                } else
                    usage();
            }

            i++;
            break;
        }
        case 'W':
            i++;
            if (i >= argc)
                quit("no minimal allowed weight found after -W\n");
            s->wmin = str2double(argv[i], "-W");
            i++;
            break;
        case 'T':
            i++;
            if (i >= argc)
                quit("no vertex id found after -T\n");
            nn_test_vertice = atoi(argv[i]);
            nn_verbose = 1;
            i++;
            break;
        case 'V':
            i++;
            nn_verbose = 2;
            break;
#if defined(NN_SERIAL)
        case '%':
            i++;
            if (i < argc && argv[i][0] != '-') {
                s->npoints = atoi(argv[i]);
                i++;
            } else
                s->npoints = 1;
            break;
#endif
        default:
            usage();
            break;
        }
    }

    if (nn_verbose && argc == 2)
        version();

    if (s->thin) {
        if (s->nxd == -1)
            s->nxd = s->nx;
        if (s->nyd == -1)
            s->nyd = s->ny;
        if (s->nxd <= 0 || s->nyd <= 0)
            quit("invalid grid size for thinning\n");
    }
#if defined(NN_SERIAL)
    if (s->npoints == 1) {
        if (s->nx <= 0)
            s->npoints = 0;
        else
            s->npoints = s->nx * s->ny;
    }
#endif
}
Example #21
0
/**
 * Extract covaraite from file @param fn.
 * Only samples included in @param includedSample will be processed
 * If some samples appear more than once, only the first appearance will be
 * readed
 * Only covaraites provided in @param covNameToUse will be included
 * Missing values will be imputed to the mean columnwise.
 * Result will be put to @param mat (sample by covariate) and @param
 * sampleToDrop
 * @return number of sample loaded (>=0); or a minus number meaning error
 * @param sampleToDrop: store samples that are not found in covariate.
 */
int extractCovariate(const std::string& fn,
                     const std::vector<std::string>& sampleToInclude,
                     const std::vector<std::string>& covNameToUse,
                     DataLoader::HandleMissingCov handleMissingCov,
                     SimpleMatrix* mat, std::set<std::string>* sampleToDrop) {
  std::set<std::string> includeSampleSet;
  makeSet(sampleToInclude, &includeSampleSet);
  if (includeSampleSet.size() != sampleToInclude.size()) {
    logger->warn(
        "Some samples have appeared more than once, and we record covariate "
        "for its first appearance");
  }
  std::vector<std::string> noPhenotypeSample;

  std::map<std::string, int>
      processed;  // record how many times a sample is processed
  std::set<std::pair<int, int> >
      missing;  // record which number is covaraite is missing.
  int missingCovariateWarning =
      0;  // record how many times a missing warning is geneated.
  bool missingValueInLine;  // record whether there is missing value in the
                            // line
  int missingLines = 0;     // record how many lines has missing values
  std::vector<int> columnToExtract;
  std::vector<std::string> extractColumnName;
  std::vector<std::string> fd;
  LineReader lr(fn);
  int lineNo = 0;
  int fieldLen = 0;
  while (lr.readLineBySep(&fd, "\t ")) {
    ++lineNo;
    if (lineNo == 1) {  // header line
      fieldLen = fd.size();
      if (fieldLen < 2) {
        logger->error(
            "Insufficient column number (<2) in the first line of covariate "
            "file!");
        return -1;
      };
      if (tolower(fd[0]) != "fid" || tolower(fd[1]) != "iid") {
        logger->error("Covariate file header should begin with \"FID IID\"!");
        return -1;
      }
      std::map<std::string, int> headerMap;
      makeMap(fd, &headerMap);
      if (fd.size() != headerMap.size()) {
        logger->error("Covariate file have duplicated header!");
        return -1;
      }
      // specify which covariates to extract
      if (covNameToUse.size()) {
        for (size_t i = 0; i < covNameToUse.size(); ++i) {
          if (headerMap.count(covNameToUse[i]) == 0) {
            logger->error(
                "The covariate [ %s ] you specified cannot be found from "
                "covariate file!",
                covNameToUse[i].c_str());
            continue;
          }
          columnToExtract.push_back(headerMap[covNameToUse[i]]);
          extractColumnName.push_back(covNameToUse[i]);
        }
      } else {
        for (size_t i = 2; i < fd.size(); ++i) {
          columnToExtract.push_back(headerMap[fd[i]]);
          extractColumnName.push_back(fd[i]);
        }
      }
    } else {  // body lines
      if (fd.empty() ||
          (fd[0].empty() && fd.size() == 1)) {  // skip empty lines
        continue;
      }
      if ((int)fd.size() != fieldLen) {
        logger->error(
            "Inconsistent column number in covariate file line [ %d ] - skip "
            "this file!",
            lineNo);
        return -1;
      }
      if (includeSampleSet.find(fd[1]) ==
          includeSampleSet.end()) {  // does not have phenotype
        noPhenotypeSample.push_back(fd[1]);
        continue;
      };
      processed[fd[1]]++;
      if (processed[fd[1]] > 1) {
        logger->info("Duplicate sample [ %s ] in covariate file, skipping",
                     fd[1].c_str());
        continue;
      };
      int idx = (*mat).nrow();
      (*mat).resize(idx + 1, columnToExtract.size());
      (*mat).setRowName(idx, fd[1]);

      missingValueInLine = false;
      for (int i = 0; i < (int)columnToExtract.size(); ++i) {
        double d;
        if (str2double(fd[columnToExtract[i]], &d)) {
          (*mat)[idx][i] = d;
        } else {  // found missing
          missingValueInLine = true;
          ++missingCovariateWarning;
          if (missingCovariateWarning <= 10) {
            if (handleMissingCov == DataLoader::COVARIATE_IMPUTE) {
              logger->warn(
                  "Covariate file line [ %d ] has non-numerical value [ %s "
                  "], "
                  "we will impute to its mean",
                  lineNo, fd[columnToExtract[i]].c_str());
            } else if (handleMissingCov == DataLoader::COVARIATE_DROP) {
              logger->warn(
                  "Covariate file line [ %d ] has non-numerical value [ %s "
                  "], "
                  "we will skip this sample",
                  lineNo, fd[columnToExtract[i]].c_str());
            }
          }
          (*mat)[idx][i] = 0.0;  // will later be updated
          missing.insert(std::make_pair(idx, i));
        };
      }
      if (!missing.empty() && handleMissingCov == DataLoader::COVARIATE_DROP) {
        // drop row and row name
        (*mat).deleteRow((*mat).nrow() - 1);
        missing.clear();
      }
      missingLines += missingValueInLine ? 1 : 0;
    }
  }
  if (missingCovariateWarning > 10) {
    if (handleMissingCov == DataLoader::COVARIATE_IMPUTE) {
      logger->warn(
          "Total [ %d ] lines in covariate file contain non-numerical "
          "values, "
          "we will impute these to their mean",
          missingLines);
    } else if (handleMissingCov == DataLoader::COVARIATE_DROP) {
      logger->warn(
          "Total [ %d ] lines in covariate file contain non-numerical "
          "values, "
          "we will skip these lines",
          missingLines);
    }
  }

  // output samples in covaraite but without phenotype
  for (size_t i = 0; i < noPhenotypeSample.size(); ++i) {
    if (i == 0)
      logger->warn(
          "Total [ %zu ] samples are skipped from covariate file due to "
          "missing phenotype",
          noPhenotypeSample.size());
    if (i > 10) {
      logger->warn(
          "Skip outputting additional [ %d ] samples from covariate file "
          "with "
          "missing phenotypes",
          ((int)noPhenotypeSample.size() - 10));
      break;
    }
    logger->warn(
        "Skip sample [ %s ] from covariate file due to missing phenotype",
        (noPhenotypeSample)[i].c_str());
  }

  // set up labels
  for (size_t i = 0; i < extractColumnName.size(); ++i) {
    mat->setColName(i, extractColumnName[i]);
  }
  for (size_t i = 0; i < sampleToInclude.size(); i++) {
    if (processed.find(sampleToInclude[i]) == processed.end()) {
      logger->warn("Covariate file does not contain sample [ %s ]",
                   sampleToInclude[i].c_str());
      sampleToDrop->insert(sampleToInclude[i]);
    };
  }

  if (handleMissingCov == DataLoader::COVARIATE_DROP) {
    assert(missing.empty());
    return (*mat).nrow();
  }
  // impute missing covariates to mean by column
  for (int col = 0; col < mat->ncol(); ++col) {
    double sum = 0;
    int nonZero = 0;
    for (int row = 0; row < (*mat).nrow(); ++row) {
      if (missing.count(std::make_pair(row, col))) continue;  // missing
      sum += (*mat)[row][col];
      ++nonZero;
    }
    if (nonZero == 0) {  // all column are missing, drop column
      logger->info(
          "Covariate [ %s ] is missing for all samples. Exclude please "
          "before "
          "continue!",
          mat->getColName()[col].c_str());
      return -1;
    }
    // some elements are missing
    double mean = sum / nonZero;
    for (int row = 0; row < (*mat).nrow(); ++row) {
      if (missing.count(std::make_pair(row, col))) {
        (*mat)[row][col] = mean;
      }
    }
  }
  return (*mat).nrow();
}  // end extractCovariate
Example #22
0
/** For files of the form ??_yyyymmdd.nc. They are assumed to have "time" 
 * variable.
 */
void reader_rads_standard(char* fname, int fid, obsmeta* meta, model* m, observations* obs)
{
    double mindepth = MINDEPTH_DEF;
    char* addname = NULL;
    int ncid;
    int dimid_nobs;
    size_t nobs_local;
    int varid_lon, varid_lat, varid_pass, varid_sla, varid_time, varid_add;
    double* lon;
    double* lat;
    int* pass;
    double* sla;
    double* time;
    double error_std;
    double* add = NULL;
    size_t tunits_len;
    char* tunits;
    double tunits_multiple, tunits_offset;
    char* basename;
    char instname[3];
    int mvid;
    float** depth;
    int i, ktop;

    for (i = 0; i < meta->npars; ++i) {
        if (strcasecmp(meta->pars[i].name, "MINDEPTH") == 0) {
            if (!str2double(meta->pars[i].value, &mindepth))
                enkf_quit("observation prm file: can not convert MINDEPTH = \"%s\" to double\n", meta->pars[i].value);
        } else if (strcasecmp(meta->pars[i].name, "ADD") == 0) {
            addname = meta->pars[i].value;
            enkf_printf("        ADDING \"%s\"\n", addname);
        } else
            enkf_quit("unknown PARAMETER \"%s\"\n", meta->pars[i].name);
    }
    enkf_printf("        MINDEPTH = %.0f\n", mindepth);

    ncw_open(fname, NC_NOWRITE, &ncid);

    ncw_inq_dimid(ncid, "nobs", &dimid_nobs);
    ncw_inq_dimlen(ncid, dimid_nobs, &nobs_local);
    enkf_printf("        nobs = %u\n", (unsigned int) nobs_local);

    if (nobs_local == 0) {
        ncw_close(ncid);
        return;
    }

    ncw_inq_varid(ncid, "lon", &varid_lon);
    lon = malloc(nobs_local * sizeof(double));
    ncw_get_var_double(ncid, varid_lon, lon);

    ncw_inq_varid(ncid, "lat", &varid_lat);
    lat = malloc(nobs_local * sizeof(double));
    ncw_get_var_double(ncid, varid_lat, lat);

    ncw_inq_varid(ncid, "pass", &varid_pass);
    pass = malloc(nobs_local * sizeof(int));
    ncw_get_var_int(ncid, varid_pass, pass);

    ncw_inq_varid(ncid, "sla", &varid_sla);
    sla = malloc(nobs_local * sizeof(double));
    ncw_get_var_double(ncid, varid_sla, sla);
    ncw_get_att_double(ncid, varid_sla, "error_std", &error_std);
    enkf_printf("        error_std = %3g\n", error_std);

    if (addname != NULL) {
        ncw_inq_varid(ncid, addname, &varid_add);
        add = malloc(nobs_local * sizeof(double));
        ncw_get_var_double(ncid, varid_add, add);
    }

    ncw_inq_varid(ncid, "time", &varid_time);
    time = malloc(nobs_local * sizeof(double));
    ncw_get_var_double(ncid, varid_time, time);
    ncw_inq_attlen(ncid, varid_time, "units", &tunits_len);
    tunits = calloc(tunits_len + 1, 1);
    ncw_get_att_text(ncid, varid_time, "units", tunits);

    ncw_close(ncid);

    tunits_convert(tunits, &tunits_multiple, &tunits_offset);

    basename = strrchr(fname, '/');
    if (basename == NULL)
        basename = fname;
    else
        basename += 1;
    strncpy(instname, basename, 2);
    instname[2] = 0;

    mvid = model_getvarid(m, obs->obstypes[obstype_getid(obs->nobstypes, obs->obstypes, meta->type, 1)].varnames[0], 1);
    ktop = grid_gettoplayerid(model_getvargrid(m, mvid));
    depth = model_getdepth(m, mvid, 1);

    for (i = 0; i < (int) nobs_local; ++i) {
        observation* o;
        obstype* ot;

        obs_checkalloc(obs);
        o = &obs->data[obs->nobs];

        o->product = st_findindexbystring(obs->products, meta->product);
        assert(o->product >= 0);
        o->type = obstype_getid(obs->nobstypes, obs->obstypes, meta->type, 1);
        ot = &obs->obstypes[o->type];
        o->instrument = st_add_ifabscent(obs->instruments, instname, -1);
        o->id = obs->nobs;
        o->fid = fid;
        o->batch = pass[i];
        o->value = sla[i];
        if (add != NULL)
            o->value += add[i];
        o->std = error_std;
        o->lon = lon[i];
        o->lat = lat[i];
        o->depth = 0.0;
        o->status = model_xy2fij(m, mvid, o->lon, o->lat, &o->fi, &o->fj);
        if (!obs->allobs && o->status == STATUS_OUTSIDEGRID)
            continue;
        o->fk = (double) ktop;
        o->model_depth = (isnan(o->fi + o->fj)) ? NaN : depth[(int) (o->fj + 0.5)][(int) (o->fi + 0.5)];
        o->date = time[i] * tunits_multiple + tunits_offset;
        if (o->status == STATUS_OK && o->model_depth < mindepth)
            o->status = STATUS_SHALLOW;
        if ((o->status == STATUS_OK) && (o->lon <= ot->xmin || o->lon >= ot->xmax || o->lat <= ot->ymin || o->lat >= ot->ymax))
            o->status = STATUS_OUTSIDEOBSDOMAIN;

        o->aux = -1;

        obs->nobs++;
    }

    if (add != NULL)
        free(add);
    free(lon);
    free(lat);
    free(pass);
    free(sla);
    free(tunits);
    free(time);
}
Example #23
0
/**
 * @return number of phenotypes read. -1 if errors
 * @param phenoCol, which phenotype column to use, similar to plink, it should
 * be the order of phenotype, e.g. phenoCol = 2, meaning the second phenotype
 * @param phenoName, which phenotype header to use.
 */
int loadPedPhenotypeByColumn(const char* fn, std::map<std::string, double>* p,
                             int phenoCol) {
  if (phenoCol < 0) {
    logger->error("Phenotype column cannot be negative: [ %d ]", phenoCol);
    return -1;
  };
  std::map<std::string, double>& pheno = *p;
  std::map<std::string, int> dup;  // duplicates

  std::string line;
  std::vector<std::string> fd;
  LineReader lr(fn);
  int lineNo = 0;
  double v;
  int numMissingPhenotype = 0;
  while (lr.readLine(&line)) {
    stringNaturalTokenize(line, "\t ", &fd);
    ++lineNo;
    if ((int)fd.size() < 5 + phenoCol) {
      logger->warn("Skip line %d (short of columns) in phenotype file [ %s ]",
                   lineNo, fn);
      continue;
    }
    if (toupper(fd[0]) == "FID" && toupper(fd[1]) == "IID") {
      if (lineNo == 1) {
        // skip header line
        continue;
      } else {
        logger->warn(
            "SKip line %d because the abnormal family and individual ids [ "
            "FID "
            "] and [ IID ]",
            lineNo);
        continue;
      }
    }
    std::string& pid = fd[1];
    if (pheno.count(pid) == 0) {
      // check missing
      if (str2double(fd[5 + phenoCol - 1].c_str(), &v)) {
        pheno[pid] = v;
      } else {
        ++numMissingPhenotype;
        if (numMissingPhenotype <= 10) {
          logger->warn(
              "Skip: Missing or invalid phenotype type, skipping line %d [ "
              "%s "
              "] ... ",
              lineNo, line.c_str());
        }
        continue;
      }
    } else {
      // logger->warn("line %s have duplicated id, skipped..", pid.c_str());
      dup[pid]++;
      continue;
    }
  }
  if (numMissingPhenotype > 10) {
    logger->warn(
        "Skip: Additional [ %d ] lines have missing or invalid phenotype "
        "type",
        numMissingPhenotype - 10);
  }

  for (std::map<std::string, int>::iterator iter = dup.begin();
       iter != dup.end(); ++iter) {
    logger->warn(
        "Sample [ %s ] removed from phenotype file [ %s ] for its duplicity "
        "[ "
        "%d ]",
        iter->first.c_str(), fn, iter->second + 1);
    pheno.erase(iter->first);
  };
  return pheno.size();
};
Example #24
0
/**
 * Load kinship file and store the kinship matrix internally
 */
int KinshipHolder::loadK() {
  // check kinship file existance
  if (!fileExists(this->fileName)) {
    logger->warn("Cannot open kinship file [ %s ]", this->fileName.c_str());
    return -1;
  }

  const std::vector<std::string>& names = *this->pSample;
  if (this->fileName == "IDENTITY" || this->fileName == "UNRELATED") {
    this->matK->mat.resize(names.size(), names.size());
    this->matK->mat.setZero();
    this->matK->mat.diagonal().setOnes();
    this->matK->mat.diagonal() *= 0.5;
    return 0;
  }

  LineReader lr(this->fileName);
  int lineNo = 0;
  int fieldLen = 0;
  std::vector<std::string> fd;
  std::vector<int> columnToExtract;
  std::vector<std::string> header;  // kinship header line
  Eigen::MatrixXf& mat = this->matK->mat;
  std::map<std::string, int> nameMap;
  makeMap(names, &nameMap);

  while (lr.readLineBySep(&fd, "\t ")) {
    ++lineNo;
    if (lineNo == 1) {  // check header
      header = fd;
      fieldLen = fd.size();
      if (fieldLen < 2) {
        logger->error(
            "Insufficient column number (<2) in the first line of kinsihp "
            "file!");
        return -1;
      };
      if (tolower(fd[0]) != "fid" || tolower(fd[1]) != "iid") {
        logger->error("Kinship file header should begin with \"FID IID\"!");
        return -1;
      }
      std::map<std::string, int> headerMap;
      makeMap(fd, &headerMap);
      if (fd.size() != headerMap.size()) {
        logger->error("Kinship file have duplicated header!");
        return -1;
      }
      for (size_t i = 0; i < names.size(); ++i) {
        if (headerMap.count(names[i]) == 0) {
          logger->error("Cannot find sample [ %s ] from the kinship file!",
                        names[i].c_str());
          return -1;
        }
        columnToExtract.push_back(headerMap[names[i]]);
      }
      mat.resize(columnToExtract.size(), columnToExtract.size());
      continue;
    }
    // body lines
    if ((int)fd.size() != fieldLen) {
      logger->error(
          "Inconsistent column number [ %zu ] (used to be [ %d ]) in kinship "
          "file line [ %d ] - skip this file!",
          fd.size(), fieldLen, lineNo);
      // todo: this error may be false alarm - as the last line may be empty
      continue;
      // exit(1);
    }
    if (fd[1] != header[lineNo]) {  // PID in line i (1-based) should matched
                                    // the (i+1) (1-based) column in the header
      logger->error(
          "Inconsistent IID names in kinship file line [ %d ], file corrupted!",
          lineNo);
      return -1;
    }
    int row;
    if (nameMap.find(fd[1]) == nameMap.end()) {
      continue;
    }
    row = nameMap[fd[1]];
    for (size_t i = 0; i < columnToExtract.size(); ++i) {
      double d;
      if (str2double(fd[columnToExtract[i]], &d)) {
        mat(row, i) = d;
      } else {
        // unable to read, then set it to zero
        mat(row, i) = 0.0;
      }
    }
  }
#ifdef DEBUG
  std::string tmp = fn;
  tmp += ".tmp";
  std::ofstream ofs(tmp.c_str(), std::ofstream::out);
  ofs << mat;
  ofs.close();
#endif

  // fprintf(stderr, "Kinship matrix [ %d x %d ] loaded", (int)mat.rows(),
  // (int)mat.cols());
  this->loaded = true;
  return 0;
}
Example #25
0
model* model_create(enkfprm* prm)
{
    model* m = calloc(1, sizeof(model));
    char* modelprm = prm->modelprm;
    char* gridprm = prm->gridprm;

    model_setgrids(m, gridprm);

    /*
     * read model parameter file
     */
    {
        FILE* f = NULL;
        char buf[MAXSTRLEN];
        int line;
        variable* now = NULL;

        /*
         * get model tag, type and variables
         */
        f = enkf_fopen(modelprm, "r");
        line = 0;
        while (fgets(buf, MAXSTRLEN, f) != NULL) {
            char seps[] = " =\t\n";
            char* token = NULL;

            line++;
            if (buf[0] == '#')
                continue;
            if ((token = strtok(buf, seps)) == NULL)
                continue;
            if (strcasecmp(token, "NAME") == 0) {
                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: NAME not specified", modelprm, line);
                else if (m->name != NULL)
                    enkf_quit("%s, l.%d: NAME specified twice", modelprm, line);
                else
                    m->name = strdup(token);
            } else if (strncasecmp(token, "VAR", 3) == 0) {
                int i;

                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: VAR not specified", modelprm, line);
                for (i = 0; i < m->nvar; ++i)
                    if (strcasecmp(m->vars[i].name, token) == 0)
                        enkf_quit("%s, l.%d: VAR \"%s\" already specified", modelprm, line, token);
                if (m->nvar % NVAR_INC == 0)
                    m->vars = realloc(m->vars, (m->nvar + NVAR_INC) * sizeof(variable));
                now = &m->vars[m->nvar];
                variable_new(now, m->nvar, token);
                m->nvar++;
            } else if (strcasecmp(token, "GRID") == 0) {
                int i;

                if (now == NULL)
                    enkf_quit("%s, l.%d: VAR not specified", modelprm, line);
                if (now->gridid >= 0)
                    enkf_quit("%s, l.%d: GRID already specified for \"%s\"", modelprm, line, now->name);
                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: GRID not specified", modelprm, line);
                for (i = 0; i < m->ngrid; ++i)
                    if (strcasecmp(token, grid_getname(m->grids[i])) == 0) {
                        now->gridid = i;
                        break;
                    }
                if (i == m->ngrid)
                    enkf_quit("%s, l.%d: grid \"%s\" not specified", modelprm, line, token);
            } else if (strcasecmp(token, "INFLATION") == 0) {
                if (now == NULL)
                    enkf_quit("%s, l.%d: VAR not specified", modelprm, line);
                if (!isnan(now->inflation))
                    enkf_quit("%s, l.%d: INFLATION already specified for \"%s\"", modelprm, line, now->name);
                if ((token = strtok(NULL, seps)) == NULL)
                    enkf_quit("%s, l.%d: INFLATION not specified", modelprm, line);
                if (!str2double(token, &now->inflation))
                    enkf_quit("%s, l.%d: could not convert \"%s\" to double", modelprm, line, token);
                if ((token = strtok(NULL, seps)) != NULL) {
                    if (!str2double(token, &now->inf_ratio))
                        enkf_quit("%s, l.%d: could not convert \"%s\" to double", modelprm, line, token);
                }
            } else
                enkf_quit("%s, l.%d: unknown token \"%s\"", modelprm, line, token);
        }                       /* while reading modelprm */
        fclose(f);
        assert(m->name != NULL);
        assert(m->nvar > 0);
        {
            int i;

            for (i = 0; i < m->nvar; ++i)
                if (m->vars[i].gridid == -1) {
                    if (m->ngrid == 1)
                        m->vars[i].gridid = 0;
                    else
                        enkf_quit("%s: grid not specified for variable \"%s\"\n", modelprm, m->vars[i].name);
                }
        }
    }

    /*
     * set inflations
     */
    {
        int i;

        for (i = 0; i < m->nvar; ++i)
            if (isnan(m->vars[i].inflation)) {
                m->vars[i].inflation = prm->inflation_base;
                m->vars[i].inf_ratio = prm->inf_ratio;
            }
        prm->inflation_base = NaN;
        prm->inf_ratio = NaN;
    }

    model_print(m, "    ");

    assert(m->ngrid > 0);

    return m;
}
Example #26
0
void ParameterParser::Read(int argc, char** argv) {
  std::string flag;
  for (int i = 1; i < argc; i++) {
    // removing leading - or -- for flags
    flag = argv[i];
    // flag = '--' meaning the end of all parameter
    if (flag == "--") {
      ++i;
      while (i < argc) {
        FLAG_REMAIN_ARG.push_back(argv[i++]);
      }
      return;
    }

    // flag = '-' meaning standard input, we will put it in the remaingArg
    if (flag == "-") {
      FLAG_REMAIN_ARG.push_back(argv[i]);
      continue;
    }

    unsigned int choppedLeadingDash = 0;
    // user may input ---flag, ----flag, ..., and we will chop all leading -
    // user may also input ---, ----, but I don't understand what that means,
    // so report error
    while (flag.size() > 0) {
      if (flag[0] == '-') {
        choppedLeadingDash++;
        flag = flag.substr(1);
      } else {
        break;
      }
    }
    if (flag.empty()) {
      fprintf(stderr, "ERROR: we don't understand the argument \"%s\"\n",
              argv[i]);
      return;
    }
    if (choppedLeadingDash > 2) {
      fprintf(stderr, "WARNING: you typed too many dash in \"%s\"?\n", argv[i]);
    } else if (choppedLeadingDash == 1 && flag.size() > 1) {
      fprintf(stderr,
              "WARNING: long parameter should have two lead dashes: \"%s\"?\n",
              argv[i]);
    } else if (choppedLeadingDash > 1 && flag.size() == 1) {
      fprintf(stderr,
              "WARNING: long parameter should have two lead dashes: \"%s\"?\n",
              argv[i]);
    }

    // check if variable flag is a predefined flag or not
    std::vector<std::string>::iterator it;
    for (it = flagVec.begin(); it != flagVec.end(); it++) {
      if (*it == flag) {
        break;
      }
    }
    // variable flag will be added to remainingArg
    if (it == flagVec.end()) {
      FLAG_REMAIN_ARG.push_back(argv[i]);
      continue;
    }
    // NOTE:
    // we could check if user misuse -- and -
    // but i did not see much use here.

    // parse data
    unsigned int idx = it - flagVec.begin();
    void* data = flagInfoMap[idx].data;
    if (flagInfoMap[idx].isParsed) {
      fprintf(stderr,
              "WARNING: flag \"%s\" provided more than once, the previous "
              "value will be overwritten\n",
              argv[i]);
    }
    flagInfoMap[idx].isParsed = true;
    switch (flagInfoMap[idx].pt) {
      case BOOL_TYPE:
        *(bool*)data = true;
        break;
      case INT_TYPE:
        if (i + 1 == argc) {
          fprintf(stderr, "ERROR: missing parameter value after [ %s ]!\n",
                  argv[i]);
          exit(1);
        }
        if (!str2int(argv[i + 1], (int*)data)) {
          fprintf(stderr,
                  "WARNING: arg \"%s %s\" does not give valid integer\n",
                  argv[i], argv[i + 1]);
        }
        ++i;
        break;
      case DOUBLE_TYPE:
        if (i + 1 == argc) {
          fprintf(stderr, "ERROR: missing parameter value after [ %s ]!\n",
                  argv[i]);
          exit(1);
        }
        if (!str2double(argv[i + 1], (double*)data)) {
          fprintf(stderr, "WARNING: arg \"%s %s\" does not give valid double\n",
                  argv[i], argv[i + 1]);
        }
        ++i;
        break;
      case STRING_TYPE:
        if (i + 1 == argc) {
          fprintf(stderr, "ERROR: missing parameter value after [ %s ]!\n",
                  argv[i]);
          exit(1);
        }
        *(std::string*)data = argv[++i];
        break;
      default:
        fprintf(stderr, "ERROR: Unrecognized parameter type for flag %s\n",
                argv[i]);
        return;
    }
  }
}
Example #27
0
/**
 * Invoked to handle ASCII commands. This is the default
 * mode for statsite, to be backwards compatible with statsd
 * @arg handle The connection related information
 * @return 0 on success.
 */
static int handle_ascii_client_connect(statsite_conn_handler *handle) {
    // Look for the next command line
    char *buf, *key, *val_str, *type_str, *sample_str, *endptr;
    metric_type type;
    int buf_len, should_free, status, i, after_len;
    double val, sample_rate;
    while (1) {
        status = extract_to_terminator(handle->conn, '\n', &buf, &buf_len, &should_free);
        if (status == -1) return 0; // Return if no command is available

        // Check for a valid metric
        // Scan for the colon
        status = buffer_after_terminator(buf, buf_len, ':', &val_str, &after_len);
        if (likely(!status)) status |= buffer_after_terminator(val_str, after_len, '|', &type_str, &after_len);
        if (unlikely(status)) {
            syslog(LOG_WARNING, "Failed parse metric! Input: %s", buf);
            goto ERR_RET;
        }

        // Convert the type
        switch (*type_str) {
            case 'c':
                type = COUNTER;
                break;
            case 'm':
                type = TIMER;
                break;
            case 'k':
                type = KEY_VAL;
                break;
            case 'g':
                type = GAUGE;

                // Check if this is a delta update
                switch (*val_str) {
                    case '+':
                        // Advance past the + to avoid breaking str2double
                        val_str++;
                    case '-':
                        type = GAUGE_DELTA;
                }
                break;
            case 's':
                type = SET;
                break;
            default:
                type = UNKNOWN;
                syslog(LOG_WARNING, "Received unknown metric type! Input: %c", *type_str);
                goto ERR_RET;
        }

        // Increment the number of inputs received
        if (GLOBAL_CONFIG->input_counter)
            metrics_add_sample(GLOBAL_METRICS, COUNTER, GLOBAL_CONFIG->input_counter, 1);

        // Fast track the set-updates
        if (type == SET) {
            metrics_set_update(GLOBAL_METRICS, buf, val_str);
            goto END_LOOP;
        }

        // Convert the value to a double
        val = str2double(val_str, &endptr);
        if (unlikely(endptr == val_str)) {
            syslog(LOG_WARNING, "Failed value conversion! Input: %s", val_str);
            goto ERR_RET;
        }

        // Handle counter sampling if applicable
        if (type == COUNTER && !buffer_after_terminator(type_str, after_len, '@', &sample_str, &after_len)) {
            sample_rate = str2double(sample_str, &endptr);
            if (unlikely(endptr == sample_str)) {
                syslog(LOG_WARNING, "Failed sample rate conversion! Input: %s", sample_str);
                goto ERR_RET;
            }
            if (sample_rate > 0 && sample_rate <= 1) {
                // Magnify the value
                val = val * (1.0 / sample_rate);
            }
        }

        // Store the sample
        metrics_add_sample(GLOBAL_METRICS, type, buf, val);

END_LOOP:
        // Make sure to free the command buffer if we need to
        if (should_free) free(buf);
    }

    return 0;
ERR_RET:
    if (should_free) free(buf);
    return -1;
}
Example #28
0
static spt str_to_spt(const struct formatter_field *field,
                      const char *str,
                      bool *success)
{
    spt value;
    bool conv_ok = false;
    const unsigned int field_width = get_width(field);
    const uint64_t mask = (field_width < 64) ?
                            (1llu << field_width) - 1 :
                            0xffffffffffffffffllu;

    *success = false;

    switch (field->format) {
        case FORMATTER_FMT_HEX:
        case FORMATTER_FMT_UNSIGNED_DEC: {
            uint64_t tmp = str2uint64(str, 0, UINT64_MAX, &conv_ok);
            if (!conv_ok) {
                goto inval;
            }

            tmp = (uint64_t) (((float) tmp - field->offset) / field->scaling);
            value = spt_from_uint64(tmp);
            break;
        }

        case FORMATTER_FMT_TWOS_COMPLEMENT: {
            int64_t tmp = str2int64(str, INT64_MIN, INT64_MAX, &conv_ok);
            if (!conv_ok) {
                goto inval;
            }

            tmp = (int64_t) (((float) tmp - field->offset) / field->scaling);
            value = spt_from_int64(tmp);

            /* Mask off sign-extended bits to field with */
            value &= mask;
            break;
        }


        case FORMATTER_FMT_SIGN_MAGNITUDE: {
            int64_t tmp = str2int64(str, INT64_MIN, INT64_MAX, &conv_ok);
            bool negative = (tmp < 0);
            if (!conv_ok) {
                goto inval;
            }

            tmp = (int64_t) (((float) tmp - field->offset) / field->scaling);

            /* Field width includes sign bit (assumed to be MSB) */
            tmp &= (1 << (field_width - 1)) - 1;

            if (negative) {
                tmp |= (1 << (field_width - 1));
            }

            value = spt_from_uint64((uint64_t) tmp);
            break;
        }

        case FORMATTER_FMT_FLOAT: {
            float tmp = (float) str2double(str, -DBL_MAX, DBL_MAX, &conv_ok);
            if (!conv_ok) {
                goto inval;
            }

            value = spt_from_float((float) tmp, field->scaling, field->offset);

            /* Mask off sign-extended bits to field with */
            value &= mask;
            break;
        }

        case FORMATTER_FMT_ENUM: {
            size_t i;
            bool have_enum = false;

            for (i = 0; i < field->enum_count && !have_enum; i++) {
                if (!strcasecmp(str, field->enums[i].str)) {
                    value = field->enums[i].value;
                    have_enum = true;
                }
            }

            if (!have_enum) {
                uint64_t tmp = str2uint64(str, 0, UINT64_MAX, &conv_ok);
                if (!conv_ok) {
                    goto inval;
                }

                value = spt_from_uint64(tmp);
            }

            break;
        }

        default:
            log_critical("Bug: Invalid field format: %d\n", field->format);
            return spt_from_uint64(0);
    }

    if ((spt_to_uint64(value) & mask) != spt_to_uint64(value)) {
        log_error("Value is too large for field \"%s\": %s\n",
                    field->name, str);

        return spt_from_uint64(0);
    }

    *success = true;
    return value;

inval:
    log_error("Invalid value for field \"%s\": %s\n", field->name, str);
    return spt_from_uint64(0);
}
Example #29
0
int KinshipHolder::loadDecomposed() {
  LineReader lr(this->eigenFileName);
  int lineNo = 0;
  int fieldLen = 0;
  std::vector<std::string> fd;
  std::vector<int> columnToExtract;
  std::vector<std::string> header;  // header line of the kinship eigen file
  Eigen::MatrixXf& matK = this->matK->mat;
  Eigen::MatrixXf& matS = this->matS->mat;
  Eigen::MatrixXf& matU = this->matU->mat;
  const std::vector<std::string>& names = *this->pSample;
  const int NumSample = (int)names.size();
  std::map<std::string, int> nameMap;
  makeMap(names, &nameMap);
  std::map<std::string, int> headerMap;

  while (lr.readLineBySep(&fd, "\t ")) {
    ++lineNo;
    if (lineNo == 1) {  // check header
      header = fd;
      fieldLen = fd.size();
      if (fieldLen < 3) {  // at least three columns: IID, Lambda, U1
        logger->error(
            "Insufficient column number (<3) in the first line of kinsihp "
            "file!");
        return -1;
      };
      for (size_t i = 0; i != fd.size(); ++i) {
        fd[i] = tolower(fd[i]);
      }
      makeMap(fd, &headerMap);
      if (fd.size() != headerMap.size()) {
        logger->error("Kinship file have duplicated headers!");
        return -1;
      }

      // check IID, Lambda, U1, U2, ... U(N) where (N) is the sample size
      if (headerMap.count("iid") == 0) {
        logger->error("Missing 'IID' column!");
        return -1;
      }
      columnToExtract.push_back(headerMap["iid"]);

      if (headerMap.count("lambda") == 0) {
        logger->error("Missing 'Lambda' column!");
        return -1;
      }
      columnToExtract.push_back(headerMap["lambda"]);

      std::string s;
      for (int i = 0; i < NumSample; ++i) {
        s = "u";
        s += toString(i + 1);
        if (headerMap.count(s) == 0) {
          logger->error("Missing '%s' column!", s.c_str());
          return -1;
        }
        columnToExtract.push_back(headerMap[s]);
      }
      s = "u";
      s += toString(NumSample + 1);
      if (headerMap.count(s) != 0) {
        logger->error("Unexpected column '%s'!", s.c_str());
        return -1;
      }

      matS.resize(NumSample, 1);
      matU.resize(NumSample, NumSample);
      continue;
    }
    // body lines
    if ((int)fd.size() != fieldLen) {
      logger->error(
          "Inconsistent column number [ %zu ] (used to be [ %d ])in kinship "
          "file line [ %d ] - skip this file!",
          fd.size(), fieldLen, lineNo);
      return -1;
    }

    const int iidColumn = columnToExtract[0];
    const std::string& iid = fd[iidColumn];
    if (nameMap.count(iid) == 0) {
      logger->error("Unexpected sample [ %s ]!", iid.c_str());
      return -1;
    }
    const int row = nameMap[iid];

    const int lambdaColumn = columnToExtract[1];
    double temp = 0.0;
    if (!str2double(fd[lambdaColumn], &temp)) {
      logger->warn("Invalid numeric value [ %s ] treated as zero!",
                   fd[lambdaColumn].c_str());
    }
    matS(lineNo - 2, 0) = temp;

    for (int i = 0; i < NumSample; ++i) {
      int uColumn = columnToExtract[i + 2];
      if (!str2double(fd[uColumn], &temp)) {
        logger->warn("Invalid numeric value [ %s ] treated as zero!",
                     fd[lambdaColumn].c_str());
      }
      matU(row, i) = temp;
    }
  }

  // verify eigen decomposition results make senses
  // check largest eigen vector and eigen value
  Eigen::MatrixXf v1 = matK * matU.col(0);
  Eigen::MatrixXf v2 = matS(0, 0) * matU.col(0);
  if (matS(0, 0) > 0.5 && v1.col(0).norm() > .5 && v2.col(0).norm() > 0.5 &&
      corr(v1, v2) < 0.8) {
    logger->warn("Cannot verify spectral decompose results!");
    return -1;
  }

  // check the min(10, NumSample) random eigen vector and eigen value
  int randomCol = 10;
  if (randomCol > NumSample - 1) {
    randomCol = NumSample - 1;
  }
  v1 = matK * matU.col(randomCol);
  v2 = matS(randomCol, 0) * matU.col(randomCol);
  if (matS(randomCol, 0) > 0.5 && v1.col(0).norm() > 0.5 &&
      v2.col(0).norm() > 0.5 && corr(v1, v2) < 0.8) {
    logger->warn("Cannot verify spectral decompose results!");
    return -1;
  }

#ifdef DEBUG
  std::string tmp = fn;
  tmp += ".tmp";
  std::ofstream ofs(tmp.c_str(), std::ofstream::out);
  ofs << mat;
  ofs.close();
#endif

  // fprintf(stderr, "Kinship matrix [ %d x %d ] loaded", (int)mat.rows(),
  // (int)mat.cols());

  if (this->matK) {
    delete this->matK;
    this->matK = NULL;
  }

  return 0;
}
Example #30
0
void obstypes_read(char fname[], int* n, obstype** types, double rfactor_base)
{
    FILE* f = NULL;
    char buf[MAXSTRLEN];
    int line;
    obstype* now = NULL;
    int i;

    assert(*n == 0);

    f = enkf_fopen(fname, "r");
    line = 0;
    while (fgets(buf, MAXSTRLEN, f) != NULL) {
        char seps[] = " =\t\n";
        char* token;

        line++;
        if (buf[0] == '#')
            continue;
        if ((token = strtok(buf, seps)) == NULL)
            continue;
        if (strcasecmp(token, "NAME") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: NAME not specified", fname, line);
            if (obstype_getid(*n, *types, token) >= 0)
                enkf_quit("%s: l.%d: type \"%s\" already specified", fname, line, token);

            *types = realloc(*types, (*n + 1) * sizeof(obstype));
            now = &(*types)[*n];
            obstype_new(now, *n, token);
            (*n)++;
            continue;
        }

        if (now == NULL)
            enkf_quit("%s, l.%d: NAME not specified", fname, line);

        if (strcasecmp(token, "ISSURFACE") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: ISSURFACE not specified", fname, line);
            now->issurface = read_bool(token);
            if (now->issurface < 0)
                enkf_quit("%s, l.%d: could not convert \"%s\" to boolean", fname, line, token);
        } else if (strcasecmp(token, "VAR") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: VAR not specified", fname, line);
            if (now->varname != NULL)
                enkf_quit("%s, l.%d: VAR already specified", fname, line);
            now->varname = strdup(token);
        } else if (strcasecmp(token, "VAR2") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: VAR2 not specified", fname, line);
            if (now->varname2 != NULL)
                enkf_quit("%s, l.%d: VAR2 already specified", fname, line);
            now->varname2 = strdup(token);
        } else if (strcasecmp(token, "OFFSET") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: OFFSET file name not specified", fname, line);
            if (now->offset_fname != NULL)
                enkf_quit("%s, l.%d: OFFSET entry already specified", fname, line);
            now->offset_fname = strdup(token);
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: OFFSET variable name not specified", fname, line);
            now->offset_varname = strdup(token);
        } else if (strcasecmp(token, "HFUNCTION") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: HFUNCTION not specified", fname, line);
            if (now->hfunction != NULL)
                enkf_quit("%s, l.%d: HFUNCTION already specified", fname, line);
            now->hfunction = strdup(token);
        } else if (strcasecmp(token, "MINVALUE") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: MINVALUE not specified", fname, line);
            if (!str2double(token, &now->allowed_min))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "MAXVALUE") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: MAXVALUE not specified", fname, line);
            if (!str2double(token, &now->allowed_max))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strncasecmp(token, "ASYNC", 5) == 0) {
            now->isasync = 1;
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: ASYNC time interval not specified", fname, line);
            if (!str2double(token, &now->async_tstep))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "RFACTOR") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: RFACTOR not specified", fname, line);
            if (!str2double(token, &now->rfactor))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "XMIN") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: XMIN not specified", fname, line);
            if (!str2double(token, &now->xmin))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "XMAX") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: XMAX not specified", fname, line);
            if (!str2double(token, &now->xmax))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "YMIN") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: YMIN not specified", fname, line);
            if (!str2double(token, &now->ymin))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "YMAX") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: YMAX not specified", fname, line);
            if (!str2double(token, &now->ymax))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "ZMIN") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: ZMIN not specified", fname, line);
            if (!str2double(token, &now->zmin))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else if (strcasecmp(token, "ZMAX") == 0) {
            if ((token = strtok(NULL, seps)) == NULL)
                enkf_quit("%s, l.%d: ZMAX not specified", fname, line);
            if (!str2double(token, &now->zmax))
                enkf_quit("%s, l.%d: could not convert \"%s\" to double", fname, line, token);
        } else
            enkf_quit("%s, l.%d: unknown token \"%s\"", fname, line, token);
    }
    fclose(f);

    for (i = 0; i < *n; ++i)
        (*types)[i].rfactor *= rfactor_base;

    for (i = 0; i < *n; ++i) {
        obstype_check(&(*types)[i]);
        obstype_print(&(*types)[i]);
    }
}