Пример #1
0
int
main(int argc, char **argv) 
{			/* create tst_classic_fills.nc */
   printf("\n*** Testing fill values.\n");
   printf("*** testing read of string record var with no data...");
   {
#define STRING_VAR_NAME "blood_toil_tears_sweat"
#define NDIMS_STRING 1
#define DATA_START 1 /* Real data here. */

      int  ncid, varid, dimid, varid_in;
      const char *data_out[1] = {
	 "We have before us an ordeal of the most grievous kind. We have before "
	 "us many, many long months of struggle and of suffering. You ask, what "
	 "is our policy? I can say: It is to wage war, by sea, land and air, "
	 "with all our might and with all the strength that God can give us; to "
	 "wage war against a monstrous tyranny, never surpassed in the dark, "
	 "lamentable catalogue of human crime. That is our policy. You ask, what "
	 "is our aim? "
	 "I can answer in one word: It is victory, victory at all costs, victory "
	 "in spite of all terror, victory, however long and hard the road may "
	 "be; for without victory, there is no survival."};
      char *data_in;
      size_t index = DATA_START;

      /* Create file with a 1D string var. Set its fill value to the
       * empty string. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, "sentence", NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, 
		     &dimid, &varid)) ERR;

      /* Check it out. */
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;

      /* Write one string, leaving some blank records which will then
       * get the fill value. */
      if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR;

      /* Get all the data from the variable. */
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

   }
   SUMMARIZE_ERR;
   printf("*** testing read of string record var w/fill-value with no data...");
   {
#undef STRING_VAR_NAME
#define STRING_VAR_NAME "I_Have_A_Dream"
#undef NDIMS_STRING
#define NDIMS_STRING 1
#define FILLVALUE_LEN 1 /* There is 1 string, the empty one. */
#undef DATA_START
#define DATA_START 2 /* Real data here. */

      int  ncid, varid, dimid, varid_in;
      const char *missing_val[FILLVALUE_LEN] = {""};
      const char *missing_val_in[FILLVALUE_LEN];
      const char *data_out[1] = {
	 "With this faith, we will be able to hew out of the mountain of "
	 "despair a stone of hope. With this faith, we will be able to "
	 "transform the jangling discords of our nation into a beautiful "
	 "symphony of brotherhood. With this faith, we will be able to work "
	 "together, to pray together, to struggle together, to go to jail "
	 "together, to stand up for freedom together, knowing that we will "
	 "be free one day."};
      char *data_in;
      size_t index = DATA_START;

      /* Create file with a 1D string var. Set its fill value to the
       * empty string. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, "sentence", NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, 
		     &dimid, &varid)) ERR;
      if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN,
	missing_val)) ERR;

      /* Check it out. */
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue",
      			    (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR;

      /* Write one string, leaving some blank records which will then
       * get the fill value. */
      if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR;

      /* Get all the data from the variable. */
      index = 0;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
      index = 1;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
      index = DATA_START;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue",
      			    (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR;

      /* Get all the data from the variable. */
/* As of HDF5-1.8.12, reading from an unwritten chunk in a dataset with a
 *      variable-length datatype and a fill-value set will error, instead
 *      of retrieving the fill-value. -QAK
 */
#ifdef NOT_YET
      index = 0;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
      index = 1;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, missing_val[0])) ERR;
      free(data_in);
#endif /* NOT_YET */
      index = DATA_START;
      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

   }
   SUMMARIZE_ERR;
/*    printf("*** testing empty fill values of a string var..."); */
/*    { */
/* #define STRING_VAR_NAME "The_String" */
/* #define NDIMS_STRING 1 */
/* #define FILLVALUE_LEN 1 /\* There is 1 string, the empty one. *\/ */
/* #define DATA_START 2 /\* Real data here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       const char *missing_val[FILLVALUE_LEN] = {""}; */
/*       const char *missing_val_in[FILLVALUE_LEN]; */
/*       const char *data_out[1] = {"The evil that men do lives after them; " */
/* 				 "the good is oft interred with their bones."}; */
/*       char **data_in; */
/*       size_t index = DATA_START; */
/*       int i; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN, missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR; */

/*       /\* Write one string, leaving some blank records which will then */
/*        * get the fill value. *\/ */
/*       if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR; */

/*       /\* Get all the data from the variable. *\/ */
/*       if (!(data_in = malloc((DATA_START + 1) * sizeof(char *)))) ERR; */
/*       if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */

/*       /\* First there should be fill values, then the data value we */
/*        * wrote. *\/ */
/*       for (i = 0; i < DATA_START; i++) */
/* 	 if (strcmp(data_in[i], "")) ERR; */
/*       if (strcmp(data_in[DATA_START], data_out[0])) ERR; */

/*       /\* Close everything up. Don't forget to free the string! *\/ */
/*       if (nc_free_string(DATA_START + 1, data_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, read data, and check values again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       /\*if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in[0])) ERR;*\/ */
/*       if (nc_close(ncid)) ERR; */
/*        free(data_in);  */
/*    } */

/*    SUMMARIZE_ERR; */
/*    printf("*** testing non-empty fill values of a string var..."); */
/*    { */
/* #define STRING_VAR_NAME2 "CASSIUS" */
/* #define FILLVALUE_LEN2 1 /\* There is 1 string in the fillvalue array. *\/ */
/* #define DATA_START2 9 /\* Real data starts here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       const char *missing_val[FILLVALUE_LEN2] = {"I know that virtue to be in you, Brutus"}; */
/*       const char *missing_val_in[FILLVALUE_LEN2]; */
/*       const char *data_out[1] = {"The evil that men do lives after them; " */
/* 				 "the good is oft interred with their bones."}; */
/*       char **data_in; */
/*       size_t index = DATA_START2; */
/*       int i; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * a non-empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME2, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN2, missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR; */

/*       /\* Write one string, leaving some blank records which will then */
/*        * get the fill value. *\/ */
/*       if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR; */

/*       /\* Get all the data from the variable. *\/ */
/*       if (!(data_in = malloc((DATA_START2 + 1) * sizeof(char *)))) ERR; */
/*       if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */

/*       /\* First there should be fill values, then the data value we */
/*        * wrote. *\/ */
/*       for (i = 0; i < DATA_START2; i++) */
/* 	 if (strcmp(data_in[i], missing_val[0])) ERR; */
/*       if (strcmp(data_in[DATA_START2], data_out[0])) ERR; */

/*       /\* Close everything up. *\/ */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, read data, and check values again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */
/*       free(data_in); */
/*    } */
/*    printf("*** testing read of string record var with no data..."); */
/*    { */
/* #define STRING_VAR_NAME "Moon_Is_A_Harsh_Mistress" */
/* #define NDIMS_STRING 1 */
/* #define FILLVALUE_LEN 1 /\* There is 1 string, the empty one. *\/ */
/* #define DATA_START 2 /\* Real data here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       char *missing_val[FILLVALUE_LEN] = {""}; */
/*       char *missing_val_in; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "Lunar_Years", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING,  */
/* 		     &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN,  */
/* 			    missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", &missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in)) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, &missing_val_in)) ERR; */

/*       /\* Get all the data from the variable. There is none! *\/ */
/*       if (nc_get_var_string(ncid, varid_in, NULL)) ERR; */
      
/*       /\* Close file. *\/ */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, and check again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", &missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in)) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, &missing_val_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */
/*    } */
/*    SUMMARIZE_ERR; */
   FINAL_RESULTS;
}
Пример #2
0
int main(int argc, char* argv[])
{
	program_name = argv[0];

	if (argc < 3) {
		print_help();
		exit(EXIT_FAILURE);
	}
	
	char binfile[50];
	base = argv[1];
	strapp(binfile, ".nc");

	int total_ids = atoi(argv[2]);

	FILE *fp;
	if (argc > 2) {
		infile = argv[3];
		fp = fopen(infile, "r");
		if (!fp) 
			error(1, errno, "%s", infile);
	} else {
		infile = "stdin";
		fp = stdin;
	}

	/* Get the first line with the SNP names */
	char *line = NULL;
	size_t n = 0;
	int read = getlin(&line, &n, fp);
	if (read == -1)
		error(1, errno, "%s", infile);
   	int nsnps = getsnps(line);
	free(line);
	line = NULL;
	printf("%d markers\n", nsnps);

	printf("Converting intensity values to binary\n");
	int ncid, stat, sampid, varid;
	size_t nids = 0, perkey;
	size_t count[3] = { 1, 0, 0 };
	size_t start[3] = { 0, 0, 0 };
	char *id;	
	float *vals;

	while (getlin(&line, &n, fp) != -1) {
		if (nids == 0) {
			/* Count number of vals in first line to alloc indv_dat */
			read = getstrings(line, NULL, DELIM) - 1;
			if (read < 1)
				error(2, 0, "No values found");
			if (read % nsnps != 0)
				error(2, 0, "Not same number of values for each marker");
			perkey = read / nsnps;
			printf("%zu values per marker\n", perkey);
			vals = malloc(sizeof(float) * read);
			if (vals == NULL)
				error(1, errno, "dat.vals");


			/* Create netcdf file and write snp names */
 			ncid = create_nc(binfile, total_ids, nsnps, perkey);
			stat = nc_inq_varid(ncid, "sample", &sampid);
			check_err(stat,__LINE__,__FILE__);
			stat = nc_inq_varid(ncid, "intensity", &varid);
			check_err(stat,__LINE__,__FILE__);
			writesnps(ncid, nsnps);

			count[1] = nsnps;
			count[2] = perkey;
		}
				
		/* Read valuess from line */
		id = linevals(line, vals);

		/* Write sample id */
		stat = nc_put_var1_string(ncid, sampid, &nids, (const char **) &id);
		check_err(stat,__LINE__,__FILE__);

		/* Write intensity values */
		start[0] = nids;
		stat = nc_put_vara_float(ncid, varid, start, count, vals);
    	check_err(stat,__LINE__,__FILE__);

		free(line);
		line = NULL;
		nids++;
		printf("Read %zu\r", nids);
		fflush(stdout);
	}
			
	fclose(fp);
    stat = nc_close(ncid);
    check_err(stat,__LINE__,__FILE__);
	free(vals);
	free(line);

	printf("Wrote %zu samples to [ %s ]\n", nids, binfile);
	exit(EXIT_SUCCESS);
}
Пример #3
0
int mov_session::save()
{
    int ierr,ncid,i;
    int dimid_ntimeseries,dimid_one;
    int varid_filename,varid_colors,varid_units,varid_names;
    int varid_xshift,varid_yshift,varid_type,varid_coldstart;
    int varid_stationfile,varid_plottitle,varid_xlabel,varid_ylabel;
    int varid_startdate,varid_enddate,varid_precision,varid_ymin,varid_ymax;
    int varid_autodate,varid_autoy,varid_checkState;
    int dims_1d[1];
    int nTimeseries;
    QString relPath,TempFile,Directory,tempString;
    QByteArray tempByte;
    size_t start[1];
    size_t iu;
    double mydatadouble[1];
    int mydataint[1];
    const char * mydatastring[1];

    QFile Session(this->sessionFileName);

    QVector<QString> filenames_ts;
    QVector<QString> filetype_ts;
    QVector<QString> colors_ts;
    QVector<double> units_ts;
    QVector<QString> seriesname_ts;
    QVector<double> xshift_ts;
    QVector<double> yshift_ts;
    QVector<QString> date_ts;
    QVector<QString> stationfile_ts;
    QVector<int> checkStates_ts;

    //Remove the old file
    if(Session.exists())
        Session.remove();

    //Get the path of the session file so we can save a relative path later
    mov_generic::splitPath(this->sessionFileName,TempFile,Directory);
    QDir CurrentDir(Directory);

    ierr = mov_generic::NETCDF_ERR(nc_create(this->sessionFileName.toUtf8(),NC_NETCDF4,&ncid));
    if(ierr!=NC_NOERR)return 1;

    //Start setting up the definitions
    nTimeseries = this->tableWidget->rowCount();

    filenames_ts.resize(nTimeseries);
    colors_ts.resize(nTimeseries);
    units_ts.resize(nTimeseries);
    seriesname_ts.resize(nTimeseries);
    xshift_ts.resize(nTimeseries);
    yshift_ts.resize(nTimeseries);
    date_ts.resize(nTimeseries);
    stationfile_ts.resize(nTimeseries);
    filetype_ts.resize(nTimeseries);
    checkStates_ts.resize(nTimeseries);

    for(i=0;i<nTimeseries;i++)
    {
        filenames_ts[i] = this->tableWidget->item(i,6)->text();
        seriesname_ts[i] = this->tableWidget->item(i,1)->text();
        colors_ts[i] = this->tableWidget->item(i,2)->text();
        units_ts[i] = this->tableWidget->item(i,3)->text().toDouble();
        xshift_ts[i] = this->tableWidget->item(i,4)->text().toDouble();
        yshift_ts[i] = this->tableWidget->item(i,5)->text().toDouble();
        date_ts[i] = this->tableWidget->item(i,7)->text();
        filetype_ts[i] = this->tableWidget->item(i,8)->text();
        stationfile_ts[i] = this->tableWidget->item(i,10)->text();
        if(this->tableWidget->item(i,0)->checkState()==Qt::Checked)
            checkStates_ts[i] = 1;
        else
            checkStates_ts[i] = 0;
    }

    ierr = mov_generic::NETCDF_ERR(nc_def_dim(ncid,"ntimeseries",static_cast<size_t>(nTimeseries),&dimid_ntimeseries));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_dim(ncid,"one",1,&dimid_one));
    if(ierr!=NC_NOERR)return 1;

    //Arrays
    dims_1d[0] = dimid_ntimeseries;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_filename",NC_STRING,1,dims_1d,&varid_filename));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_colors",NC_STRING,1,dims_1d,&varid_colors));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_names",NC_STRING,1,dims_1d,&varid_names));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_filetype",NC_STRING,1,dims_1d,&varid_type));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_coldstartdate",NC_STRING,1,dims_1d,&varid_coldstart));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_stationfile",NC_STRING,1,dims_1d,&varid_stationfile));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_xshift",NC_DOUBLE,1,dims_1d,&varid_xshift));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_yshift",NC_DOUBLE,1,dims_1d,&varid_yshift));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_units",NC_DOUBLE,1,dims_1d,&varid_units));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_checkState",NC_INT,1,dims_1d,&varid_checkState));
    if(ierr!=NC_NOERR)return 1;

    //Scalars
    dims_1d[0] = dimid_one;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_plottitle",NC_STRING,1,dims_1d,&varid_plottitle));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_xlabel",NC_STRING,1,dims_1d,&varid_xlabel));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_ylabel",NC_STRING,1,dims_1d,&varid_ylabel));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_precision",NC_INT,1,dims_1d,&varid_precision));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_startdate",NC_STRING,1,dims_1d,&varid_startdate));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_enddate",NC_STRING,1,dims_1d,&varid_enddate));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_ymin",NC_DOUBLE,1,dims_1d,&varid_ymin));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_ymax",NC_DOUBLE,1,dims_1d,&varid_ymax));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_autodate",NC_INT,1,dims_1d,&varid_autodate));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_def_var(ncid,"timeseries_autoy",NC_INT,1,dims_1d,&varid_autoy));
    if(ierr!=NC_NOERR)return 1;
    ierr = mov_generic::NETCDF_ERR(nc_enddef(ncid));
    if(ierr!=NC_NOERR)return 1;

    tempByte = this->plotTitleWidget->text().toUtf8();
    mydatastring[0] = tempByte.data();
    ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_plottitle,mydatastring));
    if(ierr!=NC_NOERR)return 1;


    tempByte = this->xLabelWidget->text().toUtf8();
    mydatastring[0] = tempByte.data();
    ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_xlabel,mydatastring));
    if(ierr!=NC_NOERR)return 1;


    tempByte = this->yLabelWidget->text().toUtf8();
    mydatastring[0] = tempByte.data();
    ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_ylabel,mydatastring));
    if(ierr!=NC_NOERR)return 1;


    tempByte = this->startDateEdit->dateTime().toString("yyyy-MM-dd hh:mm:ss").toUtf8();
    mydatastring[0] = tempByte.data();
    ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_startdate,mydatastring));
    if(ierr!=NC_NOERR)return 1;


    tempByte = this->endDateEdit->dateTime().toString("yyyy-MM-dd hh:mm:ss").toUtf8();
    mydatastring[0] = tempByte.data();
    ierr = mov_generic::NETCDF_ERR(nc_put_var_string(ncid,varid_enddate,mydatastring));
    if(ierr!=NC_NOERR)return 1;


    mydataint[0] = 3;
    ierr = mov_generic::NETCDF_ERR(nc_put_var_int(ncid,varid_precision,mydataint));
    if(ierr!=NC_NOERR)return 1;
    mydatadouble[0] = this->yMinSpinBox->value();
    ierr = mov_generic::NETCDF_ERR(nc_put_var_double(ncid,varid_ymin,mydatadouble));
    if(ierr!=NC_NOERR)return 1;
    mydatadouble[0] = this->yMaxSpinBox->value();
    ierr = mov_generic::NETCDF_ERR(nc_put_var_double(ncid,varid_ymax,mydatadouble));
    if(ierr!=NC_NOERR)return 1;

    if(this->checkAllData->isChecked())
        mydataint[0] = 1;
    else
        mydataint[0] = 0;
    ierr = mov_generic::NETCDF_ERR(nc_put_var_int(ncid,varid_autodate,mydataint));
    if(ierr!=NC_NOERR)return 1;

    if(this->checkYAuto->isChecked())
        mydataint[0] = 1;
    else
        mydataint[0] = 0;
    ierr = mov_generic::NETCDF_ERR(nc_put_var_int(ncid,varid_autoy,mydataint));
    if(ierr!=NC_NOERR)return 1;

    for(iu=0;iu<static_cast<unsigned int>(nTimeseries);iu++)
    {
        start[0] = iu;

        relPath = CurrentDir.relativeFilePath(filenames_ts[iu]);
        tempByte = relPath.toUtf8();
        mydatastring[0] = tempByte.data();
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_filename,start,mydatastring));
        if(ierr!=NC_NOERR)return 1;
        mydatastring[0] = NULL;

        tempString = seriesname_ts[iu];
        tempByte  = tempString.toUtf8();
        mydatastring[0] = tempByte.data();
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_names,start,mydatastring));
        if(ierr!=NC_NOERR)return 1;
        mydatastring[0] = NULL;

        tempByte = colors_ts[iu].toUtf8();
        mydatastring[0] = tempByte.data();
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_colors,start,mydatastring));
        if(ierr!=NC_NOERR)return 1;
        mydatastring[0] = NULL;

        tempByte = date_ts[iu].toUtf8();
        mydatastring[0] = tempByte.data();
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_coldstart,start,mydatastring));
        if(ierr!=NC_NOERR)return 1;
        mydatastring[0] = NULL;

        relPath = CurrentDir.relativeFilePath(stationfile_ts[iu]);
        tempByte  = relPath.toUtf8();
        mydatastring[0] = tempByte.data();
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_stationfile,start,mydatastring));
        if(ierr!=NC_NOERR)return 1;
        mydatastring[0] = NULL;

        tempByte = filetype_ts[iu].toUtf8();
        mydatastring[0] = tempByte.data();
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_string(ncid,varid_type,start,mydatastring));
        if(ierr!=NC_NOERR)return 1;
        mydatastring[0] = NULL;

        mydatadouble[0]  = xshift_ts[iu];
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_double(ncid,varid_xshift,start,mydatadouble));
        if(ierr!=NC_NOERR)return 1;

        mydatadouble[0]  = yshift_ts[iu];
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_double(ncid,varid_yshift,start,mydatadouble));
        if(ierr!=NC_NOERR)return 1;

        mydatadouble[0]  = units_ts[iu];
        ierr  = mov_generic::NETCDF_ERR(nc_put_var1_double(ncid,varid_units,start,mydatadouble));
        if(ierr!=NC_NOERR)return 1;

        mydataint[0] = checkStates_ts[iu];
        ierr = mov_generic::NETCDF_ERR(nc_put_var1_int(ncid,varid_checkState,start,mydataint));
        if(ierr!=NC_NOERR)return 1;

    }

    ierr = mov_generic::NETCDF_ERR(nc_close(ncid));
    if(ierr!=NC_NOERR)return 1;
}
Пример #4
0
int
main(int argc, char **argv) 
{			/* create tst_classic_fills.nc */
   printf("\n*** Testing fill values.\n");
   printf("*** testing empty fill values of a string var...");
   {
#define STRING_VAR_NAME "The_String"
#define NDIMS_STRING 1
#define FILLVALUE_LEN 1 /* There is 1 string, the empty one. */
#define DATA_START 2 /* Real data here. */

      int  ncid, varid, dimid, varid_in;
      const char *missing_val[FILLVALUE_LEN] = {""};
      const char *missing_val_in[FILLVALUE_LEN];
      const char *data_out[1] = {"The evil that men do lives after them; "
				 "the good is oft interred with their bones."};
      char **data_in;
      size_t index = DATA_START;
      int i;

      /* Create file with a 1D string var. Set its fill value to the
       * empty string. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR;
      if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN, missing_val)) ERR;

      /* Check it out. */
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR;

      /* Write one string, leaving some blank records which will then
       * get the fill value. */
      if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR;

      /* Get all the data from the variable. */
      if (!(data_in = malloc((DATA_START + 1) * sizeof(char *)))) ERR;
      if (nc_get_var_string(ncid, varid_in, data_in)) ERR;

      /* First there should be fill values, then the data value we
       * wrote. */
      for (i = 0; i < DATA_START; i++)
	 if (strcmp(data_in[i], "")) ERR;
      if (strcmp(data_in[DATA_START], data_out[0])) ERR;

      /* Close everything up. */
      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR;
      if (nc_close(ncid)) ERR;
      free(data_in);
   }

   SUMMARIZE_ERR;
   printf("*** testing non-empty fill values of a string var...");
   {
#define STRING_VAR_NAME2 "CASSIUS"
#define FILLVALUE_LEN2 1 /* There is 1 string in the fillvalue array. */
#define DATA_START2 9 /* Real data starts here. */

      int  ncid, varid, dimid, varid_in;
      const char *missing_val[FILLVALUE_LEN2] = {"I know that virtue to be in you, Brutus"};
      const char *missing_val_in[FILLVALUE_LEN2];
      const char *data_out[1] = {"The evil that men do lives after them; "
				 "the good is oft interred with their bones."};
      char **data_in;
      size_t index = DATA_START2;
      int i;

      /* Create file with a 1D string var. Set its fill value to the
       * a non-empty string. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_var(ncid, STRING_VAR_NAME2, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR;
      if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN2, missing_val)) ERR;

      /* Check it out. */
      if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR;

      /* Write one string, leaving some blank records which will then
       * get the fill value. */
      if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR;

      /* Get all the data from the variable. */
      if (!(data_in = malloc((DATA_START2 + 1) * sizeof(char *)))) ERR;
      if (nc_get_var_string(ncid, varid_in, data_in)) ERR;

      /* First there should be fill values, then the data value we
       * wrote. */
      for (i = 0; i < DATA_START2; i++)
	 if (strcmp(data_in[i], missing_val[0])) ERR;
      if (strcmp(data_in[DATA_START2], data_out[0])) ERR;

      /* Close everything up. */
      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR;
      if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR;
      if (strcmp(missing_val[0], missing_val_in[0])) ERR;
      if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR;
      if (nc_close(ncid)) ERR;
      free(data_in);
   }

   SUMMARIZE_ERR;
   printf("*** testing fill values of one var...");
   {
#define V1_NAME "v1"
#define MAX_VALS 10
      int  ncid, varid, rec_id, dims[2];
      static int rec[1] = {1};
      size_t start[2] = {0, 0};
      size_t count[2] = {1, MAX_VALS};
      char vals[MAX_VALS];
      int i;

      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* Define dimensions and two vars, a 1D coordinate var for
       * unlimited dimension, and a 2D var which uses the unlimited
       * dimension. */
      if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dims[0])) ERR;
      if (nc_def_dim(ncid, "len", MAX_VALS, &dims[1])) ERR;
      if (nc_def_var(ncid, "rec", NC_INT, 1, dims, &rec_id)) ERR;
      if (nc_def_var(ncid, V1_NAME, NC_CHAR, 2, dims, &varid)) ERR;

      /* Extend record dimension by 1. */
      if (nc_put_vara_int(ncid, rec_id, start, count, rec)) ERR;

      /* Read the other variable; it must have only fill values. */
      if (nc_get_vara_text(ncid, 1, start, count, vals)) ERR;
      for (i = 0; i < MAX_VALS; i++)
	 if(vals[i] != NC_FILL_CHAR) ERR;

      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      /* Read the other variable; it must have only fill values. */
      if (nc_get_vara_text(ncid, 1, start, count, vals)) ERR;
      for (i = 0; i < MAX_VALS; i++)
	 if(vals[i] != NC_FILL_CHAR) ERR;

      if(nc_close(ncid)) ERR;
   }

   SUMMARIZE_ERR;
   printf("*** testing fill values of lots of vars...");

   {
      int  ncid;			/* netCDF id */

#define NVALS 10		/* values per fixed-size variable or record */
#define NFIXVARS 6		/* number of fixed-size vars, one of each type */
#define NRECVARS 6		/* number of record vars, one of each type */
#define RANK_REC 1
#define RANK_FIXVARS 1
#define RANK_RECVARS 2

      /* dimension ids */
      int rec_dim;
      int len_dim;

      /* dimension lengths */
      size_t rec_len = NC_UNLIMITED;
      size_t len_len = NVALS;

      /* variable ids */
      int rec_id;
      int fixvar_ids[NFIXVARS];
      int recvar_ids[NRECVARS];
      int rec_dims[RANK_REC];
      int fixvar_dims[RANK_FIXVARS];
      int recvar_dims[RANK_RECVARS];
      int fixvar, recvar, i;

      char *fnames[] = {"c", "b", "s", "i", "f", "d"};
      char *rnames[] = {"cr", "br", "sr", "ir", "fr", "dr"};
      nc_type types[] = {NC_CHAR, NC_BYTE, NC_SHORT, NC_INT, NC_FLOAT, NC_DOUBLE};

      /*if (nc_set_default_format(format + 1, NULL)) ERR;*/
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* define dimensions */
      if (nc_def_dim(ncid, "rec", rec_len, &rec_dim)) ERR;
      if (nc_def_dim(ncid, "len", len_len, &len_dim)) ERR;

      rec_dims[0] = rec_dim;
      if (nc_def_var(ncid, "rec", NC_INT, RANK_REC, rec_dims, &rec_id)) ERR;

      /* define fixed and record variables of all 6 primitive types */
      fixvar_dims[0] = len_dim;
      for (fixvar = 0; fixvar < NFIXVARS; fixvar++)
	 if (nc_def_var(ncid, fnames[fixvar], types[fixvar], RANK_FIXVARS,
			fixvar_dims, &fixvar_ids[fixvar])) ERR;

      recvar_dims[0] = rec_dim;
      recvar_dims[1] = len_dim;
      for (recvar = 0; recvar < NRECVARS; recvar++)
	 if (nc_def_var(ncid, rnames[recvar], types[recvar], RANK_RECVARS,
			recvar_dims, &recvar_ids[recvar])) ERR;

      /* leave define mode */
      if (nc_enddef(ncid)) ERR;

      {			/* store rec */
	 static size_t rec_start[RANK_REC];
	 static size_t rec_count[RANK_REC];
	 static int rec[] = {1};
	 rec_len = 1;			/* number of records of rec data */
	 rec_start[0] = 0;
	 rec_count[0] = rec_len;
	 if (nc_put_vara_int(ncid, rec_id, rec_start, rec_count, rec)) ERR;
      }
      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      /* Check that fixed-size variables are full of fill values */
      for (fixvar = 0; fixvar < NFIXVARS; fixvar++) {
	 int varid;
	 nc_type type;

	 if (nc_inq_varid(ncid, fnames[fixvar], &varid)) ERR;
	 if (nc_inq_vartype(ncid, varid, &type)) ERR;
	 switch(type) {
	    case NC_CHAR:
	    {
	       char vals[NVALS];
	       if (nc_get_var_text(ncid, varid, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_CHAR) ERR;
	    }
	    break;
	    case NC_BYTE:
	    {
	       signed char vals[NVALS];
	       if (nc_get_var_schar(ncid, varid, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_BYTE) ERR;
	    }
	    break;
	    case NC_SHORT:
	    {
	       short vals[NVALS];
	       if (nc_get_var_short(ncid, varid, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_SHORT) ERR;
	    }
	    break;
	    case NC_INT:
	    {
	       int vals[NVALS];
	       if (nc_get_var_int(ncid, varid, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_INT) ERR;
	    }
	    break;
	    case NC_FLOAT:
	    {
	       float vals[NVALS];
	       if (nc_get_var_float(ncid, varid, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_FLOAT) ERR;
	    }
	    break;
	    case NC_DOUBLE:
	    {
	       double vals[NVALS];
	       if (nc_get_var_double(ncid, varid, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if (vals[i] != NC_FILL_DOUBLE) ERR;
	    }
	    break;
	    default:
	       ERR;
	 }
      }

      /* Read record, check record variables have only fill values */
      for (recvar = 0; recvar < NRECVARS; recvar++) {
	 int varid;
	 nc_type type;
	 size_t start[] = {0, 0};
	 size_t count[] = {1, NVALS};

	 if (nc_inq_varid(ncid, rnames[recvar], &varid)) ERR;
	 if (nc_inq_vartype(ncid, varid, &type)) ERR;
	 switch(type) {
	    case NC_CHAR:
	    {
	       char vals[NVALS];
	       if (nc_get_vara_text(ncid, varid, start, count, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_CHAR) ERR;
	    }
	    break;
	    case NC_BYTE:
	    {
	       signed char vals[NVALS];
	       if (nc_get_vara_schar(ncid, varid, start, count, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_BYTE) ERR;
	    }
	    break;
	    case NC_SHORT:
	    {
	       short vals[NVALS];
	       if (nc_get_vara_short(ncid, varid, start, count, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_SHORT) ERR;
	    }
	    break;
	    case NC_INT:
	    {
	       int vals[NVALS];
	       if (nc_get_vara_int(ncid, varid, start, count, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_INT) ERR;
	    }
	    break;
	    case NC_FLOAT:
	    {
	       float vals[NVALS];
	       if (nc_get_vara_float(ncid, varid, start, count, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_FLOAT) ERR;
	    }
	    break;
	    case NC_DOUBLE:
	    {
	       double vals[NVALS];
	       if (nc_get_vara_double(ncid, varid, start, count, vals)) ERR;
	       for (i = 0; i < NVALS; i++)
		  if(vals[i] != NC_FILL_DOUBLE) ERR;
	    }
	    break;
	    default:
	       ERR;
	 }
      }

      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;

   return 0;
}