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; }
static PyObject *PyCMOR_axis(PyObject * self, PyObject * args) { signal(signal_to_catch, signal_handler); int ierr, axis_id, n = 0; char *name; char *units; char *interval; int length; char type; void *coord_vals; void *cell_bounds; int cell_bounds_ndim; char *tmpstr = NULL; PyObject *coords_obj, *bounds_obj; PyArrayObject *coords = NULL, *bounds = NULL; /************************************************************************/ /* HUGE assumtion here is that the data is contiguous! */ /************************************************************************/ if (!PyArg_ParseTuple (args, "ssiOcOis", &name, &units, &length, &coords_obj, &type, &bounds_obj, &cell_bounds_ndim, &interval)) return NULL; if (coords_obj == Py_None) { coord_vals = NULL; } else { coords = (PyArrayObject *) PyArray_ContiguousFromObject(coords_obj, NPY_NOTYPE, 1, 0); if (PyArray_NDIM(coords) != 1) { printf("ok we need to pass contiguous flattened arrays only!\n"); return NULL; } if (type != 'c') { coord_vals = (void *)PyArray_DATA(coords); n = cell_bounds_ndim; } else { tmpstr = (char *)malloc(sizeof(char) * length * (cell_bounds_ndim + 1)); for (ierr = 0; ierr < length; ierr++) { coord_vals = (void *)PyArray_GETPTR1(coords, ierr); strncpy(&tmpstr[ierr * (cell_bounds_ndim + 1)], coord_vals, cell_bounds_ndim); tmpstr[ierr * (cell_bounds_ndim + 1) + cell_bounds_ndim] = '\0'; } coord_vals = &tmpstr[0]; n = cell_bounds_ndim + 1; for (ierr = 0; ierr < length; ierr++) { } } } if (bounds_obj == Py_None) { cell_bounds = NULL; } else { bounds = (PyArrayObject *) PyArray_ContiguousFromObject(bounds_obj, NPY_NOTYPE, 1, 0); if (PyArray_NDIM(bounds) != 1) { printf("ok we need to pass contiguous flattened arrays only!\n"); return NULL; } cell_bounds = (void *)PyArray_DATA(bounds); } ierr = cmor_axis(&axis_id, name, units, length, coord_vals, type, cell_bounds, n, interval); if (coords != NULL) { Py_DECREF(coords); } if (bounds != NULL) { Py_DECREF(bounds); } if (type == 'c') { free(tmpstr); } if (ierr != 0 || raise_exception) { raise_exception = 0; PyErr_Format(CMORError, exception_message, "axis"); return NULL; } return (Py_BuildValue("i", axis_id)); }
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; }