Skip to content. | Skip to navigation

Personal tools

read_chan_props.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "airs_chan_props.h"


/*
* Compute radiances from frequency and temperature
*
* Units of radiance are milliwatts per ( steradian m**2 cm**-1 )
*/

static double planck(
double temp, /* brightness temperature in Kelvins */
double freq) /* Frequency in wavenumbers (cm**-1) */
{
#define PLANCKS_CONSTANT 6.62620e-34
#define SPEED_OF_LIGHT 2.99793e+10
#define BOLTZMANS_CONSTANT 1.38062e-23
static const double planck_const_1 = 2.0 * (PLANCKS_CONSTANT) *
(SPEED_OF_LIGHT) * (SPEED_OF_LIGHT) * 1.e7;
static const double planck_const_2 = (PLANCKS_CONSTANT) * (SPEED_OF_LIGHT) /
(BOLTZMANS_CONSTANT);

if (temp <= 0.0 || freq <= 0.0) {
fprintf(stderr, "Error: bad args to planck() function:\n");
fprintf(stderr, " Temperature %f K\n", temp);
fprintf(stderr, " Frequency %f cm**-1\n", freq);
exit(EXIT_FAILURE);
}
return planck_const_1 * freq * freq * freq /
(exp(planck_const_2 * freq / temp) - 1.0);
}


/*
* Read a channel properties file into the chan_props[] array.
*/
int read_chan_props(
char * file_name,
airs_chan_props_t chan_props[NUM_AIRS_CHANS])
{
FILE * fp; /* file pointer for CP file */
char line[256]; /* buffer for current line of CP file */
char * linep; /* pointer returned from fgets. NULL for error */
int chan_ix = 0; /* 0-based index of current channel */
int num_read; /* number of items read from a line */
airs_chan_props_t local_chan_props[NUM_AIRS_CHANS];
/* local copy of chan_props, so no changes
* are made to the argument until we know
* we've successfully read the whole thing.
*/
airs_chan_props_t *lcp; /* local pointer for convenience */

fp = fopen(file_name, "r");
if (!fp) {
fprintf(stderr, "Error opening channel props file \"%s\"\n", file_name);
return 1;
}

memset(local_chan_props, 0, sizeof(local_chan_props));

do {
if (chan_ix >= NUM_AIRS_CHANS)
break;
line[0] = '\0'; /* So we can't process a line twice by mistake */
linep = fgets(line, 255, fp);
if (!linep) /* error reading -- probably just end-of-file */
break;
/* exclamation point as first character marks comment lines */
if (line[0] == '!')
continue;
lcp = &local_chan_props[chan_ix];
num_read = sscanf(line,
" %hd %f %s %hd %f %f %f %f %f %f %hd %hd %hd %[^\n]",
&lcp->num,
&lcp->freq,
lcp->array,
&lcp->cal_num,
&lcp->NeDT,
&lcp->FWHM,
&lcp->Cij,
&lcp->centroid_x,
&lcp->centroid_y,
&lcp->RTA_fitting_error,
&lcp->AB_State,
&lcp->Rad_Qual,
&lcp->L2_ignore,
lcp->comment_str);
if (num_read == 13) {
/* no comment */
lcp->comment_str[0] = '\0';
lcp->comment = None;
} else if (num_read == 14) {
/* have a comment -- must parse */
if (!strncmp(lcp->comment_str, "Dead", 4))
lcp->comment = Dead;
else if (!strncmp(lcp->comment_str, "Noise", 5))
lcp->comment = Noise;
else if (!strncmp(lcp->comment_str, "Popping", 7))
lcp->comment = Popping;
else if (!strncmp(lcp->comment_str, "SRF Fit", 7))
lcp->comment = SRF_Fit;
else if (!strncmp(lcp->comment_str, "SRF Shap", 8))
lcp->comment = SRF_Shape;
else if (!strncmp(lcp->comment_str, "Spatial", 7))
lcp->comment = Spatial;
else {
fprintf(stderr, "Error: could not parse comment \"%s\" in file\n"
"\"%s\"\n\n", lcp->comment_str, file_name);
}
} else {
fprintf(stderr, "Error reading channel props file \"%s\"\n", file_name);
fprintf(stderr, "Could not parse line:\n \"%s\"\n\n", line);
exit(EXIT_FAILURE);
}
if (lcp->num < 1 || lcp->num > NUM_AIRS_CHANS) {
fprintf(stderr, "Bad channel number %d from this line:\n \"%s\"\n",
lcp->num, line);
fprintf(stderr, "in file \"%s\"\n\n", file_name);
}
if (lcp->num != chan_ix + 1) {
fprintf(stderr, "Bad channel number %d from this line:\n \"%s\"\n",
lcp->num, line);
fprintf(stderr, "expected %d\n", chan_ix + 1);
fprintf(stderr, "in file \"%s\"\n\n", file_name);
}
if (lcp->freq < 600.0 || lcp->freq > 3000.0) {
fprintf(stderr, "Bad frequency %.4f from this line:\n \"%s\"\n",
lcp->freq, line);
fprintf(stderr, "expected [%.4f, %.4f]\n", 600.0, 3000.0);
fprintf(stderr, "in file \"%s\"\n\n", file_name);
}

/* Convert wavenumbers in cm**-1 to wavelength in microns */
lcp->wavelength_um = 1.0e4 / lcp->freq;

/*
* Convert Noise equivalent delta temperature to noise
* equivalent radiance. Use 250 Kelvins as reference temperature.
*/
if (lcp->NeDT > 0.0 && lcp->NeDT < 9.99 && lcp->freq > 0.0) {
lcp->NeN = planck(250.0 + lcp->NeDT, lcp->freq)
- planck(250.0, lcp->freq);
} else {
lcp->NeN = -9999.0;
}

chan_ix++;
} while (!feof(fp));

if (chan_ix != NUM_AIRS_CHANS) {
fprintf(stderr, "Error: only got info for %d channels from file \"%s\"."
" Expected %d\n",
chan_ix, file_name, NUM_AIRS_CHANS);
}


memcpy(chan_props, local_chan_props,sizeof(local_chan_props));
return 0;
}

Document Actions
NASA Logo - nasa.gov
NASA Privacy Policy and Important Notices
Last updated: Sep 09, 2009 02:26 PM ET
Top