Skip to content
util.c 80.4 KiB
Newer Older
Fabio Roberto Vitello's avatar
Fabio Roberto Vitello committed
#include <stdlib.h>
#include <stdio.h>
#include "util.h"
#include <mpi.h>
#include <math.h>
#ifdef OMP
#include <omp.h>
#endif
#define __MSGSIZ_MAX 100000

/* The function instrIndexIdToColIndexGlobal gets *instrIndexPointer and *instrConst, and computes *matrixIndexIntsr,
   which contains the columns of the 6 instrument parameters in the design matrix.
   The input pointers refers to:
   a) which FoV, CCD, PixelColumn, and TimeInterval each observation has occurred according to the following schema
      instrIndexPointer[0]=FoV instrIndexPointer[1]=CCD instrIndexPointer[2]=PixelColumn instrIndexPointer[3]=TimeInterval
   b) the characteristics of the instrument according to the following schema
      instrConst[0]=nFoVs instrConst[1]=nCCDs instrConst[2]=nPixelColumns instrConst[3]=nTimeIntervals
   For performance reasons, it accepts some (five) pre-calculated constant offsets, i.e.:
   1) offsetCMag         = nCCDs = instrConst[1]
   2) offsetCnu          = nCCDs*(1+nFoVs) = instrConst[1]*(1+instrConst[0])
   3) offsetCdelta_eta   = nCCDs*(1+nFoVs+nPixelColumns) = instrConst[1]*(1+instrConst[0]+instrConst[2])
   4) offsetCDelta_eta_1 = nCCDs*(1+nFoVs*(1+nTimeIntervals)+nPixelColumns) = instrConst[1]*(1+instrConst[0]*(1+instrConst[3])+instrConst[2])
   5) offsetCDelta_eta_2 = nCCDs*(1+nFoVs*(1+2*nTimeIntervals)+nPixelColumns) = instrConst[1]*(1+instrConst[0]*(1+2*instrConst[3])+instrConst[2])
 
*/

void instrIndexIdToColIndexGlobal(int* instrIndexPointer, int* instrConst,int totrows ,struct comData comlsqr, int* instrCols)
{
long relPos_ls;
int nInstrParam=comlsqr.nInstrParam;
short nInstrPSolved=comlsqr.nInstrPSolved;
int maInstrFlag=comlsqr.maInstrFlag;
int nuInstrFlag=comlsqr.nuInstrFlag;
int ssInstrFlag=comlsqr.ssInstrFlag;
int lsInstrFlag=comlsqr.lsInstrFlag;
int nFoVs =1+instrConst[0];
int nCCDs = instrConst[1];
int nPixCols = instrConst[2];
int nTInts = instrConst[3];

for(int jj=0;jj<totrows;jj++){
    
    if(instrIndexPointer[jj*(DEFAULT_NINSTRINDEXES+1)]==-1){ //is a Constraint
		for(int kk=0;kk<nInstrPSolved;kk++)
            instrCols[jj*nInstrPSolved+kk]=0;
        continue;
    }
	// FoV = instrIndexPointer[jj*DEFAULT_NINSTRINDEXES], CCD = instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+1]
	// PixelColumn = instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+2], TimeInterval = instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+3]
	int FoV = instrIndexPointer[jj*(DEFAULT_NINSTRINDEXES+1)];
	int CCD = instrIndexPointer[jj*(DEFAULT_NINSTRINDEXES+1)+1];
    int PixCol = instrIndexPointer[jj*(DEFAULT_NINSTRINDEXES+1)+2];
    int TInt = instrIndexPointer[jj*(DEFAULT_NINSTRINDEXES+1)+3];
    int ACALFlag = instrIndexPointer[jj*(DEFAULT_NINSTRINDEXES+1)+4];

	int counter=0;
		if(maInstrFlag) {
			instrCols[jj*nInstrPSolved+counter] = CCD-1; // Index_CMag = CCD-1
			counter++;
		}
		if(nuInstrFlag) {
			// Index_Cnu = offsetCMag + (FoV−1)*nCCDs + (CCD−1)
			instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCMag + FoV*nCCDs + (CCD-1);
			counter++;
		}
		if(ssInstrFlag) {
			if(ACALFlag) {
				// Index Cdelta_zeta = offsetCDelta_eta_3 + (CCD−1)*nPixelColumns + (PixelColumn−1)
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCDelta_eta_3 + (CCD-1)*nPixCols + (PixCol-1);
				counter++;
			} else {
				// Index Cdelta_eta = offsetCnu + (CCD−1)*nPixelColumns + (PixelColumn−1)
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCnu + (CCD-1)*nPixCols + (PixCol-1);
				counter++;
			}
		}
	// For performance reasons, compute the relative Delta_eta index only once
    // relPos_Delta_eta   = (FoV-1)*nCCDs*nTimeIntervals+(CCD-1)*nTimeIntervals+(TimeInterval-1)
    //                    = (instrIndexPointer[0]-1)*instrConst[1]*instrConst[3]+(instrIndexPointer[1]-1)*instrConst[3]+(instrIndexPointer[3]-1)
		if(lsInstrFlag) {
			relPos_ls = FoV*nCCDs*nTInts+(CCD-1)*nTInts+(TInt-1);
			if(ACALFlag) {
				// Index CDelta_zeta_1 = offsetCdelta_zeta + relPos_Delta_zeta
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCdelta_zeta + relPos_ls;
				counter++;
				// Index CDelta_zeta_2 = offsetCDelta_zeta_1 + relPos_Delta_zeta
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCDelta_zeta_1 + relPos_ls;
				counter++;
				// Index CDelta_zeta_3 = offsetCDelta_zeta_2 + relPos_Delta_zeta
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCDelta_zeta_2 + relPos_ls;
				counter++;
			} else {
				// Index CDelta_eta_1 = offsetCdelta_eta + relPos_Delta_zeta
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCdelta_eta + relPos_ls;
				counter++;
				// Index CDelta_eta_2 = offsetCDelta_eta_1 + relPos_Delta_zeta
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCDelta_eta_1 + relPos_ls;
				counter++;
				// Index CDelta_eta_3 = offsetCDelta_eta_2 + relPos_Delta_zeta
				instrCols[jj*nInstrPSolved+counter] = comlsqr.offsetCDelta_eta_2 + relPos_ls;
				counter++;
			}
		}
//	}
	if(counter!=nInstrPSolved) {
		printf("SEVERE ERROR PE=%d counter=%d != nInstrPSolved=%d on row #%d\n",comlsqr.myid, counter, nInstrPSolved, jj);
		MPI_Abort(MPI_COMM_WORLD, 1);
		exit(EXIT_FAILURE);

	}
    for(int k=0;k<nInstrPSolved;k++){
        if(instrCols[jj*nInstrPSolved+k]>=nInstrParam ||instrCols[jj*nInstrPSolved+k]<0 ){
            printf("SEVERE ERROR on instrCols[%d]=%d > nInstrparam=%d\n",jj*nInstrPSolved+k,instrCols[jj*nInstrPSolved+k],nInstrParam);
            MPI_Abort(MPI_COMM_WORLD, 1);
            exit(EXIT_FAILURE);
        }
    }

}
return;
} 

void ColIndexToinstrIndexIdGlobal(int* instrIndexPointer, int* instrConst,int totrows ,struct comData comlsqr, int* instrCols)
{
    long relPos_Delta_eta;
    int testConstr=0;
    
    for(int jj=0;jj<totrows;jj++){
        testConstr=0;
         for(int k=0;k<6;k++){
            testConstr+=instrCols[jj*DEFAULT_NINSTRVALUES+k];
        }
        if(testConstr==0){ //is a Constraint
            instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+0]=0;
            instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+1]=0;
            instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+2]=0;
            instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+3]=0;
            continue;
        }
        // Index_CMag = CCD-1
        instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+1]=instrCols[jj*DEFAULT_NINSTRVALUES+0]+1;
        // Index_Cnu = offsetCMag + (FoV−1)*nCCDs + (CCD−1)
        instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+0]=(instrCols[jj*DEFAULT_NINSTRVALUES+1]-instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+1]+1-comlsqr.offsetCMag)/instrConst[1] + 1;
        // Index Cdelta_eta = offsetCnu + (CCD−1)*nPixelColumns + (PixelColumn−1)
        instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+2]=instrCols[jj*DEFAULT_NINSTRVALUES+2]-comlsqr.offsetCnu-(instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+1]-1)*instrConst[2]+1;
        
        relPos_Delta_eta=instrCols[jj*DEFAULT_NINSTRVALUES+3]-comlsqr.offsetCdelta_eta;
        instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+3]=relPos_Delta_eta -(instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+0]-1)*instrConst[1]*instrConst[3]-(instrIndexPointer[jj*DEFAULT_NINSTRINDEXES+1]-1)*instrConst[3]+1;
    }
    return;
}

/*--------------------------------------------------------------------------*/
void printerror(int status) {
	/*****************************************************/
	/* Print out cfitsio error messages and exit program */
	/*****************************************************/

	if (status) {
//		fits_report_error(stderr, status); /* print error report */
		MPI_Abort(MPI_COMM_WORLD, status);
		exit(status); /* terminate the program, returning error status */
	}
	return;
}

/*--------------------------------------------------------------------------*/
void printerrorsingle(int status) {
	/*****************************************************/
	/* Print out cfitsio error messages and exit program */
	/*****************************************************/

	if (status) {
//		fits_report_error(stderr, status); /* print error report */
		exit(status); /* terminate the program, returning error status */
	}
	return;
}

int err_malloc(const char *s,int id) {
	printf("out of memory while allocating %s on PE=%d.\n", s, id);
	MPI_Abort(MPI_COMM_WORLD, 1);
	return 1;
}

int sel(const struct dirent *a) {
	return ((strncmp(a->d_name, "dpccu3dpctavugsrgsrsystemrow", 28) == 0) ? 1 : 0);
}
int selextConstrStar(const struct dirent *a) {
    return ((strstr(a->d_name, "nullspaceastrofit") != NULL) ? 1 : 0);
}
int selextConstrAtt(const struct dirent *a) {
    return ((strstr(a->d_name, "nullspaceattitudefit") != NULL) ? 1 : 0);
}
int selbarConstrStar(const struct dirent *a) {
    return ((strstr(a->d_name, "barconstrastrofit") != NULL) ? 1 : 0);
}
int selSM(const struct dirent *a) {
	return ((strstr(a->d_name, "_SM.bin") != NULL) ? 1 : 0);
}
Loading full blame...