Difference between revisions of "UCVM API"

From SCECpedia
Jump to navigationJump to search
Line 1: Line 1:
= UCVM API =
+
= Overview =
== API Definitions ==
+
 
 +
The Unified California Velocity Model API (UCVM API) is a programming interface that allows the user to directly
 +
query multiple velocity models in a program. The API currently support these models:
 +
 
 +
* CVM-S
 +
* CVM-H
 +
* USGS Central California
 +
* Hadley-Kanamori 1D
 +
 
 +
 
 +
= API =
 +
 
 +
== Data Types and Definitions ==
  
 
<pre>
 
<pre>
// Supported models. This list may be specified in a configuration file, along with their installation
+
/* Maximum number of supported models */
// directories
+
#define UCVM_MAX_MODELS 10
enumerated CVM_MODEL_TYPES = {CVM_MODEL_NONE, CVM_MODEL_ALL, CVM_MODEL_BACKGROUND, CVM_MODEL_H63, CVM_MODEL_H62, CVM_MODEL_H10_12_0, CVM_MODEL_4, CVM_MODEL_ETREE}
 
  
// Supported GTLs. This list may be specified in a configuration file, along with their installation
+
/* Maximum projection description length */
// directories
+
#define UCVM_MAX_PROJ_LEN 256
enumerated CVM_GTL_TYPES = {CVM_GTL_NONE, CVM_GTL_ALL, CVM_GTL_VS30}
 
  
// Supported model coordinates. Geo versus UTM, and depth versus elevation
+
/* Maximum model label length */
enumerated CVM_COORD_TYPES = {CVM_COORD_GEO_DEPTH, CVM_COORD_GEO_ELEV, CVM_COORD_UTM_DEPTH, CVM_COORD_UTM_ELEV}
+
#define UCVM_MAX_LABEL_LEN 64
  
// A single query point (x, y, z). The API knows how to interpet the point with the coord_type flag
+
/* Maximum model version length */
structure CVM_POINT = {DOUBLE x, DOUBLE y, DOUBLE z, CVM_COORD_TYPES coord_type}
+
#define UCVM_MAX_VERSION_LEN 64
  
// A single set of return data. Vp, Vs, and Rho may not be available, and this is denoted with the boolean
+
/* Maximum path length */
// flags. The source of the data is returned in model_id.
+
#define UCVM_MAX_PATH_LEN 256
structure CVM_DATA = {DOUBLE vp, DOUBLE vs, DOUBLE rho, CVM_MODEL_TYPES model_id, BOOL vp_valid, BOOL vs_valid, BOOL, rho_valid}
+
 
ARRAY CVM_POINT_ARRAY = array of CVM_POINT
+
/* NO CVM flag */
ARRAY CVM_DATA_ARRAY = array of CVM_DATA
+
#define UCVM_MODEL_NONE -1
 +
 
 +
/* Supported error codes */
 +
typedef enum { UCVM_CODE_SUCCESS = 0,  
 +
              UCVM_CODE_ERROR,
 +
              UCVM_CODE_DATAGAP,
 +
              UCVM_CODE_NODATA} ucvm_code_t;
 +
 
 +
/* Supported coordinate query modes */
 +
typedef enum { UCVM_COORD_GEO_DEPTH = 0,
 +
              UCVM_COORD_UTM_DEPTH,
 +
              UCVM_COORD_GEO_ELEV,
 +
              UCVM_COORD_UTM_ELEV } ucvm_ctype_t;
 +
 
 +
/* Supported grid types */
 +
typedef enum { UCVM_GRID_CELL_CENTER = 0,
 +
              UCVM_GRID_CELL_VERTEX} ucvm_gtype_t;
 +
 
 +
/* 3D point */
 +
typedef struct ucvm_point_t
 +
{
 +
  double coord[3];
 +
} ucvm_point_t;
 +
 
 +
/* 3D dims */
 +
typedef struct ucvm_dim_t
 +
{
 +
  int dim[3];
 +
} ucvm_dim_t;
 +
 
 +
/* Material properties */
 +
typedef struct ucvm_prop_t
 +
{
 +
  int model;
 +
  double vp;
 +
  double vs;
 +
  double rho;
 +
} ucvm_prop_t;
 +
 
 +
/* Region box */
 +
typedef struct ucvm_region_t
 +
{
 +
  ucvm_ctype_t cmode;
 +
  double p1[3];
 +
  double p2[3];
 +
} ucvm_region_t;
 +
 
 +
/* Projection */
 +
typedef struct ucvm_proj_t
 +
{
 +
  char proj[UCVM_MAX_PROJ_LEN];
 +
} ucvm_proj_t;
 +
 
 +
/* Projection transformation */
 +
typedef struct ucvm_trans_t
 +
{
 +
  double origin[3];
 +
  double rotate;
 +
  double translate[3];
 +
  ucvm_gtype_t gtype;
 +
} ucvm_trans_t;
 +
 
 +
/* Model */
 +
typedef struct ucvm_model_t
 +
{
 +
  char label[UCVM_MAX_LABEL_LEN];
 +
  ucvm_region_t region;
 +
  char config[UCVM_MAX_PATH_LEN];
 +
  int (*init)(int id, ucvm_region_t *r, const char *config);
 +
  int (*finalize)();
 +
  const char* (*version)();
 +
  int (*query)(ucvm_ctype_t cmode,
 +
              int n, ucvm_point_t *pnt,  
 +
              ucvm_prop_t *prop);
 +
} ucvm_model_t;
 
</pre>
 
</pre>
  
== API Functions ==
+
 
 +
== UCVM Interface ==
  
 
<pre>
 
<pre>
// Initialization routine. If the API is invoked in a serial program, stage_dir is set to “”. If invoked in
+
/* Initializer */
// an MPI program or on a system with a HPFS, stage_path is a target directory available to the API for
+
int ucvm_init(const char *config);
// staging copies of the enabled models/GTLs. The stage_path arguments passed to the API within each
 
// rank of an MPI program must be unique across all ranks.
 
// The prestaged flag denotes that the desired models have been pre-staged at stage_path and do not
 
// need to be copied again.
 
// Returns success or error.
 
INT cvm_init(STRING stage_path, BOOL prestaged)
 
  
// User selects which models they want to use. This function may be called multiple times for different
+
/* Finalizer */
// model ids. Model preference is determined by the order in which they are enabled.
+
int ucvm_finalize();
// Returns success or error.
 
INT cvm_enable_model(CVM_MODEL_TYPES model_id)
 
  
// User selects which GTLs they want to use. This function may be called multiple times for different
+
/* Set query mode */
// GTL ids. GTL preference is determined by the order in which they are enabled.
+
int ucvm_query_mode(ucvm_ctype_t c);
// Returns success or error.
 
INT cvm_enable_gtl(CVM_GTL_TYPES gtl_id)
 
  
// Clear list of enabled models
+
/* Enable specific model, return new model id */
INT cvm_clear_models()
+
int ucvm_add_model(ucvm_model_t *m, int *id);
  
// Clear list of enabled GTLs
+
/* Get label for a model */
INT cvm_clear_gtls()
+
int ucvm_model_label(int m, char *label, int len);
  
// Query the enabled CVMs for the specified point.
+
/* Get version for a model */
// Returns the material properties data for that point.
+
int ucvm_model_version(int m, char *ver, int len);
CVM_DATA cvm_query(CVM_POINT point)
+
 
 +
/* Get model id from a label */
 +
int ucvm_model_id(const char *label, int *id);
 +
 
 +
/* Query underlying models */
 +
int ucvm_query(int n, ucvm_point_t *pnt, ucvm_prop_t *prop);
 +
</pre>
 +
 
 +
 
 +
== UCVM Grid Interface ==
 +
 
 +
<pre>
 +
/* Generate grid from projection and dimensions */
 +
int ucvm_grid_gen(ucvm_proj_t *iproj, ucvm_trans_t *trans,
 +
                  ucvm_proj_t *oproj,
 +
                  ucvm_dim_t *dims, double spacing,
 +
                  ucvm_point_t *pnts);
 +
 
 +
/* Generate grid from projection and dimensions */
 +
int ucvm_grid_gen_file(ucvm_proj_t *iproj, ucvm_trans_t *trans,
 +
                      ucvm_proj_t *oproj,
 +
                      ucvm_dim_t *dims, double spacing,
 +
                      const char *filename);
 +
 
 +
/* Convert point list from one projection to another */
 +
int ucvm_grid_convert(ucvm_proj_t *iproj,
 +
                      ucvm_proj_t *oproj,
 +
                      size_t n, ucvm_point_t *pnts);
 +
 
 +
/* Convert point list from one projection to another */
 +
int ucvm_grid_convert_file(ucvm_proj_t *iproj,
 +
                          ucvm_proj_t *oproj,
 +
                          size_t n, const char *filename);
  
// Query the enabled CVMs for the specified array of points
 
// Returns an array of material properties data for those points.
 
CVM_DATA_ARRAY cvm_query_array(CVM_POINT_ARRAY point_array)
 
 
</pre>
 
</pre>
+
 
= Example Usage of API in Pseudocode =
+
 
 +
= Example Usage of API =
  
 
<pre>
 
<pre>
INT FUNCTION main:
+
  int i;
// Initialize in serial mode
+
  int nn = NUM_POINTS;
if cvm_init(stage_path=””, prestaged=False) == ERROR:
+
  ucvm_point_t *pnt1;
return ERROR
+
  ucvm_prop_t *prop1;
// Enable CVM-H 10.12.0
+
 
if cvm_enable(model_id=CVM_MODEL_H10_12_0) == ERROR:
+
  int num_models;
return ERROR
+
  ucvm_model_t models[UCVM_MAX_MODELS];
// Define a point
+
  int model_ids[UCVM_MAX_MODELS];
CVM_POINT p = {x=-120.0, y=34.25, z=-100.0, coord_type=CVM_COORD_GEO_DEPTH}
+
 
// Query the API for this point
+
  printf("Init\n");
CVM_DATA d = cvm_query(p)
+
  if (ucvm_init("./ucvm.conf") != UCVM_CODE_SUCCESS) {
if d.model_id == CVM_MODEL_NONE:
+
    fprintf(stderr, "Init failed\n");
print “Point not found”
+
    return(1);
return ERROR
+
  }
if d.vp_valid == False or d.vs_flag == False or d.rho_flag == False:
+
 
print “Not all material properties defined”
+
  printf("Query Mode\n");
return ERROR
+
  if (ucvm_query_mode(UCVM_COORD_GEO_DEPTH) != UCVM_CODE_SUCCESS) {
// Do something with these material properties
+
    fprintf(stderr, "Set query mode failed\n");
return SUCCESS
+
    return(1);
 +
  }
 +
 
 +
  printf("Get Model\n");
 +
  if (ucvm_cvms_get_model(&(models[0])) != UCVM_CODE_SUCCESS) {
 +
    fprintf(stderr, "Retrieval of CVM-S failed\n");
 +
    return(1);
 +
  }
 +
 
 +
  if (ucvm_add_model(&(models[0]), &(model_ids[0])) != UCVM_CODE_SUCCESS) {
 +
    fprintf(stderr, "Enable CVM-S failed\n");
 +
    return(1);
 +
  }
 +
 
 +
  printf("Create points\n");
 +
  for (i = 0; i < NUM_POINTS; i++) {
 +
    pnt1[i].coord[0] = -119.0;
 +
    pnt1[i].coord[1] = 31.0;
 +
    pnt1[i].coord[2] = 2000.0;
 +
  }
 +
 
 +
  printf("Query Models\n");
 +
  if (ucvm_query(nn, pnt1, prop1) != UCVM_CODE_SUCCESS) {
 +
    fprintf(stderr, "Query CVM failed\n");
 +
    return(1);
 +
  }
 +
 +
  printf("Results\n");
 +
  for (i = 0; i < NUM_POINTS; i++) {
 +
    printf("%d: model=%d, vp=%lf, vs=%lf, rho=%lf\n", i,
 +
          prop1[i].model, prop1[i].vp, prop1[i].vs, prop1[i].rho);
 +
  }
 
</pre>
 
</pre>
 
 

Revision as of 03:07, 17 February 2011

Overview

The Unified California Velocity Model API (UCVM API) is a programming interface that allows the user to directly query multiple velocity models in a program. The API currently support these models:

  • CVM-S
  • CVM-H
  • USGS Central California
  • Hadley-Kanamori 1D


API

Data Types and Definitions

/* Maximum number of supported models */
#define UCVM_MAX_MODELS 10

/* Maximum projection description length */
#define UCVM_MAX_PROJ_LEN 256

/* Maximum model label length */
#define UCVM_MAX_LABEL_LEN 64

/* Maximum model version length */
#define UCVM_MAX_VERSION_LEN 64

/* Maximum path length */
#define UCVM_MAX_PATH_LEN 256

/* NO CVM flag */
#define UCVM_MODEL_NONE -1

/* Supported error codes */
typedef enum { UCVM_CODE_SUCCESS = 0, 
               UCVM_CODE_ERROR,
               UCVM_CODE_DATAGAP,
               UCVM_CODE_NODATA} ucvm_code_t;

/* Supported coordinate query modes */
typedef enum { UCVM_COORD_GEO_DEPTH = 0, 
               UCVM_COORD_UTM_DEPTH,
               UCVM_COORD_GEO_ELEV,
               UCVM_COORD_UTM_ELEV } ucvm_ctype_t;

/* Supported grid types */
typedef enum { UCVM_GRID_CELL_CENTER = 0, 
               UCVM_GRID_CELL_VERTEX} ucvm_gtype_t;

/* 3D point */
typedef struct ucvm_point_t 
{
  double coord[3];
} ucvm_point_t;

/* 3D dims */
typedef struct ucvm_dim_t 
{
  int dim[3];
} ucvm_dim_t;

/* Material properties */
typedef struct ucvm_prop_t 
{
  int model;
  double vp;
  double vs;
  double rho;
} ucvm_prop_t;

/* Region box */
typedef struct ucvm_region_t 
{
  ucvm_ctype_t cmode;
  double p1[3];
  double p2[3];
} ucvm_region_t;

/* Projection */
typedef struct ucvm_proj_t 
{
  char proj[UCVM_MAX_PROJ_LEN];
} ucvm_proj_t;

/* Projection transformation */
typedef struct ucvm_trans_t 
{
  double origin[3];
  double rotate;
  double translate[3];
  ucvm_gtype_t gtype;
} ucvm_trans_t;

/* Model */
typedef struct ucvm_model_t 
{
  char label[UCVM_MAX_LABEL_LEN];
  ucvm_region_t region;
  char config[UCVM_MAX_PATH_LEN];
  int (*init)(int id, ucvm_region_t *r, const char *config);
  int (*finalize)();
  const char* (*version)();
  int (*query)(ucvm_ctype_t cmode,
               int n, ucvm_point_t *pnt, 
               ucvm_prop_t *prop);
} ucvm_model_t;


UCVM Interface

/* Initializer */
int ucvm_init(const char *config);

/* Finalizer */
int ucvm_finalize();

/* Set query mode */
int ucvm_query_mode(ucvm_ctype_t c);

/* Enable specific model, return new model id */
int ucvm_add_model(ucvm_model_t *m, int *id);

/* Get label for a model */
int ucvm_model_label(int m, char *label, int len);

/* Get version for a model */
int ucvm_model_version(int m, char *ver, int len);

/* Get model id from a label */
int ucvm_model_id(const char *label, int *id);

/* Query underlying models */
int ucvm_query(int n, ucvm_point_t *pnt, ucvm_prop_t *prop);


UCVM Grid Interface

/* Generate grid from projection and dimensions */
int ucvm_grid_gen(ucvm_proj_t *iproj, ucvm_trans_t *trans,
                  ucvm_proj_t *oproj,
                  ucvm_dim_t *dims, double spacing, 
                  ucvm_point_t *pnts);

