Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

niml_dataset.c

Go to the documentation of this file.
00001 #include "niml_private.h"
00002 
00003 /*-----------------------------------------------------------*/
00004 /* This macro copies the basic elements of a struct,
00005    from struct qold to struct qnew.  Of course, the new
00006    struct gets a new idcode.  This macro may be used after
00007    creating a new struct with NI_new(), for example.
00008 -------------------------------------------------------------*/
00009 
00010 #undef  COPY_BASIC_STRUCT
00011 #define COPY_BASIC_STRUCT(qnew,qold)               \
00012  do{ (qnew)->type = (qold)->type ;                 \
00013      (qnew)->nref = 1 ;                            \
00014      (qnew)->idcode = UNIQ_idcode() ;              \
00015      NI_register_struct( (qnew) ) ;                \
00016      (qnew)->name = NI_strdup((qold)->name) ;      \
00017  } while(0)
00018 
00019 /*-----------------------------------------------------------*/
00020 /*! Transpose a dataset, so that rows are columns and vice-
00021     versa.
00022      - Requires that all vectors (columns) have the same type
00023      - Requires that all vectors do NOT have statistical
00024        distribution types attached.
00025      - Returns pointer to a new dataset (NULL for errors).
00026 -------------------------------------------------------------*/
00027 
00028 void * NI_dataset_transpose( void *ndd )
00029 {
00030    NI_dataset *ndnew , *nd = (NI_dataset *)ndd ;
00031    NI_index_t ii,jj , nvec_old,nvec_new , len_old,len_new ;
00032    int tt , lt ;
00033 
00034    if( nd       == NULL            ||
00035        nd->type != NI_DATASET_TYPE ||
00036        nd->vec  == NULL              ) return NULL ;  /* bad input */
00037 
00038    /* check if all columns have same type, etc. */
00039 
00040    nvec_old = NI_dataset_vecnum(nd) ;
00041    len_old  = NI_dataset_veclen(nd) ;
00042 
00043    if( nvec_old <= 0 || len_old <= 0 ) return NULL ;
00044    if( nd->vec[0]            == NULL ) return NULL ;
00045    if( nd->vec[0]->statistic != NULL ) return NULL ;
00046 
00047    tt = nd->vec[0]->vec_typ ;
00048    lt = NI_datatype_size(tt) ;
00049    for( ii=1 ; ii < nvec_old ; ii++ ){
00050      if( nd->vec[ii]            == NULL ) return NULL ;
00051      if( nd->vec[ii]->vec_typ   != tt   ) return NULL ;
00052      if( nd->vec[ii]->statistic != NULL ) return NULL ;
00053    }
00054 
00055    /* create output struct */
00056 
00057    ndnew = NI_new(NI_dataset) ;
00058    COPY_BASIC_STRUCT(ndnew,nd) ;
00059    ndnew->num_node = nd->num_node ;
00060    ndnew->num_val  = nd->num_val  ;
00061    ndnew->order    = NI_opposite_order(nd->order) ;   /* flipped */
00062    ndnew->domain   = (NI_struct *)NI_pointto_struct(nd->domain) ;  /* same domain */
00063 
00064    /* create new vectors */
00065 
00066    nvec_new = NI_dataset_vecnum(ndnew) ;
00067    len_new  = NI_dataset_veclen(ndnew) ;
00068 
00069    ndnew->vec = NI_malloc(NI_vector*, sizeof(NI_vector *) * nvec_new ) ;
00070    for( ii=0 ; ii < nvec_new ; ii++ )
00071      ndnew->vec[ii] = (NI_vector *)NI_new_vector( tt , len_new ) ;
00072 
00073    /* copy data from old vectors to new vectors */
00074 
00075    if( tt != NI_STRING ){                 /* copy fixed length content */
00076 
00077      char *vnew , *vold ;
00078      for( ii=0 ; ii < nvec_new ; ii++ ){
00079        vnew = (char *)ndnew->vec[ii]->vec ;
00080        for( jj=0 ; jj < nvec_old ; jj++ ){
00081          vold = (char *)nd->vec[jj]->vec ;
00082          memcpy( vnew+lt*jj , vold+lt*ii , lt ) ;
00083        }
00084      }
00085 
00086      for( ii=0 ; ii < nvec_new ; ii++ )
00087        NI_set_vector_range( ndnew->vec[ii] ) ;
00088 
00089    } else {                                 /* duplicate strings */
00090 
00091      char **vnew , **vold ;
00092      for( ii=0 ; ii < nvec_new ; ii++ ){
00093        vnew = (char **)ndnew->vec[ii]->vec ;
00094        for( jj=0 ; jj < nvec_old ; jj++ ){
00095          vold = (char **)nd->vec[jj]->vec ;
00096          vnew[jj] = NI_strdup( vold[ii] ) ;
00097        }
00098      }
00099 
00100    }
00101 
00102    /** done **/
00103 
00104    return (void *)ndnew ;
00105 }
 

Powered by Plone

This site conforms to the following standards: