Skip to content

Instantly share code, notes, and snippets.

@serihiro
Created November 22, 2018 03:17
Show Gist options
  • Save serihiro/f785af9c998feaf15a0355704639a615 to your computer and use it in GitHub Desktop.
Save serihiro/f785af9c998feaf15a0355704639a615 to your computer and use it in GitHub Desktop.
wgrib2 extended csv output plugin
/******************************************************************************************
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"
extern int decode, flush_mode;
extern int file_append;
extern int WxText, WxNum;
extern double *lat, *lon;
extern int decode, latlon;
/*
* HEADER:100:csv3:output:3:make comma separated file, A=file, B=lon, C=lat (WxText enabled)
*/
int f_csv3(ARG3) {
char new_inv_out[STRING_SIZE];
char name[100], desc[100], unit[100];
FILE *out;
float target_lon, target_lat;
sscanf(arg2, "%g", &target_lon);
sscanf(arg3, "%g", &target_lat);
unsigned int j;
char vt[20],rt[20];
int year, month, day, hour, minute, second;
/* initialization phase */
file_append = 1;
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) {
ffclose((FILE *) *local);
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;
getExtName(sec, mode, NULL, name, desc, unit,".","_");
/* Lage if-setning rundt hele som sjekker om alt eller deler skal ut*/
float tmp_lon;
float tmp_lat;
char *tmp_lon_str[10], tmp_lat_str[10];
#pragma omp parallel for private(j,tmp_lon,tmp_lat,tmp_lon_str,tmp_lat_str)
for (j = 0; j < ndata; j++)
{
if (!UNDEFINED_VAL(data[j]))
{
tmp_lon = lon[j] > 180.0 ? lon[j] - 360.0: lon[j];
sprintf(tmp_lon_str, "%g", tmp_lon);
sscanf(tmp_lon_str, "%g", &tmp_lon);
tmp_lat = lat[j];
sprintf(tmp_lat_str, "%g", tmp_lat);
sscanf(tmp_lat_str, "%g", &tmp_lat);
if(target_lat != tmp_lat)
continue;
if(target_lon != tmp_lon)
continue;
if (WxNum > 0){
fprintf(out, "\"%s\",%g,%g,\"%s\"\n", rt, tmp_lon, tmp_lat, WxLabel(data[j]));
}else{
fprintf(out, "\"%s\",%g,%g,%lg\n", rt, tmp_lon, tmp_lat, data[j]);
}
}
}
if (flush_mode) fflush(out);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment