/*----------------------------------------------------------------------*\ | Functions to return, given the name of any sensor, its offset in the | | dwd structure; given a list of such names, to return a list of the | | corresponding offsets; and, given an offset into the structure, to | | return the name of the sensor. | | | | Peter N. Schweitzer (U.S. Geological Survey, Reston, VA 22092) | \*----------------------------------------------------------------------*/ #include #include #include #include "dw.h" static char msg [1024]; #define offsetof(p_type,field) ((int)&(((p_type)NULL)->field)) struct offset_table { int offset; char *name; }; static struct offset_table dwd_table [] = { {offsetof (struct dwd *,hum), "humidity"}, {offsetof (struct dwd *,ppt), "precipitation"}, {offsetof (struct dwd *,bp), "barometric pressure"}, {offsetof (struct dwd *,wd), "wind direction"}, {offsetof (struct dwd *,ws20), "wind speed at 6.1m"}, {offsetof (struct dwd *,pg20), "peak gust at 6.1m"}, {offsetof (struct dwd *,ws88), "wind speed at 2.7m"}, {offsetof (struct dwd *,ws4), "wind speed at 1.2m"}, {offsetof (struct dwd *,pg4), "peak gust at 1.2m"}, {offsetof (struct dwd *,at20), "air temperature at 6.1m"}, {offsetof (struct dwd *,at4), "air temperature at 1.2m"}, {offsetof (struct dwd *,st4), "soil temperature at 4cm"}, {offsetof (struct dwd *,st10), "soil temperature at 10cm"}, {offsetof (struct dwd *,st20), "soil temperature at 20cm"}, {offsetof (struct dwd *,srsi), "solar radiation (short, in)"}, {offsetof (struct dwd *,srli), "solar radiation (long, in)"}, {offsetof (struct dwd *,srso), "solar radiation (short, out)"}, {offsetof (struct dwd *,srlo), "solar radiation (long, out)"}, {offsetof (struct dwd *,sb), "shadow band"}, {offsetof (struct dwd *,bt1), "brightness temperature, band 1"}, {offsetof (struct dwd *,bt2), "brightness temperature, band 2"}, {offsetof (struct dwd *,bt3), "brightness temperature, band 3"}, {offsetof (struct dwd *,bt4), "brightness temperature, band 4"}, {-1, NULL} }; int get_variable_offset (char *sensor_name) { int j; for (j=0; dwd_table[j].name; j++) if (strcmp (sensor_name,dwd_table[j].name) == 0) return (dwd_table[j].offset); return (-1); } int *get_offset_list (char **selected_sensor, int number_selected) { int i,j; int *offset_list,*L; if (offset_list = (int *) malloc ((1 + number_selected)*sizeof(int))) { L = offset_list; for (i=0; i < number_selected; i++) { j = get_variable_offset (selected_sensor[i]); if (j == -1) { sprintf (msg,"Warning: unrecognized sensor name: \"%s\"",selected_sensor[i]); dw_warn (msg); } else *L++ = j; } } *L = -1; /*------------------------------------------------------------------*\ | If any sensor names were recognized, return the array. If none | | were recognized, free the array and return NULL. | \*------------------------------------------------------------------*/ if (L == offset_list) { free (offset_list); offset_list = NULL; } return (offset_list); } char *get_variable_name (int offset) { int j; for (j=0; dwd_table[j].offset != -1; j++) if (offset == dwd_table[j].offset) return (dwd_table[j].name); return (NULL); } /*----------------------------------------------------------------------*\ \*----------------------------------------------------------------------*/