Last active
November 28, 2018 06:12
-
-
Save serihiro/17bd2dea87f826ec6caae58d543037f3 to your computer and use it in GitHub Desktop.
wgrib2 csv plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /****************************************************************************************** | |
| Copyright (C) 2008 Niklas Sondell, Storm Weather Center | |
| This file is part of wgrib2 and could be distributed under terms of the GNU General Public License | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include "grb2.h" | |
| #include "wgrib2.h" | |
| #include "fnlist.h" | |
| #include <dirent.h> | |
| extern int decode, flush_mode; | |
| extern int WxText, WxNum; | |
| extern double *lat, *lon; | |
| extern int decode, latlon; | |
| /* | |
| * HEADER:100:csv4:output:2:make comma separated file, A=output_base_directory_path, B=target_observation_list_file_path (WxText enabled) | |
| */ | |
| int f_csv4(ARG2) | |
| { | |
| 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) | |
| { | |
| //do nothing | |
| WxText = decode = latlon = 1; | |
| return 0; | |
| } | |
| /* cleanup phase */ | |
| if (mode == -2) | |
| { | |
| // do nothing | |
| return 0; | |
| } | |
| /* processing phase */ | |
| /*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; | |
| getExtName(sec, mode, NULL, name, desc, unit, ".", "_"); | |
| /* Lage if-setning rundt hele som sjekker om alt eller deler skal ut*/ | |
| FILE *target_observation_list_file = fopen(arg2, "r"); | |
| if (target_observation_list_file == NULL) | |
| { | |
| perror("target_observation_list_file_path could not be opened"); | |
| return 1; | |
| } | |
| char target_observation_id[5]; | |
| float target_lon, target_lat; | |
| int int_target_lon, int_target_lat; | |
| float tmp_lon; | |
| float tmp_lat; | |
| char tmp_lon_str[10], tmp_lat_str[10], output_file_name[10]; | |
| int break_loop = 0; | |
| while (fscanf(target_observation_list_file, "%[^,],%g,%g\n", target_observation_id, &target_lon, &target_lat) != EOF) | |
| { | |
| int_target_lon = (int)target_lon; | |
| int_target_lat = (int)target_lat; | |
| break_loop = 0; | |
| #pragma omp parallel for private(j,tmp_lon,tmp_lat,tmp_lon_str,tmp_lat_str,output_file_name) | |
| for (j = 0; j < ndata; j++) | |
| { | |
| if(break_loop == 1){ | |
| continue; | |
| } | |
| if (!UNDEFINED_VAL(data[j])) | |
| { | |
| tmp_lon = lon[j] > 180.0 ? lon[j] - 360.0 : lon[j]; | |
| tmp_lat = lat[j]; | |
| if(int_target_lon != (int)tmp_lon || int_target_lat != (int)tmp_lat) | |
| continue; | |
| sprintf(tmp_lon_str, "%g", tmp_lon); | |
| sscanf(tmp_lon_str, "%g", &tmp_lon); | |
| sprintf(tmp_lat_str, "%g", tmp_lat); | |
| sscanf(tmp_lat_str, "%g", &tmp_lat); | |
| if (target_lon != tmp_lon || target_lat != tmp_lat) | |
| continue; | |
| break_loop = 1; | |
| char output_file_path[255]; | |
| strcpy(output_file_path, arg1); | |
| // ignore error | |
| mkdir(output_file_path, 0777); | |
| strcat(output_file_path, "/"); | |
| strcat(output_file_path, target_observation_id); | |
| // ignore error | |
| mkdir(output_file_path, 0777); | |
| strcat(output_file_path, "/"); | |
| sprintf(output_file_name, "%d%02d%02d", year, month, day); | |
| strcat(output_file_path, output_file_name); | |
| strcat(output_file_path, ".csv"); | |
| FILE *out = fopen(output_file_path, "a"); | |
| if (out == NULL) | |
| { | |
| perror("Failed to open output file path"); | |
| continue; | |
| } | |
| if (WxNum > 0) | |
| { | |
| fprintf(out, "\"%s\",\"%s\"\n", rt, WxLabel(data[j])); | |
| } | |
| else | |
| { | |
| fprintf(out, "\"%s\",%lg\n", rt, data[j]); | |
| } | |
| fclose(out); | |
| } | |
| } | |
| } | |
| close(target_observation_list_file); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment