Skip to content

Instantly share code, notes, and snippets.

@serihiro
Last active January 29, 2020 05:23
Show Gist options
  • Select an option

  • Save serihiro/7837218dc0965ff097b17fb48e2a73e7 to your computer and use it in GitHub Desktop.

Select an option

Save serihiro/7837218dc0965ff097b17fb48e2a73e7 to your computer and use it in GitHub Desktop.
extended csv output plugin of wgrib2 http://www.cpc.ncep.noaa.gov/products/wesley/wgrib2/
/******************************************************************************************
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:csv2:output:5:make comma separated file, A=file, B=lon_from, C=lon_to, D=lat_from, E=lat_to (WxText enabled)
*/
int f_csv2(ARG5)
{
char new_inv_out[STRING_SIZE];
char name[100], desc[100], unit[100];
FILE *out;
double lon_from, lon_to, lat_from, lat_to;
sscanf(arg2, "%lf", &lon_from);
sscanf(arg3, "%lf", &lon_to);
sscanf(arg4, "%lf", &lat_from);
sscanf(arg5, "%lf", &lat_to);
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)
{
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;
// 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*/
double tmp_lon;
double tmp_lat;
#pragma omp parallel for private(j)
for (j = 0; j < ndata; j++)
{
if (!UNDEFINED_VAL(data[j]))
{
tmp_lon = lon[j] > 180.0 ? lon[j] - 360.0 : lon[j];
tmp_lat = lat[j];
if(lon_from != 0.0 && lon_to != 0.0)
if(!(lon_from <= tmp_lon && tmp_lon <= lon_to))
continue;
if(lat_from != 0.0 && lat_to != 0.0)
if(!(lat_from <= tmp_lat && tmp_lat <= lat_to))
continue;
if (WxNum > 0) {
fprintf(out, "\"%s\",\"%s\"\n", rt, WxLabel(data[j]));
} else {
fprintf(out, "\"%s\",%lg\n", rt, 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