int write_sac(const char *name, SACHEAD hd, const float *ar ) { FILE *strm; unsigned sz; float *data; int error = 0; strm = NULL; sz = hd.npts*sizeof(float); if (hd.iftype == IXY) sz *= 2; if ((data = (float *) malloc(sz)) == NULL) { fprintf(stderr, "Error in allocating memory for writing %s\n",name); error = 1; } if ( !error && memcpy(data, ar, sz) == NULL) { fprintf(stderr, "Error in copying data for writing %s\n",name); error = 1; } #ifdef i386 swab4((char *) data, sz); swab4((char *) &hd, HD_SIZE); #endif if ( !error && (strm = fopen(name, "w")) == NULL ) { fprintf(stderr,"Error in opening file for writing %s\n",name); error = 1; } if ( !error && fwrite(&hd, sizeof(SACHEAD), 1, strm) != 1 ) { fprintf(stderr,"Error in writing SAC header for writing %s\n",name); error = 1; } if ( !error && fwrite(data, sz, 1, strm) != 1 ) { fprintf(stderr,"Error in writing SAC data for writing %s\n",name); error = 1; } free(data); fclose(strm); return (error==0) ? 0 : -1; }
/* -------------------------------------------------------------------------- */ float * MM5_getfield(t_mm5_file *file, int findex, int timestep, int vflag) { static float *value; int i; int datanum; int dim1, dim2, dim3; z_off_t start; /* wrong request */ if (findex < 0 || findex >= file->total_num || timestep < 0 || timestep >= file->total_times || file == NULL) return NULL; /* get space */ value = NULL; if ((value = malloc(file->vars[findex].fsize)) == NULL) return NULL; /* locate variable */ start = file->vars[findex].fpos + file->timesize * timestep; if (file->mm5version == 3 && timestep > 0) start -= timestep * (5 * sizeof(int) + sizeof(t_mm5v3_big_header)); /* go there and read it !! */ gzseek(file->fp, start, SEEK_SET); if (fortran_read(file->fp, file->machorder, file->vars[findex].fsize, (char *) value) < 0) { fprintf(stderr, "Error read at pos %ld\n", start); free(value); return NULL; } if (file->machorder == LITTLE) { datanum = file->vars[findex].fsize / sizeof(float); for (i = 0; i < datanum; i ++) swab4(value+i); } /* fill dimensions */ dim1 = file->vars[findex].fstopi[0] - file->vars[findex].fstarti[0] + 1; dim2 = file->vars[findex].fstopi[1] - file->vars[findex].fstarti[1] + 1; dim3 = file->vars[findex].fstopi[2] - file->vars[findex].fstarti[2] + 1; /* get rid of full sigma levels. I do not need them anymore */ if (vflag) { if (file->vars[findex].full) { if (file->vars[findex].fstopi[2] > 1) { value = vertint(value, dim1, dim2, dim3 + 1); } } } /* sorry but in C we have transpost matrices */ value = transpost(value, dim1, dim2, dim3); /* all ok */ return value; }
int gwrite (char *imgt, size_t bytes, int n, FILE *fp, char control) { int i, swab_flag; char *imgb; /* i/o buffer */ int status; swab_flag = ((CPU_is_bigendian() != 0) && (control == 'l' || control == 'L')) || ((CPU_is_bigendian() == 0) && (control == 'b' || control == 'B')); if (0) printf ("gwrite swab_flag=%d\n", swab_flag); status = 0; if (swab_flag) { if (!(imgb = malloc (bytes*n))) errm ("gwrite"); for (i = 0; i < bytes*n; i++) imgb[i] = imgt[i]; for (i = 0; i < n; i++) switch (bytes) { case 2: swab2 (imgb + 2*i); break; case 4: swab4 (imgb + 4*i); break; default: errf ("gwrite"); } } else { imgb = imgt; } status = fwrite (imgb, bytes, n, fp) != n; if (swab_flag) free (imgb); return status; }
int read_sachead(const char *name, SACHEAD *hd ) { FILE *strm; if ((strm = fopen(name, "rb")) == NULL) { fprintf(stderr, "Unable to open %s\n",name); return -1; } if (fread(hd, sizeof(SACHEAD), 1, strm) != 1) { fprintf(stderr, "Error in reading SAC header %s\n",name); fclose(strm); return -1; } #ifdef i386 swab4((char *) hd, HD_SIZE); #endif fclose(strm); return 0; }
int eread (float *imgt, int n, int isbig, FILE *fp) { int i, swab_flag; swab_flag = (CPU_is_bigendian() != 0) != (isbig != 0); if (0) printf ("eread swab_flag=%d\n", swab_flag); if (fread (imgt, sizeof (float), n, fp) != n) return -1; if (swab_flag) for (i = 0; i < n; i++) swab4 ((char *) (imgt + i)); return 0; }
float* read_sac(const char *name, SACHEAD *hd ) { FILE *strm; float *ar; unsigned sz; if ((strm = fopen(name, "rb")) == NULL) { fprintf(stderr, "Unable to open %s\n",name); return NULL; } if (fread(hd, sizeof(SACHEAD), 1, strm) != 1) { fprintf(stderr, "Error in reading SAC header %s\n",name); return NULL; } #ifdef i386 swab4((char *) hd, HD_SIZE); #endif sz = hd->npts*sizeof(float); if ((ar = (float *) malloc(sz)) == NULL) { fprintf(stderr, "Error in allocating memory for reading %s\n",name); return NULL; } if (fread((char *) ar, sz, 1, strm) != 1) { fprintf(stderr, "Error in reading SAC data %s\n",name); return NULL; } fclose(strm); #ifdef i386 swab4((char *) ar, sz); #endif return ar; }
static void convert_pixels(unsigned char *src, int pixel_size, int count) { switch (pixel_size) { case 1: { unsigned char *end = src + count - 1; while (src < end) { unsigned char b = *src; *src++ = *end; *end-- = b; } } break; case 2: { unsigned short *start = (unsigned short *) src; unsigned short *end = start + count - 1; while (start <= end) { unsigned short startPixel = *start; unsigned short endPixel = *end; *end-- = swab2 (startPixel); *start++ = swab2 (endPixel); } } break; case 4: { unsigned long *start = (unsigned long *) src; unsigned long *end = start + count - 1; while (start <= end) { unsigned long startPixel = *start; unsigned long endPixel = *end; *end-- = swab4 (startPixel); *start++ = swab4 (endPixel); } } break; } }
int gread (char *imgt, size_t bytes, int n, FILE *fp, int isbig) { int i, swab_flag; swab_flag = (CPU_is_bigendian() != 0) != (isbig != 0); if (0) printf ("gread swab_flag=%d\n", swab_flag); if (fread (imgt, bytes, n, fp) != n) return -1; if (swab_flag) for (i = 0; i < n; i++) switch (bytes) { case 2: swab2 (imgt + 2*i); break; case 4: swab4 (imgt + 4*i); break; default: errf ("gread"); } return 0; }
void load_4dfp_frame (char *fileroot, int *imgdim, int frame, int isbig, float *fimg) { FILE *fp; static char subr[] = "load_4dfp_frame"; char filespc[4*MAXL]; int vdim, i, swab_flag; vdim = imgdim[0]*imgdim[1]*imgdim[2]; sprintf (filespc, "%s.4dfp.img", fileroot); fprintf (stdout, "Reading: %s frame %d\n", filespc, frame + 1); if (!(fp = fopen (filespc, "rb")) || fseek (fp, (long) frame * vdim * sizeof (float), SEEK_SET) || fread (fimg, sizeof (float), vdim, fp) != vdim || fclose (fp)) errr (subr, filespc); swab_flag = (CPU_is_bigendian() != 0) != (isbig != 0); if (swab_flag) for (i = 0; i < vdim; i++) swab4 ((char *) (fimg + i)); }
int ewrite (float *imgt, int n, char control, FILE *fp) { int i, swab_flag; float f; swab_flag = ((CPU_is_bigendian() != 0) && (control == 'l' || control == 'L')) || ((CPU_is_bigendian() == 0) && (control == 'b' || control == 'B')); if (0) printf ("ewrite swab_flag=%d\n", swab_flag); if (swab_flag) { for (i = 0; i < n; i++) { f = imgt[i]; swab4 ((char *) &f); if (fwrite (&f, sizeof (float), 1, fp) != 1) return -1; } return 0; } else { return (fwrite (imgt, sizeof (float), n, fp) != n); } }
float* read_sac2(const char *name, SACHEAD *hd, int tmark, float t1, float t2 ) { FILE *strm; int nn, nt1, nt2, npts; float tref, *ar, *fpt; if ((strm = fopen(name, "rb")) == NULL) { fprintf(stderr, "Unable to open %s\n",name); return NULL; } if (fread(hd, sizeof(SACHEAD), 1, strm) != 1) { fprintf(stderr, "Error in reading SAC header %s\n",name); return NULL; } #ifdef BYTE_SWAP swab4((char *) hd, HD_SIZE); #endif nn = (int) rint((t2-t1)/hd->delta); if (nn<=0 || (ar = (float *) calloc(nn,sizeof(float)))==NULL) { fprintf(stderr, "Error in allocating memory for reading %s n=%d\n",name,nn); return NULL; } tref = 0.; if (tmark==-5 || tmark==-3 || tmark==-2 || (tmark>=0&&tmark<10) ) { tref = *( (float *) hd + 10 + tmark); if (tref==-12345.) { fprintf(stderr,"Time mark undefined in %s\n",name); return NULL; } } t1 += tref; nt1 = (int) rint((t1-hd->b)/hd->delta); nt2 = nt1+nn; npts = hd->npts; hd->npts = nn; hd->b = t1; hd->e = t1+nn*hd->delta; if ( nt1>=npts || nt2<0 ) return ar; if (nt1<0) { fpt = ar-nt1; nt1 = 0; } else { if (fseek(strm,nt1*sizeof(float),SEEK_CUR) < 0) { fprintf(stderr, "error in seek %s\n",name); return NULL; } fpt = ar; } if (nt2>npts) nt2=npts; nn = nt2-nt1; if (fread((char *) fpt, sizeof(float), nn, strm) != (unsigned int)nn) { fprintf(stderr, "Error in reading SAC data %s\n",name); return NULL; } fclose(strm); #ifdef BYTE_SWAP swab4((char *) fpt, nn*sizeof(float)); #endif return ar; }
void swabf (float *f) /* ptr to float to byteswap. */ { swab4 ((int *)f); }
/* -------------------------------------------------------------------------- */ float * get_levels(t_mm5_file *file, int *nlev, int *levtype) { static float *levels; static float surface; char v3order[2][2] = { "S", "P" }; char *pnt; int found; int i, j; int pgi; /* there are not 3d fields in terrain or 2d only outputs from fdda */ pgi = file->mm5_prog; if (pgi == 1 || pgi == 4 || (file->mm5version == 3 && pgi == 6)) { *nlev = 1; *levtype = PRESSURELEV; surface = 101300.0; return(&surface); } /* in v2 there is not a field with vertical levels informations */ if (file->mm5version == 2) { *nlev = get_v2nlevs(file); *levtype = get_v2levtype(file); /* get memory */ levels = NULL; if ((levels = malloc(*nlev * sizeof(float))) == NULL) return NULL; /* extract informations from v2 header */ if (*levtype == SIGMALEV) for (i = 0; i < *nlev; i ++) levels[i] = file->header.v2.MRF[v2bin(102 + i, pgi)]; else for (i = 0; i < *nlev; i ++) levels[i] = (float) file->header.v2.MIF[v2bin(102 + i, pgi)] * 100.0; } /* in v3 we have a field whith vertical level informations */ else { /* sigma level or pressure level ? */ pnt = v3order[1]; *levtype = PRESSURELEV; if (pgi == 5 || pgi == 11) { pnt = v3order[0]; *levtype = SIGMALEV; } /* search for v3 level field */ found = 0; for (i = 0; i < file->total_num && (! found); i ++) { if (pnt[0] == file->vars[i].forder[0]) { *nlev = file->vars[i].fsize / sizeof(float); /* get space */ levels = NULL; if ((levels = malloc(file->vars[i].fsize)) == NULL) return NULL; /* go to level field in file */ gzseek(file->fp, file->vars[i].fpos, SEEK_SET); /* read in field */ if (fortran_read(file->fp, file->machorder, file->vars[i].fsize, (char *) levels) < 0) { fprintf(stderr, "Error read at pos %ld\n", file->vars[i].fpos); free(levels); return NULL; } if (file->machorder == LITTLE) for (j = 0; j < *nlev; j ++) swab4(levels+j); found = 1; } } /* nasty error. should never happen */ if ( ! found) { fprintf(stderr, "Vertical info not found !!\n"); return NULL; } } /* all ok */ return (levels); }
/* -------------------------------------------------------------------------- */ int get_fieldlist(t_mm5_file *file) { /* local variables. are used only for temp storage. all i/o on t_mm5_file structure. see mm5_io.h */ char *point; char *finfo; size_t start; int i, j; int dim_z; int idx; int num3d; int flag; char v2levtype[4]; z_off_t newpos = 0; char stag[2]; t_mm5v3_sub_header subh; file->uid = -1; file->vid = -1; /* MM5 version 2 */ if (file->mm5version == 2) { /* Skip v2 header */ start = 2*sizeof(int) + sizeof(t_mm5v2_header); gzseek(file->fp, start, SEEK_SET); /* Get total number of 3D and 2D fields */ num3d = get_v2num3d(file); file->total_num = num3d + get_v2num2d(file); /* Get vertical dimension length and type */ dim_z = get_v2nlevs(file); if (get_v2levtype(file) == PRESSURELEV) strcpy(v2levtype, "YXP"); else strcpy(v2levtype, "YXS"); for (i = 0; i < file->total_num; i ++) { /* Initialise fields */ file->vars[i].fstarti[0] = 1; file->vars[i].fstarti[1] = 1; file->vars[i].fstarti[2] = 1; file->vars[i].fstarti[3] = 1; memset(file->vars[i].fname, 0, 10); memset(file->vars[i].funit, 0, 26); memset(file->vars[i].fdesc, 0, 47); memset(file->vars[i].forder, 0, 4); file->vars[i].fdims = 3; /* in v2 we have first all 3d fields and then v2 fields */ if (i >= num3d) file->vars[i].fdims = 2; /* extract field name and substitute spaces with underscores */ finfo = get_v2fieldname(file, i); strncpy(file->vars[i].fname, finfo, 8); while ((point = strchr(file->vars[i].fname, ' ')) != NULL) { if (*(point+1) == ' ' || (point-file->vars[i].fname) == 7) *point = 0; else *point = '_'; } strncpy(file->vars[i].funit, finfo+9, 16); /* extract units */ idx = strspn(file->vars[i].funit, " "); strncpy(file->vars[i].funit, file->vars[i].funit+idx, 16-idx); while ((point = strrchr(file->vars[i].funit, ' ')) != NULL && *(point-1) == ' ') *point = 0; point = strrchr(file->vars[i].funit, ' '); if (*(point+1) == 0) *point = 0; /* extract field description */ strncpy(file->vars[i].fdesc, finfo+26, 39); idx = strspn(file->vars[i].fdesc, " "); strncpy(file->vars[i].fdesc, file->vars[i].fdesc+idx, 39-idx); while ((point = strrchr(file->vars[i].fdesc, ' ')) != NULL && *(point-1) == ' ') *point = 0; point = strrchr(file->vars[i].fdesc, ' '); if (*(point+1) == 0) *point = 0; /* v2 levtype as in v3 format */ strcpy(file->vars[i].forder, v2levtype); /* field information: pressure coupling, cross/dot point */ file->vars[i].fcoupl = get_v2fieldcoupl(file, i); file->vars[i].fcross = get_v2fieldcross(file, i); /* extract field dimension getting record dimension from fortran format (works, at least on UNIX machines !!) */ file->vars[i].fsize = fortran_recsize(file->fp, file->machorder); /* 3d field on full sigma levels has got a +1 on dim_z */ file->vars[i].full = 0; if (file->vars[i].fsize > file->dim_i * file->dim_j * dim_z * sizeof(float)) file->vars[i].full = 1; /* Extract position of filed in file as offset from beginning */ file->vars[i].fpos = gztell(file->fp); newpos = file->vars[i].fpos + file->vars[i].fsize + 2 * sizeof(int); gzseek(file->fp, newpos, SEEK_SET); /* fill in dimension records */ file->vars[i].fstopi[0] = file->dim_i; file->vars[i].fstopi[1] = file->dim_j; if (file->vars[i].fdims > 2) file->vars[i].fstopi[2] = dim_z; else file->vars[i].fstopi[2] = 1; /* we do not have 4d fields in v2 version of mm5 */ file->vars[i].fstopi[3] = 1; /* special care for vectorial wind components. make a flag 1 */ if (! strcmp(file->vars[i].fname, "u")) file->uid = i; if (! strcmp(file->vars[i].fname, "v")) file->vid = i; } /* in v2 we have constant size of a timestep */ file->timesize = newpos; /* so we can infer how many timesteps we have in our file (at least an approximation if we have a gzipped file !!!) */ file->total_times = (int) (file->size / newpos); /* rewind */ gzseek(file->fp, start, SEEK_SET); } /* v3 format. I do not expect a v4 format to exist ;-) */ else { /* position after a flag and big header */ start = 5*sizeof(int) + sizeof(t_mm5v3_big_header); gzseek(file->fp, start, SEEK_SET); /* read v3 flag */ fortran_read(file->fp, file->machorder, sizeof(int), (char *) &flag); if (file->machorder == LITTLE) swab4(&flag); /* Loop until end of timestep flag is encountered */ i = 0; while (flag < 2) { /* much smarter v3 format gives complete field informations in a subheader, one for each field */ fortran_read(file->fp, file->machorder, sizeof(t_mm5v3_sub_header), (char *) &subh); point = (char *) &subh; if (file->machorder == LITTLE) for (j = 0; j < 10; j ++) swab4(point+j*sizeof(int)); if (i == 0) { memset(file->v3vartime, 0, 25); memcpy(file->v3vartime, subh.current_date, 24); file->v3vartime[24] = 0; } /* initialise local memory */ memset(file->vars[i].fname, 0, 10); memset(file->vars[i].funit, 0, 26); memset(file->vars[i].fdesc, 0, 47); memset(file->vars[i].forder, 0, 4); /* extract all subheader informations. get number of dimensions */ file->vars[i].fdims = subh.ndim; /* extract field name. substitute spaces with underscores */ for (j = 0; j < 8; j ++) subh.name[j] = (char) tolower((int) subh.name[j]); strncpy(file->vars[i].fname, subh.name, 9); while ((point = strchr(file->vars[i].fname, ' ')) != NULL) { if (*(point+1) == ' ' || (point-file->vars[i].fname) == 8) *point = 0; else *point = '_'; } /* extract units. */ for (j = 0; j < 24; j ++) subh.unit[j] = (char) tolower((int) subh.unit[j]); strncpy(file->vars[i].funit, subh.unit, 25); idx = strspn(file->vars[i].funit, " "); strncpy(file->vars[i].funit, file->vars[i].funit+idx, 25-idx); while ((point = strrchr(file->vars[i].funit, ' ')) != NULL && *(point-1) == ' ') *point = 0; point = strrchr(file->vars[i].funit, ' '); if (*(point+1) == 0) *point = 0; /* extract description */ strncpy(file->vars[i].fdesc, subh.description, 46); idx = strspn(file->vars[i].fdesc, " "); strncpy(file->vars[i].fdesc, file->vars[i].fdesc+idx, 46-idx); while ((point = strrchr(file->vars[i].fdesc, ' ')) != NULL && *(point-1) == ' ') *point = 0; point = strrchr(file->vars[i].fdesc, ' '); if (*(point+1) == 0) *point = 0; /* extract dimensions */ memcpy(file->vars[i].fstarti, subh.start_index, 4*sizeof(int)); memcpy(file->vars[i].fstopi, subh.end_index, 4*sizeof(int)); /* extract ordering */ memcpy(file->vars[i].forder, subh.ordering, 3); /* for v3 we do not have pressure coupled fields */ file->vars[i].fcoupl = 0; /* staggering stands for cross/dot point info */ strncpy(stag, subh.staggering, 1); file->vars[i].fcross = stag[0] == 'C'; /* special care for vectorial components */ if (! strcmp(file->vars[i].fname, "u")) file->uid = i; if (! strcmp(file->vars[i].fname, "v")) file->vid = i; /* Extract field dimension from fortran format. */ file->vars[i].fsize = fortran_recsize(file->fp, file->machorder); file->vars[i].fpos = gztell(file->fp); /* Jump after field to read next subheader */ newpos = file->vars[i].fpos + file->vars[i].fsize + 2 * sizeof(int); gzseek(file->fp, newpos, SEEK_SET); /* check flag for end of timestep */ fortran_read(file->fp, file->machorder, sizeof(int), (char *) &flag); if (file->machorder == LITTLE) swab4(&flag); /* field counter */ i++; } /* preserve total number of fields */ file->total_num = i; /* try calculating total number of timesteps from file size */ file->timesize = gztell(file->fp); file->total_times = (int) ((file->size - start) / (newpos - start)); /* rewind */ gzseek(file->fp, start, SEEK_SET); } /* all done */ return(0); }
DATA_HDR *decode_hdr_sdr (SDR_HDR *ihdr, /* input SDR header. */ int maxbytes) /* max # bytes in buffer. */ { char tmp[80]; DATA_HDR *ohdr; BS *bs; /* ptr to blockette structure. */ char *p; char *pc; int i, next_seq; int seconds, usecs; int swapflag; int itmp[2]; short int stmp[2]; unsigned short int ustmp[2]; int wo; qlib2_errno = 0; if (my_wordorder < 0) get_my_wordorder(); /* Perform data integrity check, and pick out pertinent header info.*/ if (! (is_data_hdr_ind (ihdr->data_hdr_ind) || is_vol_hdr_ind (ihdr->data_hdr_ind))) { /* Don't have a data header. See if the entire header is */ /* composed of NULLS. If so, print warning and return NULL. */ /* Some early Quanterras output a spurious block with null */ /* header info every 16 blocks. That block should be ignored. */ if (allnull((char *)ihdr, sizeof(SDR_HDR))) { return ((DATA_HDR *)NULL); } else { qlib2_errno = 1; return ((DATA_HDR *)NULL); } } if ((ohdr = new_data_hdr()) == NULL) return (NULL); ohdr->record_type = ihdr->data_hdr_ind; ohdr->seq_no = atoi (charncpy (tmp, ihdr->seq_no, 6) ); /* Handle volume header. */ /* Return a pointer to a DATA_HDR structure containing blksize. */ /* Save actual blockette for later use. */ if (is_vol_hdr_ind(ihdr->data_hdr_ind)) { /* Get blksize from volume header. */ p = (char *)ihdr+8; ohdr->blksize = 4096; /* default tape blksize. */ /* Put volume blockette number in data_type field. */ ohdr->data_type = atoi (charncpy (tmp, p, 3)); switch (ohdr->data_type) { int ok; case 5: case 8: case 10: ohdr->blksize = (int)pow(2.0,atoi(charncpy(tmp,p+11,2))); ok = add_blockette (ohdr, p, ohdr->data_type, atoi(charncpy(tmp,p+3,4)), my_wordorder, 0); if (! ok) { qlib2_errno = 2; free_data_hdr(ohdr); return ((DATA_HDR *)NULL); } break; default: break; } return (ohdr); } /* Determine word order of the fixed record header. */ if ((wo = wordorder_from_time((unsigned char *)&(ihdr->time))) < 0) { qlib2_errno = 3; free_data_hdr (ohdr); return ((DATA_HDR *)NULL); } ohdr->hdr_wordorder = wo; ohdr->data_wordorder = ohdr->hdr_wordorder; swapflag = (ohdr->hdr_wordorder != my_wordorder); charncpy (ohdr->station_id, ihdr->station_id, 5); charncpy (ohdr->location_id, ihdr->location_id, 2); charncpy (ohdr->channel_id, ihdr->channel_id, 3); charncpy (ohdr->network_id, ihdr->network_id, 2); trim (ohdr->station_id); trim (ohdr->location_id); trim (ohdr->channel_id); trim (ohdr->network_id); ohdr->hdrtime = decode_time_sdr(ihdr->time, ohdr->hdr_wordorder); if (swapflag) { /* num_samples. */ ustmp[0] = ihdr->num_samples; swab2 ((short int *)&ustmp[0]); ohdr->num_samples = ustmp[0]; /* data_rate */ stmp[0] = ihdr->sample_rate_factor; stmp[1] = ihdr->sample_rate_mult; swab2 ((short int *)&stmp[0]); swab2 ((short int *)&stmp[1]); ohdr->sample_rate = stmp[0]; ohdr->sample_rate_mult = stmp[1]; /* num_ticks_correction. */ itmp[0] = ihdr->num_ticks_correction; swab4 (&itmp[0]); ohdr->num_ticks_correction = itmp[0]; /* first_data */ ustmp[0] = ihdr->first_data; swab2 ((short int *)&ustmp[0]); ohdr->first_data = ustmp[0]; /* first_blockette */ ustmp[1] = ihdr->first_blockette; swab2 ((short int *)&ustmp[1]); ohdr->first_blockette = ustmp[1]; } else { ohdr->num_samples = ihdr->num_samples; ohdr->sample_rate = ihdr->sample_rate_factor; ohdr->sample_rate_mult = ihdr->sample_rate_mult; ohdr->num_ticks_correction = ihdr->num_ticks_correction; ohdr->first_data = ihdr->first_data; ohdr->first_blockette = ihdr->first_blockette; } /* WARNING - may need to convert flags to independent format */ /* if we ever choose a different flag format for the DATA_HDR. */ ohdr->activity_flags = ihdr->activity_flags; ohdr->io_flags = ihdr->io_flags; ohdr->data_quality_flags = ihdr->data_quality_flags; ohdr->num_blockettes = ihdr->num_blockettes; ohdr->data_type = 0; /* assume unknown datatype. */ ohdr->pblockettes = (BS *)NULL; /* Do not parse blockettes here.*/ if (ohdr->num_blockettes == 0) ohdr->pblockettes = (BS *)NULL; else { if (read_blockettes (ohdr, (char *)ihdr) != 1) { free ((char *)ohdr); return ((DATA_HDR *)NULL); } } /* Process any blockettes that follow the fixed data header. */ /* If a blockette 1000 exists, fill in the datatype. */ /* Otherwise, leave the datatype as unknown. */ ohdr->data_type = UNKNOWN_DATATYPE; ohdr->num_data_frames = -1; if ((bs=find_blockette(ohdr, 1000))) { /* Ensure we have proper output blocksize in the blockette. */ BLOCKETTE_1000 *b1000 = (BLOCKETTE_1000 *) bs->pb; ohdr->data_type = b1000->format; ohdr->blksize = (int)pow(2.0,b1000->data_rec_len); ohdr->data_wordorder = b1000->word_order; } if ((bs=find_blockette(ohdr, 1001))) { /* Add in the usec99 field to the hdrtime. */ BLOCKETTE_1001 *b1001 = (BLOCKETTE_1001 *) bs->pb; ohdr->hdrtime = add_time (ohdr->hdrtime, 0, b1001->usec99); ohdr->num_data_frames = b1001->frame_count; } /* If the time correction has not already been added, we should */ /* add it to the begtime. Do NOT change the ACTIVITY flag, since */ /* it refers to the hdrtime, NOT the begtime/endtime. */ ohdr->begtime = ohdr->hdrtime; if ( ohdr->num_ticks_correction != 0 && ((ohdr->activity_flags & ACTIVITY_TIME_CORR_APPLIED) == 0) ) { ohdr->begtime = add_dtime (ohdr->begtime, (double)ohdr->num_ticks_correction * USECS_PER_TICK); } /* Compute endtime. Use precise sample interval in blockette 100. */ /* For client convenience convert it to my_wordorder if not already.*/ if ((bs=find_blockette(ohdr, 100))) { double actual_rate, dusecs; BLOCKETTE_100 *b = (BLOCKETTE_100 *) bs->pb; if (bs->wordorder != my_wordorder) { swab_blockette (bs->type, bs->pb, bs->len); bs->wordorder = my_wordorder; } actual_rate = b->actual_rate; dusecs = ((double)((ohdr->num_samples-1)/actual_rate))*USECS_PER_SEC; ohdr->endtime = add_dtime (ohdr->begtime, dusecs); ohdr->rate_spsec = actual_rate; } else { time_interval2(ohdr->num_samples - 1, ohdr->sample_rate, ohdr->sample_rate_mult, &seconds, &usecs); ohdr->endtime = add_time(ohdr->begtime, seconds, usecs); } /* Attempt to determine blocksize if current setting is 0. */ /* We can detect files of either 512 byte or 4K byte blocks. */ if (ohdr->blksize == 0) { for (i=1; i< 4; i++) { pc = ((char *)(ihdr)) + (i*512); if (pc - (char *)(ihdr) >= maxbytes) break; if ( allnull ( pc,sizeof(SDR_HDR)) ) continue; next_seq = atoi (charncpy (tmp, ((SDR_HDR *)pc)->seq_no, 6) ); if (next_seq == ohdr->seq_no + i) { ohdr->blksize = 512; break; } } /* Can't determine the blocksize. Assume default. */ /* Assume all non-MiniSEED SDR data is in STEIM1 format. */ /* Assume data_wordorder == hdr_wordorder. */ if (ohdr->blksize == 0) ohdr->blksize = (maxbytes >= 1024) ? 4096 : 512; if (ohdr->num_samples > 0 && ohdr->sample_rate != 0) { ohdr->data_type = STEIM1; ohdr->num_data_frames = (ohdr->blksize-ohdr->first_data)/sizeof(FRAME); ohdr->data_wordorder = ohdr->hdr_wordorder; } } /* Fill in num_data_frames, since there may not be a blockette 1001.*/ if (IS_STEIM_COMP(ohdr->data_type) && ohdr->num_samples > 0 && ohdr->sample_rate != 0 && ohdr->num_data_frames < 0) { ohdr->num_data_frames = (ohdr->blksize-ohdr->first_data)/sizeof(FRAME); } return (ohdr); }
int pack_steim2 (SDF *p_sdf, /* ptr to SDR structure. */ int data[], /* unpacked data array. */ int diff[], /* unpacked diff array. */ int ns, /* num_samples. */ int nf, /* total number of data frames. */ int pad, /* flag to specify padding to nf. */ int data_wordorder, /* wordorder of data (NOT USED). */ int *pnframes, /* number of frames actually packed. */ int *pnsamples) /* number of samples actually packed. */ { int points_remaining = ns; int *minbits; /* min bits for difference. */ int i, j; int mask; int ipt = 0; /* index of initial data to pack. */ int fn = 0; /* index of initial frame to pack. */ int wn = 2; /* index of initial word to pack. */ int itmp; short int stmp; int swapflag; int nb; /* number of minbits to compute. */ int max_samples_per_frame; if (my_wordorder < 0) get_my_wordorder(); swapflag = (my_wordorder != data_wordorder); max_samples_per_frame = 8 * VALS_PER_FRAME; /* steim2 compression. */ nb = max_samples_per_frame * nf; if (nb > points_remaining) nb = points_remaining; minbits = NULL; minbits = (int *)malloc(nb * sizeof(int)); if (minbits == NULL) { fprintf (stderr, "Error: mallocing minbits in pack_steim1\n"); fflush (stderr); if (QLIB2_CLASSIC) exit(1); return (MS_ERROR); } for (i=0; i<nb; i++) MINBITS(diff[i],minbits[i]); p_sdf->f[fn].ctrl = 0; /* Set new X0 value in first frame. */ X0 = data[0]; if (swapflag) swab4((int *)&X0); p_sdf->f[fn].ctrl = (p_sdf->f[fn].ctrl<<2) | STEIM2_SPECIAL_MASK; XN = data[ns-1]; if (swapflag) swab4((int *)&XN); p_sdf->f[fn].ctrl = (p_sdf->f[fn].ctrl<<2) | STEIM2_SPECIAL_MASK; while (points_remaining > 0) { /* Pack the next available datapoints into the most compact form. */ if (BIT4PACK(ipt,points_remaining)) { PACK(4,7,0x0000000f,02) if (swapflag) swab4 ((int *)&p_sdf->f[fn].w[wn].fw); mask = STEIM2_567_MASK; points_remaining -= 7; } else if (BIT5PACK(ipt,points_remaining)) { PACK(5,6,0x0000001f,01) if (swapflag) swab4 ((int *)&p_sdf->f[fn].w[wn].fw); mask = STEIM2_567_MASK; points_remaining -= 6; } else if (BIT6PACK(ipt,points_remaining)) {
float* read_sac2(const char *name, SACHEAD *hd, int tmark, float t1, int npts ) { FILE *strm; int i, nt1, nt2; float *ar, *fpt; void rtrend(float *, int); if ((strm = fopen(name, "rb")) == NULL) { fprintf(stderr, "Unable to open %s\n",name); return NULL; } if (fread(hd, sizeof(SACHEAD), 1, strm) != 1) { fprintf(stderr, "Error in reading SAC header %s\n",name); return NULL; } #ifdef i386 swab4((char *) hd, HD_SIZE); #endif t1 += *( (float *) hd + 10 + tmark); nt1 = (int) rint((t1-hd->b)/hd->delta); nt2 = nt1+npts-1; if (nt1>=hd->npts-1 || nt2<=0) { fprintf(stderr,"data not in the specified window %s\n",name); return NULL; } if ((ar = (float *) malloc(npts*sizeof(float))) == NULL) { fprintf(stderr, "Error in allocating memory for reading %s\n",name); return NULL; } for(i=0;i<npts;i++) ar[i]=0.; if (nt1 > 0 ) { if (fseek(strm,nt1*sizeof(float),SEEK_CUR) < 0) { fprintf(stderr, "error in seek %s\n",name); return NULL; } fpt = ar; } else { fpt = ar-nt1; nt1 = 0; } if (nt2>hd->npts-1) nt2=hd->npts-1; i = nt2-nt1+1; if ((int)fread((char *) fpt, sizeof(float), i, strm) != i) { fprintf(stderr, "Error in reading SAC data %s\n",name); return NULL; } fclose(strm); #ifdef i386 swab4((char *) fpt, i*sizeof(float)); #endif /* remove trend */ rtrend(fpt, i); hd->npts = npts; hd->b = t1; hd->e = hd->b+npts*hd->delta; return ar; }
int swab_blockette (int type, /* blockette number. */ char *contents, /* string containing blockette. */ int len) /* length of blockette (incl header). */ { int status = 0; char *p = contents; swab2 ((short int *)(p)); swab2 ((short int *)(p+2)); p += 4; len -= 4; switch (type) { case 100: swabf ((float *)(p+0)); break; case 200: case 201: swabf ((float *)(p+0)); swabf ((float *)(p+4)); swabf ((float *)(p+8)); swabt ((SDR_TIME *)(p+14)); break; case 300: swabt ((SDR_TIME *)(p+0)); swab4 ((int *)(p+12)); swab4 ((int *)(p+16)); swabf ((float *)(p+20)); if (len > 28) swabf ((float *)(p+28)); break; case 310: swabt ((SDR_TIME *)(p+0)); swab4 ((int *)(p+12)); swabf ((float *)(p+16)); swabf ((float *)(p+20)); if (len > 28) swabf ((float *)(p+28)); break; case 320: swabt ((SDR_TIME *)(p+0)); swabf ((float *)(p+12)); swabf ((float *)(p+16)); if (len > 24) swabf ((float *)(p+24)); break; case 390: swabt ((SDR_TIME *)(p+0)); swabf ((float *)(p+12)); swabf ((float *)(p+16)); break; case 395: swabt ((SDR_TIME *)(p+0)); swab2 ((short int *)(p+10)); break; case 400: swabf ((float *)(p+0)); swabf ((float *)(p+4)); swab2 ((short int *)(p+8)); swab2 ((short int *)(p+10)); break; case 405: swab2 ((short int *)(p+0)); break; case 500: swabf ((float *)(p+0)); swabt ((SDR_TIME *)(p+4)); swab4 ((int *)(p+16)); break; case 1000: case 1001: break; case 2000: /* Swap only numeric fields in opaque blockette header. */ swab2 ((short int *)(p)); swab2 ((short int *)(p+2)); swab4 ((int *)(p+4)); break; default: p -= 4; len += 4; swab2 ((short int *)(p)); swab2 ((short int *)(p+2)); status = -1; } return (status); }
float* read_sac_swap(const char *name, SACHEAD *hd, int do_swap ) { FILE *strm; float *ar; unsigned sz; int swap; if ((strm = fopen(name, "rb")) == NULL) { fprintf(stderr, "Unable to open %s\n",name); fclose(strm); return NULL; } if (fread(hd, sizeof(SACHEAD), 1, strm) != 1) { fprintf(stderr, "Error in reading SAC header %s\n",name); fclose(strm); return NULL; } swap = sac_byte_order(hd); if(swap) { if(do_swap) { swab4((char *) hd, HD_SIZE); if(sac_byte_order(hd)) { fprintf(stderr, "Error deciphering SAC header %s\n", name); fclose(strm); return(NULL); } } else { fprintf(stderr, "Error deciphering SAC header %s\n", name); fclose(strm); return(NULL); } } sz = hd->npts*sizeof(float); if ((ar = (float *) malloc(sz)) == NULL) { fprintf(stderr, "Error in allocating memory for reading %s\n",name); fclose(strm); return NULL; } if (fread((char *) ar, sz, 1, strm) != 1) { fprintf(stderr, "Error in reading SAC data %s\n",name); fclose(strm); free(ar); return NULL; } fclose(strm); if(swap) { if(do_swap) { swab4((char *) ar, sz); } else{ fprintf(stderr, "I am not really certain how I got here\n"); fprintf(stderr, "%s needs the data swapped but I was told not to\n",name); fclose(strm); free(ar); return(NULL); } } return ar; }
DATA_HDR *decode_fixed_data_hdr (SDR_HDR *ihdr) /* MiniSEED header. */ { char tmp[80]; DATA_HDR *ohdr; int seconds, usecs; char *p; int swapflag; int itmp[2]; short int stmp[2]; unsigned short int ustmp[2]; int wo; if (my_wordorder < 0) get_my_wordorder(); /* Perform data integrity check, and pick out pertinent header info.*/ if (! (is_data_hdr_ind (ihdr->data_hdr_ind) || is_vol_hdr_ind (ihdr->data_hdr_ind))) { return ((DATA_HDR *)NULL); } if ((ohdr = new_data_hdr()) == NULL) return (NULL); ohdr->record_type = ihdr->data_hdr_ind; ohdr->seq_no = atoi (charncpy (tmp, ihdr->seq_no, 6) ); /* Handle volume header. */ /* Return a pointer to a DATA_HDR structure containing blksize. */ /* Save actual blockette for later use. */ if (is_vol_hdr_ind(ihdr->data_hdr_ind)) { if ((ohdr = new_data_hdr()) == NULL) return (NULL); ohdr->record_type = ihdr->data_hdr_ind; ohdr->seq_no = atoi (charncpy (tmp, ihdr->seq_no, 6) ); ohdr->blksize = 4096; /* default tape blksize. */ p = (char *)ihdr+8; /* point to start of blockette. */ ohdr->data_type = atoi(charncpy(tmp,p,3)); switch (ohdr->data_type) { case 5: case 8: case 10: ohdr->blksize = (int)pow(2.0,atoi(charncpy(tmp,p+11,2))); /* Do not add the blockette here, since we are not */ /* assured that the entire blockette is in this buffer. */ break; default: break; } return (ohdr); } /* Determine word order of the fixed record header. */ if ((wo = wordorder_from_time((unsigned char *)&(ihdr->time)))< 0) { free_data_hdr (ohdr); return ((DATA_HDR *)NULL); } ohdr->hdr_wordorder = wo; ohdr->data_wordorder = ohdr->hdr_wordorder; swapflag = (ohdr->hdr_wordorder != my_wordorder); charncpy (ohdr->station_id, ihdr->station_id, 5); charncpy (ohdr->location_id, ihdr->location_id, 2); charncpy (ohdr->channel_id, ihdr->channel_id, 3); charncpy (ohdr->network_id, ihdr->network_id, 2); trim (ohdr->station_id); trim (ohdr->location_id); trim (ohdr->channel_id); trim (ohdr->network_id); ohdr->hdrtime = ohdr->begtime = decode_time_sdr(ihdr->time, ohdr->hdr_wordorder); if (swapflag) { /* num_samples. */ ustmp[0] = ihdr->num_samples; swab2 ((short int *)&ustmp[0]); ohdr->num_samples = ustmp[0]; /* data_rate */ stmp[0] = ihdr->sample_rate_factor; stmp[1] = ihdr->sample_rate_mult; swab2 (&stmp[0]); swab2 (&stmp[1]); ohdr->sample_rate = stmp[0]; ohdr->sample_rate_mult = stmp[1]; /* num_ticks_correction. */ itmp[0] = ihdr->num_ticks_correction; swab4 (&itmp[0]); ohdr->num_ticks_correction = itmp[0]; /* first_data */ ustmp[0] = ihdr->first_data; swab2 ((short int *)&ustmp[0]); ohdr->first_data = ustmp[0]; /* first_blockette */ ustmp[1] = ihdr->first_blockette; swab2 ((short int *)&ustmp[1]); ohdr->first_blockette = ustmp[1]; } else { ohdr->num_samples = ihdr->num_samples; ohdr->sample_rate = ihdr->sample_rate_factor; ohdr->sample_rate_mult = ihdr->sample_rate_mult; ohdr->num_ticks_correction = ihdr->num_ticks_correction; ohdr->first_data = ihdr->first_data; ohdr->first_blockette = ihdr->first_blockette; } /* WARNING - may need to convert flags to independent format */ /* if we ever choose a different flag format for the DATA_HDR. */ ohdr->activity_flags = ihdr->activity_flags; ohdr->io_flags = ihdr->io_flags; ohdr->data_quality_flags = ihdr->data_quality_flags; ohdr->num_blockettes = ihdr->num_blockettes; ohdr->data_type = 0; /* assume unknown datatype. */ ohdr->pblockettes = (BS *)NULL; /* Do not parse blockettes here.*/ /* If the time correction has not already been added, we should */ /* add it to the begtime. Do NOT change the ACTIVITY flag, since */ /* it refers to the hdrtime, NOT the begtime/endtime. */ if ( ohdr->num_ticks_correction != 0 && ((ohdr->activity_flags & ACTIVITY_TIME_CORR_APPLIED) == 0) ) { ohdr->begtime = add_dtime (ohdr->begtime, (double)ohdr->num_ticks_correction * USECS_PER_TICK); } time_interval2(ohdr->num_samples - 1, ohdr->sample_rate, ohdr->sample_rate_mult, &seconds, &usecs); ohdr->endtime = add_time(ohdr->begtime, seconds, usecs); return(ohdr); }
void swab_hdr (struct dsr *phdr) { int i; float *fptr; swab4 ((char *) &phdr->hk.sizeof_hdr); swab4 ((char *) &phdr->hk.extents); swab2 ((char *) &phdr->hk.session_error); for (i = 0; i < 8; i++) swab2 ((char *) &phdr->dime.dim[i]); swab2 ((char *) &phdr->dime.datatype); swab2 ((char *) &phdr->dime.bitpix); swab2 ((char *) &phdr->dime.dim_un0); for (i = 0; i < 8; i++) swab4 ((char *) &phdr->dime.pixdim[i]); fptr = &phdr->dime.funused8; for (i = 0; i < 8; i++) swab4 ((char *) (fptr + i)); swab4 ((char *) &phdr->dime.compressed); swab4 ((char *) &phdr->dime.verified); swab4 ((char *) &phdr->dime.glmax); swab4 ((char *) &phdr->dime.glmin); swab4 ((char *) &phdr->hist.views); swab4 ((char *) &phdr->hist.vols_added); swab4 ((char *) &phdr->hist.start_field); swab4 ((char *) &phdr->hist.field_skip); swab4 ((char *) &phdr->hist.omax); swab4 ((char *) &phdr->hist.omin); swab4 ((char *) &phdr->hist.smax); swab4 ((char *) &phdr->hist.smin); }
/* * swab a block * flag = 0 -- convert from foreign to native * flag = 1 -- convert from native to foreign */ void swab(void *c, int flag) { uint8_t *p; Tag *t; int i, j; Dentry *d; Cache *h; Bucket *b; Superb *s; Fbuf *f; Off *l; /* swab the tag */ p = (uint8_t*)c; t = (Tag*)(p + BUFSIZE); if(!flag) { swab2(&t->pad); swab2(&t->tag); swaboff(&t->path); } /* swab each block type */ switch(t->tag) { default: print("no swab for tag=%G rw=%d\n", t->tag, flag); for(j=0; j<16; j++) print(" %.2x", p[BUFSIZE+j]); print("\n"); for(i=0; i<16; i++) { print("%.4x", i*16); for(j=0; j<16; j++) print(" %.2x", p[i*16+j]); print("\n"); } panic("swab"); break; case Tsuper: s = (Superb*)p; swaboff(&s->fbuf.nfree); for(i=0; i<FEPERBUF; i++) swaboff(&s->fbuf.free[i]); swaboff(&s->fstart); swaboff(&s->fsize); swaboff(&s->tfree); swaboff(&s->qidgen); swaboff(&s->cwraddr); swaboff(&s->roraddr); swaboff(&s->last); swaboff(&s->next); break; case Tdir: for(i=0; i<DIRPERBUF; i++) { d = (Dentry*)p + i; swab2(&d->uid); swab2(&d->gid); swab2(&d->mode); swab2(&d->muid); swaboff(&d->qid.path); swab4(&d->qid.version); swaboff(&d->size); for(j=0; j<NDBLOCK; j++) swaboff(&d->dblock[j]); for (j = 0; j < NIBLOCK; j++) swaboff(&d->iblocks[j]); swab4(&d->atime); swab4(&d->mtime); } break; case Tind1: case Tind2: #ifndef COMPAT32 case Tind3: case Tind4: /* add more Tind tags here ... */ #endif l = (Off *)p; for(i=0; i<INDPERBUF; i++) { swaboff(l); l++; } break; case Tfree: f = (Fbuf*)p; swaboff(&f->nfree); for(i=0; i<FEPERBUF; i++) swaboff(&f->free[i]); break; case Tbuck: for(i=0; i<BKPERBLK; i++) { b = (Bucket*)p + i; swab4(&b->agegen); for(j=0; j<CEPERBK; j++) { swab2(&b->entry[j].age); swab2(&b->entry[j].state); swaboff(&b->entry[j].waddr); } } break; case Tcache: h = (Cache*)p; swaboff(&h->maddr); swaboff(&h->msize); swaboff(&h->caddr); swaboff(&h->csize); swaboff(&h->fsize); swaboff(&h->wsize); swaboff(&h->wmax); swaboff(&h->sbaddr); swaboff(&h->cwraddr); swaboff(&h->roraddr); swab4(&h->toytime); swab4(&h->time); break; case Tnone: // unitialized case Tfile: // someone elses problem case Tvirgo: // bit map -- all bytes case Tconfig: // configuration string -- all bytes break; } /* swab the tag */ if(flag) { swab2(&t->pad); swab2(&t->tag); swaboff(&t->path); } }
int pack_steim1 (SDF *p_sdf, /* ptr to SDR structure. */ int data[], /* unpacked data array. */ int diff[], /* unpacked diff array. */ int ns, /* num_samples. */ int nf, /* total number of data frames. */ int pad, /* flag to specify padding to nf. */ int data_wordorder, /* wordorder of data (NOT USED). */ int *pnframes, /* number of frames actually packed. */ int *pnsamples) /* number of samples actually packed. */ { int points_remaining = ns; int *minbits; /* min bytes for difference. */ int i, j; int mask; int ipt = 0; /* index of initial data to pack. */ int fn = 0; /* index of initial frame to pack. */ int wn = 2; /* index of initial word to pack. */ int itmp; short int stmp; int swapflag; int nb; /* number of minbits to compute. */ int max_samples_per_frame; if (my_wordorder < 0) get_my_wordorder(); swapflag = (my_wordorder != data_wordorder); max_samples_per_frame = 4 * VALS_PER_FRAME; /* steim1 compression. */ nb = max_samples_per_frame * nf; if (nb > points_remaining) nb = points_remaining; minbits = NULL; minbits = (int *)malloc(nb * sizeof(int)); if (minbits == NULL) { fprintf (stderr, "Error: mallocing minbits in pack_steim1\n"); fflush (stderr); if (QLIB2_CLASSIC) exit(1); return (MS_ERROR); } for (i=0; i<nb; i++) MINBITS(diff[i],minbits[i]); p_sdf->f[fn].ctrl = 0; /* Set new X0 value in first frame. */ X0 = data[0]; if (swapflag) swab4((int *)&X0); p_sdf->f[fn].ctrl = (p_sdf->f[fn].ctrl<<2) | STEIM1_SPECIAL_MASK; XN = data[ns-1]; if (swapflag) swab4((int *)&XN); p_sdf->f[fn].ctrl = (p_sdf->f[fn].ctrl<<2) | STEIM1_SPECIAL_MASK; while (points_remaining > 0) { /* Pack the next available data into the most compact form. */ if (BYTEPACK(ipt,points_remaining)) { mask = STEIM1_BYTE_MASK; for (j=0; j<4; j++) p_sdf->f[fn].w[wn].byte[j] = diff[ipt++]; points_remaining -= 4; } else if (HALFPACK(ipt,points_remaining)) { mask = STEIM1_HALFWORD_MASK; for (j=0; j<2; j++) { stmp = diff[ipt++]; if (swapflag) swab2 (&stmp); p_sdf->f[fn].w[wn].hw[j] = stmp; } points_remaining -= 2; } else { mask = STEIM1_FULLWORD_MASK; itmp = diff[ipt++]; if (swapflag) swab4 (&itmp); p_sdf->f[fn].w[wn].fw = itmp; points_remaining -= 1; } /* Append mask for this word to current mask. */ p_sdf->f[fn].ctrl = (p_sdf->f[fn].ctrl<<2) | mask; /* Check for full frame or full block. */ if (++wn >= VALS_PER_FRAME) { if (swapflag) swab4 ((int *)&p_sdf->f[fn].ctrl); /* Reset output index to beginning of frame. */ wn = 0; /* If block is full, output block and reinitialize. */ if (++fn >= nf) break; p_sdf->f[fn].ctrl = 0; } } /* Set new XN value in first frame. */ XN = data[(ns-1)-points_remaining]; if (swapflag) swab4((int *)&XN); /* End of data. Pad current frame and optionally rest of block. */ /* Do not pad and output a completely empty block. */ if (! EMPTY_BLOCK(fn,wn)) { *pnframes = pad_steim_frame(p_sdf,fn,wn,nf,swapflag,pad); } else { *pnframes = 0; } *pnsamples = ns - points_remaining; free ((char *)minbits); return(0); }
int init_sdr_hdr (SDR_HDR *sh, /* ptr to space for sdr data hdr. */ DATA_HDR *hdr, /* initial DATA_HDR for sdr record. */ BS *extra_bs) /* ptr to block-specific blockettes. */ { int status = 0; int blockette_space; /* # of bytes required for blockettes. */ int n_extra_bs; /* # of extra blockettes. */ BS *bs; /* ptr to blockette structure. */ BS *last_bs; /* ptr to last permanent blockette. */ int align; /* alignment in bytes required for data.*/ int swapflag; /* flag to indicate byteswapping. */ MS_ATTR attr; int blksize = hdr->blksize; if (my_wordorder < 0) get_my_wordorder(); swapflag = (my_wordorder != hdr->hdr_wordorder); /* Determine the space required for all of the blockettes. */ for (bs=hdr->pblockettes, blockette_space=0, last_bs=NULL; bs!=NULL; last_bs=bs, bs=bs->next) { blockette_space += bs->len + ((bs->len%4) ? 4-(bs->len%4) : 0); } for (bs=extra_bs, n_extra_bs=0; bs!=NULL; bs=bs->next, n_extra_bs++) { blockette_space += bs->len + ((bs->len%4) ? 4-(bs->len%4) : 0); } /* Temporarily add the list of extra blockettes to the list of */ /* permanent blockettes. */ if (extra_bs) { if (last_bs) last_bs->next = extra_bs; else hdr->pblockettes = extra_bs; hdr->num_blockettes += n_extra_bs; } /* Ensure that first_data points to appropriate offset for data. */ /* Some data formats (eg the STEIM compressed formats) require */ /* first_data to be on a frame boundary. */ attr = get_ms_attr(hdr); if (attr.alignment == 0) return (MS_ERROR); align = attr.alignment; hdr->first_data = ((sizeof(SDR_HDR)+blockette_space+align-1)/align)*align; /* Update any blockettes that have block-specific info. */ if ((bs=find_blockette(hdr, 1000))) { /* Ensure we have proper data in the blockette. */ BLOCKETTE_1000 *b1000 = (BLOCKETTE_1000 *) bs->pb; /* These are all byte values, so I can ignore wordorder. */ b1000->data_rec_len = roundoff(log2((double)blksize)); b1000->format = hdr->data_type; b1000->word_order = hdr->data_wordorder; } if ((bs=find_blockette(hdr, 1001))) { /* Ensure we have proper data in the blockette. */ /* Mark all frames as being in use. */ BLOCKETTE_1001 *b1001 = (BLOCKETTE_1001 *) bs->pb; /* These are all byte values, so I can ignore wordorder. */ b1001->frame_count = hdr->num_data_frames; b1001->usec99 = hdr->hdrtime.usec % 100; } /* Create the SDR fixed data header and data blockettes. */ /* Parts of the header that do not change from block to block. */ sh->space_1 = ' '; capnstr(sh->station_id,hdr->station_id,5); capnstr(sh->channel_id,hdr->channel_id,3); capnstr(sh->network_id,hdr->network_id,2); capnstr(sh->location_id,hdr->location_id,2); sh->sample_rate_factor = hdr->sample_rate; sh->sample_rate_mult = (hdr->sample_rate) ? hdr->sample_rate_mult : 0; /* Parts of the header that change with each block. */ sh->data_hdr_ind = hdr->record_type; capnint(sh->seq_no,hdr->seq_no,6); sh->time = encode_time_sdr(hdr->hdrtime, hdr->hdr_wordorder); sh->activity_flags = hdr->activity_flags; sh->io_flags = hdr->io_flags; sh->data_quality_flags = hdr->data_quality_flags; sh->num_samples = 0; sh->num_ticks_correction = hdr->num_ticks_correction; /* Parts of the header that depend on the blockettes. */ sh->first_data = hdr->first_data; sh->num_blockettes = hdr->num_blockettes; sh->first_blockette = hdr->first_blockette; /* Output any data blockettes. */ if (hdr->num_blockettes > 0) { write_blockettes(hdr, (char *)sh); } /* Unlink the extra blockettes from the data_hdr. */ if (extra_bs) { if (last_bs) last_bs->next = NULL; else hdr->pblockettes = NULL; hdr->num_blockettes -= n_extra_bs; } if (swapflag) { swab2 ((short int *)&sh->num_samples); swab2 ((short int *)&sh->sample_rate_factor); swab2 ((short int *)&sh->sample_rate_mult); swab2 ((short int *)&sh->first_data); swab2 ((short int *)&sh->first_blockette); swab4 ((int *)&sh->num_ticks_correction); } /* Zero any space between the end of the blockettes and first_data. */ memset ((char*)sh + (sizeof(SDR_HDR)+blockette_space), 0, hdr->first_data - (sizeof(SDR_HDR)+blockette_space)); /* Return status our SDR header creation. */ return (status); }