Nick Craig-Wood skrev:
Sheldon <sh******@gmail .comwrote:
gdb msgppscomp.py core.3203
Run
gdb python
Then type
run msgppscomp.py
at the gdb prompt.
When it crashes, type "bt" for a back trace. This will pinpoint
exactly where it crashes.
Hopefully that will make things clearer. If not post the output.
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
I will post the entire extention if it helps:
/* These must be defined and included before all other standard
libraries */
#define ACPG_PYMODULE_W ITH_IMPORT_ARRA Y
#include "acpg_arrayobje ct_wrap.h"
/*************** *************** *************** *************** ************/
#define NumberOfTiles 12
#define res 15
#define AreaRow 4860
#define AreaCol 3645
#define LenTot 17714700
#define TileTot 1476225
#define ResRow 324
#define ResCol 243
#define LenRes 78732
#define PHYS
"/data/proj/safworks/sheldon/pps_v11/import/AUX_data/remapped/physiography."
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "read_vhlhd f.h"
#include "read_hlhdf .h"
#include "hlhdf.h"
#include <sys/types.h>
#include <dirent.h>
#include "vhlhdf.h"
#include <math.h>
#include "smhi_saf.h "
#include <stdarg.h>
#include <mcheck.h>
/* function declaration */
static int inithdf = 1; /* 1 om initHlHdf ej har anropats, 0 annars.
*/
/* Functions for C wrapper */
//static PyObject *ErrorObject;
static int createPythonObj ect(void);
static int readPythonObjec t(void);
PyMODINIT_FUNC initmsgppsarea( void); /* msgppsarea will be the name of
the SO file */
/* Copied from Sarah */
static int arrShortFromHdf Node(HL_NodeLis t *nodelist, char *nodename);
static int arrIntFromHdfNo de(HL_NodeList *nodelist, char *nodename,
char *satp);
/* *************** ******** */
static int readHDFPPS(void );
static int readHDFMSG(void );
static int readHDFQual(voi d);
static int readHDFPHYS(voi d);
static int evaluateBIT(uns igned short int value, int bitnumber);
static int Statistics(void );
static int FillArea(void);
static int Percent(void);
static int InterpAvgF(floa t** array, float** array15km);
static int InterpAvgI(int* * array, int** array15km);
static int Set2Zero(void);
static int Reset(void);
static int Bias(void);
static int LightCon(void);
static int PrintInt(int **array, int R, int C);
static int Memory(void);
static int MemoryFree1D(vo id);
static int MemoryFreeOrd(v oid);
static int MemoryFree15km( void);
static int MemoryFreeA(voi d);
/* Data structure */
static struct Region {
int ysize;
int xsize;
} reg;
static struct Variables {
int** dayA;
int** twilightA;
int** msgFCA;
int** ppsFCA;
int** msgcloudyA;
int** ppscloudyA;
float** lightconA;
float** bias100A;
float** bias75A;
float** bias50A;
float** bias25A;
int** valconcenA;
float** lightcon15km;
float** bias10015km;
float** bias7515km;
float** bias5015km;
float** bias2515km;
int** valconcen15km;
int** day;
int** twilight;
int** msgcloudy;
int** msgFC;
int** ppsFC;
int** ppscloudy;
int** msgdata;
int** ppsdata;
int** valconcen;
int** frac;
int* stats;
int Row;
int Col;
int Lentot;
int p1,p2,p3,p4;
int sumscenes;
int* nscenes;
int Cat;
unsigned short** qual;
int datein;
float* percentage;
char date[10];
char** tiles;
char** msg_scenes;
char** pps_scenes;
char tile[5];
char Region[20];
char Reprodir[200];
char PPSfile[200];
char MSGfile[200];
char PHYSfile[200];
PyObject* nscenesobj;
PyObject* tileobj;
PyObject* msgobj;
PyObject* ppsobj;
PyObject* outobj1;
PyObject* outobj2;
} work;
char *tileinfo[NumberOfTiles][6] = {
"05","ibla_68n2 9w","0","1215", "0","1215",
"06","ibla_68n0 0e","0","1215", "1215","243 0",
"07","ibla_68n2 9e","0","1215", "2430","364 5",
"0B","ibla_57n2 0w","1215","243 0","0","1215 ",
"0C","ibla_57n0 0e","1215","243 0","1215","2430 ",
"0D","ibla_57n2 0e","1215","243 0","2430","3645 ",
"13","ibla_46n1 6w","2430","364 5","0","1215 ",
"14","ibla_46n0 0e","2430","364 5","1215","2430 ",
"15","ibla_46n1 6e","2430","364 5","2430","3645 ",
"1D","ibla_35n1 3w","3645","486 0","0","1215 ",
"1E","ibla_35n0 0e","3645","486 0","1215","2430 ",
"1F","ibla_35n1 3e","3645","486 0","2430","3645 "
};
PyObject *Rlightcon=NULL ;
PyObject *Rbias100=NULL;
PyObject *Rbias75=NULL;
PyObject *Rbias50=NULL;
PyObject *Rbias25=NULL;
PyObject *Rvalcon=NULL;
PyObject *Rstats=NULL;
PyObject *Rpercentage=NU LL;
static PyObject* msgppsarea(PyOb ject* self, PyObject* args, PyObject
*kwargs) { /* Start main function */
mtrace ();
mcheck(NULL);
static char *kwlist[] =
{"nscenesobj"," tileobj","msgob j","ppsobj","da teint","sumscen es",NULL};
int i,j, xp;
int q,p;
int snum;
int counter;
int oldpos;
/*Take care of the input. Remember that the & sign is need for all
types of variables*/
if (!PyArg_ParseTu pleAndKeywords( args,kwargs,"OO OOii:nothing",
kwlist,
&work.nscenesob j,
&work.tileob j,
&work.msgobj ,
&work.ppsobj ,
&work.datein ,
&work.sumscenes )) {
printf("Error while receiving data in C\n");
goto fail;
}
/* Converts the date to a string */
sprintf(work.da te,"%d",work.da tein);
printf("Date: %s\n", work.date);
/* Allocate memory to python objects */
work.pps_scenes = malloc(sizeof *work.pps_scene s*work.sumscene s);
work.msg_scenes = malloc(sizeof *work.msg_scene s*work.sumscene s);
if (work.pps_scene s == NULL) {
printf("fail to allocate memory!");
exit(EXIT_FAILU RE);
}
if (work.msg_scene s == NULL) {
printf("fail to allocate memory!");
exit(EXIT_FAILU RE);
}
work.tiles = malloc(sizeof*w ork.tiles*Numbe rOfTiles);
for (i = 0; i < work.sumscenes; i++) {
work.pps_scenes[i] = malloc(sizeof 300);
work.msg_scenes[i] = malloc(sizeof 300);
}
for (i = 0; i < NumberOfTiles; i++) {
work.tiles[i] = malloc(sizeof 5);
}
work.nscenes = malloc(sizeof(i nt)*NumberOfTil es);
if (work.pps_scene s==NULL) {
printf("Failed to allocate memory for pps and msg scenes\n");
goto fail;
}
if (work.msg_scene s==NULL) {
printf("Failed to allocate memory for pps and msg scenes\n");
goto fail;
}
if (work.tiles==NU LL) {
printf("Failed to allocate memory for pps and msg scenes\n");
goto fail;
}
if (work.nscenes== NULL) {
printf("Failed to allocate memory for pps and msg scenes\n");
goto fail;
}
work.Row = 1215;
work.Col = 1215;
reg.xsize = 1215;
reg.ysize = 1215;
work.Cat = 60;
/* Reading the Python lists into character arrays */
if (!readPythonObj ect()) {
printf("readPyt honObject_int error\n");
goto fail;
}
if (!Memory()) {
printf("Failed to assign memory\n");
goto fail;
}
if (!(Set2Zero())) {
printf("Failed to initialize arrays\n");
goto fail;
}
snum = 0;
counter = 0;
oldpos = 0;
for (i = 0; i < NumberOfTiles; i++) {
snum += work.nscenes[i];
strcpy(work.til e,work.tiles[i]);
/*printf("%d\n%d \n%s\n",work.Ro w,work.Col,work .Region);*/
/*printf("\nDoin g tile: %s\n", work.tile);*/
for (xp = 0; xp < NumberOfTiles; xp++) {
if (strncmp(work.t ile,tileinfo[xp][0],2) == 0) {
strcpy(work.Reg ion,tileinfo[xp][1]);
work.p1 = atoi(tileinfo[xp][2]);
work.p2 = atoi(tileinfo[xp][3]);
work.p3 = atoi(tileinfo[xp][4]);
work.p4 = atoi(tileinfo[xp][5]);
}
}
strcpy(work.PHY Sfile,PHYS);
strcat(work.PHY Sfile,work.Regi on);
strcat(work.PHY Sfile,".hdf");
printf("PHYSfil e: %s\n", work.PHYSfile);
/* fetching the latlon data */
if (!(readHDFPHYS( ))) {
printf("Failed reading frac data\n");
goto fail;
}
//printf("\nsnum: %d\n", snum);
/* Popuplate the arrays */
printf("\nProce ssing %d scenes for tile %s\n",
work.nscenes[i],work.tile);
for (q = oldpos; q < snum; q++) {
oldpos = snum;
/*printf("\n Processing scene number %d \n", ++counter);*/
/* printf("Copying strings\n"); */
strcpy(work.MSG file,work.msg_s cenes[q]);
/*printf("%s\n", work.MSGfile);*/
strcpy(work.PPS file,work.pps_s cenes[q]);
/*printf("%s\n", work.PPSfile);*/
/*printf("Copyin g complete\n"); */
/* Reading the data into the structure */
if (!(readHDFPPS() )) {
printf("Failed reading pps data\n");
goto fail;
}
if (!(readHDFMSG() )) {
printf("Failed reading msg data\n");
goto fail;
}
if (!(readHDFQual( ))) {
printf("Failed reading quality data\n");
goto fail;
}
/* Doing the statistics */
if (!(Statistics() )) {
printf("Failed collecting the stats\n");
goto fail;
}
} /* End loop over scenes */
if (!(FillArea())) {
printf("Failed collecting the stats\n");
goto fail;
}
printf("Moving on to next tile\n");
/* Reinitialize arrays */
if (!(Reset())) {
printf("Failed to reinitialize arrays\n");
goto fail;
}
} /* End loop over tiles */
for (i = 0; i < work.sumscenes; i++) {
free(work.pps_s cenes[i]);
free(work.msg_s cenes[i]);
}
free(work.pps_s cenes);
free(work.msg_s cenes);
for (i = 0; i < NumberOfTiles; i++) {
free(work.tiles[i]);
}
free(work.tiles );
free(work.nscen es);
if(!Percent()) {
printf("Failed calculate percentages\n") ;
goto fail;
}
/* for (i = 0; i < work.Cat; i++) {
printf("percent age[%d]: %f\n",i,work.pe rcentage[i]);
} */
if (!(Bias())) {
printf("Failed to calculate biases\n");
goto fail;
}
if (!(LightCon())) {
printf("Failed to calculate lightcon\n");
goto fail;
}
/* Create Python Objects */
/* Free up memory from the ordinary arrays */
if (!(MemoryFreeOr d())) {
printf("Allocat ed free memory for ordinary arrays\n");
goto fail;
}
work.lightcon15 km = malloc(sizeof(f loat*)*ResRow);
work.bias10015k m = malloc(sizeof(f loat*)*ResRow);
work.bias7515km = malloc(sizeof(f loat*)*ResRow);
work.bias5015km = malloc(sizeof(f loat*)*ResRow);
work.bias2515km = malloc(sizeof(f loat*)*ResRow);
work.valconcen1 5km = malloc(sizeof(i nt*)*ResRow);
for (i = 0; i < ResRow; i++) {
work.lightcon15 km[i] = malloc(sizeof(f loat)*ResCol);
work.bias10015k m[i] = malloc(sizeof(f loat)*ResCol);
work.bias7515km[i] = malloc(sizeof(f loat)*ResCol);
work.bias5015km[i] = malloc(sizeof(f loat)*ResCol);
work.bias2515km[i] = malloc(sizeof(f loat)*ResCol);
work.valconcen1 5km[i] = malloc(sizeof(i nt)*ResCol);
}
if ((work.lightcon 15km)==NULL) {
printf("Failed to allocating lightcon memory\n");
exit(EXIT_FAILU RE);
}
if ((work.bias1001 5km)==NULL) {
printf("Failed to allocating bias100 memory\n");
exit(EXIT_FAILU RE);
}
if ((work.bias7515 km)==NULL) {
printf("Failed to allocating bias75 memory\n");
exit(EXIT_FAILU RE);
}
if ((work.bias5015 km)==NULL) {
printf("Failed to allocating bias50 memory\n");
exit(EXIT_FAILU RE);
}
if ((work.bias2515 km)==NULL) {
printf("Failed to allocating bias25 memory\n");
exit(EXIT_FAILU RE);
}
if ((work.valconce n15km)==NULL) {
printf("Failed to allocating val memory\n");
exit(EXIT_FAILU RE);
}
/* Interpolating data array */
if(!InterpAvgF( work.lightconA, work.lightcon15 km)) {
printf("Failed interpolation lightcon\n");
goto fail;
}
if (!InterpAvgI(wo rk.valconcenA,w ork.valconcen15 km)) {
printf("Failed interpolation val\n");
goto fail;
}
if(!InterpAvgF( work.bias100A,w ork.bias10015km )) {
printf("Failed interpolation b100\n");
goto fail;
}
if (!InterpAvgF(wo rk.bias75A,work .bias7515km)) {
printf("Failed interpolation b75\n");
goto fail;
}
if(!InterpAvgF( work.bias50A,wo rk.bias5015km)) {
printf("Failed interpolation b50\n");
goto fail;
}
if (!InterpAvgF(wo rk.bias25A,work .bias2515km)) {
printf("Failed interpolation b25\n");
goto fail;
}
/* Free Area arrays */
if ((MemoryFreeA() )) {
printf("Allocat ed free memory for area arrays\n");
goto fail;
}
/*printf("VA[%d]: %f\n",i, work.va_area[i]);
printf("biasdat a[%d]: %f\n",i, work.biasdata[i]);
printf("Number[%d]: %f\n",i, work.number[i]);*/
printf("Creatin g python objects\n");
Rlightcon=PyTup le_New(ResRow*R esCol);
Rbias100=PyTupl e_New(ResRow*Re sCol);
Rbias75=PyTuple _New(ResRow*Res Col);
Rbias50=PyTuple _New(ResRow*Res Col);
Rbias25=PyTuple _New(ResRow*Res Col);
Rvalcon=PyTuple _New(ResRow*Res Col);
Rstats=PyTuple_ New(work.Cat);
Rpercentage=PyT uple_New(work.C at);
if (!(createPython Object())) {
printf("Failed to create python arrays objects\n");
goto fail;
}
printf("Returni ng data back to Python!\n");
muntrace ();
return Py_BuildValue(" OOOOOOOO",Rbias 100,
Rbias75,
Rbias50,
Rbias25,
Rlightcon,
Rvalcon,
Rstats,
Rpercentage);
fail:
exit(EXIT_FAILU RE);
}
/* Functions for the C-wrapper */
/* This structure is needed for the python-C-interface */
static struct PyMethodDef msgppsareaMetho ds[] = {
{"msgppsarea ", (PyCFunction)ms gppsarea,
METH_VARARGS|ME TH_KEYWORDS,"HE LP for msgppsarea\n"},
{NULL,NULL,0,NU LL}
};
PyMODINIT_FUNC initmsgppsarea( void) {
(void) Py_InitModule(" msgppsarea",msg ppsareaMethods) ;
import_array(); /* access to Numeric PyArray functions */
}
/*Read an list of strings from a python object*/
static int readPythonObjec t(void) {
int i;
PyObject *msgop;
PyObject *ppsop;
PyObject *tileop;
PyObject *sceneop;
for (i = 0; i < work.sumscenes; i++) {
msgop = PyList_GetItem( work.msgobj, i);
work.msg_scenes[i] = PyString_AsStri ng(msgop);
ppsop = PyList_GetItem( work.ppsobj, i);
work.pps_scenes[i] = PyString_AsStri ng(ppsop);
}
for (i = 0; i < NumberOfTiles; i++) {
tileop = PyList_GetItem( work.tileobj, i);
work.tiles[i] = PyString_AsStri ng(tileop);
sceneop = PyList_GetItem( work.nscenesobj , i);
work.nscenes[i] = PyInt_AsLong(sc eneop);
}
return 1;
} /*end readPythonObjec t*/
static int createPythonObj ect(void) {
int i,j,k;
PyObject* op1;
PyObject* op2;
PyObject* ligop;
PyObject* valop;
PyObject* b100op;
PyObject* b75op;
PyObject* b50op;
PyObject* b25op;
for (i = 0; i < work.Cat; i++) {
op1 = PyInt_FromLong( (long)work.stat s[i]);
op2 = PyFloat_FromDou ble((double)wor k.percentage[i]);
if (PyTuple_SetIte m(Rstats,i,op1) !=0) {
printf("Error in creating python object Rstats\n");
exit(EXIT_FAILU RE);
}
if (PyTuple_SetIte m(Rpercentage,i ,op2) !=0) {
printf("Error in creating python object Rpercentage\n") ;
exit(EXIT_FAILU RE);
}
}
printf("Complet ed 1D arrays\n");
k=0;
for (i = 0; i < ResRow; i++) {
for (j = 0; j < ResCol; j++) {
ligop = PyFloat_FromDou ble((double)wor k.lightcon15km[i][j]);
if (PyTuple_SetIte m(Rlightcon,k,l igop) !=0) {
printf("Error in creating python light object\n");
exit(EXIT_FAILU RE);
}
valop = PyInt_FromLong( (long)work.valc oncen15km[i][j]);
if (PyTuple_SetIte m(Rvalcon,k,val op) !=0) {
printf("Error in creating python val object\n");
exit(EXIT_FAILU RE);
}
b100op = PyFloat_FromDou ble((double)wor k.bias10015km[i][j]);
if (PyTuple_SetIte m(Rbias100,k,b1 00op) !=0) {
printf("Error in creating python bias100 object\n");
exit(EXIT_FAILU RE);
}
b75op = PyFloat_FromDou ble((double)wor k.bias7515km[i][j]);
if (PyTuple_SetIte m(Rbias75,k,b75 op) !=0) {
printf("Error in creating python bias75 object\n");
exit(EXIT_FAILU RE);
}
b50op = PyFloat_FromDou ble((double)wor k.bias5015km[i][j]);
if (PyTuple_SetIte m(Rbias50,k,b50 op) !=0) {
printf("Error in creating python bias50 object\n");
exit(EXIT_FAILU RE);
}
b25op = PyFloat_FromDou ble((double)wor k.bias2515km[i][j]);
if (PyTuple_SetIte m(Rbias25,k,b25 op) !=0) {
printf("Error in creating python bias25 object\n");
exit(EXIT_FAILU RE);
}
k++;
}
}
printf("Complet ed 2D arrays.\nNow freeing 1D arrays\n");
if (!(MemoryFree1D ())) {
printf("Couldn' t free memory for area arrays\n");
goto fail;
}
printf("freed 1D arrays\n");
printf("freeing 15km arrays\n");
if (!(MemoryFree15 km())) {
printf("Couldn' t free memory for 15km arrays\n");
goto fail;
}
printf("Complet e\n");
return 1;
fail:
exit(EXIT_FAILU RE);
} /* end createPythonObj ect */
static int readHDFPHYS(voi d) {
HL_NodeList *nodelist = NULL; /* These are take from read_vlhdf.h
where some explanation is given */
char *field = "/fracofland";
if (inithdf) { /* Körs bara första gången rutinen anropas. Måste
vara med */
initHlHdf();
inithdf = 0;
printf("Finishe d setting inithdf\n");
}
if (!(nodelist=rea dHL_NodeList(wo rk.PHYSfile))) {
printf("Error reading nodelist! from files: %s\n", work.PHYSfile);
goto fail;
}
/*printf("Checki ng dataset's existens\n"); */
if (!selectAllNode s(nodelist)) {
printf("Fail to select items\n");
goto fail;
}
if(!fetchMarked Nodes(nodelist) ) {
printf("Failed to fetch data\n");
goto fail;
}
/* Reading cloudmask data into arrays */
if (!arrIntFromHdf Node(nodelist,f ield,"f\0")) {
printf("Failed getting fracofland data\n");
goto fail;
}
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
return 1;
fail:
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
exit(EXIT_FAILU RE);
}
static int readHDFPPS(void ) {
HL_NodeList *nodelist = NULL; /* These are take from read_vlhdf.h
where some explanation is given */
char *field = "/cloudmask";
if (inithdf) { /* Körs bara första gången rutinen anropas. Måste
vara med */
initHlHdf();
inithdf = 0;
printf("Finishe d setting inithdf\n");
}
if (!(nodelist=rea dHL_NodeList(wo rk.PPSfile))) {
printf("Error reading nodelist! from files: %s\n", work.PPSfile);
goto fail;
}
/*printf("Checki ng dataset's existens\n"); */
if (!selectAllNode s(nodelist)) {
printf("Fail to select items\n");
goto fail;
}
if(!fetchMarked Nodes(nodelist) ) {
printf("Failed to fetch data\n");
goto fail;
}
/* Reading cloudmask data into arrays */
if (!arrIntFromHdf Node(nodelist,f ield,"p\0")) {
printf("Failed getting the cloud mask\n");
goto fail;
}
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
return 1;
fail:
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
exit(EXIT_FAILU RE);
}
static int readHDFMSG(void ) {
HL_NodeList *nodelist = NULL; /* These are take from read_vlhdf.h
where some explanation is given */
char *field = "/cloudmask";
if (inithdf) { /* Körs bara första gången rutinen anropas. Måste
vara med */
initHlHdf();
inithdf = 0;
printf("Finishe d setting inithdf\n");
}
if (!(nodelist=rea dHL_NodeList(wo rk.MSGfile))) {
printf("Error reading nodelist! from files: %s\n", work.MSGfile);
goto fail;
}
/*printf("Checki ng dataset's existens\n"); */
if (!selectAllNode s(nodelist)) {
printf("Fail to select items\n");
goto fail;
}
if(!fetchMarked Nodes(nodelist) ) {
printf("Failed to fetch data\n");
goto fail;
}
/* Reading cloudmask data into arrays */
if (!arrIntFromHdf Node(nodelist,f ield,"m\0")) {
printf("Failed getting the cloud mask\n");
goto fail;
}
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
return 1;
fail:
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
exit(EXIT_FAILU RE);
}
static int readHDFQual(voi d) {
HL_NodeList *nodelist = NULL; /* These are take from read_vlhdf.h
where some explanation is given */
char *field = "/quality_flag";
if (inithdf) { /* Körs bara första gången rutinen anropas. Måste
vara med */
initHlHdf();
inithdf = 0;
printf("Finishe d setting inithdf\n");
}
if (!(nodelist=rea dHL_NodeList(wo rk.PPSfile))) {
printf("Error reading nodelist from files: %s\n", work.PPSfile);
goto fail;
}
if (!selectAllNode s(nodelist)) {
printf("Fail to select items\n");
goto fail;
}
if(!fetchMarked Nodes(nodelist) ) {
printf("Failed to fetch data\n");
goto fail;
}
if (!(arrShortFrom HdfNode(nodelis t,field))) {
printf("Failed getting the quality flag\n");
goto fail;
}
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
return 1;
fail:
if (nodelist!=NULL ) {
freeHL_NodeList (nodelist);
}
exit(EXIT_FAILU RE);
}
/* Statistics function */
static int Statistics(void ) { /* Start */
int clear = 1;
int cloudy = 3;
int i, j;
int island, iscoast, isnight, istwilight, isday;
/*printf("Calcul ating the stats\n");*/
for(i = 0; i < work.Row; i++) { /* Start loop */
for(j = 0; j < work.Col; j++) { /* Start loop */
if (work.ppsdata[i][j] == 0) work.ppsdata[i][j] = 255;
if (work.msgdata[i][j] == 0) work.msgdata[i][j] = 255;
if (work.ppsdata[i][j] == 5) work.ppsdata[i][j] = 255;
if (work.msgdata[i][j] == 5) work.msgdata[i][j] = 255;
if (work.ppsdata[i][j] == 4) work.ppsdata[i][j] = 1;
if (work.msgdata[i][j] == 4) work.msgdata[i][j] = 1;
if (work.msgdata[i][j] != 255 && work.ppsdata[i][j] != 255) {
work.valconcen[i][j]++;
if (work.ppsdata[i][j] == 2) {
work.ppsFC[i][j]++;
}
if (work.ppsdata[i][j] == 3) {
work.ppscloudy[i][j]++;
}
if (work.msgdata[i][j] == 2) {
work.msgFC[i][j]++;
}
if (work.msgdata[i][j] == 3) {
work.msgcloudy[i][j]++;
}
if (work.frac[i][j] 0 && work.frac[i][j] < 255) {
iscoast = 1;
} else {
iscoast = 0;
}
island = evaluateBIT((un signed short int)work.qual[i][j],0);
/*iscoast = evaluateBIT((un signed short int)work.qual[i][j],1);
coast not set in DWD files */
isnight = evaluateBIT((un signed short int)work.qual[i][j],2);
istwilight = evaluateBIT((un signed short int)work.qual[i][j],3);
/* Test */
/*if (iscoast == 1) printf("Found coast\n"); */
/* Creating day logical and doing some stats */
if(isnight) {
isday = 0;
work.stats[5]++; /*6*/
} else if (istwilight) {
isday = 0;
work.twilight[i][j]++;
work.stats[10]++; /*11*/
} else {
isday = 1;
work.day[i][j]++;
work.stats[0]++; /*1*/
}
/* Clear conditions */
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear &&
isday) {
work.stats[1]++; /*2*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& isnight) {
work.stats[6]++; /*7*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& istwilight) {
work.stats[11]++; /*12*/
}
/* Cloudy conditions */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == cloudy &&
isday) {
work.stats[2]++; /*3*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && isnight) {
work.stats[7]++; /*8*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && istwilight) {
work.stats[12]++; /*13*/
}
/* Clear work.msgdata & cloudy work.ppsdata */
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy &&
isday) {
work.stats[3]++; /*4*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy
&& isnight) {
work.stats[8]++; /*9*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy
&& istwilight) {
work.stats[13]++; /*14*/
}
/* Cloudy work.msgdata & clear pps */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear &&
isday) {
work.stats[4]++; /*5*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& isnight) {
work.stats[9]++; /*10*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& istwilight) {
work.stats[14]++; /*15*/
}
/* Counting the pixels over land */
if (isday && island) {
work.stats[15]++; /*16*/
} else if (isnight && island) {
work.stats[20]++; /*21*/
} else if (istwilight && island) {
work.stats[25]++; /*26*/
}
/* Land clear */
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear &&
isday && island) {
work.stats[16]++; /*17*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& isnight && island) {
work.stats[21]++; /*22*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& istwilight && island) {
work.stats[26]++; /*27*/
}
/* Land cloudy */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == cloudy &&
isday && island) {
work.stats[17]++; /*18*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && isnight && island) {
work.stats[22]++; /*23*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && istwilight && island) {
work.stats[27]++; /*28*/
}
/* work.msgdata clear pps cloudy */
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy &&
isday && island) {
work.stats[18]++; /*19*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy
&& isnight && island) {
work.stats[23]++; /*24*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy
&& istwilight && island) {
work.stats[28]++; /*29*/
}
/* work.msgdata cloudy pps clear */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear &&
isday && island) {
work.stats[19]++; /*20*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& isnight && island) {
work.stats[24]++; /*25*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& istwilight && island) {
work.stats[29]++; /*30*/
}
/* Counting pixels over water */
if (isday && !island) {
work.stats[30]++; /*31*/
} else if (isnight && !island) {
work.stats[35]++; /*36*/
} else if (istwilight && !island) {
work.stats[40]++; /*41*/
}
/* both clear */
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear &&
isday && !island) {
work.stats[31]++; /*32*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& isnight && !island) {
work.stats[36]++; /*37*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& istwilight && !island) {
work.stats[41]++; /*42*/
}
/* both cloudy */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == cloudy &&
isday && !island) {
work.stats[32]++; /*33*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && isnight && !island) {
work.stats[37]++; /*38*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && istwilight && !island) {
work.stats[42]++; /*43*/
}
/*work.msgdata clear pps cloudy */
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy &&
isday && !island) {
work.stats[33]++; /*34*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy
&& isnight && !island) {
work.stats[38]++; /*39*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == cloudy
&& istwilight && !island) {
work.stats[43]++; /*44*/
}
/* work.msgdata cloudy pps clear */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear &&
isday && !island) {
work.stats[34]++; /*35*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& isnight && !island) {
work.stats[39]++; /*40*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& istwilight && !island) {
work.stats[44]++; /*45*/
}
/* Coastal effects */
if (isday && iscoast) {
work.stats[45]++; /*46 Number of valid pixs*/
} else if (isnight && iscoast) {
work.stats[46]++; /* 47 Number of valid pixs */
} else if (istwilight && iscoast) {
work.stats[47]++; /* 48 Number of valid pixs */
}
/* Clear conditions */
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear &&
isday && iscoast) {
work.stats[48]++; /*49*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& isnight && iscoast) {
work.stats[49]++; /* 50*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& istwilight && iscoast) {
work.stats[50]++; /*51*/
}
/* Cloudy conditions */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == cloudy &&
isday && iscoast) {
work.stats[51]++; /*52*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && isnight && iscoast) {
work.stats[52]++; /*53*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] ==
cloudy && istwilight && iscoast) {
work.stats[53]++; /*54*/
}
/* Mixed conditions */
if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear &&
isday && iscoast) {
work.stats[54]++; /*55*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& isnight && iscoast) {
work.stats[55]++; /*56*/
} else if (work.msgdata[i][j] == cloudy && work.ppsdata[i][j] == clear
&& istwilight && iscoast) {
work.stats[56]++; /*57*/
}
if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear &&
isday && iscoast) {
work.stats[57]++; /*58*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& isnight && iscoast) {
work.stats[58]++; /*59*/
} else if (work.msgdata[i][j] == clear && work.ppsdata[i][j] == clear
&& istwilight && iscoast) {
work.stats[59]++; /* 60 */
}
}
}
}
/*for (i = 0; i < work.Cat; i++) {
printf("Stats[%d]: %d\t", i, work.stats[i]);
}*/
return 1;
}
static int evaluateBIT(uns igned short int value, int bitnumber) {
int one=1;
return ( one & (value >bitnumber) ); /* This does a & logical
calculation after the bitwise shift */
}
/*Read an array of integers, unknow type (i.e. int or ushort or uchar),
from node.
Give it as output in form of an array of integers*/
static int arrIntFromHdfNo de(HL_NodeList *nodelist, char *nodename,
char *satp) {
HL_Node *node;
unsigned char *hdfdata=NULL;
int ii,i,j;
/*Read from node*/
if(!(node=getNo de(nodelist,nod ename))){
printf("Failed reading node %s\n",nodename) ;
goto fail;
}
/*if (node->format == "uchar or U8LE") {*/
/*put data first in array of "unsigned char", then in array of int*/
if ((hdfdata = malloc(sizeof(i nt)*TileTot))== NULL) {
printf("Failed allocating data\n");
goto fail;
}
/*put data first in array of "unsigned char", then in array of int*/
memcpy(hdfdata, node->data,node->dSize*TileTot) ;
ii = 0;
for (i = 0; i < work.Row; i++) {
for (j = 0; j < work.Col; j++) {
if ((strcmp(satp," m\0")) == 0) {
work.msgdata[i][j] = (int)hdfdata[ii];
} else if ((strcmp(satp," p\0")) == 0) {
work.ppsdata[i][j] = (int)hdfdata[ii];
} else if ((strcmp(satp," f\0")) == 0) {
work.frac[i][j] = (int)hdfdata[ii];
}
ii++;
}
}
if (hdfdata!=NULL)
free(hdfdata);
return 1;
fail:
exit(EXIT_FAILU RE);
} /*end arrIntFromHdfNo de*/
/*Read an array of short from node.
Give it as output in form of an array of short */
static int arrShortFromHdf Node(HL_NodeLis t *nodelist, char *nodename) {
HL_Node *node;
unsigned short *hdfdata=NULL;
int ii;
int i,j;
/*Read from node*/
if(!(node=getNo de(nodelist,nod ename))) {
printf("Failed getting nodename %s\n",nodename) ;
goto fail;
}
/*else if (node->format == "ushort") {*/
/*put data first in array of "unsigned short", then in array of int*/
if ((hdfdata = malloc(sizeof(u nsigned short)*TileTot) )==NULL) {
printf("Failed allocating short array data\n");
goto fail;
}
/*printf("dSize short: %d", node->dSize);*/
memcpy(hdfdata, node->data,node->dSize*TileTot) ;
ii = 0;
for (i = 0; i < work.Row; i++) {
for (j = 0; j < work.Col; j++) {
work.qual[i][j] = (unsigned short)hdfdata[ii];
ii++;
}
}
if(hdfdata!=NUL L)
free(hdfdata);
return 1;
fail:
exit(EXIT_FAILU RE);
} /*end arrShortFromHdf Node*/
static int FillArea(void) {
/* Function to create 2D array and fill in with data */
int i,j,x,y;
x=0;
y=0;
for (i = work.p1; i < work.p2; i++) {
for (j = work.p3; j < work.p4; j++) {
work.valconcenA[i][j] = work.valconcen[x][y];
work.msgFCA[i][j] = work.msgFC[x][y];
work.ppsFCA[i][j] = work.ppsFC[x][y];
work.ppscloudyA[i][j] = work.ppscloudy[x][y];
work.msgcloudyA[i][j] = work.msgcloudy[x][y];
work.dayA[i][j] = work.day[x][y];
work.twilightA[i][j] = work.twilight[x][y];
y++;
}
y=0;
x++;
}
return 1;
}
/* Calculating the percentages */
static int Percent(void) {
int i;
printf("Calcula ting the percentage\n");
for (i = 0; i < work.Cat; i++) {
if (i 0 && i < 5) { /* Day only */
if (work.stats[0] 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[0] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Day error!\n");
goto error;
}
}
}
if (i 5 && i < 10) { /* Night only */
if (work.stats[5] 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[5] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Night error!\n");
goto error;
}
}
}
if (i 10 && i < 15) { /* Twilight only */
if (work.stats[10] 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[10] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Twiligh t error!\n");
goto error;
}
}
}
if (i 15 && i < 20) { /* Day land */
if (work.stats[15] 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[15] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Day land error!\n");
goto error;
}
}
}
if (i 20 && i < 25) { /* Night land */
if (work.stats[20] 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[20] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Night land error!\n");
goto error;
}
}
}
if (i 25 && i < 30) { /* Twilight land */
if (work.stats[25] 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[25] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Twiligh t land error!\n");
goto error;
}
}
}
if (i 30 && i < 35) { /* Water day */
if (work.stats[30] 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[30] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Water day error!\n");
goto error;
}
}
}
if (i 35 && i < 40) { /* Water night */
if (work.stats[35] != 0) {
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[35] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Water night error!\n");
goto error;
}
}
}
if (i 40 && i < 45) {
if (work.stats[40] != 0) { /* Twilight water */
work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[40] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Twiligh t water error!\n");
goto error;
}
}
}
if (work.stats[45] 0) { /*day coast*/
if (i == 48) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[45] * 100.0);
if (i == 51) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[45] * 100.0);
if (i == 54) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[45] * 100.0);
if (i == 57) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[45] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Day coast error!\n");
goto error;
}
}
if (work.stats[46] 0) { /*night coast */
if (i == 49) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[46] * 100.0);
if (i == 52) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[46] * 100.0);
if (i == 55) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[46] * 100.0);
if (i == 58) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[46] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Night coast error!\n");
goto error;
}
}
if (work.stats[47] 0) { /*twilight coast */
if (i == 50) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[47] * 100.0);
if (i == 53) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[47] * 100.0);
if (i == 56) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[47] * 100.0);
if (i == 59) work.percentage[i] = (float)((float) work.stats[i] /
(float)work.sta ts[47] * 100.0);
if (work.percentag e[i] 100.0) {
printf("Twiligh t coast error!\n");
goto error;
}
}
/*printf("At end of loop: percent[%d]: %f\t",i,work.pe rcentage[i]);
*/
}
return 1;
error:
exit(EXIT_FAILU RE);
}
/* Interpolate array to lower resolution */
/* Reduces the resolution of the 1km array to res km by
taking the average of all valid pixels in the res x res window. */
static int InterpAvgF(floa t** array, float** array15km) {
int i, j, r, k;
float avg;
float num;
printf("Calling InterpAvgF\n");
/* Slides over the window determined by res */
for (r = 0; r < ResRow; r++) { /* Start loop r */
for (k = 0; k < ResCol; k++) { /* Start loop k */
avg = 0.0;
num = 0.0;
for (i = (r*res); i < r*res+res; i++) {
for (j = (k*res); j < k*res+res; j++) {
if (array[i][j] != 255.0) {
avg += array[i][j];
num++;
}
}
}
if (num 0) {
array15km[r][k] = avg / num;
} else {
array15km[r][k] = 255.0;
}
}
}
return 1;
}
static int InterpAvgI(int* * array, int** array15km) {
int i, j, r, k;
int avg;
int num;
printf("Calling InterpAvgI\n");
/* Slides over the window determined by res */
for (r = 0; r < ResRow; r++) { /* Start loop r */
for (k = 0; k < ResCol; k++) { /* Start loop k */
avg = 0;
num = 0;
for (i = r*res; i < r*res+res; i++) {
for (j = k*res; j < k*res+res; j++) {
//printf("Val[%d][%d]: %d", i,j,array[i][j]);
if (array[i][j] 0) {
avg += array[i][j];
num++;
}
}
}
if (num 0) {
//printf("avg: %d\tnum: %d\n");
//printf("avg: %d", (int)((float)av g / (float)num));
array15km[r][k] = (int)( ((float)avg / (float)num) +.5);
} else {
array15km[r][k] = 255;
}
}
}
return 1;
}
static int Set2Zero(void) {
int i,j;
printf("Initial izing all arrays\n");
for (i = 0; i < work.Row; i++) {
for (j = 0; j < work.Col; j++) {
work.day[i][j] = 0;
work.twilight[i][j] = 0;
work.ppsFC[i][j] = 0;
work.msgFC[i][j] = 0;
work.msgcloudy[i][j] = 0;
work.ppscloudy[i][j] = 0;
work.valconcen[i][j] = 0;
work.frac[i][j] = 0;
}
}
for (i = 0; i < work.Cat; i++) {
work.stats[i] = 0;
work.percentage[i] = 0;
}
for (i = 0; i < AreaRow; i++) {
for (j = 0; j < AreaCol; j++) {
work.dayA[i][j] = 0;
work.twilightA[i][j] = 0;
work.msgFCA[i][j] = 0;
work.ppsFCA[i][j] = 0;
work.msgcloudyA[i][j] = 0;
work.ppscloudyA[i][j] = 0;
work.lightconA[i][j] = 0.0;
work.bias100A[i][j] = 0.0;
work.bias75A[i][j] = 0.0;
work.bias50A[i][j] = 0.0;
work.bias25A[i][j] = 0.0;
work.valconcenA[i][j] = 0;
}
}
return 1;
}
static int Reset(void) {
int i,j;
printf("Resetin g the tile arrays to zero\n");
for (i = 0; i < work.Row; i++) {
for (j = 0; j < work.Col; j++) {
work.day[i][j] = 0;
work.twilight[i][j] = 0;
work.ppsFC[i][j] = 0;
work.msgFC[i][j] = 0;
work.msgcloudy[i][j] = 0;
work.ppscloudy[i][j] = 0;
work.valconcen[i][j] = 0;
work.ppsdata[i][j] = 0;
work.msgdata[i][j] = 0;
work.frac[i][j] = 0;
}
}
return 1;
}
static int Bias(void) {
printf("Calcula ting the cloudiness per pixel\n");
int i,j;
float FC75 = 0.75;
float FC50 = 0.5;
float FC25 = 0.25;
for (i = 0; i < AreaRow; i++) {
for (j = 0; j < AreaCol; j++) {
if (work.valconcen A[i][j] 0) {
work.bias100A[i][j] =
(float)(((((flo at)work.msgclou dyA[i][j]+(float)work.ms gFCA[i][j])/(float)work.val concenA[i][j])*100.0)-
(float)((((floa t)work.ppscloud yA[i][j]+(float)work.pp sFCA[i][j])/(float)work.val concenA[i][j])*100.0));
work.bias75A[i][j] =
(float)(((((flo at)work.msgclou dyA[i][j]+(float)work.ms gFCA[i][j])*FC75/(float)work.val concenA[i][j])*100.0)-
(float)((((floa t)work.ppscloud yA[i][j]+(float)work.pp sFCA[i][j])*FC75/(float)work.val concenA[i][j])*100.0));
work.bias50A[i][j] =
(float)(((((flo at)work.msgclou dyA[i][j]+(float)work.ms gFCA[i][j]*FC50)/(float)work.val concenA[i][j])*100.0)-
(float)((((floa t)work.ppscloud yA[i][j]+(float)work.pp sFCA[i][j]*FC50)/(float)work.val concenA[i][j])*100.0));
work.bias25A[i][j] =
(float)(((((flo at)work.msgclou dyA[i][j]+(float)work.ms gFCA[i][j]*FC25)/(float)work.val concenA[i][j])*100.0)-
(float)((((floa t)work.ppscloud yA[i][j]+(float)work.pp sFCA[i][j]*FC25)/(float)work.val concenA[i][j])*100.0));
} else {
work.bias100A[i][j] = 255.0;
work.bias75A[i][j] = 255.0;
work.bias50A[i][j] = 255.0;
work.bias25A[i][j] = 255.0;
}
}
}
printf("Complet e\n");
return 1;
}
static int LightCon(void) { /* Start */
printf("Calcula ting the light condition array\n");
int i,j;
float twi_weight = 0.3;
for (i = 0; i < AreaRow; i++) {
for (j = 0; j < AreaCol; j++) {
if(work.valconc enA[i][j] 0) {
work.lightconA[i][j] = (float)((
((float)work.da yA[i][j]+((float)work.t wilightA[i][j]*twi_weight)) /
(float)work.val concenA[i][j])*100.0);
if (work.lightconA[i][j] 100.0) {
printf("Light condition greater than 100%\n");
exit(EXIT_FAILU RE);
} else if (work.lightconA[i][j] < 0) {
printf("Light condition less than 0%\n");
exit(EXIT_FAILU RE);
}
} else {
work.lightconA[i][j] = 255.0;
}
}
}
printf("Complet e\n");
return 1;
}
static int PrintInt(int **array, int R, int C) {
int i,j;
int min, max;
min = 0;
max = 0;
for (i = 0; i < R; i++) {
for (j = 0; j< C; j++) {
if ( i == 0 && j == 0) {
min = array[i][j];
max = array[i][j];
}
if (array[i][j] < min) {
min = array[i][j];
}
if (array[i][j] max) {
max = array[i][j];
}
}
}
printf("Arraymi n: %d\tArraymax: %d\n",min,max);
return 1;
}
static int PrintFloat(floa t **array, int R, int C) {
int i,j;
float min,max;
for (i = 0; i < R; i++) {
for (j = 0; j< C; j++) {
if ( i == 0 && j == 0) {
min = array[i][j];
max = array[i][j];
}
if (array[i][j] < min) {
min = array[i][j];
}
if (array[i][j] max) {
max = array[i][j];
}
}
}
printf("Arraymi n: %f\tArraymax: %f\n",min,max);
return 1;
}
static int Memory(void) {
int i;
/* Allocate memory */
work.day = malloc(sizeof(i nt*)*work.Row);
work.twilight = malloc(sizeof(i nt*)*work.Row);
work.msgdata = malloc(sizeof(i nt*)*work.Row);
work.ppsdata = malloc(sizeof(i nt*)*work.Row);
work.ppsFC = malloc(sizeof(i nt*)*work.Row);
work.msgFC = malloc(sizeof(i nt*)*work.Row);
work.msgcloudy = malloc(sizeof(i nt*)*work.Row);
work.ppscloudy = malloc(sizeof(i nt*)*work.Row);
work.valconcen = malloc(sizeof(i nt*)*work.Row);
work.frac = malloc(sizeof(i nt*)*work.Row);
work.qual = malloc(sizeof(u nsigned short*)*work.Ro w);
work.stats = malloc(sizeof(f loat)*work.Cat) ;
work.percentage = malloc(sizeof(f loat)*work.Cat) ;
for (i = 0; i < work.Row; i++) {
work.day[i] = malloc(sizeof(i nt)*work.Col);
work.twilight[i] = malloc(sizeof(i nt)*work.Col);
work.msgdata[i] = malloc(sizeof(i nt)*work.Col);
work.ppsdata[i] = malloc(sizeof(i nt)*work.Col);
work.ppsFC[i] = malloc(sizeof(i nt)*work.Col);
work.msgFC[i] = malloc(sizeof(i nt)*work.Col);
work.msgcloudy[i] = malloc(sizeof(i nt)*work.Col);
work.ppscloudy[i] = malloc(sizeof(i nt)*work.Col);
work.valconcen[i] = malloc(sizeof(i nt)*work.Col);
work.frac[i] = malloc(sizeof(i nt)*work.Col);
work.qual[i] = malloc(sizeof(u nsigned short)*work.Col );
}
if (work.frac==NUL L) {
printf("Failed to allocate memory: frac\n");
exit(EXIT_FAILU RE);
}
if (work.day==NULL ) {
printf("Failed to allocate memory: day\n");
exit(EXIT_FAILU RE);
}
if (work.twilight= =NULL) {
printf("Failed to allocate memory: twilight\n");
exit(EXIT_FAILU RE);
}
if(work.msgdata ==NULL) {
printf("Failed to allocate memory: day\n");
exit(EXIT_FAILU RE);
}
if (work.ppsdata== NULL) {
printf("Failed to allocating memory ppsdata\n");
exit(EXIT_FAILU RE);
}
if (work.ppsFC==NU LL) {
printf("Failed to allocate memory: ppsFC\n");
exit(EXIT_FAILU RE);
}
if (work.msgFC==NU LL) {
printf("Failed to allocate memory: msgFC\n");
exit(EXIT_FAILU RE);
}
if (work.msgcloudy ==NULL) {
printf("Failed to allocate memory: day\n");
exit(EXIT_FAILU RE);
}
if (work.ppscloudy ==NULL) {
printf("Failed to allocate memory: ppscloudy\n");
exit(EXIT_FAILU RE);
}
if (work.valconcen ==NULL) {
printf("Failed to allocate memory: val\n");
exit(EXIT_FAILU RE);
}
if (work.qual==NUL L) {
printf("Failed to allocate memory: qual\n");
exit(EXIT_FAILU RE);
}
if (work.stats==NU LL) {
printf("Failed to allocate memory: stats\n");
exit(EXIT_FAILU RE);
}
if(work.percent age==NULL) {
printf("Failed to allocate memory: percentage\n");
exit(EXIT_FAILU RE);
}
/* 2D memory */
/* Allocate memory using MACRO */
printf("Allocat ing 2D memory\n");
work.dayA = malloc(sizeof(i nt*)*AreaRow);
work.twilightA = malloc(sizeof(i nt*)*AreaRow);
work.ppsFCA = malloc(sizeof(i nt*)*AreaRow);
work.msgFCA = malloc(sizeof(i nt*)*AreaRow);
work.msgcloudyA = malloc(sizeof(i nt*)*AreaRow);
work.ppscloudyA = malloc(sizeof(i nt*)*AreaRow);
work.lightconA = malloc(sizeof(f loat*)*AreaRow) ;
work.bias100A = malloc(sizeof(f loat*)*AreaRow) ;
work.bias75A = malloc(sizeof(f loat*)*AreaRow) ;
work.bias50A = malloc(sizeof(f loat*)*AreaRow) ;
work.bias25A = malloc(sizeof(f loat*)*AreaRow) ;
work.valconcenA = malloc(sizeof(i nt*)*AreaRow);
for (i = 0; i < AreaRow; i++) {
work.dayA[i] = malloc(sizeof(i nt)*AreaCol);
work.twilightA[i] = malloc(sizeof(i nt)*AreaCol);
work.ppsFCA[i] = malloc(sizeof(i nt)*AreaCol);
work.msgFCA[i] = malloc(sizeof(i nt)*AreaCol);
work.msgcloudyA[i] = malloc(sizeof(i nt)*AreaCol);
work.ppscloudyA[i] = malloc(sizeof(i nt)*AreaCol);
work.lightconA[i] = malloc(sizeof(f loat)*AreaCol);
work.bias100A[i] = malloc(sizeof(f loat)*AreaCol);
work.bias75A[i] = malloc(sizeof(f loat)*AreaCol);
work.bias50A[i] = malloc(sizeof(f loat)*AreaCol);
work.bias25A[i] = malloc(sizeof(f loat)*AreaCol);
work.valconcenA[i] = malloc(sizeof(i nt)*AreaCol);
}
if (work.dayA==NUL L) {
printf("Failed to allocate memory: dayA\n");
exit(EXIT_FAILU RE);
}
if (work.twilightA ==NULL) {
printf("Failed to allocate memory: twilightA\n");
exit(EXIT_FAILU RE);
}
if (work.ppsFCA==N ULL) {
printf("Failed to allocate memory: ppsFCA\n");
exit(EXIT_FAILU RE);
}
if (work.msgFCA==N ULL) {
printf("Failed to allocate memory: msgFCA\n");
exit(EXIT_FAILU RE);
}
if (work.msgcloudy A==NULL) {
printf("Failed to allocate msgcloudyA\n");
exit(EXIT_FAILU RE);
}
if (work.ppscloudy A==NULL) {
printf("Failed to allocate memory: ppscloudyA\n");
exit(EXIT_FAILU RE);
}
if (work.lightconA ==NULL) {
printf("Failed to allocate memory: lightconA\n");
exit(EXIT_FAILU RE);
}
if (work.bias100A= =NULL) {
printf("Failed to allocate memory: bias100A\n");
exit(EXIT_FAILU RE);
}
if (work.bias75A== NULL) {
printf("Failed to allocate memory: bias75A\n");
exit(EXIT_FAILU RE);
}
if (work.bias50A== NULL) {
printf("Failed to allocate memory: bias50A\n");
exit(EXIT_FAILU RE);
}
if (work.bias25A== NULL) {
printf("Failed to allocate memory: bias25A\n");
exit(EXIT_FAILU RE);
}
if (work.valconcen A==NULL) {
printf("Failed to allocate memory valA\n");
exit(EXIT_FAILU RE);
}
printf("Allocat ed memory for all arrays\n");
return 1;
}
static int MemoryFreeA(voi d) {
int i;
for (i = 0; i < AreaCol; i++) {
free(work.dayA[i]);
free(work.twili ghtA[i]);
free(work.msgFC A[i]);
free(work.ppsFC A[i]);
free(work.msgcl oudyA[i]);
free(work.ppscl oudyA[i]);
free(work.light conA[i]);
free(work.bias1 00A[i]);
free(work.bias7 5A[i]);
free(work.bias5 0A[i]);
free(work.bias2 5A[i]);
free(work.valco ncenA[i]);
}
free(work.dayA) ;
free(work.twili ghtA);
free(work.msgFC A);
free(work.ppsFC A);
free(work.msgcl oudyA);
free(work.ppscl oudyA);
free(work.light conA);
free(work.bias1 00A);
free(work.bias7 5A);
free(work.bias5 0A);
free(work.bias2 5A);
free(work.valco ncenA);
return 1;
}
static int MemoryFree15km( void) {
int i;
for (i = 0; i < ResCol; i++) {
free(work.light con15km[i]);
free(work.bias1 0015km[i]);
free(work.bias7 515km[i]);
free(work.bias5 015km[i]);
free(work.bias2 515km[i]);
free(work.valco ncen15km[i]);
}
free(work.light con15km);
free(work.bias1 0015km);
free(work.bias7 515km);
free(work.bias5 015km);
free(work.bias2 515km);
free(work.valco ncen15km);
return 1;
}
static int MemoryFreeOrd(v oid) {
int i;
for (i = 0; i < work.Col; i++) {
free(work.day[i]);
free(work.twili ght[i]);
free(work.msgcl oudy[i]);
free(work.msgFC[i]);
free(work.ppsFC[i]);
free(work.ppscl oudy[i]);
free(work.msgda ta[i]);
free(work.ppsda ta[i]);
free(work.valco ncen[i]);
free(work.frac[i]);
free(work.qual[i]);
}
free(work.day);
free(work.twili ght);
free(work.msgcl oudy);
free(work.msgFC );
free(work.ppsFC );
free(work.ppscl oudy);
free(work.msgda ta);
free(work.ppsda ta);
free(work.valco ncen);
free(work.frac) ;
free(work.qual) ;
free(work.date) ;
free(work.tile) ;
free(work.Regio n);
free(work.Repro dir);
free(work.PPSfi le);
free(work.MSGfi le);
free(work.PHYSf ile);
return 1;
}
static int MemoryFree1D(vo id) {
int i;
free(work.stats );
free(work.nscen es);
free(work.perce ntage);
return 1;
}