rdtmp.c 5.54 KB
Newer Older
saeed's avatar
saeed committed
1 2 3 4
#include<stdio.h>
#include<stdlib.h>
#include<netcdf.h>
#include<time.h>
saeed's avatar
saeed committed
5
#include<stdarg.h>
saeed's avatar
saeed committed
6 7


saeed's avatar
saeed committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
int lenstr(char *str){
  if (str == NULL) return 0;
  char *tmp = str;
  int n = 0;
  while(*tmp != '\0'){
    n++;
    tmp++;
  }
  return n;
}


void reverse(char *s, char *t, int sign, int npadzero){
  
  char *tmp = s;
  int len = 0;
  while (*tmp !='\0' ){
    tmp++;
    len++;
  }
  tmp--;

  if (sign < 0){
    *t = '-';
    t++;
  }

  while(npadzero--){
    *t = '0';
    t++;
  }
  
  while (len >0 ){
    *t = *tmp;
    tmp--;
    len--;
    t++;
  }
  *t = '\0';
}

void intostr(int a, char *st, int pad){
  int sign = 1;
  if (a < 0){
    sign = -1;
    a = (-a);
  }
  int index = 0;
  int r = -1 ;
  int q = 0;
  char cl[64] = {'\0'};
  do {
    r = a % 10;
    q = (a - r) / 10;
    *(cl+index) = (char)(r+'0');
    a = q;
    index++;
  }while(a >0);
  
  *(cl+index) = '\0';

  reverse(cl,st,sign,pad);
  
}

void writetostring(char *buffer, int N, char *str,...){
  if (str == NULL) return;
  va_list vv;
  va_start(vv,str);
  char *tmp = buffer;
  int counter = 0;
  while(*str!='\0'){
    if (counter < N){
      *tmp = *str;
      tmp++;
    }
    counter++;
    str++;
  }
  while((str=va_arg(vv,char *)) != NULL){
    while(*str!='\0'){
      if (counter < N ){
	*tmp = *str;
	tmp++;
      }
      counter++;
      str++;
    }
  }
  va_end(vv);
  *tmp = '\0';
  return;
}

saeed's avatar
saeed committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
void fill_time(double *vtime, int *year, int *month, int *day, int nl){
  
  struct tm begin = {0,0,0,0,0,0,0};
  begin.tm_isdst = -1;
  time_t t_of_day;
  for (int i=0; i < nl; ++i){
    begin.tm_year  = year[i]-1900;
    begin.tm_mon   = month[i] -1;                                                                                                                                                                          
    begin.tm_mday  = day[i];                                                                                                                                                                    
    begin.tm_hour  = 0;
    begin.tm_min   = 0;
    begin.tm_sec   = 0;
    begin.tm_isdst = -1;
    t_of_day       = mktime(&begin);
    vtime[i]       = (double)(t_of_day);
  }
}  

void fill_time_days(double *vtime, int nl){
  for(int i=0; i <nl;++i){
    vtime[i]       = (double)(i);
  }
}  

saeed's avatar
saeed committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

void getfirstdate(int year,int month,int day, char *st){

  char *tmp = st;
  int n = 0;
  while(*tmp != '\0'){
    n++;
    tmp++;
  }
  tmp = st;
  char cyear[64]  = {'\0'};
  char cmonth[64] = {'\0'};
  char cday[64]   = {'\0'};
  intostr(year,cyear,0);
  printf("%s\n",cyear);
  intostr(month,cmonth,1);
  printf("%s\n",cmonth);
  intostr(day,cday,1);
  printf("%s\n",cday);
  char lst[64] = {'\0'};
  tmp = lst;
  writetostring(lst, sizeof(lst)-1, cyear,"-",cmonth,"-",cday,NULL);
  tmp = lst;
  char *cout = st;
  while(*tmp != '\0'){
    *cout = *tmp;
    tmp++;
    cout++;
  }
  *cout = '\0';
}


saeed's avatar
saeed committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
int main(int argc, char *argv[]){

  FILE *fin = NULL;
  if (argc != 3){
    printf("Usage %s filenamein filenameout\n",argv[0]);
    return 1;
  }
  fin = fopen(argv[1],"r");
  if (fin  == NULL ) return 1;
  int c = 0;
  int nl = 0;
  while ((c = fgetc(fin)) != EOF ){
      if ( c== '\n') nl++; 
    }
  printf("number of line is %d \n",nl);
  rewind(fin);
  
  int *year   = (int *)malloc(nl*sizeof(int));
  int  *month = (int *)malloc(nl*sizeof(int));
  int   *day  = (int *)malloc(nl*sizeof(int));
  float *avg1 = (float *)malloc(nl*sizeof(float));
  float *avg2 = (float *)malloc(nl*sizeof(float));
  float *avg3 = (float *)malloc(nl*sizeof(float));
  int   *id   = (int *)malloc(nl*sizeof(int));
  double   *vtime   = (double *)malloc(nl*sizeof(double));
  
  int index = 0;
  while ((fscanf(fin,"%d%d%d%f%f%f%d",&year[index],&month[index],&day[index],&avg1[index],&avg2[index],&avg3[index],&id[index])) && !feof(fin)){
    index++;
  }
saeed's avatar
saeed committed
189 190 191
  char cfdate[64] = {'\0'};
  getfirstdate(year[0],month[0],day[0],cfdate);
  char units[128] = {'\0'};
saeed's avatar
saeed committed
192
  writetostring(units, sizeof(units)-1,"days since ",cfdate," 00:00:00",NULL);
saeed's avatar
saeed committed
193 194 195
  char  time_origin[128] = {'\0'};
  writetostring(time_origin, sizeof(time_origin)-1,cfdate," 00:00:00",NULL);
  
saeed's avatar
saeed committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  char *filename = argv[2];
  int ncid;
  int rec_dimid;
  int av1id, av2id, av3id, idid;
  int time_varid;
  
  nc_create(filename, NC_CLOBBER, &ncid);
  nc_def_dim(ncid, "time",nl,&rec_dimid);
  nc_def_var(ncid, "avg1", NC_FLOAT,1,&rec_dimid, &av1id);
  nc_def_var(ncid, "avg2", NC_FLOAT,1,&rec_dimid, &av2id);
  nc_def_var(ncid, "avg3",NC_FLOAT,1,&rec_dimid, &av3id);
  nc_def_var(ncid, "id",  NC_INT,1,&rec_dimid,&idid);
  nc_def_var(ncid, "time",NC_DOUBLE,1,&rec_dimid, &time_varid);
  
  char calendar[]        = "gregorian";
  char standard_name[]   = "time" ;
  char long_name[]       = "Time axis" ;
saeed's avatar
saeed committed
213
  
saeed's avatar
saeed committed
214
  nc_put_att_text (ncid,time_varid, "calender",sizeof(calendar) / sizeof(calendar[0]), calendar);
saeed's avatar
saeed committed
215 216
  nc_put_att_text (ncid,time_varid, "units",lenstr(units),units);
  nc_put_att_text (ncid,time_varid, "time_origin",lenstr(time_origin), time_origin);
saeed's avatar
saeed committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  nc_put_att_text (ncid,time_varid, "standard_name",sizeof(standard_name) / sizeof(standard_name[0]), standard_name);
  nc_put_att_text (ncid,time_varid, "long_name",sizeof(long_name) / sizeof(long_name[0]), long_name);

  nc_enddef(ncid);
  
  fill_time_days(vtime,nl);
  nc_put_var_double(ncid,time_varid,&vtime[0]);
  nc_put_var_int(ncid,idid,&id[0]);
  nc_put_var_float(ncid,av1id,&avg1[0]);
  nc_put_var_float(ncid,av2id,&avg2[0]);
  nc_put_var_float(ncid,av3id,&avg3[0]);
  

  nc_close(ncid);



  free(year);
  free(month);
  free(day);
  free(avg1);
  free(avg2);
  free(avg3);
  free(id);
  free(vtime);

  fclose(fin);

  return 0;
}