Skip to content

Commit 6056645

Browse files
committed
Attempts (but fails) to completely fix issue 271.
- The grid file is now written with DENSMOLn columns. As for reading of the grid file, if DENSMOLn columns are present, they will be read; if not, and if ABUNMOLn columns are present, they will be read. The existence of DENSMOLn columns is passed back to grid.c:readGridWrapper(), where it is used to set par->useAbun if appropriate, but in fact this has no wider effect on the task, since all the relevant initialization is already done by the time the grid file is read. This won't be fixable without some reorganization.
1 parent 99a1e00 commit 6056645

File tree

7 files changed

+153
-87
lines changed

7 files changed

+153
-87
lines changed

src/grid.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@ exit(1);
104104

105105
/*....................................................................*/
106106
void
107-
readGridWrapper(configInfo *par, struct grid **gp, char ***collPartNames, int *numCollPartRead){
107+
readGridWrapper(configInfo *par, struct grid **gp, char ***collPartNames\
108+
, int *numCollPartRead){
108109

109110
const int numDesiredKwds=3;
110111
struct keywordType *desiredKwds=malloc(sizeof(struct keywordType)*numDesiredKwds);
111112
int i,status=0;
112113
struct gridInfoType gridInfoRead;
114+
_Bool densMolColsExists;
113115

114116
i = 0;
115117
initializeKeyword(&desiredKwds[i]);
@@ -127,7 +129,8 @@ readGridWrapper(configInfo *par, struct grid **gp, char ***collPartNames, int *n
127129
snprintf(desiredKwds[i].keyname, STRLEN_KNAME, "NSOLITER");
128130

129131
status = readGrid(par->gridInFile, &gridInfoRead, desiredKwds\
130-
, numDesiredKwds, gp, collPartNames, numCollPartRead, &(par->dataFlags));
132+
, numDesiredKwds, gp, collPartNames, numCollPartRead\
133+
, &(par->dataFlags), &densMolColsExists);
131134

132135
par->radius = desiredKwds[0].doubleValue;
133136
par->minScale = desiredKwds[1].doubleValue;
@@ -136,6 +139,9 @@ readGridWrapper(configInfo *par, struct grid **gp, char ***collPartNames, int *n
136139
par->radiusSqu = par->radius*par->radius;
137140
par->minScaleSqu = par->minScale*par->minScale;
138141

142+
if(gridInfoRead.nSpecies>0)
143+
par->useAbun = !densMolColsExists;
144+
139145
sanityCheckOfRead(status, par, gridInfoRead);
140146

141147
freeKeywords(desiredKwds, numDesiredKwds);

src/grid2fits.c

Lines changed: 65 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Where column names contain a lower-case letter, this is a placeholder for a digi
4747
1 FIRST_NN V # See explanation in section 3 below.
4848
2 VELj D # 1 col per jth dimension.
4949
3 DENSITYn D # 1 per nth collision partner.
50-
4 ABUNMOLm E # 1 per mth molecular species.
50+
4 DENSMOLm E # 1 per mth molecular species. (Note that we will allow reading of ABUNMOLm for backwards compatibility.)
5151
5 TURBDPLR E # Given Gaussian lineshape exp(-v^2/[B^2 + 2*k*T/m]), this is B.
5252
6 TEMPKNTC E # From t[0].
5353
6 TEMPDUST E # From t[1].
@@ -285,10 +285,10 @@ The function does two things:
285285
dataTypeAllCols[colI] = TDOUBLE;
286286
}
287287

288-
/* should rather have a vector column? */
288+
/* Should rather have a vector column? */
289289
for(i_us=0;i_us<gridInfo.nSpecies;i_us++){
290290
colI++;
291-
sprintf((*allColNames)[colI], "ABUNMOL%d", (int)i_us+1);
291+
sprintf((*allColNames)[colI], "DENSMOL%d", (int)i_us+1);
292292
if(bitIsSet(dataFlags, DS_bit_abundance)){
293293
colToWriteI++;
294294
(*allColNumbers)[colI] = colToWriteI;
@@ -409,7 +409,7 @@ Note that data types in all capitals are defined in fitsio.h.
409409
_Bool *sink=NULL;/* Probably should be char* but this seems to work. */
410410
unsigned short *numNeigh=NULL,i_us,localNumCollPart;
411411
double *velj=NULL,*densn=NULL;
412-
float *dopb=NULL, *t=NULL, *abunm=NULL, *bField=NULL;
412+
float *dopb=NULL, *t=NULL, *densm=NULL, *bField=NULL;
413413
int status=0, colI=0, i, di, maxNumCols;
414414
LONGLONG firstRow=1, firstElem=1;
415415
char genericComment[80];
@@ -537,25 +537,25 @@ Ok we have a bit of a tricky situation here in that the number of columns we wri
537537
free(densn);
538538
}
539539

540-
/* Check if first ABUNMOL column has info:
540+
/* Check if first DENSMOL column has info:
541541
*/
542-
colI = allColNumbers[getColIndex(allColNames, maxNumCols, "ABUNMOL1")];
542+
colI = allColNumbers[getColIndex(allColNames, maxNumCols, "DENSMOL1")];
543543
if(colI>0){
544-
abunm = malloc(sizeof(*abunm)*totalNumGridPoints);
544+
densm = malloc(sizeof(*densm)*totalNumGridPoints);
545545
for(i_us=0;i_us<gridInfo.nSpecies;i_us++){
546-
sprintf(colName, "ABUNMOL%d", (int)i_us+1);
546+
sprintf(colName, "DENSMOL%d", (int)i_us+1);
547547
colI = allColNumbers[getColIndex(allColNames, maxNumCols, colName)];
548548
if(colI<=0){
549549
if(!silent) bail_out("This should not occur, it is some sort of bug.");
550550
exit(1);
551551
}
552552

553553
for(i_ui=0;i_ui<totalNumGridPoints;i_ui++)
554-
abunm[i_ui] = (float)gp[i_ui].mol[i_us].abun;
555-
fits_write_col(fptr, colDataTypes[colI-1], colI, firstRow, firstElem, (LONGLONG)totalNumGridPoints, abunm, &status);
554+
densm[i_ui] = (float)gp[i_ui].mol[i_us].nmol;
555+
fits_write_col(fptr, colDataTypes[colI-1], colI, firstRow, firstElem, (LONGLONG)totalNumGridPoints, densm, &status);
556556
processFitsError(status);
557557
}
558-
free(abunm);
558+
free(densm);
559559
}
560560

561561
colI = allColNumbers[getColIndex(allColNames, maxNumCols, "TURBDPLR")];
@@ -944,10 +944,9 @@ readKeywordsFromFITS(fitsfile *fptr, struct keywordType *kwds\
944944

945945
/*....................................................................*/
946946
void
947-
readGridExtFromFITS(fitsfile *fptr\
948-
, struct gridInfoType *gridInfoRead, struct grid **gp\
949-
, unsigned int **firstNearNeigh, char ***collPartNames\
950-
, int *numCollPartRead, int *dataFlags){
947+
readGridExtFromFITS(fitsfile *fptr, struct gridInfoType *gridInfoRead\
948+
, struct grid **gp, unsigned int **firstNearNeigh, char ***collPartNames\
949+
, int *numCollPartRead, int *dataFlags, _Bool *densMolColsExists){
951950
/*
952951
The present function mallocs 'gp' and sets defaults for all the simple or first-level struct elements.
953952
@@ -956,17 +955,17 @@ If COLLPARn keywords are found in the GRID extension header then collPartNames i
956955
Note that the calling routine needs to free gp, firstNearNeigh and collPartNames after use.
957956
*/
958957

959-
LONGLONG numGridCells, firstRow=1, firstElem=1, i_LL;
960-
int status=0, colNum, anynul=0, i;
958+
LONGLONG numGridCells,firstRow=1,firstElem=1,i_LL;
959+
int status=0,colNum,anynul=0,i;
961960
char colName[20];
962961
char genericKwd[9];
963962
char message[STR_LEN_0];
964963
unsigned int *ids=NULL;
965964
double *xj=NULL;
966965
_Bool *sink=NULL;/* Probably should be char* but this seems to work. */
967-
unsigned short *numNeigh=NULL, i_us;
968-
double *velj=NULL, *densn=NULL;
969-
float *dopb=NULL, *t=NULL, *abunm=NULL, *bField=NULL;
966+
unsigned short *numNeigh=NULL,i_us;
967+
double *velj=NULL,*densn=NULL;
968+
float *dopb=NULL,*t=NULL,*abunm=NULL,*densm=NULL,*bField=NULL;
970969

971970
/* Go to the GRID extension.
972971
*/
@@ -982,9 +981,21 @@ Note that the calling routine needs to free gp, firstNearNeigh and collPartNames
982981
return; /* I.e. with dataFlags left unchanged. */
983982
}
984983

985-
/* Count the numbers of ABUNMOL columns to get the number of species:
984+
/* Find out if the user has supplied ABUNMOLn or DENSMOLn columns.
985+
*/
986+
*densMolColsExists = FALSE;
987+
fits_get_colnum(fptr, CASEINSEN, "DENSMOL1", &colNum, &status);
988+
if(status==0)
989+
*densMolColsExists = TRUE;
990+
else
991+
processFitsError(status);
992+
993+
/* Count the numbers of ABUNMOLn/DENSMOLn columns to get the number of species:
986994
*/
987-
gridInfoRead->nSpecies = (unsigned short)countColsBasePlusInt(fptr, "ABUNMOL");
995+
if(*densMolColsExists)
996+
gridInfoRead->nSpecies = (unsigned short)countColsBasePlusInt(fptr, "DENSMOL");
997+
else
998+
gridInfoRead->nSpecies = (unsigned short)countColsBasePlusInt(fptr, "ABUNMOL");
988999
mallocAndSetDefaultGrid(gp, (size_t)numGridCells, gridInfoRead->nSpecies);
9891000

9901001
/* Read the columns.
@@ -1144,23 +1155,43 @@ Note that the calling routine needs to free gp, firstNearNeigh and collPartNames
11441155
}
11451156

11461157
if(gridInfoRead->nSpecies > 0){
1147-
/* Read the ABUNMOL columns:
1148-
*/
1149-
abunm = malloc(sizeof(*abunm)*numGridCells);
1150-
for(i_us=0;i_us<gridInfoRead->nSpecies;i_us++){
1151-
sprintf(colName, "ABUNMOL%d", (int)i_us+1);
1152-
fits_get_colnum(fptr, CASEINSEN, colName, &colNum, &status);
1153-
processFitsError(status);
1158+
if(*densMolColsExists){
1159+
/* Read the DENSMOL columns:
1160+
*/
1161+
densm = malloc(sizeof(*densm)*numGridCells);
1162+
for(i_us=0;i_us<gridInfoRead->nSpecies;i_us++){
1163+
sprintf(colName, "DENSMOL%d", (int)i_us+1);
1164+
fits_get_colnum(fptr, CASEINSEN, colName, &colNum, &status);
1165+
processFitsError(status);
11541166

1155-
fits_read_col(fptr, TFLOAT, colNum, firstRow, firstElem, numGridCells, 0, abunm, &anynul, &status);
1156-
processFitsError(status);
1167+
fits_read_col(fptr, TFLOAT, colNum, firstRow, firstElem, numGridCells, 0, densm, &anynul, &status);
1168+
processFitsError(status);
11571169

1158-
for(i_LL=0;i_LL<numGridCells;i_LL++) {
1159-
(*gp)[i_LL].mol[i_us].abun = (double)abunm[i_LL];
1170+
for(i_LL=0;i_LL<numGridCells;i_LL++) {
1171+
(*gp)[i_LL].mol[i_us].nmol = (double)densm[i_LL];
1172+
}
1173+
}
1174+
free(densm);
1175+
}else{
1176+
/* Read the ABUNMOL columns:
1177+
*/
1178+
abunm = malloc(sizeof(*abunm)*numGridCells);
1179+
for(i_us=0;i_us<gridInfoRead->nSpecies;i_us++){
1180+
sprintf(colName, "ABUNMOL%d", (int)i_us+1);
1181+
fits_get_colnum(fptr, CASEINSEN, colName, &colNum, &status);
1182+
processFitsError(status);
1183+
1184+
fits_read_col(fptr, TFLOAT, colNum, firstRow, firstElem, numGridCells, 0, abunm, &anynul, &status);
1185+
processFitsError(status);
1186+
1187+
for(i_LL=0;i_LL<numGridCells;i_LL++) {
1188+
(*gp)[i_LL].mol[i_us].abun = (double)abunm[i_LL];
1189+
}
11601190
}
1191+
free(abunm);
11611192
}
1162-
free(abunm);
11631193

1194+
// par->useAbun = !densMolColsExists;
11641195
(*dataFlags) |= (1 << DS_bit_abundance);
11651196
}
11661197

src/grid2fits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ NOTE! This file is not stand-alone, in needs to be included in an environment wh
2828

2929
_Bool checkPopsFITSExtExists(fitsfile*, const unsigned short);
3030
void closeFITSFile(fitsfile*);
31-
void readGridExtFromFITS(fitsfile*, struct gridInfoType*, struct grid**, unsigned int**, char***, int*, int*);
31+
void readGridExtFromFITS(fitsfile*, struct gridInfoType*, struct grid**, unsigned int**, char***, int*, int*, _Bool *densMolColsExists);
3232
void readKeywordsFromFITS(fitsfile*, struct keywordType*, const int);
3333
void readLinksExtFromFITS(fitsfile*, struct gridInfoType*, struct grid*, struct linkType**, int*);
3434
void readNnIndicesExtFromFITS(fitsfile*, struct linkType*, struct linkType***, struct gridInfoType*, int*);

0 commit comments

Comments
 (0)