/* This program recursively reads a station # from a name_list, builds */ /* directory paths, and explodes all working files into the working */ /* directory for processing */ /* ------------------------------------------------------------------------ */ #include "string.h" #include "process.h" #include "stdio.h" #include "stdlib.h" #include "math.h" #include "io.h" void main(argc, argv) int argc; char *argv[]; { int i, j, k, l, n, nyr = 0, nyr1 = 0, nyr2 = 0, nyrs = 0, nchdir = 0, nchexec = 0, nstns = 0, next_yr[30] , year, year2, years[30], nchcdr, nrecs, remain, lstns, select_type, begin_yr, begin_month, begin_day, begin_hr, end_yr, end_month, end_day, end_hr, p0; char data_dir[80] = "",work_dir[80], exec_dir[80], user_dir[80], output_path[80], exchange[80] = "", input_dir[80], src_dir[80] = "", errcode_file[80] = ""; char del0[] = "Del ", del[132] = "", zcat0[] = "zcat ", zcat1[80] = "", zcat[132] = "", stnos5[32], stnos[250][6], temp_str[80], st_no[6], sel3282x0[] = "sel3282x ", sel3282x[132], ayr2[3], ayr[5]; /* char *p; */ FILE *fortran, *errcodes; if(argc == 1) { printf("This program requires 4 arguments : \n"); printf(" The 1st argument is the user specified output directory path \n"); printf(" which will contain all the uncompressed station data files\n"); printf(" (e.g. C:\\NREL\\.....)\n"); printf(" The 2nd argument is the directory path which will contain\n"); printf(" all the input working uncompressed data files\n"); printf(" (e.g. C:\\DATA\\.....)\n"); printf(" The 3rd argument is the directory path which will contain\n"); printf(" all the executable files (e.g. C:\\NREL\\EXEC\\.....)\n"); printf(" The 4th argument is cdrom source data directory path which\n"); printf(" contains the comprerssed input data files\n"); printf(" (e.g. E:\\NREL\\DATA\\...)\n"); printf("\n\nEnter path to output directory :\n"); gets(user_dir); printf("\n\nEnter path to input working data directory :\n"); gets(data_dir); printf("\n\nEnter path to input executable directory :\n"); gets(exec_dir); printf("\n\nEnter path to the cdrom zipped input data directory :\n"); gets(src_dir); } else if(argc == 2) { printf(" The 2nd argument is the directory path which will contain\n"); printf(" all the input working uncompressed data files"); printf(" (e.g. C:\\DATA\\.....)\n"); printf(" The 3rd argument is the directory path which will contain\n"); printf(" all the executable files (e.g. C:\\NREL\\EXEC\\.....)\n"); printf(" The 4th argument is cdrom source data directory path which\n"); printf(" contains the comprerssed input data files\n"); printf(" (e.g. E:\\NREL\\DATA\\...)\n"); printf("\n\nEnter path to input working data directory :\n"); gets(data_dir); printf("\n\nEnter path to input executable directory :\n"); gets(exec_dir); printf("\n\nEnter path to the cdrom zipped input data directory :\n"); gets(src_dir); } else if(argc == 3) { printf(" The 3rd argument is the directory path which will contain\n"); printf(" all the executable files (e.g. C:\\NREL\\EXEC\\.....)\n"); printf(" The 4th argument is cdrom source data directory path which\n"); printf(" contains the comprerssed input data files\n"); printf(" (e.g. E:\\NREL\\DATA\\...)\n"); printf("\n\nEnter path to input executable directory :\n"); gets(exec_dir); printf("\n\nEnter path to the cdrom zipped input data directory :\n"); gets(src_dir); } else if(argc == 4) { printf(" The 4th argument is cdrom source data directory path which\n"); printf(" contains the comprerssed input data files\n"); printf(" (e.g. E:\\NREL\\DATA\\...)\n"); printf("\n\nEnter path to the cdrom zipped input data directory :\n"); gets(src_dir); } else if(argc >= 5) { strcpy(user_dir, argv[1]); strcpy(data_dir, argv[2]); strcpy(exec_dir, argv[3]); strcpy(src_dir, argv[4]); } strcpy(exchange, user_dir); strcat(exchange, "SAMSON.xch"); strcpy(errcode_file, user_dir); strcat(errcode_file, "errcodes.dat"); /* exchange = file containing station data selection information */ /* and station list*/ if((fortran = fopen(exchange,"r+t"))==NULL) { errcodes = fopen(errcode_file,"a+t"); fprintf(errcodes, "Can't open exchange file =%s\n", exchange); exit(0); } nchdir = strlen(data_dir); nchcdr = strlen(src_dir); nchexec = strlen(exec_dir); if(data_dir[nchdir-1] != '\\') strcat(data_dir, "\\"); if(src_dir[nchcdr-1] != '\\') strcat(src_dir, "\\"); if(exec_dir[nchexec-1] != '\\') strcat(exec_dir, "\\"); nchcdr = strlen(src_dir); /* Read the appropriate information from the exchange file */ fgets(temp_str, sizeof(temp_str), fortran); sscanf(temp_str, "%d", &select_type); if(select_type == 0) { fgets(temp_str, sizeof(temp_str), fortran); sscanf(temp_str, "%d %d %d", &begin_yr, &begin_month, &begin_day, &begin_hr); fgets(temp_str, sizeof(temp_str), fortran); sscanf(temp_str, "%d %d %d", &end_yr, &end_month, &end_day, &end_hr); nyr1 = begin_yr; nyr2 = end_yr; nyrs = nyr2 - nyr1 +1; } else if(select_type == 1) { fgets(temp_str, sizeof(temp_str), fortran); sscanf(temp_str, " %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d %1d\n", &years[0], &years[1], &years[2], &years[3], &years[4], &years[5], &years[6], &years[7], &years[8], &years[9], &years[10], &years[11], &years[12], &years[13], &years[14], &years[15], &years[16], &years[17], &years[18], &years[19], &years[20], &years[21], &years[22], &years[23], &years[24], &years[25], &years[26], &years[27], &years[28], &years[29]); fgets(temp_str,sizeof(temp_str), fortran); fgets(temp_str, sizeof(temp_str), fortran); fgets(temp_str, sizeof(temp_str), fortran); j = 0; nyrs = 0; /* find all of the tagged years and log their subscript position */ for(i = 0; i <= 29; ++i) { if(years[i] == 1) { next_yr[j] = i+1; ++j; ++nyrs; } } nyr1 = 0; nyr2 = nyrs -1; } else { errcodes = fopen(errcode_file,"a+t"); fprintf(errcodes, "SELRD : Invalid selection type : %d\n *****", select_type); exit(0); } /* retrieve element select array */ fgets(temp_str, sizeof(temp_str), fortran); /* retrieve number of stations */ fgets(temp_str, sizeof(temp_str), fortran); sscanf(temp_str, "%d", &nstns); nrecs = (nstns / 5) + 1; remain = nstns % 5; l = 0; for (j = 1; j <= nrecs; ++j) { fgets(stnos5, sizeof(stnos5), fortran); lstns = 5; p0 = 0; if(j == nrecs) lstns = remain; for (k = 1; k <= lstns; ++k) { /* get all station nos. within record No. k */ strncpy(stnos[l], &stnos5[p0], 5); stnos[l][5] = NULL; ++l; p0 += 6; } } for (i = 0; i < nstns; ++i) { /* Build directory path for each station file to be exploded for processing */ strncpy(st_no, stnos[i], 5); st_no[5] = NULL; strcpy(input_dir, src_dir); strcat(input_dir, st_no); strcat(input_dir, "\\"); strcpy(zcat1, zcat0); strcat(zcat1, input_dir); strcpy(output_path, user_dir); strcat(output_path, st_no); strcat(output_path, ".dat"); if(access(output_path, 0) == 0) { strcpy(del, del0); strcat(del, " "); strcat(del, output_path); system(del); } for(nyr = nyr1; nyr <= nyr2; ++nyr) { /* uncompress each annual intput file into the working directory */ /* (i.e. zcat l:\nrel\xxxxx\nn.z > data_dir\\nn.dat, */ /* where xxxxx = station wban #, nn = 2 digit year) */ if(select_type == 0) { n = nyr - 1900; itoa(n, ayr2, 10); itoa(nyr, ayr, 10); } else if(select_type == 1) { year = 1960 + next_yr[nyr]; year2 = 60 + next_yr[nyr]; if(year < 1961 || year > 1990) { /* printf("next year out of range : %d %d %d\n", */ /* year, next_yr[nyr], n); */ exit(0); } itoa(year2, ayr2, 10); itoa(year, ayr, 10); } else { errcodes = fopen(errcode_file,"a+t"); fprintf(errcodes, "Invalid selection type : %d\n", select_type); exit(0); } strcpy(zcat, exec_dir); strcat(zcat, zcat0); strcat(zcat, input_dir); strcat(zcat, st_no); strcat(zcat, "-"); strcat(zcat, ayr2); strcat(zcat, ".z > "); strcat(zcat, data_dir); strcat(zcat, st_no); strcat(zcat, "-"); strcat(zcat, ayr2); strcat(zcat, ".dat"); /* printf("%s\n", zcat); */ /* uncompress annual data file */ system(zcat); fclose(fortran); /* Build fortran command line */ strcpy(work_dir, data_dir); strcat(work_dir, st_no); strcat(work_dir, "-"); strcat(work_dir, ayr2); strcat(work_dir, ".dat"); strcpy(sel3282x, exec_dir); strcat(sel3282x, sel3282x0); strcat(sel3282x, work_dir); strcat(sel3282x, " "); strcat(sel3282x, exchange); strcat(sel3282x, " "); strcat(sel3282x, output_path); strcat(sel3282x, " "); strcat(sel3282x, errcode_file); /* call FORTRAN selection program */ system(sel3282x); /* printf("%s\n ", sel3282x); */ /* delete each annual intput file from the working directory */ /* (i.e. del e:\work\year.dat */ strcpy(del, del0); strcat(del, work_dir); system(del); /* initialize and get next year or station */ zcat[0] = NULL; del[0] = NULL; /* Go back to next year */ } zcat1[0] = NULL; work_dir[0] = NULL; output_path[0] = NULL; /* Go back to next station */ } }