Commit 864d8d69 authored by saeed's avatar saeed

rdtmp is modified

parent 580cd84b
......@@ -2,8 +2,103 @@
#include<stdlib.h>
#include<netcdf.h>
#include<time.h>
#include<stdarg.h>
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;
}
void fill_time(double *vtime, int *year, int *month, int *day, int nl){
struct tm begin = {0,0,0,0,0,0,0};
......@@ -28,6 +123,39 @@ void fill_time_days(double *vtime, int nl){
}
}
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';
}
int main(int argc, char *argv[]){
FILE *fin = NULL;
......@@ -58,7 +186,13 @@ int main(int argc, char *argv[]){
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 cfdate[64] = {'\0'};
getfirstdate(year[0],month[0],day[0],cfdate);
char units[128] = {'\0'};
writetostring(units, sizeof(units)-1,"days since",cfdate," 00:00:00",NULL);
char time_origin[128] = {'\0'};
writetostring(time_origin, sizeof(time_origin)-1,cfdate," 00:00:00",NULL);
char *filename = argv[2];
int ncid;
int rec_dimid;
......@@ -74,16 +208,15 @@ int main(int argc, char *argv[]){
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 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" ;
//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, "units",lenstr(units),units);
nc_put_att_text (ncid,time_varid, "time_origin",lenstr(time_origin), 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);
......
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