Пример #1
0
void
write_nc_surface3d(pcsurface3d gr, const char *filename)
{
#ifdef USE_NETCDF
  const     real(*x)[3] = (const real(*)[3]) gr->x;
  const     uint(*e)[2] = (const uint(*)[2]) gr->e;
  const     uint(*t)[3] = (const uint(*)[3]) gr->t;
  const     uint(*s)[3] = (const uint(*)[3]) gr->s;
  uint      vertices = gr->vertices;
  uint      edges = gr->edges;
  uint      triangles = gr->triangles;
  int       res, nc_out;
  int       x_id, e_id, t_id, s_id;
  int       vertices_id, edges_id, triangles_id;
  int       n2_id, n3_id;
  int       dimid[2];

  /* Create CDF file */
  res = nc_create(filename, NC_NETCDF4, &nc_out);
  nc_handle_error(res);

  /* Define dimensions */
  res = nc_def_dim(nc_out, "vertices", vertices, &vertices_id);
  nc_handle_error(res);
  res = nc_def_dim(nc_out, "edges", edges, &edges_id);
  nc_handle_error(res);
  res = nc_def_dim(nc_out, "triangles", triangles, &triangles_id);
  nc_handle_error(res);
  res = nc_def_dim(nc_out, "two", 2, &n2_id);
  nc_handle_error(res);
  res = nc_def_dim(nc_out, "three", 3, &n3_id);
  nc_handle_error(res);

  /* Define x variable */
  dimid[0] = vertices_id;
  dimid[1] = n3_id;
  res = nc_def_var(nc_out, "x", NC_DOUBLE, 2, dimid, &x_id);
  nc_handle_error(res);
  res = nc_def_var_deflate(nc_out, x_id, 0, 1, NC_DEFLATE_LEVEL);
  nc_handle_error(res);

  /* Define e variable */
  dimid[0] = edges_id;
  dimid[1] = n2_id;
  res = nc_def_var(nc_out, "e", NC_UINT, 2, dimid, &e_id);
  nc_handle_error(res);
  res = nc_def_var_deflate(nc_out, e_id, 0, 1, NC_DEFLATE_LEVEL);
  nc_handle_error(res);

  /* Define t variable */
  dimid[0] = triangles_id;
  dimid[1] = n3_id;
  res = nc_def_var(nc_out, "t", NC_UINT, 2, dimid, &t_id);
  nc_handle_error(res);
  res = nc_def_var_deflate(nc_out, t_id, 0, 1, NC_DEFLATE_LEVEL);
  nc_handle_error(res);

  /* Define s variable */
  dimid[0] = triangles_id;
  dimid[1] = n3_id;
  res = nc_def_var(nc_out, "s", NC_UINT, 2, dimid, &s_id);
  nc_handle_error(res);
  res = nc_def_var_deflate(nc_out, s_id, 0, 1, NC_DEFLATE_LEVEL);
  nc_handle_error(res);

  /* Leave define mode */
  res = nc_enddef(nc_out);
  nc_handle_error(res);

  /* Write variables */
  res = nc_put_var(nc_out, x_id, x);
  nc_handle_error(res);
  res = nc_put_var(nc_out, e_id, e);
  nc_handle_error(res);
  res = nc_put_var(nc_out, t_id, t);
  nc_handle_error(res);
  res = nc_put_var(nc_out, s_id, s);
  nc_handle_error(res);

  /* Close file */
  res = nc_close(nc_out);
  nc_handle_error(res);

#else
  (void) gr;
  (void) filename;

  (void) printf("Sorry, no NetCDF support.\n");
#endif
}
Пример #2
0
psurface3d
read_nc_surface3d(const char *filename)
{
#ifdef USE_NETCDF
  psurface3d gr;
  real(*x)[3];
  uint(*e)[2];
  uint(*t)[3];
  uint(*s)[3];
  uint      vertices;
  uint      edges;
  uint      triangles;
  int       res, nc_in;
  int       x_id, e_id, t_id, s_id;
  int       vertices_id, edges_id, triangles_id;
  size_t    dim;

  /* Open CDF file */
  res = nc_open(filename, NC_NOWRITE, &nc_in);
  nc_handle_error(res);

  /* Obtain dimensions */
  res = nc_inq_dimid(nc_in, "vertices", &vertices_id);
  nc_handle_error(res);
  res = nc_inq_dimid(nc_in, "edges", &edges_id);
  nc_handle_error(res);
  res = nc_inq_dimid(nc_in, "triangles", &triangles_id);
  nc_handle_error(res);

  /* Get values of dimensions */
  res = nc_inq_dimlen(nc_in, vertices_id, &dim);
  nc_handle_error(res);
  vertices = dim;
  res = nc_inq_dimlen(nc_in, edges_id, &dim);
  nc_handle_error(res);
  edges = dim;
  res = nc_inq_dimlen(nc_in, triangles_id, &dim);
  nc_handle_error(res);
  triangles = dim;

  /* Create surface3d object */
  gr = new_surface3d(vertices, edges, triangles);
  x = gr->x;
  e = gr->e;
  t = gr->t;
  s = gr->s;

  /* Obtain variables */
  res = nc_inq_varid(nc_in, "x", &x_id);
  nc_handle_error(res);
  res = nc_inq_varid(nc_in, "e", &e_id);
  nc_handle_error(res);
  res = nc_inq_varid(nc_in, "t", &t_id);
  nc_handle_error(res);
  res = nc_inq_varid(nc_in, "s", &s_id);
  nc_handle_error(res);

  /* Read variables */
  res = nc_get_var(nc_in, x_id, x);
  res = nc_get_var(nc_in, e_id, e);
  res = nc_get_var(nc_in, t_id, t);
  res = nc_get_var(nc_in, s_id, s);

  /* Close file */
  res = nc_close(nc_in);
  nc_handle_error(res);

  return gr;

#else
  (void) filename;

  (void) printf("Sorry, no NetCDF support.\n");
  return 0;
#endif
}
Пример #3
0
//FUNCTION : createNc
int createNc(char* ncfile, SpiceDouble n_iter, SpiceDouble t[], SpiceDouble *pos_hci[3], SpiceDouble *pos_iau_sun[3], SpiceDouble *pos_hee[3], SpiceDouble lon_hci[], SpiceDouble lat_hci[], SpiceDouble lon_iau_sun[], SpiceDouble lat_iau_sun[], SpiceDouble dist[])
{
	
	/**************** LOCAL VARIABLES ***********************/
	//NC file ID
	int ncid;

	//Dimensions IDs
	int time_dimid;
	int timelength_dimid;
	int dataId;
	int time_tab_dimid[2];
	int data_tab_dimid[2];

	////Variables IDs
	int time_varid;
	int position1_varid;
	int position2_varid;
	int position3_varid;
	int longitude_hci_varid;
	int latitude_hci_varid;
	int longitude_iau_varid;
	int latitude_iau_varid;
	int distance_varid;
	int startTime_varid;
	int stopTime_varid;

	//Stocking time reference
	time_t p;
	char *s;

	//Buffers
	size_t start[2];
	size_t timeCount[2];
	size_t dataCount[2];

	//Dates
    dd_time_t *DDdates = NULL;

	//Other
	int step;
	int retval;
	size_t i;
	size_t j;


	/**************** END OF LOCAL VARIABLES ***********************/


	/**************** OPEN NC FILE ***********************/
	//Create nc file (NC_CLOBBER to overwrite)
	retval = nc_create(ncfile, NC_CLOBBER, &ncid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Create nc file");
	} 
	/**************** END OF OPEN NC FILE ***********************/



	/*************************** DEFINE MODE ****************************/

	//Define dimensions
	retval = nc_def_dim(ncid, TIME_DIM, NC_UNLIMITED, &time_dimid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Time dimension definition");
	} 
	retval = nc_def_dim(ncid, TIMELEN_DIM, TIME_STR_LEN, &timelength_dimid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "TimeLength dimension definition");
	} 
	retval = nc_def_dim(ncid, POS_DIM, 3L, &dataId);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Position dimension definition");
	} 
	

	//Define the netCDF variables
	time_tab_dimid[0] = time_dimid;
	time_tab_dimid[1] = timelength_dimid;
  	data_tab_dimid[0] = time_dimid;
  	data_tab_dimid[1] = dataId;

  	retval = nc_def_var(ncid, TIME_VAR, NC_CHAR, 2, time_tab_dimid, &time_varid);
  	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Time variable");
	}
	retval = nc_def_var(ncid, POS_VAR_HCI, NC_DOUBLE, 2, data_tab_dimid, &position1_varid);
  	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "HCI Position variable");
	} 
	retval = nc_def_var(ncid, POS_VAR_IAU, NC_DOUBLE, 2, data_tab_dimid, &position2_varid);
  	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "IAU_SUN Position variable");
	}
	retval = nc_def_var(ncid, POS_VAR_HEE, NC_DOUBLE, 2, data_tab_dimid, &position3_varid);
  	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "HEE Position variable");
	}
	retval = nc_def_var(ncid, LON_VAR_HCI, NC_DOUBLE, 1, &time_dimid, &longitude_hci_varid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "LON HCI variable");
	}
	retval = nc_def_var(ncid, LAT_VAR_HCI, NC_DOUBLE, 1, &time_dimid, &latitude_hci_varid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "LAT HCI variable");
	} 
	retval = nc_def_var(ncid, LON_VAR_IAU_SUN, NC_DOUBLE, 1, &time_dimid, &longitude_iau_varid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "LON IAU_SUN variable");
	}
	retval = nc_def_var(ncid, LAT_VAR_IAU_SUN, NC_DOUBLE, 1, &time_dimid, &latitude_iau_varid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "LAT IAU_SUN variable");
	}
	retval = nc_def_var(ncid, DIST_VAR, NC_DOUBLE, 1, &time_dimid, &distance_varid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Distance variable");
	}
	retval = nc_def_var(ncid, START_VAR, NC_CHAR, 1, &timelength_dimid, &startTime_varid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "StartTime variable");
	}
	retval = nc_def_var(ncid, STOP_VAR, NC_CHAR, 1, &timelength_dimid, &stopTime_varid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "StopTime variable");
	}

	//Units attributes for netCDF variables
	retval = nc_put_att_text(ncid, position1_varid, "units", strlen(POS_UNIT), POS_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Position unit");
	}
	retval = nc_put_att_text(ncid, position2_varid, "units", strlen(POS_UNIT), POS_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Position unit");
	}
	retval = nc_put_att_text(ncid, position3_varid, "units", strlen(POS_UNIT), POS_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Position unit");
	}
	retval = nc_put_att_text(ncid, distance_varid, "units", strlen(DIST_UNIT), DIST_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Distance unit");
	}
	retval = nc_put_att_text(ncid, longitude_hci_varid, "units", strlen(LON_UNIT), LON_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Longitude unit");
	}
	retval = nc_put_att_text(ncid, latitude_hci_varid, "units", strlen(LAT_UNIT), LAT_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Latitude unit");
	}
	retval = nc_put_att_text(ncid, longitude_iau_varid, "units", strlen(LON_UNIT), LON_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Longitude unit");
	}
	retval = nc_put_att_text(ncid, latitude_iau_varid, "units", strlen(LAT_UNIT), LAT_UNIT);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Latitude unit");
	}
	retval = nc_put_att_text(ncid, NC_GLOBAL, "Source", strlen(SOURCE), SOURCE);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Source");
	}

	time(&p);
	s = ctime(&p);
	retval = nc_put_att_text(ncid, NC_GLOBAL, "Created", 24, s);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Created argument");
	}
	

	//End of define mode
	retval = nc_enddef(ncid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "End define mode");
	}

	/*********************** END OF DEFINE MODE ****************************/





	/************************ WRITING MODE ********************************/
  	//Configure buffers
	timeCount[0] = 1L;
	timeCount[1] = TIME_STR_LEN;
	dataCount[0] = 1;
	dataCount[1] = 3L;
	start[1] = 0L;

	//Build DDdate
	DDdates = (dd_time_t*)malloc((int)n_iter*sizeof(dd_time_t));
	if(DDdates == NULL)
	{
		printf("[ERROR] Unable to build DD dates\n");

		exit(EXIT_FAILURE);
	}
	
	for (i = 0; i < n_iter; i++)
	{
		time2DDtime(t[i], &DDdates[i]);
	}

	//Write datas
    for (i = 0; i < n_iter; i++)
    {
    	start[0] = i;
	    retval = nc_put_vara_text(ncid, time_varid, start, timeCount, DDdates[i]);
		if (retval != NC_NOERR)
		{
			nc_handle_error(retval, "Write time variable");
		}
    }

	for (i = 0; i < n_iter; i++)
	{
		start[0] = i;
		retval = nc_put_vara_double(ncid, position1_varid, start, dataCount, pos_hci[i]);
	 	if (retval != NC_NOERR)
		{
			nc_handle_error(retval, "Write HCI position variable");
		}
	}

	for (i = 0; i < n_iter; i++)
	{
		start[0] = i;
		retval = nc_put_vara_double(ncid, position2_varid, start, dataCount, pos_iau_sun[i]);
	 	if (retval != NC_NOERR)
		{
			nc_handle_error(retval, "Write IAU_SUN position variable");
		}
	}

	for (i = 0; i < n_iter; i++)
	{
		start[0] = i;
		retval = nc_put_vara_double(ncid, position3_varid, start, dataCount, pos_hee[i]);
	 	if (retval != NC_NOERR)
		{
			nc_handle_error(retval, "Write HEE position variable");
		}
	}

	retval = nc_put_var_double(ncid, longitude_hci_varid, lon_hci);
 	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Write LON HCI variable");
	}

	retval = nc_put_var_double(ncid, latitude_hci_varid, lat_hci);
 	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Write LAT HCI variable");
	}

	retval = nc_put_var_double(ncid, longitude_iau_varid, lon_iau_sun);
 	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Write LON IAU_SUN variable");
	}

	retval = nc_put_var_double(ncid, latitude_iau_varid, lat_iau_sun);
 	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Write LAT IAU_SUN variable");
	}

	retval = nc_put_var_double(ncid, distance_varid, dist);
 	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Write distance variable");
	}

	retval = nc_put_var_text(ncid, startTime_varid, DDdates[0]);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Write start time variable");
	}

	retval = nc_put_var_text(ncid, stopTime_varid, DDdates[(int)n_iter-1]);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Write stop time variable");
	}

	//Free memory
	free(DDdates);

	/************************ END OF WRITING MODE ********************************/



	/**************** CLOSE NC FILE ***********************/
	//Close the file
	retval = nc_close(ncid);
	if (retval != NC_NOERR)
	{
		nc_handle_error(retval, "Close nc file");
	}

	printf("[INFO] %s has been created\n", ncfile);
	/**************** END OF CLOSE NC FILE ***********************/

	return 0;
}