Commit 580cd84b authored by saeed's avatar saeed

first commit

parents
#include<stdio.h>
#include<stdlib.h>
#include<netcdf.h>
#include<time.h>
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);
}
}
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++;
}
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 units[] = "days since 1756-01-01 00:00:00";
char standard_name[] = "time" ;
char long_name[] = "Time axis" ;
char time_origin[] = "1756-01-01 00:00:00" ;
nc_put_att_text (ncid,time_varid, "calender",sizeof(calendar) / sizeof(calendar[0]), calendar);
nc_put_att_text (ncid,time_varid, "units",sizeof(units) / sizeof(units[0]), units);
nc_put_att_text (ncid,time_varid, "time_origin",sizeof(time_origin) / sizeof(time_origin[0]), time_origin);
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, "time_origin",sizeof(time_origin) / sizeof(time_origin[0]), time_origin);
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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment