void main(int argc, char **argv) { FILE *fpmask, *fpgrd; /* Filepointers for to files*/ char maskfile[150]; char grdfile[150]; /*=".grd";*/ char outp_dir[250]; /* ="out/";*/ char str1[12]; char str2[12]; int nr_of_valid_cells,nr_tstep, i,start_year,start_month; int present_tstep, x; int run_years, yy, binflag; int to_start, start_period, end_period; float prec, mult; float tstp[MAX_NR_OF_CELLS]; /* Not using pointer could be dangerous */ STR_BIG *pt; pt = malloc(sizeof(STR_BIG)); /* allocates memory to the structure */ if (argc<9) { /* Must be exactly 6 arguments behind the program name */ printf("Not correct number of commandline arguments \n"); exit(EXIT_FAILURE); } strcpy(maskfile,argv[1]); strcpy(grdfile,argv[2]); strcpy(outp_dir,argv[3]); strcpy(str1,argv[4]); start_year=atoi(str1); strcpy(str1,argv[5]); start_period=atoi(str1); strcpy(str1, argv[6]); end_period=atoi(str1); strcpy(str1, argv[7]); binflag=atoi(str1); /*binary flag */ strcpy(str1, argv[8]); mult=atof(str1); /*data multiplier*/ printf("Commandline arguments\n"); printf("mk_monthly "); printf("%s %s %s %d %d %d\n",maskfile,grdfile,outp_dir,start_year, start_period,end_period); if((fpmask = fopen(maskfile,"r"))==NULL){ printf("Cannot open file %s \n",maskfile);exit(0);} if(binflag!=1) { if((fpgrd = fopen(grdfile,"r"))==NULL){ printf("Cannot open file %s \n",grdfile); exit(0);} } else { if((fpgrd = fopen(grdfile,"rb"))==NULL){ printf("Cannot open file %s \n",grdfile); exit(0);} } printf("Maximum number of cells in this program is %d\n",MAX_NR_OF_CELLS); nr_of_valid_cells = valid_cells(fpmask); printf("Number of valid cells are %d\n",nr_of_valid_cells); nr_tstep = number_of_timesteps(fpgrd, nr_of_valid_cells, binflag); printf("Number of timesteps are %d\n",nr_tstep); set_0(pt); printf("start_year = %d\n",start_year); printf("start_period = %d\n",start_period); printf("end_period = %d\n",end_period); to_start = tstp_to_start(start_year,start_period); printf("Timesteps to start = %d\n",to_start); for (i=0;i<to_start;i++)read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); /* Moving down */ run_years = tstp_to_start(start_period,(1+end_period)); printf("Timesteps to run = %d\n",run_years); run_years = end_period - start_period +1; printf("Number of years to run = %d \n",run_years); prec =0.0; for(yy=0;yy<run_years;yy++) { for (i = 0;i<31;i++){ /* January */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->jan[x] = pt->jan[x]+tstp[x]; } if (start_period%4!=0) { /*February */ for (i =0;i<28;i++) { read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->feb[x] = pt->feb[x]+tstp[x]; } printf("Regular year %d \n",start_period); } if (start_period%4==0) { /* February*/ for (i =0;i<29;i++) { read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->feb[x] = pt->feb[x]+tstp[x]; } printf("Leap year %d \n",start_period); } for (i = 0;i<31;i++){ /* March */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->mar[x] = pt->mar[x]+tstp[x]; } for (i = 0;i<30;i++){ /* April */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->apr[x] = pt->apr[x]+tstp[x]; } for (i = 0;i<31;i++){ /* May */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->may[x] = pt->may[x]+tstp[x]; } for (i = 0;i<30;i++){ /* June */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->jun[x] = pt->jun[x]+tstp[x]; } for (i = 0;i<31;i++){ /* July */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->jul[x] = pt->jul[x]+tstp[x]; } for (i = 0;i<31;i++){ /* August */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->aug[x] = pt->aug[x]+tstp[x]; } for (i = 0;i<30;i++){ /* September */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->sep[x] = pt->sep[x]+tstp[x]; } for (i = 0;i<31;i++){ /* Oktober */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->okt[x] = pt->okt[x]+tstp[x]; } for (i = 0;i<30;i++){ /* November */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->nov[x] = pt->nov[x]+tstp[x]; } for (i = 0;i<31;i++){ /* December */ read_one_timestep(fpgrd,tstp,nr_of_valid_cells,mult,binflag); for (x=0;x<nr_of_valid_cells;x++) pt->dec[x] = pt->dec[x]+tstp[x]; } start_period = start_period +1; } /* END of yearly loop */ write_data(pt,fpmask,run_years,outp_dir); /* Function writes the output file */ free(pt); /***** Removes memory ****/ fclose(fpgrd); fclose(fpmask); /*** END main *********************/ }
void main(int argc, char **argv) /* commandline arguments */ { FILE *fpprcp, *fptmax, *fptmin, *fpflist, *fpout; /* filepointers used */ char prcp[BUFSIZ+1]; char tmaxc[BUFSIZ+1]; char tminc[BUFSIZ+1]; /* strings holding filenames */ char elev_mask[BUFSIZ+1]; /* string holding filename */ char out_dir[BUFSIZ+1]; /* string holding out directory */ char old_out_dir[BUFSIZ+1]; /* string holding the original output directory */ char s1[BUFSIZ+1]; /* dummy string used to read strings */ int cells, steps, tempsteps; int i,j,k,l, binflag; /* counters used in loops */ float *prec,*tmax,*tmin,***BIGA; short int *precip,*tempmax,*tempmin,***BIG; float BIG_temp; if (argc<7) { /* Must be exactly 6 arguments behind the program name */ printf("Not correct number of commandline arguments \n"); printf("vicinput \"prcp\" \"tmax\" \"tmin\" \"elev_mask.txt\" \"out_dir\" \n"); exit(EXIT_FAILURE); } strcpy(prcp,argv[1]); printf("%s \n",prcp); strcpy(tmaxc,argv[2]); printf("%s \n",tmaxc); strcpy(tminc,argv[3]); printf("%s \n",tminc); strcpy(elev_mask,argv[4]); printf("%s \n",elev_mask); strcpy(out_dir,argv[5]); strcpy(old_out_dir,out_dir);printf("%s \n",old_out_dir); binflag=atoi(argv[6]); printf("%d \n",binflag); /*--------------------------------------------------*/ /* ALLOCATE MEMORY TO ARRAYS BASED ON BINARY STATUS */ /*--------------------------------------------------*/ if(binflag!=1) { if(!(prec = (float*) calloc(VALID_CELLS,sizeof(float)))) { printf("Memory Allocation error: PREC\n"); exit(8); } if(!(tmax = (float*) calloc(VALID_CELLS,sizeof(float)))) { printf("Memory Allocation error: TMAX\n"); exit(8); } if(!(tmin = (float*) calloc(VALID_CELLS,sizeof(float)))) { printf("Memory Allocation error: TMIN\n"); exit(8); } if(!(BIGA = (float***) calloc(3,sizeof(float**)))) { printf("Memory Allocation Error: BIGA\n"); exit(8); } for(j=0; j<3;j++) { if(!(BIGA[j] = (float**) calloc(stp50,sizeof(float*)))) { printf("Memory Allocation Error: BIGA\n");exit(8); } for(i=0; i<stp50;i++) { if(!(BIGA[j][i] = (float*) calloc(VALID_CELLS,sizeof(float)))) { printf("Memory Allocation Error: BIGA\n"); exit(8); } } } } else { if(!(precip = (short int*) calloc(VALID_CELLS,sizeof(short int)))) { printf("Memory Allocation error: PREC\n"); exit(8); } if(!(tempmax = (short int*) calloc(VALID_CELLS,sizeof(short int)))) { printf("Memory Allocation error: TMAX\n"); exit(8); } if(!(tempmin = (short int*) calloc(VALID_CELLS,sizeof(short int)))) { printf("Memory Allocation error: TMIN\n"); exit(8); } if(!(BIG = (short int***) calloc(3,sizeof(short int**)))) { printf("Memory Allocation Error: BIGA\n"); exit(8); } for(j=0; j<3;j++) { if(!(BIG[j] = (short int**) calloc(stp50,sizeof(short int*)))) { printf("Memory Allocation Error: BIGA\n");exit(8); } for(i=0; i<stp50;i++) { if(!(BIG[j][i] = (short int*) calloc(VALID_CELLS,sizeof(short int)))) { printf("Memory Allocation Error: BIGA\n"); exit(8); } } } } /*------------------------------------------------------------*/ if (valid_cells(elev_mask)>VALID_CELLS) { /* Check if array is big enough */ printf("Number of valid cells = %d \n",valid_cells(elev_mask)); printf("Change VALID_CELLS to %d in source\n",(valid_cells(elev_mask)+1)); printf("Then recompile\n"); } else printf("Number of valid cells = %d \n",valid_cells(elev_mask)); make_flist(elev_mask); /* Generates a filelist that will be used later */ make_outfiles(old_out_dir,binflag); /* Open files in write mode, closes them again */ cells = valid_cells(elev_mask); /* Calculates the number of valid cells in basin */ printf("cells = %d \n",cells); steps = nr_timesteps(prcp,cells,binflag); printf("timesteps = %d \n",steps); if((fpflist = fopen(flist,"r"))==NULL) { /* latlong99.txt */ printf("Cannot open file %s \n",flist);exit(0);} if (binflag!=1) { if((fpprcp = fopen(prcp,"r"))==NULL){ /* prcp.grd after rescale */ printf("Cannot open file %s \n",prcp);exit(0);} if((fptmax = fopen(tmaxc,"r"))==NULL){ /* tmax.grd */ printf("Cannot open file %s \n",tmaxc);exit(0);} if((fptmin = fopen(tminc,"r"))==NULL){ /* tmin.grd */ printf("Cannot open file %s \n",tminc);exit(0);} } else { /* OPEN FILES FOR BINARY READ */ if((fpprcp = fopen(prcp,"rb"))==NULL) { /* prcp.grd after rescale */ printf("Cannot open file %s \n",prcp);exit(0);} if((fptmax = fopen(tmaxc,"rb"))==NULL) { /* tmax.grd */ printf("Cannot open file %s \n",tmaxc);exit(0);} if((fptmin = fopen(tminc,"rb"))==NULL) { /* tmin.grd */ printf("Cannot open file %s \n",tminc);exit(0);} } /***** RUNNING the remainder of steps/50 ****************************/ tempsteps = steps%50; /* running timesteps then write to files */ printf("timestep %d \n",tempsteps); for (j=0;j<tempsteps;j++) { if(binflag!=1) { one_tstp_ascii(fpprcp,fptmax,fptmin,prec,tmax,tmin,cells);/* 1 timestep */ for (i=0;i<cells;i++) { BIGA[0][j][i]=prec[i]; BIGA[1][j][i]=tmax[i]; BIGA[2][j][i]=tmin[i]; } } else { one_tstp(fpprcp,fptmax,fptmin,precip, tempmax, tempmin,cells);/* 1 timestep */ for (i=0;i<cells;i++) { /* putting the numbers into BIG array */ BIG[0][j][i]=precip[i]; BIG[1][j][i]=tempmax[i]; BIG[2][j][i]=tempmin[i]; } } } for(k=0;k<cells;k++) /* How many files to run */ { fscanf(fpflist,"%s",s1); strcpy(out_dir, old_out_dir); strcat(out_dir, s1); if(binflag!=1) {if((fpout = fopen(out_dir,"a"))==NULL){ printf("Cannot open file %s \n",out_dir);exit(0);}} else {if((fpout = fopen(out_dir,"ab"))==NULL){ printf("Cannot open file %s \n",out_dir);exit(0);} } for (i=0;i<tempsteps;i++) { /* BIG [unit][timestep][gridcell] */ if(binflag!=1) BIG_temp = BIGA[0][i][k]; else BIG_temp = (float) BIG[0][i][k]; if(BIG_temp<0.00) { fprintf(stderr,"neg. prcp reset to 0 \n"); if(binflag!=1) BIGA[0][i][k] = 0; else BIG[0][i][k] = 0; } if(binflag!=1) fprintf(fpout,"%4.2f %4.2f %4.2f\n", BIGA[0][i][k],BIGA[1][i][k],BIGA[2][i][k]); else { /* WRITE OUT IN BINARY FORMAT */ fwrite(&BIG[0][i][k],sizeof(short int),1,fpout); fwrite(&BIG[1][i][k],sizeof(short int),1,fpout); fwrite(&BIG[2][i][k],sizeof(short int),1,fpout); } } fclose(fpout); } rewind(fpflist); /****** END running the remainder of steps/50 ********************/ /***** RUNNING steps/50 ****************************/ if (steps>50) { tempsteps = steps/stp50; for (l=0;l<tempsteps;l++) { printf(" timestep %d \n",(steps%50 + (1+l)*50)); for (j=0;j<stp50;j++) { if(binflag!=1) { one_tstp_ascii(fpprcp,fptmax,fptmin,prec, tmax, tmin,cells); for (i=0;i<cells;i++) { /* putting the numbers into BIG array */ BIGA[0][j][i]=prec[i]; BIGA[1][j][i]=tmax[i]; BIGA[2][j][i]=tmin[i]; } } else { one_tstp(fpprcp,fptmax,fptmin,precip, tempmax, tempmin,cells); for (i=0;i<cells;i++) { /* putting the numbers into BIG array */ BIG[0][j][i]=precip[i]; BIG[1][j][i]=tempmax[i]; BIG[2][j][i]=tempmin[i]; } } } for(k=0;k<cells;k++) /* How many files to run */ { fscanf(fpflist,"%s",s1); strcpy(out_dir, old_out_dir); strcat(out_dir, s1); if(binflag!=1) fpout = fopen(out_dir,"a"); else fpout = fopen(out_dir,"ab"); for (i=0;i<stp50;i++) { /* BIG [unit][timestep][gridcell] */ /*reconvert precip to actual values for check */ if(binflag!=1) BIG_temp = BIGA[0][i][k]; else BIG_temp = (float) (BIG[0][i][k]); if(BIG_temp<0.00) { //fprintf(stderr,"neg. prcp reset to 0 \n"); if(binflag!=1) BIGA[0][i][k] = 0; else BIG[0][i][k] = 0; } if(binflag!=1) fprintf(fpout,"%4.2f %4.2f %4.2f\n", BIGA[0][i][k],BIGA[1][i][k],BIGA[2][i][k]); else { /* WRITE OUT IN BINARY FORMAT */ fwrite(&BIG[0][i][k],sizeof(short int),1,fpout); fwrite(&BIG[1][i][k],sizeof(short int),1,fpout); fwrite(&BIG[2][i][k],sizeof(short int),1,fpout); } } fclose(fpout); } rewind(fpflist); } } /****** END running steps/50 **********************************************/ fclose(fpprcp); fclose(fptmax); fclose(fptmin); fclose(fpflist); }
std::vector<Cell*> MazeTile::validCells(unsigned int p, unsigned int q, // p, q - position in the tilemap const TileMap& tm) { std::vector<Cell*> valid_cells(9); // // Locations of indices in the array: // A B // X TS [0] X TM [1] X TE [2] // | | // X MS [3] X MM [4] X ME [5] // | | // X BS [6] X BM [7] X BE [8] // C D // // constants as labels for indices in the array // KEY: // T -> top // M -> middle // B -> bottom // S -> start // E -> end // // e.g. // TS -> top row, start // MM -> middle row, middle // const static unsigned int TS = 0, TM = 1, TE = 2, MS = 3, MM = 4, ME = 5, BS = 6, BM = 7, BE = 8; std::vector<Cell*>::size_type vc_sz = valid_cells.size(); for (std::size_t i = 0; i < vc_sz; ++i) { valid_cells[i] = new Cell; } const vector2& t_wp = worldPosition(); // // A(s[0], s[1]) ------ B(e[0], s[1]) // | | // | | // | | // C(s[0], e[1]) ------ D(e[0], e[1]) const vector2& S = t_wp; const vector2& E = t_wp + sz_; vector2 A = S, B(E[0], S[1]), C(S[0], E[1]), D = E; #define SET_CELL_POS(a, xy) (valid_cells[(a)])->screen_position = (xy) #define MIDPOINT(a, b) (((a) + (b)) / 2) SET_CELL_POS(TS, A); SET_CELL_POS(TM, MIDPOINT(A, B)); SET_CELL_POS(TE, B); SET_CELL_POS(MS, MIDPOINT(A, C)); SET_CELL_POS(MM, MIDPOINT(A, D)); SET_CELL_POS(ME, MIDPOINT(B, D)); SET_CELL_POS(BS, C); SET_CELL_POS(BM, MIDPOINT(C, D)); SET_CELL_POS(BE, D); #define FREEZE(x) (valid_cells[(x)])->solid = true #define MELT(x) (valid_cells[(x)])->solid = false if ((MAZECELL_HAS_NORTH(mc_)) || (MAZECELL_HAS_SOUTH(mc_)) || (MAZECELL_HAS_EAST(mc_)) || (MAZECELL_HAS_WEST(mc_))) { MELT(MM); } #define SET_CELL_ROW(a, b, c, wall_macro) do {\ if (!wall_macro(mc_)) {\ FREEZE((a));\ FREEZE((b));\ FREEZE((c));\ } else {\ MELT((a));\ MELT((b));\ MELT((c));\ }\ } while (0) SET_CELL_ROW(TS, TM, TE, MAZECELL_HAS_NORTH); SET_CELL_ROW(BS, BM, BE, MAZECELL_HAS_SOUTH); SET_CELL_ROW(TE, ME, BE, MAZECELL_HAS_EAST); SET_CELL_ROW(TS, MS, BS, MAZECELL_HAS_WEST); // // now the current cell (S) has been set, check the cells around it // // (i - 1, j - 1) (i, j - 1) (i + 1, j - 1) // (i - 1, j) S (i, j) (i + 1, j) // (i - 1, j + 1) (i, j + 1) (i + 1, j + 1) // MazeTile* adjt = 0; const MazeCell* xc = 0; adjt = static_cast<MazeTile*>(tm.tile(p - 1, q - 1)); if (adjt != 0) { xc = &(adjt->mc()); if ((!MAZECELL_HAS_EAST(*xc)) || (!MAZECELL_HAS_SOUTH(*xc))) { FREEZE(TS); } } adjt = static_cast<MazeTile*>(tm.tile(p, q - 1)); if (adjt != 0) { xc = &(adjt->mc()); if ((!MAZECELL_HAS_SOUTH(*xc))) { FREEZE(TS); FREEZE(TM); FREEZE(TE); } } adjt = static_cast<MazeTile*>(tm.tile(p + 1, q - 1)); if (adjt != 0) { xc = &(adjt->mc()); if ((!MAZECELL_HAS_WEST(*xc)) || (!MAZECELL_HAS_SOUTH(*xc))) { FREEZE(TE); } } adjt = static_cast<MazeTile*>(tm.tile(p - 1, q)); if (adjt != 0) { xc = &(adjt->mc()); if ((!MAZECELL_HAS_EAST(*xc))) { FREEZE(TS); FREEZE(MS); FREEZE(BS); } } adjt = static_cast<MazeTile*>(tm.tile(p + 1, q)); if (adjt != 0) { xc = &(adjt->mc()); if ((!MAZECELL_HAS_WEST(*xc))) { FREEZE(TE); FREEZE(ME); FREEZE(BE); } } adjt = static_cast<MazeTile*>(tm.tile(p - 1, q + 1)); if (adjt != 0) { xc = &(adjt->mc()); if ((!MAZECELL_HAS_NORTH(*xc)) ||(!MAZECELL_HAS_EAST(*xc))) { FREEZE(BS); } } adjt = static_cast<MazeTile*>(tm.tile(p, q + 1)); if (adjt != 0) { xc = &(adjt->mc()); if (!MAZECELL_HAS_NORTH(*xc)) { FREEZE(BS); FREEZE(BM); FREEZE(BE); } } adjt = static_cast<MazeTile*>(tm.tile(p + 1, q + 1)); if (adjt != 0) { xc = &(adjt->mc()); if ((!MAZECELL_HAS_NORTH(*xc)) ||(!MAZECELL_HAS_WEST(*xc))) { FREEZE(BE); } } return valid_cells; }