IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 1, 2012, 3:24:39 PM (14 years ago)
Author:
eugene
Message:

support for parallel dvo in mextract, avextract, avmerge, gstar, gcat; remove some if-def-ed out code already moved to libdvo; list -vectors and -buffers options (store names of vectors and buffers in lists); list -copy give error if missing source; catlist, hosts & remote functions to support parallel dvos; skyregion -save option; new skycoverage modes (-min-ubercal; -min-dmag-sys; -min-mcal; -max-mcal); gstar output formatting cleanups; add more average info to gstar output; functions for spectral similarity analysis; coords returns nans for projections off the sphere; fix cumulative function; add name:type option to opihi/read function (eg read a:float 2 b:time 3 c:int 4); line -frac option; vtype function to get vector types; threshold function; write -fits option; code for unfinished mtype function (matching vtype)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/VectorOps.c

    r31160 r33662  
    1515
    1616// this function is NOT thread protected : it is only used in startup and/or shutdown
     17void FreeVectorArray (Vector **vec, int Nvec) {
     18
     19  int i;
     20
     21  if (!vec) return;
     22  for (i = 0; i < Nvec; i++) {
     23    if (!vec[i]) continue;
     24    if (vec[i]->elements.Int) {
     25      free (vec[i]->elements.Int);
     26    }
     27    free (vec[i]);
     28  }
     29  free (vec);
     30}
     31
     32// this function is NOT thread protected : it is only used in startup and/or shutdown
    1733void FreeVectors () {
    18 
    19   int i;
    20 
    21   for (i = 0; i < Nvectors; i++) {
    22     if (vectors[i][0].elements.Int) {
    23       free (vectors[i][0].elements.Int);
    24     }
    25     free (vectors[i]);
    26   }
    27   free (vectors);
     34  FreeVectorArray (vectors, Nvectors);
    2835}
    2936
     
    93100}
    94101 
     102// Assign the given Vector to the internal array of vectors by name
     103int AssignVector (Vector *vec, char *name, int mode, int verbose) {
     104
     105  int i;
     106
     107  if (name == NULL) goto error;
     108  if (ISNUM(name[0])) goto error;
     109  if (IsBuffer(name)) goto error;
     110
     111  for (i = 0; (i < Nvectors) && (strcmp(vectors[i][0].name, name)); i++);
     112  /* is a new vector */
     113  if (i == Nvectors) {
     114    if (mode == OLDVECTOR) goto error;
     115    pthread_mutex_lock (&mutex);
     116    Nvectors ++;
     117    REALLOCATE (vectors, Vector *, Nvectors);
     118    vectors[i] = vec;
     119    pthread_mutex_unlock (&mutex);
     120    return TRUE;
     121  }
     122  /* is an old vector */
     123  if (mode == NEWVECTOR) goto error;
     124  if (vectors[i]) {
     125    if (vectors[i][0].elements.Ptr) free (vectors[i][0].elements.Ptr);
     126    free (vectors[i]);
     127  }
     128  vectors[i] = vec;
     129  return TRUE;
     130
     131 error:
     132  if (verbose) gprint (GP_ERR, "invalid vector %s\n", name);
     133  return FALSE;
     134}
     135 
    95136/* delete by pointer */
    96137int DeleteVector (Vector *vec) {
     
    148189  if (in[0].elements.Ptr) {
    149190    if (in[0].type == OPIHI_FLT) {
    150       ALLOCATE (out[0].elements.Flt, opihi_flt, out[0].Nelements);
     191      ALLOCATE (out[0].elements.Flt, opihi_flt, MAX(1,out[0].Nelements));
    151192      memcpy (out[0].elements.Flt, in[0].elements.Flt, out[0].Nelements*sizeof(opihi_flt));
    152193      out[0].type = OPIHI_FLT;
    153194    } else {
    154       ALLOCATE (out[0].elements.Int, opihi_int, out[0].Nelements);
     195      ALLOCATE (out[0].elements.Int, opihi_int, MAX(1,out[0].Nelements));
    155196      memcpy (out[0].elements.Int, in[0].elements.Int, out[0].Nelements*sizeof(opihi_int));
    156197      out[0].type = OPIHI_INT;
     
    164205  out[0].Nelements = in[0].Nelements;
    165206  if (type == OPIHI_FLT) {
    166     ALLOCATE (out[0].elements.Flt, opihi_flt, out[0].Nelements);
     207    ALLOCATE (out[0].elements.Flt, opihi_flt, MAX(1,out[0].Nelements));
    167208    out[0].type = OPIHI_FLT;
    168209  } else {
    169     ALLOCATE (out[0].elements.Int, opihi_int, out[0].Nelements);
     210    ALLOCATE (out[0].elements.Int, opihi_int, MAX(1,out[0].Nelements));
    170211    out[0].type = OPIHI_INT;
    171212  }
     
    173214}
    174215
    175 // ResetVector (vecx, OPIHI_FLT, MAX (Npts, 1));
    176216int ResetVector (Vector *vec, char type, int Nelements) {
    177217
    178   vec[0].Nelements = Nelements;
     218  // a vector can only have >= 0 elements
     219  vec[0].Nelements = MAX(Nelements,0);
    179220  if (type == OPIHI_FLT) {
    180221    REALLOCATE (vec[0].elements.Flt, opihi_flt, MAX(1, Nelements));
     
    190231int SetVector (Vector *vec, char type, int Nelements) {
    191232
    192   vec[0].Nelements = Nelements;
     233  vec[0].Nelements = MAX(Nelements,0);
    193234  if (type == OPIHI_FLT) {
    194     ALLOCATE (vec[0].elements.Flt, opihi_flt, Nelements);
     235    ALLOCATE (vec[0].elements.Flt, opihi_flt, MAX(1,Nelements));
    195236    vec[0].type = OPIHI_FLT;
    196237  } else {
    197     ALLOCATE (vec[0].elements.Int, opihi_int, Nelements);
     238    ALLOCATE (vec[0].elements.Int, opihi_int, MAX(1,Nelements));
    198239    vec[0].type = OPIHI_INT;
    199240  }
     
    287328  return (TRUE);
    288329}
     330
     331int ListVectorsToList (char *name) {
     332
     333  int i;
     334  char line[1024];
     335
     336  for (i = 0; i < Nvectors; i++) {
     337    sprintf (line, "%s:%d", name, i);
     338    set_str_variable (line, vectors[i][0].name);
     339  }
     340  sprintf (line, "%s:n", name);
     341  set_int_variable (line, Nvectors);
     342  return (Nvectors);
     343}
     344
     345// Take two arrays of vectors and merge equal named vectors.
     346// Output is a single array in (vec), with vectors lengths of len(vec) + len(invec)
     347// For ease, require that the order of the names match & number of vectors match
     348Vector **MergeVectors (Vector **vec, int *Nvec, Vector **invec, int Ninvec) {
     349
     350  int i, j;
     351
     352  if (vec == NULL) {
     353    *Nvec = Ninvec;
     354    return invec;
     355  }
     356
     357  myAssert (*Nvec == Ninvec, "programming error (1) %d vs %d", *Nvec, Ninvec);
     358
     359  for (i = 0; i < Ninvec; i++) {
     360    myAssert (!strcmp(vec[i]->name, invec[i]->name), "programming error (2) %s vs %s", vec[i]->name, invec[i]->name);
     361    myAssert (vec[i]->type == invec[i]->type, "programming error (3), %d vs %d", vec[i]->type, invec[i]->type);
     362
     363    int N = vec[i]->Nelements;
     364    if (vec[i]->type == OPIHI_FLT) {
     365      REALLOCATE (vec[i]->elements.Flt, opihi_flt, vec[i]->Nelements + invec[i]->Nelements);
     366      for (j = 0; j < invec[i]->Nelements; j++) {
     367        vec[i]->elements.Flt[N+j] = invec[i]->elements.Flt[j];
     368      }
     369    } else {
     370      REALLOCATE (vec[i]->elements.Int, opihi_int, vec[i]->Nelements + invec[i]->Nelements);
     371      for (j = 0; j < invec[i]->Nelements; j++) {
     372        vec[i]->elements.Int[N+j] = invec[i]->elements.Int[j];
     373      }
     374    }
     375    vec[i]->Nelements += invec[i]->Nelements;
     376  }
     377  return vec;
     378}
     379
     380// Take two arrays of vectors and merge equal named vectors, where the last vector
     381// specifies the element in the output vector.  All input vectors must have a max sequence
     382// value of Nelements.  Output is a single array in (vec), with vector lengths of
     383// Nelements for ease, require that the order of the names match & number of vectors match
     384Vector **MergeVectorsByIndex (Vector **vec, int *Nvec, Vector **invec, int Ninvec, int Nelements) {
     385
     386  int i, j;
     387
     388  // on first call, allocate a new vector, excluding the index
     389  int newArray = FALSE;
     390  if (vec == NULL) {
     391    ALLOCATE (vec, Vector *, Ninvec - 1);
     392    *Nvec = Ninvec - 1;
     393    newArray = TRUE;
     394  }
     395
     396  myAssert (*Nvec == Ninvec - 1, "programming error (1)");
     397
     398  // find the index vector
     399  int idx = Ninvec - 1;
     400  myAssert (!strcmp(invec[idx]->name, "index"), "failed to find index vector");
    289401 
     402  for (i = 0; i < Ninvec - 1; i++) {
     403    // on first call, create the output vector
     404    if (newArray) {
     405      vec[i] = InitVector();
     406      strcpy (vec[i]->name, invec[i]->name);
     407      ResetVector (vec[i], invec[i]->type, Nelements);
     408      for (j = 0; j < Nelements; j++) {
     409        if (vec[i][0].type == OPIHI_FLT) {
     410          vec[i][0].elements.Flt[j] = NAN;
     411        } else {
     412          vec[i][0].elements.Int[j] = 0; // or NAN_INT?
     413        }
     414      }
     415    }
     416
     417    myAssert (!strcmp(vec[i]->name, invec[i]->name), "programming error (2)");
     418    myAssert (vec[i]->type == invec[i]->type, "programming error (2)");
     419
     420    // copy vector elements from input to output, matching location
     421    if (vec[i]->type == OPIHI_FLT) {
     422      for (j = 0; j < invec[i]->Nelements; j++) {
     423        int seq = invec[idx]->elements.Int[j];
     424        vec[i]->elements.Flt[seq] = invec[i]->elements.Flt[j];
     425      }
     426    } else {
     427      for (j = 0; j < invec[i]->Nelements; j++) {
     428        int seq = invec[idx]->elements.Int[j];
     429        vec[i]->elements.Int[seq] = invec[i]->elements.Int[j];
     430      }
     431    }
     432  }
     433  return vec;
     434}
     435
Note: See TracChangeset for help on using the changeset viewer.