⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 excopy.c

📁 一个用来实现偏微分方程中网格的计算库
💻 C
📖 第 1 页 / 共 2 页
字号:
     spatial dimension, so get that now.*/  ex_get_dimension(in_id, DIM_NUM_DIM, "dimension", &spatial_dim, &temp, routine);    if (in_large == 0 && out_large == 1) {    /* output file will have coordx, coordy, coordz (if 3d).  See if       they are already defined in output file. Assume either all or       none are defined. */    {      int var_out_idx, var_out_idy, var_out_idz;      int status1 = nc_inq_varid(out_id, VAR_COORD_X, &var_out_idx);      int status2 = nc_inq_varid(out_id, VAR_COORD_Y, &var_out_idy);      int status3 = nc_inq_varid(out_id, VAR_COORD_Y, &var_out_idz);      if (status1 == NC_NOERR && status2 == NC_NOERR &&          (spatial_dim == 2 || status3 == NC_NOERR)) {        return NC_NOERR; /* already defined in output file */      }    }    /* Get dimid of the num_nodes dimension in output file... */    nc_inq_dimid(out_id, DIM_NUM_NODES, &dim_out_id[0]);    /* Define the variables in the output file */        /* Define according to the EXODUS file's IO_word_size */    nbr_dim = 1;    nc_def_var(out_id, VAR_COORD_X, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id);    nc_def_var(out_id, VAR_COORD_Y, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id);    if (spatial_dim == 3)      nc_def_var(out_id, VAR_COORD_Z, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id);  }  if (in_large == 1 && out_large == 0) {    /* input file has coordx, coordy, coordz (if 3d); output will only       have "coord".  See if is already defined in output file. */    status = nc_inq_varid(out_id, VAR_COORD, &var_out_id);    if (status == NC_NOERR)      return NC_NOERR; /* already defined in output file */    /* Get dimid of the spatial dimension and num_nodes dimensions in output file... */    nc_inq_dimid(out_id, DIM_NUM_DIM,   &dim_out_id[0]);    nc_inq_dimid(out_id, DIM_NUM_NODES, &dim_out_id[1]);    /* Define the variable in the output file */        /* Define according to the EXODUS file's IO_word_size */    nbr_dim = 2;    nc_def_var(out_id, VAR_COORD, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id);  }  return var_out_id;}int cpy_var_def(int in_id,int out_id,int rec_dim_id,char *var_nm)/*   int in_id: input netCDF input-file ID   int out_id: input netCDF output-file ID   int rec_dim_id: input input-file record dimension ID   char *var_nm: input variable name   int cpy_var_def(): output output-file variable ID */{  /* Routine to copy the variable metadata from an input netCDF file   * to an output netCDF file.    */  int status;  int *dim_in_id;  int *dim_out_id;  int idx;  int nbr_dim;  int var_in_id;  int var_out_id;  nc_type var_type;  /* See if the requested variable is already in the output file. */  status = nc_inq_varid(out_id, var_nm, &var_out_id);  if(status == NC_NOERR)    return var_out_id;  /* See if the requested variable is in the input file. */  nc_inq_varid(in_id, var_nm, &var_in_id);  /* Get the type of the variable and the number of dimensions. */  nc_inq_vartype (in_id, var_in_id, &var_type);  nc_inq_varndims(in_id, var_in_id, &nbr_dim);  /* Recall:     1. The dimensions must be defined before the variable.     2. The variable must be defined before the attributes. */  /* Allocate space to hold the dimension IDs */  dim_in_id=malloc(nbr_dim*sizeof(int));   dim_out_id=malloc(nbr_dim*sizeof(int));  /* Get the dimension IDs */  nc_inq_vardimid(in_id, var_in_id, dim_in_id);  /* Get the dimension sizes and names */  for(idx=0;idx<nbr_dim;idx++){    char dim_nm[NC_MAX_NAME];    size_t dim_sz;    nc_inq_dim(in_id, dim_in_id[idx], dim_nm, &dim_sz);    /* See if the dimension has already been defined */    status = nc_inq_dimid(out_id, dim_nm, &dim_out_id[idx]);    /* If the dimension hasn't been defined, copy it */    if (status != NC_NOERR) {      if (dim_in_id[idx] != rec_dim_id) {        nc_def_dim(out_id, dim_nm, dim_sz, &dim_out_id[idx]);      } else {        nc_def_dim(out_id, dim_nm, NC_UNLIMITED, &dim_out_id[idx]);      }     }   }   /* Define the variable in the output file */  /* If variable is float or double, define it according to the EXODUS     file's IO_word_size */  if ((var_type == NC_FLOAT) || (var_type == NC_DOUBLE)) {    nc_def_var(out_id, var_nm, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id);  } else {    nc_def_var(out_id, var_nm, var_type,            nbr_dim, dim_out_id, &var_out_id);  }  /* Free the space holding the dimension IDs */  (void)free(dim_in_id);  (void)free(dim_out_id);  return var_out_id;} /* end cpy_var_def() */intcpy_var_val(int in_id,int out_id,char *var_nm)/*   int in_id: input netCDF input-file ID   int out_id: input netCDF output-file ID   char *var_nm: input variable name */{  /* Routine to copy the variable data from an input netCDF file   * to an output netCDF file.    */  int *dim_id;  int idx;  int nbr_dim;  int var_in_id;  int var_out_id;  size_t *dim_cnt;  size_t *dim_sz;  size_t *dim_srt;  size_t var_sz=1L;  nc_type var_type_in, var_type_out;  void *void_ptr;  /* Get the var_id for the requested variable from both files. */  nc_inq_varid(in_id, var_nm, &var_in_id);  nc_inq_varid(out_id,var_nm, &var_out_id);   /* Get the number of dimensions for the variable. */  nc_inq_vartype( out_id, var_out_id, &var_type_out);  nc_inq_varndims(out_id, var_out_id, &nbr_dim);  nc_inq_vartype( in_id,   var_in_id, &var_type_in);  nc_inq_varndims(in_id,   var_in_id, &nbr_dim);   /* Allocate space to hold the dimension IDs */  dim_cnt = malloc(nbr_dim*sizeof(size_t));  dim_id=malloc(nbr_dim*sizeof(int));  dim_sz=malloc(nbr_dim*sizeof(size_t));  dim_srt=malloc(nbr_dim*sizeof(size_t));   /* Get the dimension IDs from the input file */  nc_inq_vardimid(in_id, var_in_id, dim_id);   /* Get the dimension sizes and names from the input file */  for(idx=0;idx<nbr_dim;idx++){  /* NB: For the unlimited dimension, ncdiminq() returns the maximum     value used so far in writing data for that dimension.     Thus if you read the dimension sizes from the output file, then     the ncdiminq() returns dim_sz=0 for the unlimited dimension     until a variable has been written with that dimension. This is     the reason for always reading the input file for the dimension     sizes. */    nc_inq_dimlen(in_id,dim_id[idx],dim_cnt+idx);    /* Initialize the indicial offset and stride arrays */    dim_srt[idx]=0L;    var_sz*=dim_cnt[idx];  } /* end loop over dim */  /* Allocate enough space to hold the variable */  void_ptr=malloc(var_sz * type_size(var_type_in));  /* Get the variable */  /* if variable is float or double, convert if necessary */  if(nbr_dim==0){  /* variable is a scalar */    if (var_type_in == NC_INT && var_type_out == NC_INT) {      nc_get_var1_int(in_id,  var_in_id,  0L, void_ptr);      nc_put_var1_int(out_id, var_out_id, 0L, void_ptr);    }    else if (var_type_in == NC_FLOAT) {      nc_get_var1_float(in_id,  var_in_id,  0L, void_ptr);      nc_put_var1_float(out_id, var_out_id, 0L, void_ptr);    }    else if (var_type_in == NC_DOUBLE) {      nc_get_var1_double(in_id,  var_in_id,  0L, void_ptr);      nc_put_var1_double(out_id, var_out_id, 0L, void_ptr);    }    else if (var_type_in == NC_CHAR) {      nc_get_var1_text(in_id,  var_in_id,  0L, void_ptr);      nc_put_var1_text(out_id, var_out_id, 0L, void_ptr);    }    else {      assert(1==0);    }  } else { /* variable is a vector */    if (var_type_in == NC_INT && var_type_out == NC_INT) {      nc_get_var_int(in_id,  var_in_id,  void_ptr);      nc_put_var_int(out_id, var_out_id, void_ptr);    }    else if (var_type_in == NC_FLOAT) {      nc_get_var_float(in_id,  var_in_id,  void_ptr);      nc_put_var_float(out_id, var_out_id, void_ptr);    }    else if (var_type_in == NC_DOUBLE) {      nc_get_var_double(in_id,  var_in_id,  void_ptr);      nc_put_var_double(out_id, var_out_id, void_ptr);    }    else if (var_type_in == NC_CHAR) {      nc_get_var_text(in_id,  var_in_id,  void_ptr);      nc_put_var_text(out_id, var_out_id, void_ptr);    }    else {      assert(1==0);    }  } /* end if variable is an array */  /* Free the space that held the dimension IDs */  (void)free(dim_cnt);  (void)free(dim_id);  (void)free(dim_sz);  (void)free(dim_srt);  /* Free the space that held the variable */  (void)free(void_ptr);  return(EX_NOERR);} /* end cpy_var_val() */intcpy_coord_val(int in_id,int out_id,char *var_nm,              int in_large, int out_large)/*   int in_id: input netCDF input-file ID   int out_id: input netCDF output-file ID   char *var_nm: input variable name */{  /* Routine to copy the coordinate data from an input netCDF file   * to an output netCDF file.    */  const char *routine = NULL;  int i, temp;  size_t spatial_dim, num_nodes;  size_t start[2], count[2];  nc_type var_type_in, var_type_out;  void *void_ptr;  /* Handle easiest situation first: in_large matches out_large */  if (in_large == out_large)    return cpy_var_val(in_id, out_id, var_nm);    /* At this point, know that in_large != out_large, so will need to     either copy a vector to multiple scalars or vice-versa.  Also     will need a couple dimensions, so get them now.*/  ex_get_dimension(in_id, DIM_NUM_DIM, "dimension", &spatial_dim, &temp, routine);  ex_get_dimension(in_id, DIM_NUM_NODES, "nodes",   &num_nodes, &temp, routine);  if (in_large == 0 && out_large == 1) {    /* output file will have coordx, coordy, coordz (if 3d). */    /* Get the var_id for the requested variable from both files. */    int var_in_id, var_out_id[3];    nc_inq_varid(in_id, VAR_COORD, &var_in_id);    nc_inq_varid(out_id, VAR_COORD_X, &var_out_id[0]);    nc_inq_varid(out_id, VAR_COORD_Y, &var_out_id[1]);    nc_inq_varid(out_id, VAR_COORD_Z, &var_out_id[2]);    nc_inq_vartype( in_id, var_in_id,     &var_type_in);    nc_inq_vartype(out_id, var_out_id[0], &var_type_out);    void_ptr=malloc(num_nodes * type_size(var_type_in));    /* Copy each component of the variable... */    for (i=0; i < spatial_dim; i++) {      start[0] = i; start[1] = 0;      count[0] = 1; count[1] = num_nodes;      if (var_type_in == NC_FLOAT) {	nc_get_vara_float(in_id, var_in_id,     start, count, void_ptr);	nc_put_var_float(out_id, var_out_id[i],               void_ptr);      } else {	assert(var_type_in == NC_DOUBLE);	nc_get_vara_double(in_id, var_in_id,    start, count, void_ptr);	nc_put_var_double(out_id, var_out_id[i],              void_ptr);      }    }  }  if (in_large == 1 && out_large == 0) {    /* input file will have coordx, coordy, coordz (if 3d); output has only "coord" */    int var_in_id[3], var_out_id;    nc_inq_varid(in_id,  VAR_COORD_X, &var_in_id[0]);    nc_inq_varid(in_id,  VAR_COORD_Y, &var_in_id[1]);    nc_inq_varid(in_id,  VAR_COORD_Z, &var_in_id[2]);    nc_inq_varid(out_id, VAR_COORD,   &var_out_id);        nc_inq_vartype(in_id,  var_in_id[0], &var_type_in);    nc_inq_vartype(out_id, var_out_id,   &var_type_out);    void_ptr=malloc(num_nodes * type_size(var_type_in));    /* Copy each component of the variable... */    for (i=0; i < spatial_dim; i++) {      start[0] = i; start[1] = 0;      count[0] = 1; count[1] = num_nodes;      if (var_type_in == NC_FLOAT) {        nc_get_var_float( in_id,  var_in_id[i],               void_ptr);        nc_put_vara_float(out_id, var_out_id,   start, count, void_ptr);            } else {        nc_get_var_double( in_id,  var_in_id[i],               void_ptr);        nc_put_vara_double(out_id, var_out_id,   start, count, void_ptr);      }    }  }  /* Free the space that held the variable */  (void)free(void_ptr);  return(EX_NOERR);} /* end cpy_coord_val() */void update_internal_structs( int out_exoid, ex_inquiry inqcode, struct list_item** ctr_list ){  int i;  int number;  double fdum;  char* cdum = 0;  ex_inquire (out_exoid, inqcode, &number, &fdum, cdum);  if (number > 0) {    for (i=0; i<number; i++)      ex_inc_file_item (out_exoid, ctr_list);  }}size_t type_size(nc_type type){  if (type == NC_CHAR)    return sizeof(char);  else if (type == NC_INT)    return sizeof(int);  else if (type == NC_FLOAT)    return sizeof(float);  else if (type == NC_DOUBLE)    return sizeof(double);  else    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -