OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
read_write.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include "mfhdf.h"
5 
6 int32 open_sds(int32 sd_id, char *sds_name) {
7  int32 sds_id, sds_index;
8  if ((sds_index = SDnametoindex(sd_id, sds_name)) == -1) {
9  fprintf(stderr, "Cannot locate SDS %s\n", sds_name);
10  return -1;
11  }
12  if ((sds_id = SDselect(sd_id, sds_index)) == -1) {
13  fprintf(stderr, "Cannot select SDS %s\n", sds_name);
14  return -1;
15  }
16  return sds_id;
17 }
18 
19 int read_sds_block(int32 sds_id, int startline, int buflines, void *dataP) {
20  int32 start[3] = {0, 0, 0}, dimsize[3];
21  int32 numtype, nattrs, rank;
22  char sds_name[H4_MAX_NC_NAME];
23 
24  if (SDgetinfo(sds_id, sds_name, &rank, dimsize, &numtype, &nattrs) == -1) {
25  fprintf(stderr, "Cannot get info for SDS %s\n", sds_name);
26  SDendaccess(sds_id);
27  return -1;
28  }
29  start[0] = startline;
30  if (buflines > 0) {
31  dimsize[0] = buflines;
32  if (SDreaddata(sds_id, start, NULL, dimsize, dataP) == -1) {
33  fprintf(stderr, "Cannot read line %d to %d from SDS %s\n", startline, startline + buflines - 1, sds_name);
34  SDendaccess(sds_id);
35  return -1;
36  }
37  }
38  SDendaccess(sds_id);
39  return 0;
40 }
41 
42 int read_sds(int32 sd_id, char *sds_name, void *dataP) {
43  int32 sds_id, start[6], rank, dimsize[6];
44  int32 numtype, nattrs, i;
45  char name[128];
46 
47  if ((sds_id = open_sds(sd_id, sds_name)) == -1) return -1;
48  if (SDgetinfo(sds_id, name, &rank, dimsize, &numtype, &nattrs) < 0) {
49  fprintf(stderr, "Cannot get info for SDS %s\n", sds_name);
50  return -1;
51  }
52  for (i = 0; i < rank; i++) start[i] = 0;
53  if (SDreaddata(sds_id, start, NULL, dimsize, dataP) < 0) {
54  fprintf(stderr, "Cannot read data from SDS %s\n", sds_name);
55  return -1;
56  }
57  SDendaccess(sds_id);
58  return 0;
59 }
60 
61 int read_attr(int32 sd_id, char *attrname, void *attr) {
62  int32 hdf_ind;
63 
64  if ((hdf_ind = SDfindattr(sd_id, attrname)) < 0) {
65  fprintf(stderr, "Cannot find global attribute %s\n", attrname);
66  return -1;
67  }
68  if (SDreadattr(sd_id, hdf_ind, attr) < 0) {
69  fprintf(stderr, "Cannot read global attribute %s\n", attrname);
70  return -1;
71  }
72  return 0;
73 }
74 
75 int write_sds(int32 sd_id, char *SDSname, void *data, int startline, int buflines, int Nl, int Np, int32 numtype, void *fillvalue, float64 scale_factor, char verbose, char gzip) {
76  int32 sds_id, sds_idx, rank, dimsizes[2], edges[2], start[2];
77  comp_info c_info;
78 
79  rank = 2;
80  start[0] = startline;
81  start[1] = 0;
82  edges[0] = buflines;
83  edges[1] = Np;
84  dimsizes[0] = Nl;
85  dimsizes[1] = Np;
86  if ((sds_idx = SDnametoindex(sd_id, SDSname)) == -1) {
87  if ((sds_id = SDcreate(sd_id, SDSname, numtype, rank, dimsizes)) == -1) {
88  fprintf(stderr, "Cannot create SDS %s\n", SDSname);
89  return -1;
90  }
91  if (verbose) printf(" Writing SDS %s ...\n", SDSname);
92  } else {
93  if ((sds_id = SDselect(sd_id, sds_idx)) == -1) {
94  fprintf(stderr, "Cannot select SDS %s\n", SDSname);
95  return -1;
96  }
97  if (verbose) printf(" Overwriting SDS %s ...\n", SDSname);
98  }
99  if (fillvalue)
100  SDsetfillvalue(sds_id, fillvalue);
101  if (scale_factor != 0)
102  SDsetcal(sds_id, scale_factor, scale_factor / 2., 0, 0, DFNT_FLOAT32);
103  if (gzip) {
104  c_info.deflate.level = 6;
105  SDsetcompress(sds_id, COMP_CODE_DEFLATE, &c_info);
106  }
107  if (startline >= 0) {
108  /*
109  step = 1000000 / Np;
110  printf("step %d\n", step);
111  edges[0] = step;
112  sizeoftype = DFKNTsize(numtype);
113  for (k=0; k<buflines; k+=step) {
114  start[0] = startline + k;
115  if (k + step > buflines) edges[0] = buflines - k;
116  if (SDwritedata(sds_id, start, NULL, edges, (VOIDP)((char *)data + k * Np * sizeoftype)) == -1) {
117  fprintf(stderr,"Cannot write line %d to %d in SDS %s\n",start[0], start[0] + edges[0] - 1, SDSname);
118  return -1;
119  }
120  }
121  */
122  if (SDwritedata(sds_id, start, NULL, edges, (VOIDP) data) == -1) {
123  fprintf(stderr, "Cannot write line %d to %d in SDS %s\n", start[0], start[0] + edges[0] - 1, SDSname);
124  return -1;
125  }
126 #ifndef HDF41r3
127  if (sds_idx == -1 &&
128  startline + buflines < Nl) {
129  {
130  int k;
131  switch (numtype) {
132  case DFNT_UINT8: for (k = 0; k < Np; k++) ((uint8 *) data)[k] = *((uint8 *) fillvalue);
133  break;
134  case DFNT_INT16: for (k = 0; k < Np; k++) ((int16 *) data)[k] = *((int16 *) fillvalue);
135  break;
136  default: fprintf(stderr, "Cannot initialize data type %d\n", numtype);
137  return -1;
138  }
139  printf(" Initializing line %d to %d in SDS %s ...\n", startline + buflines, Nl - 1, SDSname);
140  edges[0] = 1;
141  for (k = startline + buflines; k < Nl; k++) {
142  start[0] = k;
143  if (SDwritedata(sds_id, start, NULL, edges, (VOIDP) data) == -1) {
144  fprintf(stderr, "Cannot write line %d in SDS %s\n", k, SDSname);
145  return -1;
146  }
147  }
148  }
149  }
150 #endif
151  }
152  SDendaccess(sds_id);
153  return 0;
154 
155 }
integer, parameter int16
Definition: cubeio.f90:3
#define NULL
Definition: decode_rs.h:63
int read_sds_block(int32 sds_id, int startline, int buflines, void *dataP)
Definition: read_write.c:19
int read_attr(int32 sd_id, char *attrname, void *attr)
Definition: read_write.c:61
int read_sds(int32 sd_id, char *sds_name, void *dataP)
Definition: read_write.c:42
int write_sds(int32 sd_id, char *SDSname, void *data, int startline, int buflines, int Nl, int Np, int32 numtype, void *fillvalue, float64 scale_factor, char verbose, char gzip)
Definition: read_write.c:75
int32 open_sds(int32 sd_id, char *sds_name)
Definition: read_write.c:6
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT16
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
Extra metadata that will be written to the HDF4 file l2prod rank
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_FLOAT32
int i
Definition: decode_rs.h:71
int verbose
Definition: fmt_check.c:6
int k
Definition: decode_rs.h:73