OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
smi_climatology.c
Go to the documentation of this file.
1 #include "l12_proto.h"
2 #include "smi_climatology.h"
3 #include "allocate2d.h"
4 
5 #define NX 4096
6 #define NY 2048
7 
8 static uint8 **map [NPROD];
9 static float slope [NPROD];
10 static float offset[NPROD];
11 static int loaded[NPROD];
12 
13 int smi_climatology_init(char *file, int day, int prodID) {
14  int32 sd_id;
15  int32 sds_id;
16  int32 status;
17  int32 sds_index;
18  int32 rank;
19  int32 nt;
20  int32 dims[H4_MAX_VAR_DIMS];
21  int32 nattrs;
22  int32 start[2];
23  int32 edges[2];
24  char name[H4_MAX_NC_NAME];
25  char sdsname[256];
26 
27  int16 mon = (int) day / 31; /* month of year (no need for perfection) */
28 
29  /* Make sure this product was not already loaded */
30  if (loaded[prodID])
31  return (0);
32 
33  printf("Loading climatology file %s\n", file);
34 
35  if (prodID < 0 || prodID >= NPROD) {
36  printf("-E- %s: Invalid SMI product ID %d.\n",
37  __FILE__, prodID);
38  return (1);
39  }
40 
41 
42  /* Allocate space for requested product */
43  if (map[prodID] == NULL) {
44  map[prodID] = allocate2d_uchar(NY, NX);
45  if (map[prodID] == NULL) {
46  printf("-E- %s: Error allocating space for %s.\n",
47  __FILE__, file);
48  return (1);
49  }
50  }
51 
52 
53  /* Open the file and initiate the SD interface */
54  sd_id = SDstart(file, DFACC_RDONLY);
55  if (sd_id == -1) {
56  printf("-E- %s: Error opening file %s.\n",
57  __FILE__, file);
58  return (1);
59  }
60 
61 
62  /* Get the SDS index */
63  sprintf(sdsname, "data%2.2i", mon + 1);
64  sds_index = SDnametoindex(sd_id, sdsname);
65  if (sds_index == -1) {
66  printf("-E- %s: Error seeking %s SDS from %s.\n",
67  __FILE__, sdsname, file);
68  return (1);
69  }
70 
71  /* Select the SDS */
72  sds_id = SDselect(sd_id, sds_index);
73 
74  /* Verify the characteristics of the array */
75  status = SDgetinfo(sds_id, name, &rank, dims, &nt, &nattrs);
76  if (status != 0) {
77  printf("-E- %s: Error reading SDS info for %s from %s.\n",
78  __FILE__, sdsname, file);
79  return (1);
80  }
81  if (dims[0] != NY || dims[1] != NX) {
82  printf("-E- %s: Dimension mis-match on %s array from %s.\n",
83  __FILE__, sdsname, file);
84  printf(" Expecting %d x %d\n", NX, NY);
85  printf(" Reading %d x %d\n", dims[1], dims[0]);
86  return (1);
87  }
88 
89  start[0] = 0; /* row offset */
90  start[1] = 0; /* col offset */
91  edges[0] = NY; /* row count */
92  edges[1] = NX; /* col count */
93 
94  status = SDreaddata(sds_id, start, NULL, edges, (VOIDP) * map[prodID]);
95  if (status != 0) {
96  printf("-E- %s: Error reading SDS %s from %s.\n",
97  __FILE__, sdsname, file);
98  return (1);
99  }
100 
101  /* Read the scaling info */
102  status = SDreadattr(sds_id, SDfindattr(sds_id, "slope"), (VOIDP) & slope [prodID]);
103  if (status != 0) {
104  printf("-E- %s: Error reading slope attribute for SDS %s from %s.\n",
105  __FILE__, sdsname, file);
106  return (1);
107  }
108  status = SDreadattr(sds_id, SDfindattr(sds_id, "intercept"), (VOIDP) & offset[prodID]);
109  if (status != 0) {
110  printf("-E- %s: Error reading intercept attribute for SDS %s from %s.\n",
111  __FILE__, sdsname, file);
112  return (1);
113  }
114 
115  /* Terminate access to the array */
116  status = SDendaccess(sds_id);
117 
118  /* Terminate access to the SD interface and close the file */
119  status = SDend(sd_id);
120 
121  loaded[prodID] = 1;
122 
123  return (0);
124 }
125 
126 float smi_climatology(float lon, float lat, int prodID) {
127  int16 i, j;
128 
129  i = MAX(MIN((int16) ((lon + 180.0) / 360.0 * NX), NX - 1), 0);
130  j = MAX(MIN((int16) ((90.0 - lat) / 180.0 * NY), NY - 1), 0);
131 
132  return (map[prodID][j][i] * slope[prodID] + offset[prodID]);
133 }
134 
135 
#define MAX(A, B)
Definition: swl0_utils.h:26
integer, parameter int16
Definition: cubeio.f90:3
#define MIN(x, y)
Definition: rice.h:169
int smi_climatology_init(char *file, int day, int prodID)
int j
Definition: decode_rs.h:73
int32_t day
int status
Definition: l1_czcs_hdf.c:32
#define NY
#define NULL
Definition: decode_rs.h:63
float * lat
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed file
Definition: HISTORY.txt:413
float32 slope[]
Definition: l2lists.h:30
unsigned char ** allocate2d_uchar(size_t h, size_t w)
Allocate a two-dimensional array of type unsigned char of a given size.
Definition: allocate2d.c:41
#define NPROD
Utility functions for allocating and freeing two-dimensional arrays of various types.
float smi_climatology(float lon, float lat, int prodID)
Extra metadata that will be written to the HDF4 file l2prod rank
float * lon
#define NX
l2prod offset
int i
Definition: decode_rs.h:71