OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
wr_vdata.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 
6 #include "hdf.h"
7 #include "mfhdf.h"
8 
9 #define MAXNVDATA 128
10 
11 int32 wr_vdata(char *outname, int32 fileid_w, int32 vgid, char *name,
12  char *class1, int32 n_flds, int32 n_recs_to_write,
13  char *fldname[], int32 type[], int32 noext, uint8 *data,
14  int32 verbose) {
15 
16  int32 i, j;
17  int32 status;
18  int32 n_write;
19 
20  static int32 prod_count = 0;
21 
22  static int32 n_vdata_open = 0;
23  static int32 vdata_id_w[MAXNVDATA];
24  static int32 aid[MAXNVDATA];
25  static char *vdata_id_name[MAXNVDATA];
26  static char empty[8] = {"_EMPTY_"};
27 
28  char buffer[1000];
29 
30 
31  /* Closing Vdata */
32  /* ------------- */
33  /* if (n_recs_to_write == 0) { */
34  if (data == NULL) {
35  for (j = 0; j < n_vdata_open; j++) {
36  if (strcmp(vdata_id_name[j], name) == 0) {
37  printf("Detaching: %s\n", vdata_id_name[j]);
38  free(vdata_id_name[j]);
39  vdata_id_name[j] = &empty[0];
40  VSdetach(vdata_id_w[j]);
41  if (aid[j] != -1) Hendaccess(aid[j]);
42  return 0;
43  }
44  }
45  }
46 
47 
48  /* If existing vdata goto write */
49  /* ---------------------------- */
50  for (j = 0; j < n_vdata_open; j++) {
51  if (strcmp(vdata_id_name[j], name) == 0) {
52  goto WRITE_ONLY;
53  }
54  }
55 
56 
57  /* Setup new vdata */
58  /* --------------- */
59  j = n_vdata_open;
60  vdata_id_w[n_vdata_open] = VSattach(fileid_w, -1, "w");
61  aid[n_vdata_open] = -1;
62  vdata_id_name[n_vdata_open] = (char *) malloc(strlen(name) + 1);
63  strcpy(vdata_id_name[n_vdata_open], name);
64  printf("Setting Up: %s %d\n", name, vdata_id_w[n_vdata_open]);
65 
66  for (i = 0; i < n_flds; i++) {
67  status = VSfdefine(vdata_id_w[n_vdata_open], fldname[i], type[i], 1);
68  if (status != 0) {
69  printf("Error defining \"%s\".\n", fldname[i]);
70  }
71  }
72 
73  /* Set fieldnames */
74  /* -------------- */
75  strcpy(buffer, fldname[0]);
76  for (i = 1; i < n_flds; i++) {
77  strcat(buffer, ",");
78  strcat(buffer, fldname[i]);
79  }
80  VSsetfields(vdata_id_w[j], buffer);
81 
82  Vinsert(vgid, vdata_id_w[n_vdata_open]);
83  VSsetname(vdata_id_w[n_vdata_open], name);
84  VSsetclass(vdata_id_w[n_vdata_open], class1);
85 
86  VSsetblocksize(vdata_id_w[n_vdata_open], 4096 * 6);
87 
88 
89  /* Setup External Data Vdatas */
90  /* -------------------------- */
91  if (strcmp(class1, "DataSubordinate") == 0 && noext == 0) {
92 
93  static FILE *sfile;
94 
95  memset(buffer, 0, sizeof (buffer));
96  status = VSwrite(vdata_id_w[j], (uint8 *) buffer, 1, FULL_INTERLACE);
97  VSdetach(vdata_id_w[j]);
98  vdata_id_w[j] = VSattach(fileid_w, VSfind(fileid_w, name), "w");
99 
100  /* synthesize file name */
101  strcpy(buffer, outname);
102  sprintf(&buffer[strlen(buffer)], ".x%02d", prod_count);
103 
104  /* open file and write name of main file **
105  ** in first 512 bytes */
106  sfile = fopen(buffer, "w");
107 
108  if (strrchr(outname, '/') == NULL)
109  strcpy(buffer, outname);
110  else
111  strcpy(buffer, strrchr(outname, '/') + 1);
112  status = fwrite(buffer, 512, 1, sfile);
113  fclose(sfile);
114 
115  /* convert to external element */
116  strcpy(buffer, outname);
117  sprintf(&buffer[strlen(buffer)], ".x%02d", prod_count);
118  aid[j] = HXcreate(fileid_w, DFTAG_VS, (uint16) VSfind(fileid_w, name),
119  buffer, 512, 0);
120 
121  VSdetach(vdata_id_w[j]);
122  vdata_id_w[j] = VSattach(fileid_w, VSfind(fileid_w, name), "w");
123  VSseek(vdata_id_w[j], 0);
124 
125  prod_count++;
126  }
127 
128  n_vdata_open++;
129 
130 
131 WRITE_ONLY:
132 
133  if (n_recs_to_write > 0) {
134  n_write = VSwrite(vdata_id_w[j], data, n_recs_to_write, FULL_INTERLACE);
135 
136  if (n_write != n_recs_to_write) {
137  printf("n_write: %d n_recs_to_write: %d \n", n_write, n_recs_to_write);
138  printf("Error writing to vdata: \"%s\" (%d)\n", name, vdata_id_w[j]);
139  exit(1);
140  } else {
141  if (verbose == 1)
142  printf("Writing %d records to %s (%d)\n",
143  n_write, name, VSelts(vdata_id_w[j]));
144  }
145  }
146 
147  return 0;
148 }
149 
int j
Definition: decode_rs.h:73
int status
Definition: l1_czcs_hdf.c:32
#define NULL
Definition: decode_rs.h:63
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
int32 wr_vdata(char *outname, int32 fileid_w, int32 vgid, char *name, char *class1, int32 n_flds, int32 n_recs_to_write, char *fldname[], int32 type[], int32 noext, uint8 *data, int32 verbose)
Definition: wr_vdata.c:11
#define MAXNVDATA
Definition: wr_vdata.c:9
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int verbose
Definition: fmt_check.c:6