int f_full_name(ARG0) { int i; if (mode >= 0) { getExtName(sec, mode, NULL, inv_out, NULL, NULL,".","_"); inv_out += strlen(inv_out); *inv_out++ = '.'; *inv_out = 0; f_lev(call_ARG0(inv_out,NULL)); for (i = 0; inv_out[i]; i++) { if (inv_out[i] == ' ') inv_out[i] = '_'; } } return 0; }
/* * HEADER:100:spread:output:1:write text - spread sheet format into X (WxText enabled) */ int f_spread(ARG1) { unsigned int i; if (mode == -1) { if ((*local = (void *) ffopen(arg1,file_append ? "a" : "w")) == NULL) fatal_error("Could not open %s", arg1); WxText = latlon = decode = 1; } else if (mode == -2) { ffclose((FILE *) *local); } else if (mode >= 0) { if (lat == NULL || lon == NULL || data == NULL) { fprintf(stderr,"no code to determine lat-lon information, no spread sheet output\n"); return 0; } set_mode(0); f_var(call_ARG0(inv_out,NULL)); fprintf((FILE *) *local,"lon,lat,%s",inv_out); f_lev(call_ARG0(inv_out,NULL)); fprintf((FILE *) *local," %s", inv_out); f_t(call_ARG0(inv_out,NULL)); fprintf((FILE *) *local," %s", inv_out); f_ftime(call_ARG0(inv_out,NULL)); fprintf((FILE *) *local," %s\n", inv_out); if (WxNum > 0) { for (i = 0; i < ndata; i++) { if(!UNDEFINED_VAL(data[i])) fprintf((FILE *) *local,"%lf,%lf,\"%s\"\n",lon[i],lat[i],WxLabel(data[i])); } } else { for (i = 0; i < ndata; i++) { if(!UNDEFINED_VAL(data[i])) fprintf((FILE *) *local,"%lf,%lf,%g\n",lon[i],lat[i],data[i]); } } if (flush_mode) fflush((FILE *) *local); inv_out[0] = 0; set_mode(mode); } return 0; }
int f_S(ARG0) { if (mode >= 0) { f_T(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); f_var(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); f_lev(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); f_ftime(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); f_misc(CALL_ARG0); } return 0; }
int f_V(ARG0) { int oldmode; if (mode == -1) decode = 1; if (mode >= 0) { f_vt(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); f_lev(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); f_ftime(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); oldmode=mode; mode=1; f_var(CALL_ARG0); strcat(inv_out,item_deliminator); inv_out += strlen(inv_out); mode=oldmode; f_ens(CALL_ARG0); strcat(inv_out,"\n "); inv_out += strlen(inv_out); f_stats(CALL_ARG0); strcat(inv_out,"\n "); inv_out += strlen(inv_out); f_grid(CALL_ARG0); strcat(inv_out,"\n"); } return 0; }
int f_csv(ARG1) { char new_inv_out[STRING_SIZE]; char name[100], desc[100], unit[100]; FILE *out; unsigned int j; char vt[20],rt[20]; int year, month, day, hour, minute, second; /* initialization phase */ if (mode == -1) { WxText = decode = latlon = 1; if ((*local = (void *) ffopen(arg1,file_append ? "a" : "w")) == NULL) fatal_error("csv could not open file %s", arg1); return 0; } /* cleanup phase */ if (mode == -2) return 0; /* processing phase */ if (lat == NULL || lon == NULL) { fprintf(stderr,"csv: latitude/longitude not defined, record skipped\n"); return 0; } out = (FILE *) *local; /*Collect runtime and validtime into vt and rt*/ reftime(sec, &year, &month, &day, &hour, &minute, &second); sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second); vt[0] = 0; if (verftime(sec, &year, &month, &day, &hour, &minute, &second) == 0) { sprintf(vt,"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second); } /*Get levels, parameter name, description and unit*/ *new_inv_out = 0; f_lev(call_ARG0(new_inv_out,NULL)); if (strcmp(new_inv_out, "reserved")==0) return 0; // getName(sec, mode, NULL, name, desc, unit); getExtName(sec, mode, NULL, name, desc, unit,".","_"); // fprintf(stderr,"Start processing of %s at %s\n", name, new_inv_out); // fprintf(stderr,"Gridpoints in data: %d\n", ndata); // fprintf(stderr,"Description: %s, Unit %s\n", desc,unit); /* Lage if-setning rundt hele som sjekker om alt eller deler skal ut*/ if (WxNum > 0) { for (j = 0; j < ndata; j++) { if (!UNDEFINED_VAL(data[j])) { fprintf(out,"\"%s\",\"%s\",\"%s\",\"%s\",%g,%g,\"%s\"\n",rt,vt,name, new_inv_out,lon[j] > 180.0 ? lon[j]-360.0 : lon[j],lat[j],WxLabel(data[j])); } } } else { for (j = 0; j < ndata; j++) { if (!UNDEFINED_VAL(data[j])) { fprintf(out,"\"%s\",\"%s\",\"%s\",\"%s\",%g,%g,%lg\n",rt,vt,name, new_inv_out,lon[j] > 180.0 ? lon[j]-360.0 : lon[j],lat[j],data[j]); } } } if (flush_mode) fflush(out); return 0; }
int f_mysql(ARG5) { char sql[300]; char server[100]; char user[100]; char password[100]; char database[100]; char table[100]; char name[100], desc[100], unit[100], level_buf[100]; int year, month, day, hour, minute, second; char vt[20],rt[20]; // unsigned char *p; int j; double longitude,latitude; struct local_struct { MYSQL *conn; char filename[200]; }; struct local_struct *save; FILE *file; int tempfile; strcpy(server,arg1); strcpy(user,arg2); strcpy(password,arg3); strcpy(database,arg4); strcpy(table,arg5); /* initialization phase */ if (mode == -1) { decode = latlon = 1; *local = save = (struct local_struct *) malloc(sizeof(struct local_struct)); if (save == NULL) fatal_error("mysql memory allocation ",""); save->conn = mysql_init(NULL); /* Connect to database */ if (!mysql_real_connect(save->conn, server, user, password, database, 0, NULL, 0)) fatal_error("mysql error %s", mysql_error(save->conn)); } /* cleanup phase */ else if (mode == -2) { save = *local; mysql_close(save->conn); free(save); } /* processing phase */ else if (mode >= 0) { save = *local; /*Collect runtime and validtime into vt and rt*/ reftime(sec, &year, &month, &day, &hour, &minute, &second); // p = sec[1]; // sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", (p[12]<<8)+p[13], p[14],p[15],p[16],p[17],p[18]); sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second); if (verftime(sec, &year, &month, &day, &hour, &minute, &second) == 0) { sprintf(vt,"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second); } /* Get levels, parameter name, description and unit*/ f_lev(mode, sec, data, ndata, level_buf, local); if (strcmp(level_buf, "reserved") == 0) return 0; strcpy(save->filename, "/tmp/wgrib2_mysqlXXXXXX"); if ((tempfile = mkstemp(save->filename)) == -1) fatal_error("mysql filename setup",""); fprintf(stdout, "\nUsing temporary pathname %s\n", save->filename); if ((file = fdopen(tempfile, "w")) == NULL ) fatal_error("mysql: error opening temporary filename: %s", save->filename); getName(sec, mode, NULL, name, desc, unit); fprintf(stderr,"Start processing of %s at %s\n", name, level_buf); fprintf(stderr,"Gridpoints in data: %d\n", ndata); fprintf(stderr,"Description: %s, Unit %s\n", desc,unit); printf("Inserting into database, table=%s, runtime=%s, validtime=%s, parameter=%s, level=%s \n",table,rt,vt,name,level_buf); for (j = 0; j < ndata; j++) { if (!UNDEFINED_VAL(data[j])) { longitude = lon[j]; latitude = lat[j]; if (longitude > 180) longitude -= 360; fprintf(file,"\"%s\",\"%s\",%g,%g,\"%s\",\"%s\",%lg\n",rt,vt,latitude,longitude,name,level_buf,data[j]); } } fclose(file); sprintf(sql,"LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'",save->filename,table); printf("%s \n",sql); /* send SQL query */ if (mysql_query(save->conn, sql)) fatal_error("mysql: %s", mysql_error(save->conn)); // unlink = POSIX, remove = ANSI // unlink(save->filename); remove(save->filename); } return 0; }
int f_mysql_speed(ARG7) { char temp_pathname[STRING_SIZE]; char sql[1500]; char server[100]; char user[100]; char password[100]; char database[100]; char table[100]; MYSQL_RES *res; MYSQL_ROW row; char name[100], desc[100], unit[100], level_buf[100]; int year, month, day, hour, minute, second; char vt[20],rt[20]; // unsigned char *p; int i,j; double longitude,latitude,value; char new_level[50]; char new_name[50]; char conv[50]; char precision[10]; char last; char param[50]; int ctr; int level; unsigned int load_local_infile; struct local_struct { MYSQL *conn; FILE *temp_fileptr; unsigned int npts; char *rows[MAX_NXNY]; char has_value[MAX_NXNY]; char *params; unsigned int isset; unsigned int wlon; unsigned int remove_unlikely; char runtime[20], validtime[20]; int last_GDS_change_no; }; struct local_struct *save; strcpy(server,arg1); strcpy(user,arg2); strcpy(password,arg3); strcpy(database,arg4); strcpy(table,arg5); /* initialization phase */ if (mode == -1) { decode = latlon = 1; *local = save = (struct local_struct *) malloc( sizeof(struct local_struct)); if (save == NULL) fatal_error("mysql_speed memory allocation ",""); for (i = 0; i < MAX_NXNY; i++) { save->rows[i] = NULL; save->has_value[i] = 0; } save->last_GDS_change_no = 0; save->conn = mysql_init(NULL); save->temp_fileptr= NULL; save->params = (char *) malloc(1500*sizeof(char)); sprintf(save->params,"%s",""); save->isset = 0; save->runtime[0] = 0; save->validtime[0] = 0; load_local_infile = 1; // 1 = LOAD LOCAL INFILE, 0 = do not LOAD LOCAL INFILE if (sscanf(arg6,"%d", &save->wlon) != 1) { fatal_error("Argument 6, use western longitudes, has to be 0 or 1, error parsing %s", arg6); } if (sscanf(arg7,"%d", &save->remove_unlikely) != 1) { fatal_error("Argument 7, remove unlikely values, has to be 0 or 1, error parsing %s", arg7); } /* Set options for database */ mysql_options(save->conn,MYSQL_OPT_LOCAL_INFILE, (char *) &load_local_infile); /* Connect to database */ if (!mysql_real_connect(save->conn, server, user, password, database, 0, NULL, 0)) { fatal_error("f_mysql_speed: could not connect to %s", mysql_error(save->conn)); } return 0; } /* cleanup phase */ if (mode == -2) { save = (struct local_struct *) *local; strcpy(temp_pathname, "/tmp/wgrib2_mysqlXXXXXX"); if ( -1 == (load_local_infile = mkstemp(temp_pathname)) ) { fatal_error("f_mysql_speed: error making temporary filename",""); } if ( !(save->temp_fileptr = fdopen(load_local_infile, "w")) ) { fatal_error("f_mysql_speed: error making temporary filename",""); } fprintf(stdout, "Columns to insert: rt,vt,lat,lon%s\n", save->params); fprintf(save->temp_fileptr,"rt,vt,lat,lon%s\n",save->params); for (j = 0; j < save->npts; j++) { if (save->has_value[j] == 1) fprintf(save->temp_fileptr,"%s\n",save->rows[j]); } fflush(save->temp_fileptr); sprintf(sql,"LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES (rt,vt,lat,lon%s)",temp_pathname,table,save->params); printf("Inserting into database, table=%s \n",table); printf("%s\n",sql); /* send SQL query */ if (mysql_query(save->conn, sql)) { fatal_error("f_mysql_speed: connection error %s", mysql_error(save->conn)); } fclose(save->temp_fileptr); remove(temp_pathname); mysql_close(save->conn); return 0; } /* processing phase */ save = (struct local_struct *) *local; // if (new_GDS && save->isset == 0 ) { if ((save->last_GDS_change_no != GDS_change_no) && save->isset == 0 ) { //save->npts = GB2_Sec3_npts(sec); save->npts = ndata; if (ndata > MAX_NXNY) fatal_error_i("f_mysql_speed: MAX_NXNY exceeded %d", MAX_NXNY); for (i = 0; i < ndata; i++) { if (save->rows[i] == NULL) { save->rows[i] = (char *) malloc(1500*sizeof(char)); if (save->rows[i] == NULL) fatal_error("f_mysql_speed: memory allocation problem",""); } save->rows[i][0] = '\0'; } save->isset = 1; // } else if (new_GDS) { } else if (save->last_GDS_change_no != GDS_change_no) { fatal_error("f_mysql_speed, grid definition has to be the same for all fields",""); } save->last_GDS_change_no = GDS_change_no; /*Collect runtime and validtime into vt and rt*/ reftime(sec, &year, &month, &day, &hour, &minute, &second); // p = sec[1]; // sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", (p[12]<<8)+p[13], p[14],p[15],p[16],p[17],p[18]); sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second); if (verftime(sec, &year, &month, &day, &hour, &minute, &second) == 0) { sprintf(vt,"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second); } /*Check that runtimes are equal and validtimes are equal*/ if (save->runtime[0] != 0 && strcmp(save->runtime,rt) != 0) { fprintf(stderr, "Error, runtime has to be equal for all fields\n"); } strcpy(save->runtime,rt); if (save->validtime[0] != 0 && strcmp(save->validtime,vt) != 0) { fprintf(stderr, "Error, validtime has to be equal for all fields\n"); } strcpy(save->validtime,vt); /*Get levels, parameter name, description and unit*/ // f_lev(mode, sec, data, ndata, level_buf, local); f_lev(call_ARG0(level_buf, NULL)); //if (ndata != save->npts && save->npts>0) fprintf(stderr,"ERROR: fields do not contain equally many gridpoints, %d , %d \n",save->npts,ndata); if (strcmp(level_buf, "reserved") == 0) return(0); getName(sec, mode, NULL, name, desc, unit); fprintf(stderr,"Start processing of %s at %s, runtime=%s, validtime=%s \n", name, level_buf,rt,vt); ctr = GB2_Center(sec); sprintf(sql,"select * from wgrib2_parameter_mapping where center_id=%d and wgrib_param_name='%s' and wgrib_param_levelname='%s'", ctr, name, level_buf); fprintf(stderr,"SQL: %s\n", sql); if (mysql_query(save->conn, sql)) { fatal_error("f_mysql_speed: mysql error %s", mysql_error(save->conn)); } sprintf(new_level,"%s", "None"); sprintf(conv,"%s", "None"); sprintf(precision, "%s", "2"); printf("\nCenter_id: %d \n", ctr); res = mysql_use_result(save->conn); while ( (row = mysql_fetch_row(res) ) != NULL) { printf("our_name: %s \n", row[3]); sprintf(new_name,"%s", row[3]); sprintf(new_level,"%s", row[4]); sprintf(conv,"%s", row[5]); sprintf(precision, "%s", row[6]); } printf("val_precision: %s \n", precision); printf("conversion: %s \n", conv); printf("our_levelname: %s \n", new_level); if (strcmp(new_level,"None") != 0) { fprintf(stderr,"Sets level %s as level %s\n", level_buf, new_level); sprintf(param, "%s_%s", new_name, new_level); } else { if (strcmp(level_buf,"surface") == 0) { sprintf(param, "%s_0", name); } else { sscanf(level_buf,"%d %s", &level, &last); sprintf(param, "%s_%d", name, level); } } mysql_free_result(res); strcat(save->params,","); strcat(save->params,param); fprintf(stderr,"Gridpoints in data: %d\n", ndata); fprintf(stderr, "Gridpoints for insert: %d\n", save->npts); fprintf(stderr, "Remove unlikely: %d\n", save->remove_unlikely); fprintf(stderr, "Western longitudes: %d\n", save->wlon); for (j = 0; j < ndata; j++) { longitude = lon[j]; if (longitude > 180 && save->wlon==1) longitude-=360; latitude = lat[j]; value = data[j]; if (save->remove_unlikely == 1 && value>0 && value<10e-8) value=0; if (save->remove_unlikely == 1 && (strcmp(name,"APCP")==0 || strcmp(name,"ASNOW")==0 || strcmp(name,"ACPCP")==0 ) && value==1) value=0; value = convunit(value, conv); if (strlen(save->rows[j]) < 2) { if (!UNDEFINED_VAL(data[j])) { save->has_value[j] = 1; sprintf(save->rows[j],"\"%s\",\"%s\",%g,%g,%lg",rt,vt,latitude,longitude,value); } else { sprintf(save->rows[j],"\"%s\",\"%s\",%g,%g,NULL",rt,vt,latitude,longitude); } } else { if (!UNDEFINED_VAL(data[j])) { save->has_value[j] = 1; sprintf(sql,",%lg",value); } else { sprintf(sql,",NULL"); } strcat(save->rows[j],sql); } } return 0; }
int f_AAIG(ARG0) { int grid_template; double cellsize; char *save_inv_out, level[STRING_SIZE], file[STRING_SIZE], name[STRING_SIZE]; int year0, month0, day0, hour0, minute0, second0; int year, month, day, hour, minute, second, i, j; FILE *out; if (mode == -1) { decode = latlon = 1; return 0; } if (mode < 0) return 0; if (lat == NULL || lon == NULL) { fprintf(stderr,"f_AAIG does nothing, no lat-lon information\n"); return 0; } if (output_order != wesn) { fprintf(stderr,"f_AAIG does nothing, not in we:sn order\n"); return 0; } grid_template = code_table_3_1(sec); if (grid_template != 0) { fprintf(stderr,"f_AAIG does nothing, not lat-lon grid\n"); return 0; } if (nx == -1 || ny == -1) { fprintf(stderr,"f_AAIG does nothing, found thinned lat-lon grid\n"); return 0; } cellsize = -1.0; if (nx > 1) { cellsize = lon[1] - lon[0]; if ( fabs(lat[nx]-lat[0] - cellsize) > 0.0001*cellsize) { fprintf(stderr,"f_AAIG does nothing, dlon != dlat\n"); return 0; } } else { cellsize = lat[nx] - lat[0]; } if (getName(sec, mode, NULL, name, NULL, NULL) != 0) { fprintf(stderr,"f_AAIG does nothing, no name\n"); return 0; } save_inv_out = inv_out; inv_out = level; f_lev(CALL_ARG0); inv_out = save_inv_out; save_inv_out = level; while (*save_inv_out) { if (*save_inv_out == ' ') *save_inv_out = '_'; save_inv_out++; } reftime(sec, &year0, &month0, &day0, &hour0, &minute0, &second0); if (verftime(sec, &year, &month, &day, &hour, &minute, &second) != 0) { fprintf(stderr,"f_AAIG no verf time\n"); year = year0; month = month0; day = day0; hour = hour0; } if (year == year0 && month == month0 && day == day0 && hour == hour0) { sprintf(file,"%s.%s.%4.4d%2.2d%2.2d%2.2d.asc",name,level,year,month,day,hour); } else { sprintf(file,"%s.%s.%4.4d%2.2d%2.2d%2.2d_%4.4d%2.2d%2.2d%2.2d.asc", name,level,year0,month0,day0,hour0,year,month,day,hour); } if ((out = fopen(file,"w")) == NULL) { fprintf(stderr,"f_AAIG could not open raster file %s\n",file); return 0; } fprintf(stderr, "raster file: %s\n", file); fprintf(out,"ncols %d\n", nx); fprintf(out,"nrows %d\n", ny); fprintf(out,"xllcenter %lf\n", lon[0] > 180.0 ? lon[0]-180.0 : lon[0]); fprintf(out,"yllcenter %lf\n", lat[0]); fprintf(out,"cellsize %lf\n", cellsize); fprintf(out,"NODATA_VALUE 9.999e20\n"); for (j = ny-1; j >= 0; j--) { for (i = 0; i < nx; i++) { fprintf(out,"%f\n", data[i+j*nx]); } } fclose(out); return 0; }