Example #1
0
static PyObject *
  PyCMOR_get_variable_attribute(PyObject *self,PyObject *args)
{
  char *name;
  char value[CMOR_MAX_STRING];
  int ierr, var_id;
  if (!PyArg_ParseTuple(args,"is",&var_id,&name))
    return NULL;
  ierr = cmor_get_variable_attribute(var_id,name,(void *)value);
  if (ierr != 0 ) return NULL;
  return Py_BuildValue("s",value);
}
Example #2
0
static PyObject *PyCMOR_set_variable_attribute(PyObject * self, PyObject * args)
{
    signal(signal_to_catch, signal_handler);
    char *name;
    char *value;
    char *type;
    long lValue;
    int nValue;
    float fValue;
    double dValue;
    PyObject *oValue;
    int ierr, var_id;
    value = NULL;
    if (!PyArg_ParseTuple(args, "issO", &var_id, &name, &type, &oValue))
        return NULL;

#if PY_MAJOR_VERSION >= 3
    if(PyBytes_Check(oValue)) {
        value = PyBytes_AsString(oValue);
#else
    if(PyString_Check(oValue)) {
        value = PyString_AsString(oValue);
#endif
    } else if(PyLong_Check(oValue)) {
        lValue = PyLong_AsLong(oValue);
    } else if (PyFloat_Check(oValue)) {
        dValue = PyFloat_AsDouble(oValue);
    }

    if (type[0] == 'f') {
        fValue = (float) dValue;
        value = (char *) &fValue;
    } else if (type[0] == 'd') {
        value = (char *) &dValue;
    } else if (type[0] == 'i') {
        nValue = (int) lValue;
        value = (char *) &nValue;
    } else if (type[0] == 'l') {
        value = (char *) &lValue;
    }

    ierr = cmor_set_variable_attribute(var_id, name, type[0], (void *)value);

    if (ierr != 0 || raise_exception) {
        raise_exception = 0;
        PyErr_Format(CMORError, exception_message, "set_variable_attribute");
        return NULL;
    }

    return (Py_BuildValue("i", ierr));
}

/************************************************************************/
/*                   PyCMOR_get_variable_attribute()                    */
/************************************************************************/
static PyObject *PyCMOR_get_variable_attribute(PyObject * self, PyObject * args)
{
    signal(signal_to_catch, signal_handler);
    char *name;
    char value[CMOR_MAX_STRING];
    int ierr, var_id;

    if (!PyArg_ParseTuple(args, "is", &var_id, &name))
        return NULL;

    ierr = cmor_get_variable_attribute(var_id, name, (void *)value);

    if (ierr != 0 || raise_exception) {
        raise_exception = 0;
        PyErr_Format(CMORError, exception_message, "get_variable_attribute");
        return NULL;
    }

    return (Py_BuildValue("s", value));
}
Example #3
0
int cmor_grid(int *grid_id, int ndims, int *axes_ids, char type, void *lat, void *lon, int nvertices, void *blat, void *blon) {
  int i,j,n,did_vertices=0;
  char msg[CMOR_MAX_STRING];
  int axes[2];
  double *dummy_values;

  cmor_add_traceback("cmor_grid");
  if ((axes_ids==NULL) || (ndims==0)) {
    snprintf(msg,CMOR_MAX_STRING,"You need to define the grid axes first");
    cmor_handle_error(msg,CMOR_CRITICAL);
  }
  cmor_ngrids+=1;
  if (cmor_ngrids>=CMOR_MAX_GRIDS) {
    snprintf(msg,CMOR_MAX_STRING,"Too many grids defined, maximum possible grids is currently set to %i", CMOR_MAX_GRIDS);
    cmor_handle_error(msg,CMOR_CRITICAL);
  }
  n=1;
  for (i=0;i<ndims;i++) {
    if (axes_ids[i]>cmor_naxes) {
      snprintf(msg,CMOR_MAX_STRING,"Defining grid, Axis %i not defined yet",axes_ids[i]);
      cmor_handle_error(msg,CMOR_CRITICAL);
    }
    if (cmor_tables[cmor_axes[axes_ids[i]].ref_table_id].axes[cmor_axes[axes_ids[i]].ref_axis_id].axis=='T') {
      cmor_grids[cmor_ngrids].istimevarying = 1;
/*       snprintf(msg,CMOR_MAX_STRING,"defining grid: axis: %s is time axis, grids are not allowed to be time-varying at the moment",cmor_axes[axes_ids[i]].id); */
/*       cmor_handle_error(msg,CMOR_CRITICAL); */
    }
    cmor_grids[cmor_ngrids].original_axes_ids[i]=axes_ids[i];
    cmor_grids[cmor_ngrids].axes_ids[i]=axes_ids[i];
    cmor_axes[axes_ids[i]].isgridaxis=1;
    n*=cmor_axes[axes_ids[i]].length;
  }
  cmor_grids[cmor_ngrids].ndims = ndims;
  cmor_grids[cmor_ngrids].nvertices = nvertices;
  if (lat==NULL) {
    if (cmor_grids[cmor_ngrids].istimevarying != 1) {
      snprintf(msg,CMOR_MAX_STRING,"you need to pass the latitude values when defining a grid");
      cmor_handle_error(msg,CMOR_CRITICAL);
    }
  }
  else {
    axes[0]=-cmor_ngrids-CMOR_MAX_GRIDS;
    if (cmor_grids[cmor_ngrids].istimevarying != 1) {
      cmor_copy_data(&cmor_grids[cmor_ngrids].lats,lat,type,n);
      cmor_variable(&cmor_grids[cmor_ngrids].associated_variables[0],"latitude","degrees_north",1,&axes[0],'d',NULL,NULL,NULL,NULL,NULL,NULL);
      cmor_vars[cmor_grids[cmor_ngrids].associated_variables[0]].needsinit=0;
      /*     for (i=0;i<5;i++) { */
      /*       printf("%i:  %lf\n",i,cmor_grids[cmor_ngrids].lats[i]); */
      /*     } */
    }
  }
  if (lon==NULL) {
    if (cmor_grids[cmor_ngrids].istimevarying != 1) {
      snprintf(msg,CMOR_MAX_STRING,"you need to pass the longitude values when defining a grid");
      cmor_handle_error(msg,CMOR_CRITICAL);
    }
  }
  else {
    cmor_copy_data(&cmor_grids[cmor_ngrids].lons,lon,type,n);
    axes[0]=-cmor_ngrids-CMOR_MAX_GRIDS;
    /*     for (i=0;i<5;i++) printf("%i:  %lf\n",i,cmor_grids[cmor_ngrids].lons[i]); */
    cmor_variable(&cmor_grids[cmor_ngrids].associated_variables[1],"longitude","degrees_east",1,&axes[0],'d',NULL,NULL,NULL,NULL,NULL,NULL);
    cmor_vars[cmor_grids[cmor_ngrids].associated_variables[1]].needsinit=0;
  }
  if (blat==NULL) {
    if (cmor_grids[cmor_ngrids].istimevarying != 1) {
      snprintf(msg,CMOR_MAX_STRING,"it is recommended you pass the latitude bounds values when defining a grid");
      cmor_handle_error(msg,CMOR_WARNING);
    }
  }
  else {
    cmor_copy_data(&cmor_grids[cmor_ngrids].blats,blat,type,n*nvertices);
    dummy_values=malloc(sizeof(double)*nvertices);
    for (j=0;j<nvertices;j++) dummy_values[j]=(double)j;
    cmor_axis(&axes[1],"vertices","1",nvertices,dummy_values,'d',NULL,0,NULL);
    free(dummy_values);
    did_vertices=1;
    cmor_variable(&cmor_grids[cmor_ngrids].associated_variables[2],"vertices_latitude","degrees_north",2,&axes[0],'d',NULL,NULL,NULL,NULL,NULL,NULL);
    cmor_vars[cmor_grids[cmor_ngrids].associated_variables[2]].needsinit=0;
    if (cmor_has_variable_attribute(cmor_grids[cmor_ngrids].associated_variables[0],"bounds")==0) {
      cmor_get_variable_attribute(cmor_grids[cmor_ngrids].associated_variables[0],"bounds",&msg);
      strncat(msg," ",CMOR_MAX_STRING-strlen(msg) );
      strncat(msg,cmor_vars[cmor_grids[cmor_ngrids].associated_variables[2]].id,CMOR_MAX_STRING-strlen(msg) );
    }
    else {
      strncpy(msg,cmor_vars[cmor_grids[cmor_ngrids].associated_variables[2]].id,CMOR_MAX_STRING);
    }
    cmor_set_variable_attribute_internal(cmor_grids[cmor_ngrids].associated_variables[0],"bounds",'c',msg);
  }
  if (blon==NULL) {
    if (cmor_grids[cmor_ngrids].istimevarying != 1) {
      snprintf(msg,CMOR_MAX_STRING,"it is recommended you pass the longitude bounds values when defining a grid");
      cmor_handle_error(msg,CMOR_WARNING);
    }
  }
  else {
    cmor_copy_data(&cmor_grids[cmor_ngrids].blons,blon,type,n*nvertices);
    if (did_vertices == 0 ) {
      dummy_values=malloc(sizeof(double)*nvertices);
      for (j=0;j<nvertices;j++) dummy_values[j]=(double)j;
      cmor_axis(&axes[1],"vertices","1",nvertices,dummy_values,'d',NULL,0,NULL);
      free(dummy_values);
    }
    cmor_variable(&cmor_grids[cmor_ngrids].associated_variables[3],"vertices_longitude","degrees_east",2,&axes[0],'d',NULL,NULL,NULL,NULL,NULL,NULL);
    cmor_vars[cmor_grids[cmor_ngrids].associated_variables[3]].needsinit=0;
    if (cmor_has_variable_attribute(cmor_grids[cmor_ngrids].associated_variables[1],"bounds")==0) {
      cmor_get_variable_attribute(cmor_grids[cmor_ngrids].associated_variables[1],"bounds",&msg);
      strncat(msg," ",CMOR_MAX_STRING-strlen(msg) );
      strncat(msg,cmor_vars[cmor_grids[cmor_ngrids].associated_variables[3]].id,CMOR_MAX_STRING-strlen(msg) );
    }
    else {
      strncpy(msg,cmor_vars[cmor_grids[cmor_ngrids].associated_variables[3]].id,CMOR_MAX_STRING);
    }
    cmor_set_variable_attribute_internal(cmor_grids[cmor_ngrids].associated_variables[1],"bounds",'c',msg);
  }
  /*   if (area==NULL) { */
  /*     snprintf(msg,CMOR_MAX_STRING,"it is recommended you pass the area values when defining a grid"); */
  /*     cmor_handle_error(msg,CMOR_WARNING); */
  /*   } */
  /*   else { */
  /*     cmor_copy_data(&cmor_grids[cmor_ngrids].area,area,type,n); */
  /*     cmor_variable(&cmor_grids[cmor_ngrids].associated_variables[4],"area","m2",1,&axes[0],'d',NULL,NULL,NULL,NULL,NULL,NULL); */
  /*     cmor_vars[cmor_grids[cmor_ngrids].associated_variables[4]].needsinit=0; */
  /*   } */
  *grid_id = -cmor_ngrids-CMOR_MAX_GRIDS;
  cmor_pop_traceback();
  return 0;
}
Example #4
0
int cmor_time_varying_grid_coordinate(int *coord_grid_id, int  grid_id, char *table_entry, char *units, char type, void *missing, int *coordinate_type) {
  int ierr=0,j;
  int axes[2];
  char msg[CMOR_MAX_STRING];
  int ctype=-1;
  double *dummy_values;
  int nvertices=cmor_grids[-grid_id-CMOR_MAX_GRIDS].nvertices;

  axes[0]=grid_id;

  cmor_add_traceback("cmor_time_varying_grid_coordinate");
  cmor_is_setup();

  strcpy(msg,"not found");
  if (coordinate_type == NULL) {
    for (j=0;j<cmor_tables[cmor_axes[cmor_grids[-grid_id-CMOR_MAX_GRIDS].axes_ids[0]].ref_table_id].nvars;j++) {
      if (strcmp(cmor_tables[cmor_axes[cmor_grids[-grid_id-CMOR_MAX_GRIDS].axes_ids[0]].ref_table_id].vars[j].id,table_entry)==0) {
	strncpy(msg,cmor_tables[cmor_axes[cmor_grids[-grid_id-CMOR_MAX_GRIDS].axes_ids[0]].ref_table_id].vars[j].standard_name,CMOR_MAX_STRING);
	break;
      }
    }
    if (strcmp(msg,"latitude")==0) ctype = 0;
    if (strcmp(msg,"longitude")==0) ctype = 1;
    if (strcmp(msg,"vertices_latitude")==0) ctype = 2;
    if (strcmp(msg,"vertices_longitude")==0) ctype = 3;
  }
  else {
    ctype = *coordinate_type;
  }
  switch (ctype) {
  case (0) :
    ierr = cmor_variable(coord_grid_id,table_entry,units,1,axes,type,missing,NULL,NULL,NULL,NULL,NULL);
    cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[0] = *coord_grid_id;
    /* ??? additional attributes to add to this variable ? */
    break;
  case (1) :
    ierr = cmor_variable(coord_grid_id,table_entry,units,1,axes,type,missing,NULL,NULL,NULL,NULL,NULL);
    cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[1] = *coord_grid_id;
    /* ??? additional attributes to add to this variable ? */
    break;
  case (2) :
    if (nvertices == 0 ) {
      sprintf(msg,"your defining a vertices dependent variable (%s) associated wth grid %i, but you declared this grid as having 0 vertices",table_entry,grid_id);
      cmor_handle_error(msg,CMOR_CRITICAL);
    }
    if (cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[3] == -1) {
      dummy_values=malloc(sizeof(double)*nvertices);
      for (j=0;j<nvertices;j++) dummy_values[j]=(double)j;
      cmor_axis(&axes[1],"vertices","1",nvertices,dummy_values,'d',NULL,0,NULL);
      free(dummy_values);
      cmor_grids[-grid_id-CMOR_MAX_GRIDS].nvertices = axes[1];
    }
    else {
      axes[1] = cmor_grids[-grid_id-CMOR_MAX_GRIDS].nvertices;
    }
    ierr = cmor_variable(coord_grid_id,table_entry,units,2,axes,type,missing,NULL,NULL,NULL,NULL,NULL);
    cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[2] = *coord_grid_id;
    /* adds the bounds attribute */
    if (cmor_has_variable_attribute(cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[0],"bounds")==0) {
      cmor_get_variable_attribute(cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[0],"bounds",&msg);
      strncat(msg," ",CMOR_MAX_STRING-strlen(msg) );
      strncat(msg,cmor_vars[*coord_grid_id].id,CMOR_MAX_STRING-strlen(msg) );
    }
    else {
      strncpy(msg,cmor_vars[*coord_grid_id].id,CMOR_MAX_STRING);
    }
    cmor_set_variable_attribute_internal(cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[0],"bounds",'c',msg);
    break;
  case (3) :
    if (nvertices == 0 ) {
      sprintf(msg,"your defining a vertices dependent variable (%s) associated wth grid %i, but you declared this grid as having 0 vertices",table_entry,grid_id);
      cmor_handle_error(msg,CMOR_CRITICAL);
    }
    if (cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[2] == -1) {
      dummy_values=malloc(sizeof(double)*nvertices);
      for (j=0;j<nvertices;j++) dummy_values[j]=(double)j;
      cmor_axis(&axes[1],"vertices","1",nvertices,dummy_values,'d',NULL,0,NULL);
      free(dummy_values);
      cmor_grids[-grid_id-CMOR_MAX_GRIDS].nvertices = axes[1];
    }
    else {
      axes[1] = cmor_grids[-grid_id-CMOR_MAX_GRIDS].nvertices;
    }
    ierr = cmor_variable(coord_grid_id,table_entry,units,2,axes,type,missing,NULL,NULL,NULL,NULL,NULL);
    cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[3] = *coord_grid_id;
    /* adds the bounds attribute */
    if (cmor_has_variable_attribute(cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[1],"bounds")==0) {
      cmor_get_variable_attribute(cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[1],"bounds",&msg);
      strncat(msg," ",CMOR_MAX_STRING-strlen(msg) );
      strncat(msg,cmor_vars[*coord_grid_id].id,CMOR_MAX_STRING-strlen(msg) );
    }
    else {
      strncpy(msg,cmor_vars[*coord_grid_id].id,CMOR_MAX_STRING);
    }
    cmor_set_variable_attribute_internal(cmor_grids[cmor_vars[*coord_grid_id].grid_id].associated_variables[1],"bounds",'c',msg);
    break;
  default:
    sprintf(msg,"unknown coord type: %i",ctype);
    cmor_handle_error(msg,CMOR_CRITICAL);
    break;
  }
  cmor_vars[*coord_grid_id].needsinit=0;

  cmor_pop_traceback();
  return ierr;
}