/////////////////////////////////////////////////////////////////////////////// /// @fn int num_days(string start, string end) /// @brief gets the number of days between the start string and the end string /// @param string start is the start date /// @param string end is the end date /// @ret int that is the number of days between the 2 dates /////////////////////////////////////////////////////////////////////////////// int validate::num_days(string start, string end) { // Just convert to time_t, because it's easier that way. return num_days( str_to_time( start ), str_to_time( end ) ); }
/////////////////////////////////////////////////////////////////////////////// /// @fn bool managerApproval( string uID, bool approved ) /// @brief approves or disapproves the user with uID based on the approved bool /// @param string uID is the user ID to approve or disapprove /// @param bool approved /// true = user approved /// false = user denied /// @pre the user uID must have made a request /// @return true if the approval process went alright /// @return false if the user had not previously made a request /////////////////////////////////////////////////////////////////////////////// bool validate::managerApproval( string uID, bool approved ) { //Storage of requests, one at a time vector<string> vacationRequest; string approvedRequest = ""; string deniedRequest = ""; vector<string> users; // If uID is blank, get all users. Otherwise, just the one passed in. if (uID == "") { users = DATABASE.get_scopes(); } else { users.push_back( uID ); } //This will approve requests. if ( approved ) { //We'll run through each user for ( int x = 0; x < users.size(); x++ ) { //get the entries for the current username and put them into a temp vector vacationRequest = DATABASE.get_entries( users.at( x ), PENDING_APPROVAL ); if ( vacationRequest.at( 0 ) == DEFAULT_ENTRY ) { //Do nothing because this employee has no pending requests } else { //Since each user may have more than 1 request at a time, we're going //to run through each request the current iteration's user has while ( vacationRequest.empty() == false ) { string request = vacationRequest[0]; vector<string> request_vec = parse_request(request); // Rules are overridden if validation comes back false. bool rules_overridden = !validateLeave(uID, request); //This gets us a string in the form of RR/RR|SS/SS|EE/EE|T|AA/AA approvedRequest = request + "|" + get_today(); // If the manager overrides the rules, it needs to be logged. if (rules_overridden) { approvedRequest += "|" + RULES_OVERRIDDEN; } //Get the number of days the request is int vacaLen = num_days( request_vec[1], request_vec[2] ); //Add the current pending request to the approved requests field DATABASE.add_entry( users.at( x ), APPROVED_REQUESTS, approvedRequest ); //Now update the user's vacation balance with vacaLen if ( request_vec[3] == SICK_LEAVE ) { //This means it is a sick leave request //Multiply the vacation length by -1 to make it a subtraction addSickLeave( users.at( x ), ( vacaLen * -1 ) ); } else if ( request_vec[3] == VACATION_LEAVE ) { //This means it is a vacation leave request //Multiply the vacation length by -1 to make it a subtraction addVacationLeave( users.at( x ), vacaLen * -1 ); } //Now remove the request from the pending requests section DATABASE.delete_entry(users.at( x ), PENDING_APPROVAL ); //And now clear the request from the temp vector vacationRequest.erase( vacationRequest.begin() ); }//end while }//end else }//end for // Deny requests. } else if ( !approved ) { //We'll run through each user for ( int x = 0; x < users.size(); x++ ) { //get the entries for the current username and put them into a temp vector vacationRequest = DATABASE.get_entries( users.at( x ), PENDING_APPROVAL ); if ( vacationRequest.at( 0 ) == DEFAULT_ENTRY ) { //Do nothing because this employee has no pending requests } else { //Since each user may have more than 1 request at a time, we're going //to run through each request the current iteration's user has while ( vacationRequest.empty() == false ) { //This gets us a string in the form of RR/RR|SS/SS|EE/EE|T|DD/DD approvedRequest = vacationRequest.at( 0 ) + "|" + get_today(); //Add the current pending request to the disapproved requests field DATABASE.add_entry( users.at( x ), DISAPPROVED_REQUESTS, approvedRequest ); //Now remove the request from the pending requests section DATABASE.delete_entry( users.at( x ), PENDING_APPROVAL ); //And now clear the request from the temp vector vacationRequest.erase( vacationRequest.begin() ); }//end while }//end else }//end for }//end else if // SAVE CHANGES. DATABASE.save_file(); return true; }//end managerApproval()
/////////////////////////////////////////////////////////////////////////////// /// @fn bool validateLeave( string startDate, string endDate, rulesSystem &rules ) /// @brief checks a leave range of dates against the rules in the passed rules /// and then returns whether or not the range follows the rules /// @param string startDate is the start date for the requested vacation /// @param string endDate is the end date for the requested vacation /// @param rulesSystem &rules is rules system to check the dates against for /// verification of leave time /// @return false if the request is denied for any reason /// @return true if the request is accepted /// @note the parameters startDate and endDate must be in "MM/DD" where MM is /// the month number and DD is the day number /////////////////////////////////////////////////////////////////////////////// bool validate::validateLeave( string uID, string request ) { vector<string> request_vec = parse_request(request); string startDate = request_vec[1]; string endDate = request_vec[2]; string requestType = request_vec[3]; //First make sure both the start and end dates are valid dates if ( validateDate( startDate ) == false ) { return false; } if ( validateDate( endDate ) == false ) { return false; } // Number of days requesting off. int request_days = num_days( startDate, endDate ); int days_to_start = num_days( get_today(), startDate ) - 1; // TEST 0 // If they don't have enough time available in the leave type // of their request, they fail. if (requestType == SICK_LEAVE) { int time_available = str_to_int( DATABASE.get_entry(uID, SICK_LEAVE_BALANCE) ); // Not enough time. if (time_available < request_days) { return false; } } else if (requestType == VACATION_LEAVE) { int time_available = str_to_int( DATABASE.get_entry(uID, VACATION_LEAVE_BALANCE) ); // Not enough time. if (time_available < request_days) { return false; } } else { // Not a valid leave type. return false; } //TEST 1 //If the number of days notice is fewer than the rules allow, fail //leave time validation if ( ( days_to_start ) < m_rules->getDaysNotice() ) { return false; } //TEST 2 //Now we're going to make sure we're not requesting too much time off in a row //This will just check the size of the leaveRequestDates against the rule for //amount of time off in a row if ( request_days > m_rules->getDaysConsecutive() ) { return false; } //TEST 3 //And now we need to make sure that we're not going to leave the company //Trying to run itself with no employees left //This I'm not sure how we're going to implement yet, but I'll figure it out //I think I've got this part figured out. There's a field in the scope uID //in the database that is called ON_VACATION that will store a 1 or a 0 //to represent if that user is on vacation. This item will be edited somewhere //that isn't here. //We'll just get all the entries in all scopes in that field and count the //number of 1s >_> //THIS NEEDS TO BE CHANGED TO JUST CHECK THE CURRENT DATE VS THE DATES OF //PEOPLE WHO ARE ON VACATION, lolk double usersOnVacation = 0; vector<string> users = DATABASE.get_scopes(); vector<string> leaveRequest; //We'll run through each user for ( int x = 0; x < users.size(); x++ ) { //Gets all of the approved requests for the user leaveRequest = DATABASE.get_entries( users.at( x ), APPROVED_REQUESTS ); // Hax the vector from the request list into the last // approved request in the list. leaveRequest = parse_request(leaveRequest.back()); if (leaveRequest[0] != DEFAULT_ENTRY) { // Pull out the start/end of the most recent approved request. string request_start = leaveRequest[1]; string request_end = leaveRequest[2]; // Calculate the length of the request. int request_length = num_days(request_start, request_end); // And the number of days from the start of the request to today. int start_to_today = num_days(request_start, get_today()); // If today is between the start and end of the request... if (start_to_today > 0 && start_to_today <= request_length) { // They're on vacation. usersOnVacation++; } } /* if (leave_request[0] != DEFAULT_ENTRY) */ } if ( usersOnVacation >= m_rules->getMaxPeopleOff() ) { return false; } // Vector to store the blackout days. vector<string> blackout = m_rules->getBlackoutList(); //TEST 4 //And finally now we're going to make sure that we're not trying to leave on //A blackout date. This should run through all of the blackoutlist for each //date in the request list, not efficient, but the easiest way for ( int j = 0; j < blackout.size(); j++ ) { int days_to_blackout = num_days(startDate, blackout[j]); //And if the date in the request list matches any in the blackout list //We return false because employees can't take a vacation on a //blackout date if ( days_to_blackout > 0 && // A day in the past doesn't matter to us. days_to_blackout <= request_days ) { return false; } } //end for loop return true; }
void main(int argc, char *argv[]) { FILE *fu, *fv, *fposlat, *fposlon, *fm, *fpout,*fpflist,*fpmean; char ufile[100], vfile[100],str1[200],out_dir[200],mask[200]; char old_out_dir[200], s1[200], maskfile[200],in_dir[200],old_in_dir[200]; char uftemp[100], vftemp[100]; int MASKROWS, MASKCOLS; float MASKRES, void_nr,high,low, xll, yll, maskulong, maskulat; int i,j,k,l,t; long int count; int lon, lat, min_lat, max_lat, min_lon, max_lon, skipdays; float **uwnd, **vwnd,sum_wind, **maskx; int ROWS,COLUMNS,SIZEM; int twind, ii,cell_no; int year, start_year, start_mo, end_year, end_mo, days_per_year,active_cells; double ***wspeed,**cell_wind; float *latitude, *longitude, uinterp, vinterp, dum; float reflat, reflon,avg_wind; double vertdist, reldist, hordist; float newuleft, newvleft, newuright, newvright; short int vic_wind, cellavg; if(argc!=12) { printf("\nUsage: %s <start_year> <start_mo> <end_year> <end_mo> <min_lat> <max_lat> <min_lon> <max_lon> <mask> <in_dir> <out_dir>\n",argv[0]); printf("See code for more details\n"); exit(8); } printf("Assigning command line inputs to variables...\n\n"); start_year = atoi(argv[1]); printf("start_year %d \n",start_year); start_mo = atoi(argv[2]); printf("start_mo %d \n",start_mo); end_year = atoi(argv[3]); printf("end_year %d \n",end_year); end_mo = atoi(argv[4]); printf("end_mo %d \n",end_mo); min_lat = atoi(argv[5]); printf("gauss_t62 coords:\nmin_lat %d \n",min_lat); max_lat = atoi(argv[6]); printf("max_lat %d \n",max_lat); min_lon = atoi(argv[7]); printf("min_lon %d \n",min_lon); max_lon = atoi(argv[8]); printf("max_lon %d \n",max_lon); strcpy(mask,argv[9]); printf("mask %s \n",mask); strcpy(in_dir,argv[10]); strcpy(old_in_dir,in_dir); strcpy(out_dir,argv[11]); strcpy(old_out_dir,out_dir); printf("out_dir %s \n\n",old_out_dir); printf("Creating output file list %s\n",flist); strcpy(maskfile,mask); make_flist(mask); /* Generates a filelist that will be used later */ printf("Making Output Files\n"); make_outfiles(old_out_dir);/* Open all files in write mode and closes them again */ ROWS=max_lat - min_lat +1; /* rows in reanalysis grid */ COLUMNS=max_lon - min_lon + 1; /* columns in reanalysis grid */ if((fpflist = fopen(flist,"r"))==NULL) { printf("Cannot open file %s \n",flist);exit(0);} /*------------------------------------------------------*/ /* FIRST READ IN HEADER FOR MASK FILE (ARC/INFO STYLE) */ /*------------------------------------------------------*/ if((fm = fopen(maskfile,"r")) == NULL) { printf("Cannot open/read mask\n"); exit(0); } fscanf(fm,"%*s %s",str1); MASKCOLS = atoi(str1); fscanf(fm,"%*s %s",str1); MASKROWS = atoi(str1); fscanf(fm,"%*s %s",str1); xll = atof(str1); fscanf(fm,"%*s %s",str1); yll = atof(str1); fscanf(fm,"%*s %s",str1); MASKRES = atof(str1); fscanf(fm,"%*s %s",str1); void_nr = atof(str1); high=void_nr+0.001; low=void_nr-0.001; /* convert to deg. east and make cell centered */ maskulong = (xll+360.0)+MASKRES/2.0; /* upper left corner latitude cell center */ maskulat = yll+(MASKROWS*MASKRES)-(MASKRES/2.0); /* initialize days_per_year for memory allocation */ days_per_year=366; active_cells=0; SIZEM = MASKCOLS*MASKROWS; /*----------------------------*/ /* ALLOCATE MEMORY TO ARRAYS */ /*----------------------------*/ if(!(longitude = (float*) calloc(COLUMNS+1,sizeof(float)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } if(!(latitude = (float*) calloc(ROWS,sizeof(float)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } if(!(uwnd = (float**) calloc(ROWS,sizeof(float*)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } for(i=0; i<ROWS;i++) { if(!(uwnd[i]= (float*) calloc(COLUMNS,sizeof(float)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } } if(!(maskx = (float**) calloc(MASKROWS,sizeof(float*)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } for(i=0; i<MASKROWS;i++) { if(!(maskx[i] = (float*) calloc(MASKCOLS,sizeof(float)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } } if(!(vwnd = (float**) calloc(ROWS,sizeof(float*)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } for(i=0; i<ROWS;i++) { if(!(vwnd[i] = (float*) calloc(COLUMNS,sizeof(float)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } } if(!(wspeed = (double***) calloc(days_per_year,sizeof(double**)))) { printf("Cannot allocate memory to first record: WSPEED\n"); exit(8); } for(j=0; j<days_per_year;j++) { if(!(wspeed[j] = (double**) calloc(MASKROWS,sizeof(double*)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } for(i=0; i<MASKROWS;i++) { if(!(wspeed[j][i] = (double*) calloc(MASKCOLS,sizeof(double)))) { printf("Cannot allocate memory to first record: BASIN\n"); exit(8); } } } if(!(cell_wind = (double**) calloc(SIZEM,sizeof(double*)))) { printf("Cannot allocate memory to first record: CELL_WIND\n"); exit(8); } for(j=0; j<SIZEM;j++) { if(!(cell_wind[j] = (double*) calloc(days_per_year,sizeof(double)))) { printf("Cannot allocate memory to first record: CELL_WIND\n"); exit(8); } } /*--------------------------------------------*/ /* READ IN REMAINDER OF MASK FILE */ /*--------------------------------------------*/ printf("reading mask file\n\n"); for(i=0; i<MASKROWS;i++) { for(j=0; j<MASKCOLS; j++) { fscanf(fm,"%f",&maskx[i][j]); if(maskx[i][j]>high || maskx[i][j]<low ) active_cells++; } } fclose(fm); printf("%d active cells\n",active_cells); /*------------------------------------------------------*/ /* BEGIN LOOP FOR EACH YEAR */ /*------------------------------------------------------*/ for(year=start_year;year<=end_year;year++) { printf("Processing year %d\n",year); count = 0; sum_wind = 0.0; /* NAMES OF REANALYSIS U-WIND AND V-WIND DATA FILES FOR CURRENT YEAR */ sprintf(uftemp,"uwnd.%d.asc",year); sprintf(vftemp,"vwnd.%d.asc",year); strcat(in_dir,uftemp); strcpy(ufile,in_dir); strcpy(in_dir,old_in_dir); strcat(in_dir,vftemp); strcpy(vfile,in_dir); strcpy(in_dir,old_in_dir); printf("files %s and %s\n",ufile, vfile); days_per_year= num_days(year,end_year,end_mo); printf("days_per_year= %d\n",days_per_year); /* if (LEAPYR(year)) */ /* days_per_year=366; */ /* else */ /* days_per_year=365; */ /*--------------------------------------------*/ /* OPEN REANALYSIS WIND FILES */ /*--------------------------------------------*/ if((fu = fopen(ufile,"r")) == NULL) { printf("Cannot open/read %s\n",ufile); exit(8); } if((fv = fopen(vfile,"r")) == NULL) { printf("Cannot open/read %s\n",vfile); exit(8); } if((fposlat = fopen(ncar_lat, "r")) == NULL) { printf("Cannot open/read %s\n",ncar_lat); exit(8); } if((fposlon = fopen(ncar_lon, "r")) == NULL) { printf("Cannot open/read %s\n",ncar_lon); exit(8); } /* printf("Files successfully opened...\n\n"); */ /*----------------------------------------------------*/ /* READ LAT/LON FILES -- POSITION IN REANALYSIS GRID */ /*----------------------------------------------------*/ /* move to starting position for min lat and long (using reanalysis numbers for coordinates in each file */ for(i=1; i<min_lat;i++) fscanf(fposlat,"%f",&dum); for(i=1; i<min_lon;i++) fscanf(fposlon,"%f",&dum); for(i=0; i<ROWS;i++) fscanf(fposlat,"%f",&latitude[i]); for(i=0; i<COLUMNS+1;i++) fscanf(fposlon,"%f",&longitude[i]); fclose(fposlat); fclose(fposlon); /*-----------------------------------------------------------------*/ /* READ IN REANALYSIS WIND GRID -- RUN FOR ALL DAYS IN ONE YEAR */ /*-----------------------------------------------------------------*/ for(t=0; t<days_per_year;t++) { for(i=0; i<ROWS;i++) { for(j=0; j<COLUMNS;j++) { fscanf(fu, "%d", &twind); uwnd[i][j]= (float)twind*SCALE + OFFSET; fscanf(fv, "%d", &twind); vwnd[i][j]= (float)twind*SCALE + OFFSET; } } for(j=0; j<MASKROWS; j++) { reflat = maskulat - (float)j*MASKRES; for(i=0; i<MASKCOLS; i++) { reflon = maskulong + (float)i*MASKRES; lat=lon=9999; if(maskx[j][i]>high || maskx[j][i]<low) { /* if active cell,interpolate */ for(k=0; k<ROWS;k++) { if(reflat<=latitude[k] && reflat>latitude[k+1]){ lat = k; } } for(l=0; l<COLUMNS+1; l++) { if(reflon>=longitude[l] && reflon<longitude[l+1]){ lon=l; } } if(lat==9999 || lon==9999) { printf("reflat/lon not in range\n"); exit(0); } /*-----------------------------------*/ /* VERTICAL INTERPOLATION */ /*-----------------------------------*/ vertdist=get_dist(latitude[lat], longitude[lon], latitude[lat+1], longitude[lon]); reldist=get_dist(latitude[lat],longitude[lon], reflat, longitude[lon]); newuleft=((uwnd[lat][lon]*(1-(reldist/vertdist)))+(reldist/vertdist) *uwnd[lat+1][lon]); newvleft=((vwnd[lat][lon]*(1-(reldist/vertdist)))+(reldist/vertdist) *vwnd[lat+1][lon]); if(lon==COLUMNS) { newuright=((uwnd[lat][0]*(1-(reldist/vertdist)))+ (reldist/vertdist)*uwnd[lat+1][0]); newvright=((vwnd[lat][0]*(1-(reldist/vertdist)))+ (reldist/vertdist)*vwnd[lat+1][0]); } else { newuright=((uwnd[lat][lon+1]*(1-(reldist/vertdist)))+ (reldist/vertdist)*uwnd[lat+1][lon+1]); newvright=((vwnd[lat][lon+1]*(1-(reldist/vertdist)))+ (reldist/vertdist)*vwnd[lat+1][lon+1]); } /*-----------------------------------*/ /* HORIZONTAL INTERPOLATION */ /*-----------------------------------*/ hordist=get_dist(reflat,longitude[lon],reflat,longitude[lon+1]); reldist=get_dist(reflat,longitude[lon],reflat,reflon); uinterp=newuleft*(1-(reldist/hordist)) + newuright*(reldist/hordist); vinterp=newvleft*(1-(reldist/hordist)) + newvright*(reldist/hordist); wspeed[t][j][i] = sqrt((double) (uinterp*uinterp + vinterp*vinterp)); } /* end of if statement for interpolation only for active cell */ } /* end loop for MASKCOLS */ } /* end loop for MASKROWS */ } /* end loop for days per year */ /*--------------------------------------------------*/ /* OUTPUT TIMESERIES FOR EACH CELL IN MASK */ /*--------------------------------------------------*/ printf("Writing output\n"); skipdays=num_days( year, start_year, start_mo-1); if(year!=start_year) skipdays=0; printf("skipdays = %d\n",skipdays); cell_no=0; for(j=0; j<MASKROWS; j++) { for(i=0; i<MASKCOLS; i++) { if( maskx[j][i]>high || maskx[j][i]<low ) { /* if active cell, write */ fscanf(fpflist,"%s",s1); strcpy(out_dir,old_out_dir); strcat(out_dir,s1); /* create file name for grid cell */ if((fpout = fopen(out_dir,"ab"))==NULL) { printf("error opening output file %s\n",out_dir);exit(0);} /* convert wind to short int values -- multiplying by 100 */ for(t=skipdays;t<days_per_year;t++) { sum_wind += wspeed[t][j][i]; count+=1; /* calc sum for gross average */ vic_wind= (short int) (wspeed[t][j][i]*100); fwrite(&vic_wind,sizeof(short int),1,fpout); } ii=skipdays; for(t=skipdays;t<days_per_year;t++) { /*avg daily wind for each cell */ if( !(LEAPYR(year)) || t !=59){ /* skip feb 29 data for daily avg */ cell_wind[cell_no][ii]+=wspeed[t][j][i]; /* for each active cell, sum for each day */ ii++;} } cell_no++; fclose(fpout); } } } avg_wind = sum_wind/(float)count; /* gross average wind speed for year */ printf("Average Daily Wind Speed Year %d = %.2f m/s\n\n",year,avg_wind); rewind(fpflist); fclose(fu); /* close reanalysis data files for the current year */ fclose(fv); } /* end year loop */ /* write file with 365 daily averages for each cell */ if((fpmean = fopen("cell_avg.out","wb")) == NULL) { printf("Cannot open cell_avg.out\n"); exit(0); } for(i=0;i<active_cells;i++){ for(j=0;j<365;j++){ cellavg = (short int) (cell_wind[i][j]/(end_year-start_year+1)*100); fwrite(&cellavg,sizeof(short int),1,fpmean); } } fclose(fpmean); } /* end main program */