Exemplo n.º 1
0
long  julian (TIME gt)
{
	long temp ,year;

	year=gt.year;
	if(year>=1900)
		year-=1900;
	if(year + 1899 - FIRST_YEAR_CONVERTED<0)
		temp=Julian_Calendar1[year-70];
	else temp = Julian_Calendar[year + 1899 - FIRST_YEAR_CONVERTED] + SECCOR;
    
	if (isaleap (gt.year))
       temp = temp + (Leap_Calendar[gt.mon - 1] * 86400) ;
    else temp = temp + (Calendar[gt.mon -1] * 86400) ;
    
	temp = temp + (longhex(gt.day - 1) * 86400) + (longhex(gt.hour) * 3600) +
                    (longhex(gt.min) * 60) + longhex(gt.sec) ;
    return temp ;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
  FILE *fpinfo, *fpappf, *fpoutf;

  int i, lines, apprecs,year,LP,nyrs;
  int st_yr, end_yr, st_mo, end_mo, st_day, end_day;
  int idx,rec,k;
  int coop_id, append_id;

  char str1[MAXLINE],infofile[80],cid[6];
  char appfile[80], outfile[80];
  char junkstr[MAXLINE];
  char **apid;
  float ***prec;
  float **junk;
  int *apyr;

  int match;
      
  /*     leap year always included in input data */
      
  printf("\nBe sure that the void number used is -99\n");

  /*     read command line arguments -- filenames
         1) station info file from CD data
         2) daily data (preprocessed) to be appended to record
         3) start year for data to be appended
         4) start month for data to be appended
         5) end year for data to be appended
         6) end month for data to be appended
         7) output file of data to be appended
         8) number of records (years * stations) in .daily file
  */
  if (argc!=9) {    /* Must be exactly 8 arguments behind the program name */
    printf("Incorrect number of commandline arguments \n");
    exit(EXIT_FAILURE);
  }

  strcpy(infofile,argv[1]);      printf("infofile %s \n",infofile);
  strcpy(appfile,argv[2]);       printf("appfile %s \n",appfile);
  st_yr=atoi(argv[3]);         printf("st_yr %d \n",st_yr);
  st_mo=atoi(argv[4]);         printf("st_mo %d \n",st_mo);
  end_yr=atoi(argv[5]);         printf("end_yr %d \n",end_yr);
  end_mo=atoi(argv[6]);         printf("end_mo %d \n",end_mo);
  strcpy(outfile,argv[7]);        printf("outfile %s \n",outfile);
  apprecs=atoi(argv[8]);         printf("apprecs %d \n",apprecs);

  if((fpinfo = fopen(infofile,"r"))==NULL){
    printf("Cannot open file %s \n",infofile);exit(0);}
  if((fpappf = fopen(appfile,"r"))==NULL){
    printf("Cannot open file %s \n",appfile);exit(0);}
  if((fpoutf = fopen(outfile,"w"))==NULL){
    printf("Cannot open file %s \n",outfile);exit(0);}
  
  fgets(str1,MAXLINE,fpinfo);
  sscanf(str1,"%d",&lines);
  printf("No. of stations in info file: %d\n",lines);
  nyrs=end_yr-st_yr+1;

  /* allocate memory */
  prec=get_3d_mem_float(nyrs+1,lines+1,366+1);
  junk=get_2d_mem_float(apprecs+1,366+1);
  apid=get_2d_mem_char(apprecs+1,6);
  apyr=get_1d_mem_int(apprecs+1);

  /***read in ".daily" preprocessed append data file into memory***/

  for(rec=1;rec<=apprecs;rec++){
    fscanf(fpappf,"%s %d", apid[rec], &apyr[rec]);
    for(k=1;k<=366;k++) fscanf(fpappf,"%f", &junk[rec][k]);
  }

  /************* begin loop for each station *******************/

  for(i=1;i<=lines;i++) {
    fgets(str1,MAXLINE,fpinfo);
    strncpy(cid,&str1[55],6);
    printf("coopid= %6s ... ",cid);

    /* begin loop for current year */

    for(year=st_yr;year<=end_yr;year++){
      idx=year-st_yr+1;
      /* search append data for current station and year */
      match=FALSE;
      for(rec=1;rec<=apprecs;rec++){
	if(! match) {
	  if ((strcmp(apid[rec],cid)==0) && apyr[rec]==year ){
	    for(k=1;k<=366;k++){
	      prec[idx][i][k]=junk[rec][k];
	    }
	    match=TRUE;
	  }
	}
      }
      if (! match) {
	for(k=1;k<=366;k++){
	  prec[idx][i][k]=-99;
	}
      }
    }
    printf("completed station %d of %d\n",i,lines);
  }

  /************* end loop for each station *******************/
  /* now to write it out in the same format as the existing .fmt file */
  /* since array always is 1-366, pass 1 instead of LP to always
     adjust julian days */

  st_day = julday(st_mo-1,1);
  end_day = julday(end_mo,1)-1;

  for(year=st_yr;year<=end_yr;year++){
    idx=year-st_yr+1;
    LP = isaleap(year);

    printf("checking data for possible bad values and writing data\n");
    for(k=1;k<=366;k++){
      for(i=1;i<=lines;i++){
	if(prec[idx][i][k] > maxprec) {
	  printf("high precip at sta %d year %d line %d\n",i,year,k);
	  printf("precip (inch/day): %.2f\n", prec[idx][i][k]);
	}
	if(prec[idx][i][k] < 0.0 && prec[idx][i][k] != -99) {
	  printf("neg. precip at sta %d year %d line %d\n",i,year,k);
	  printf("reset to -99 for missing data\n");
	  prec[idx][i][k] = -99;
	}
      }
      /* write out the daily values for all stations one day at a time
	 except for feb 29th - unless leap year */
      if(LP != 0 || k != 60) {
	if((year > st_yr || k >= st_day) &&
	   (year < end_yr || k <= end_day)) {
	  for(i=1;i<=lines;i++){
	    prec[idx][i][k] *= intomm;
	    if(prec[idx][i][k]<=-99.00) prec[idx][i][k] = -99.00;
	    fprintf(fpoutf,"%7.2f ",prec[idx][i][k]);
	  }
	  fprintf(fpoutf,"\n");
	}
      }
    }

  }
  printf("closing files\n");
  fclose(fpinfo);
  fclose(fpappf);
  fclose(fpoutf);
  return(0);
}