/* Generate grid from projection and dimensions */
int ucvm_grid_gen_file(ucvm_proj_t *iproj, ucvm_trans_t *trans,
                       ucvm_proj_t *oproj,
                       ucvm_dim_t *dims, double spacing, 
                       const char *filename);

/* Convert point list from one projection to another */
int ucvm_grid_convert(ucvm_proj_t *iproj, 
                      ucvm_proj_t *oproj, 
                      size_t n, ucvm_point_t *pnts);

/* Convert point list from one projection to another */
int ucvm_grid_convert_file(ucvm_proj_t *iproj, 
                           ucvm_proj_t *oproj, 
                           size_t n, const char *filename);


Example Usage of API

  int i;
  int nn = NUM_POINTS;
  ucvm_point_t *pnt1;
  ucvm_prop_t *prop1;
  
  int num_models;
  ucvm_model_t models[UCVM_MAX_MODELS];
  int model_ids[UCVM_MAX_MODELS];

  printf("Init\n");
  if (ucvm_init("./ucvm.conf") != UCVM_CODE_SUCCESS) {
    fprintf(stderr, "Init failed\n");
    return(1);
  }
  
  printf("Query Mode\n");
  if (ucvm_query_mode(UCVM_COORD_GEO_DEPTH) != UCVM_CODE_SUCCESS) {
    fprintf(stderr, "Set query mode failed\n");
    return(1);
  }
  
  printf("Get Model\n");
  if (ucvm_cvms_get_model(&(models[0])) != UCVM_CODE_SUCCESS) {
    fprintf(stderr, "Retrieval of CVM-S failed\n");
    return(1);
  }

  if (ucvm_add_model(&(models[0]), &(model_ids[0])) != UCVM_CODE_SUCCESS) {
    fprintf(stderr, "Enable CVM-S failed\n");
    return(1);
  }

  printf("Create points\n");
  for (i = 0; i < NUM_POINTS; i++) {
    pnt1[i].coord[0] = -119.0;
    pnt1[i].coord[1] = 31.0;
    pnt1[i].coord[2] = 2000.0;
  }

  printf("Query Models\n");
  if (ucvm_query(nn, pnt1, prop1) != UCVM_CODE_SUCCESS) {
    fprintf(stderr, "Query CVM failed\n");
    return(1);
  }
 
  printf("Results\n");
  for (i = 0; i < NUM_POINTS; i++) {
    printf("%d: model=%d, vp=%lf, vs=%lf, rho=%lf\n", i, 
           prop1[i].model, prop1[i].vp, prop1[i].vs, prop1[i].rho);
  }

CVM API Implementation Details

  • Contains unified high resolution (30m) DEM for the State of California and surrounding region. Necessary to support query by elevation for CVM-4 and the Vs30 derived GTL.
  • Has low-rez (1km) default background model that supports query by elevation, and knowledge of water/soil/air. Soil regions use Hadley-Kanamori 1D. Water regions use constant water vp and density values, and vs is undefined. In air regions, all values are undefined.
  • Models and GTLs are tiled, and order of precedence is determined by the order they are enabled. No interpolation or smoothing is performed on points that fall within multiple models/GTLs; the data from the most preferred model is returned.
  • The API assumes all supported models blend into the California background model at their extents.
  • The existing CVM-H topography may need to be resampled from the California DEM used in the API

Issues

Overlapping Models

Models may overlap in ways which makes merging them difficult. Imagine the scenario outlined on the right in the diagram below.

State-wide-Overlapping-Models.png

For a point that falls within the Kern County region, simply interpolating between the two models may not work if NoCal reports water properties (vp, rho, no vs) and the SoCal model reports soil/rock. No interpolation is possible in this case. To deal with this situation, there are several options:

  • Coordinate edits to each model to bring their material properties into better agreement within the overlap region.
  • Crop one or both models to remove the overlap, and smooth them into the California background model.
  • Create a new “Transition Model” that covers Kern County and surrounding area that interpolates/averages the the two other models in a scientifically acceptable way. Register this new model in the API, and give it priority over the other two models. In effect, tile the Transition Model, the NoCal Model, and the SoCal Model.