<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Dear All, <div>The original author of this gribmap patch is no longer receiving email as this address (he's also been unsubscribed from the list due to excessive bounces). Can anyone provide me with COSMO data samples that exploit the TU=13 and TU=14 features (for 15min and 30min output) so I can test this patch? </div><div>--Jennifer</div><div><br></div><div><br></div><div><br><div><div>On Mar 29, 2010, at 8:20 AM, Gomez Breogan wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Dear GrADS users and developers:<br><br>My name is Breogan Gomez and I am working for the German Weather Service (DWD) in the data assimilation department. DWD's COSMO model (also used by other National Weather Services) has a peculiar time unit specification in their Grib output files. That are TimeUnit=13 and TimeUnit=14 to represent 15min output and 30min output. This is not WMO standard but is generally used by the German Weather Service and all the Centers within the COSMO community (<a href="http://www.cosmo-model.org/">http://www.cosmo-model.org/</a>).<br><br>GrADS is perfectly able to read COSMO model output (using "gribmap") when the TU is speciffied in any WMO standard unit, but when TU-13 or TU-14 are used he is not able to find the correct time records in the grib file.<br><br>I recently I have performed a minor modification to "gribmap" code (at "gagmap.c") to include the support for this TU-13 and TU-14. This modification is fully compatible with other TU and does not introduce any limitation in the application. I was was wondering if could be possible to introduce this modification in the standard GrADS release. This would help to expand GrADS use to a bigger set of scientists that are working with this high frecuency COSMO simulations.<br><br>Attached I include the modified gagmap.c source file (version: 2.0.a7.1 has been used for this purpose). Just search for "DWD", only four lines had been added to the original file.<br><br>I hope you find this useful,<br><br>All the best,<br><br>Breogan Gomez<br><br>Ps: Similar improvements have been performed on "wgrib" and "grib2ctl.pl" applications and had been sent to Wesley Ebisuzaki, we are waiting for reply.<br><br><br>/* Copyright (C) 1988-2008 by Brian Doty and the <br> Institute of Global Environment and Society (IGES). <br> See file COPYRIGHT for more information. */<br><br>/* Values output into the grib1 map file:<br> Header:<br> hipnt info: 0 - version number (1)<br> 1 - number of times in file<br> 2 - number of records per time<br> 3 - Grid type<br> 255 - user defined grid. descriptor<br> describes grid exactly; one record<br> per grid.<br> 29 - Predefined grid set 29 and 30.<br> Two records per grid.<br> hfpnt info: None<br> Info:<br> intpnt info (for each mapped grib record) :<br> 0 - position of start of data in file<br> 1 - position of start of bit map in file<br> 2 - number of bits per data element<br> fltpnt info :<br> 0 - decimal scale factor for this record<br> 1 - binary scale factor<br> 2 - reference value<br>*/<br><br>#ifdef HAVE_CONFIG_H<br>#include "config.h"<br><br>#ifdef HAVE_MALLOC_H<br>#include <malloc.h><br>#endif<br>#else /* undef HAVE_CONFIG_H */<br>#include <malloc.h><br>#endif /* HAVE_CONFIG_H */<br><br>#include <errno.h><br>#include <limits.h><br>#include <math.h><br>#include <stdio.h><br>#include <stdlib.h><br>#include <stddef.h><br>#include <string.h><br>#include <unistd.h><br>#include <sys/types.h><br>#include "grads.h"<br>#include "gagmap.h"<br>#if GRIB2<br>#include "grib2.h"<br>#endif<br><br><br>/* global variables */<br>extern struct gamfcmn mfcmn;<br>struct dt rtime; /* time based on input map */<br>struct dt ftime; /* time based on dd file */<br>static off_t flen;<br>gaint ng1elems=3;<br>gaint ng2elems=2;<br><br>/* Routine to scan a grib1 or grib2 file and output an index (map) file. */<br><br>gaint gribmap (void) {<br><br>#if GRIB2<br> unsigned char *cgrib=NULL;<br> g2int listsec0[3],listsec1[13],numlocal,numfields,n;<br> g2int unpack,expand,lskip,lgrib,iseek;<br> gribfield *gfld;<br> char *ptr;<br> size_t lengrib;<br> struct gag2indx *g2indx;<br>#endif<br> char *ch=NULL;<br> gaint ret,ierr,flag,rcgr,record;<br> gaint rc,i,e,tmin=0,tmax=0,told,tcur,fnum,didmatch=0;<br> gaint sp,ioff,eoff,it,write_map;<br> struct gafile *pfi;<br> struct dt dtim,dtimi;<br> struct gaens *ens;<br><br>#if GRIB2<br> unpack=0;<br> expand=0;<br>#endif<br> mfile=NULL;<br> write_map=1;<br><br> /* Get the descriptor file name */<br> if (ifile==NULL) {<br> printf ("\n");<br> cnt = nxtcmd (cmd,"Enter name of Data Descriptor file: ");<br> if (cnt==0) return(1);<br> getwrd(crec,cmd,250);<br> ifile = crec;<br> }<br><br> /* Allocate memory for gafile structure */<br> pfi = getpfi();<br> if (pfi==NULL) {<br> printf ("gribmap error: unable to allocate memory for gafile structure\n");<br> return(1);<br> }<br><br> /* Parse the descriptor file */<br> rc = gaddes (ifile, pfi, 0);<br> if (rc) return(1);<br><br> /* Check index flags */<br> if (pfi->idxflg!=1 && pfi->idxflg!=2) {<br> printf ("gribmap error: data descriptor file is not for GRIB data\n");<br> return(1);<br> } <br><br> /* * GRIB1 * */<br> else if (pfi->idxflg==1) {<br><br> /* Allocate memory for gaindx structure */<br> pindx = (struct gaindx *)galloc(sizeof(struct gaindx),"pindxgm");<br> if (pindx==NULL) {<br> printf ("grib1map error: unable to allocate memory for pindx\n");<br> return(1);<br> }<br><br> /* Save the initial time from the descriptor file for the tau0 option and the map file */<br> btimdd.yr = *(pfi->abvals[3]);<br> btimdd.mo = *(pfi->abvals[3]+1);<br> btimdd.dy = *(pfi->abvals[3]+2);<br> btimdd.hr = *(pfi->abvals[3]+3);<br> btimdd.mn = *(pfi->abvals[3]+4);<br> if (no_min) btimdd.mn = 0;<br><br> /* Set up for this grid type */<br> if (pfi->grbgrd<-900 || pfi->grbgrd==255) {<br> nrec = 1;<br> gtype[0] = 255;<br> } else if (pfi->grbgrd>-1 && pfi->ppflag) {<br> nrec=1;<br> gtype[0] = pfi->grbgrd;<br> } else if (pfi->grbgrd==29) {<br> nrec = 2;<br> gtype[0] = 29;<br> gtype[1] = 30;<br> if (pfi->dnum[0]!=144 || pfi->dnum[1]!=73 ||<br><span class="Apple-tab-span" style="white-space:pre">        </span> pfi->linear[0]!=1 || pfi->linear[1]!=1 ||<br><span class="Apple-tab-span" style="white-space:pre">        </span> *(pfi->grvals[0])!= 2.5 || *(pfi->grvals[0]+1) != -2.5 ||<br><span class="Apple-tab-span" style="white-space:pre">        </span> *(pfi->grvals[1])!= 2.5 || *(pfi->grvals[1]+1) != -92.5 ) {<br> printf("grib1map error: grid specification for GRIB grid type 29/30.\n");<br> printf(" grid scaling must indicate a 2.5 x 2.5 grid\n");<br> printf(" grid size must be 144 x 73\n");<br> printf(" grid must go from 0 to 357.5 and -90 to 90\n");<br> return(1);<br> }<br> } else {<br> nrec = 1;<br> gtype[0] = pfi->grbgrd;<br> }<br><br> /* Set up grib1 index and initialize values */<br> pindx->type = g1ver;<br> pindx->hinum = 4;<br> pindx->hfnum = 0;<br> pindx->intnum = nrec * ng1elems * pfi->trecs * pfi->dnum[3] * pfi->dnum[4];<br> pindx->fltnum = nrec * ng1elems * pfi->trecs * pfi->dnum[3] * pfi->dnum[4];<br> pindx->hipnt = (gaint *)galloc(sizeof(gaint)*pindx->hinum,"hipntgm");<br> pindx->intpnt = (gaint *)galloc(sizeof(gaint)*pindx->intnum,"intpntgm");<br> pindx->fltpnt = (gafloat *)galloc(sizeof(gafloat)*pindx->fltnum,"fltpntgm");<br> if (pindx->hipnt==NULL || pindx->intpnt==NULL || pindx->fltpnt==NULL) {<br> printf ("grib1map error: unable to allocate memory for index pointers\n");<br> return(1);<br> }<br> for (i=0; i<pindx->intnum; i++) *(pindx->intpnt+i) = -999;<br> for (i=0; i<pindx->fltnum; i++) *(pindx->fltpnt+i) = -999; <br> *(pindx->hipnt+0) = g1ver;<br> *(pindx->hipnt+1) = pfi->dnum[3];<br> *(pindx->hipnt+2) = pfi->trecs;<br> *(pindx->hipnt+3) = pfi->grbgrd;<br> if (pfi->grbgrd<-900) *(pindx->hipnt+3) = 255;<br><br> /* Loop over all files in the data set */<br> gfile = NULL;<br> for (e=1,ens=pfi->ens1; e<=pfi->dnum[4]; e++,ens++) {<br> tcur = 0;<br> while (1) { /* loop over all times for this ensemble */<br> if (pfi->tmplat) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* make sure no file is open */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (gfile!=NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> fclose(gfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> gfile=NULL;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* advance to first valid time step for this ensemble */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (tcur==0) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> told = 0;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tcur = 1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1] == -1) tcur++; <br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> else { /* tcur!=0 */<br><span class="Apple-tab-span" style="white-space:pre">        </span> told = pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1];<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* increment time step until fnums changes */<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (told==pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1] && tcur<=pfi->dnum[3]) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> tcur++;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><br><span class="Apple-tab-span" style="white-space:pre">        </span> /* make sure we haven't advanced past end of time axis */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (tcur>pfi->dnum[3]) break;<br><br><span class="Apple-tab-span" style="white-space:pre">        </span> /* check if we're past all valid time steps for this ensemble */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if ((told != -1) && (pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1] == -1)) break;<br><br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Find the range of t indexes that have the same fnums value.<br><span class="Apple-tab-span" style="white-space:pre">        </span> These are the times that are contained in this particular file */<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmin = tcur;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmax = tcur-1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> fnum = pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1];<br><br><span class="Apple-tab-span" style="white-space:pre">        </span> if (fnum != -1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (fnum == pfi->fnums[(e-1)*pfi->dnum[3]+tmax]) tmax++;<br><span class="Apple-tab-span" style="white-space:pre">        </span> gr2t(pfi->grvals[3], (gadouble)tcur, &dtim); <br><span class="Apple-tab-span" style="white-space:pre">        </span> gr2t(pfi->grvals[3], ens->gt, &dtimi);<br><span class="Apple-tab-span" style="white-space:pre">        </span> ch = gafndt(pfi->name, &dtim, &dtimi, pfi->abvals[3], pfi->pchsub1, pfi->ens1,tcur,e,&flag);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ch==NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf(" grib1map error: couldn't determine data file name for e=%d t=%d\n",e,tcur);<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(1);<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br> }<br> else { <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Data set is not templated */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ch = pfi->name;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmin = 1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmax = pfi->dnum[3];<br> }<br><br> /* Open this GRIB file and position to start of first record */<br> if (!quiet) printf(" grib1map: opening GRIB file: %s \n",ch);<br> gfile = fopen(ch,"rb");<br> if (gfile==NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (pfi->tmplat) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (!quiet) printf (" grib1map warning: could not open GRIB file: %s\n",ch);<br><span class="Apple-tab-span" style="white-space:pre">        </span> continue;<br><span class="Apple-tab-span" style="white-space:pre">        </span> } else {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf (" grib1map error: could not open GRIB file: %s\n",ch);<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(1);<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br> }<br> if (pfi->tmplat) gree(ch,"312");<br><br> /* Get file size */<br> fseeko(gfile,0L,2);<br> flen = ftello(gfile);<br><br> /* Set up to skip appropriate amount and position */<br> if (skip > -1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> fpos = skip;<br> }<br> else {<br><span class="Apple-tab-span" style="white-space:pre">        </span> fseeko (gfile,0,0);<br><span class="Apple-tab-span" style="white-space:pre">        </span> rc = fread (rec,1,100,gfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rc<100) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf (" grib1map error: I/O error reading header\n");<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(1);<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> len = gagby(rec,88,4);<br><span class="Apple-tab-span" style="white-space:pre">        </span> fpos = len*2 + 100;<br> }<br><br> /* Main Loop */<br> irec=1;<br> while (1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* read a grib record */<br><span class="Apple-tab-span" style="white-space:pre">        </span> rc = gribhdr(&ghdr); <br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rc) break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* compare to each 2-d variable in the 5-D data volume<br><span class="Apple-tab-span" style="white-space:pre">        </span> defined by the descriptor file for a match */<br><span class="Apple-tab-span" style="white-space:pre">        </span> rcgr = gribrec(&ghdr,pfi,pindx,tmin,tmax,e);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rcgr==0) didmatch=1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rcgr>=100) didmatch=rcgr;<br><span class="Apple-tab-span" style="white-space:pre">        </span> irec++;<br> }<br><br> /* see how we did */<br> if (rc==50) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf (" grib1map error: I/O error reading GRIB file\n");<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf (" possible cause is premature EOF\n");<br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br> }<br> if (rc>1 && rc!=98) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf (" grib1map error: GRIB file format error (rc = %i)\n",rc);<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(rc);<br> }<br><br> /* break out if not templating */<br> if (!pfi->tmplat) break;<br><br> } /* end of while (1) loop */<br> } /* end of for (e=1; e<=pfi->dnum[4]; e++) loop */<br><br> if (!quiet) printf (" grib1map: reached end of files\n");<br><br> /* check if file closed already for case where template was set,<br> but it was not templated and the template code above closed it. */<br> if (gfile!=NULL) {<br> fclose (gfile);<br> gfile=NULL;<br> }<br><br> /* open the map file */<br> if (write_map) {<br> mfile = fopen(pfi->mnam,"wb");<br> if (mfile==NULL) {<br> printf (" grib1map error: could not open index file: %s\n",pfi->mnam);<br> return(1);<br> } <br> else {<br> if (!quiet) printf(" grib1map: writing the map...\n\n");<br> /* output the map depending on version # */<br> if (g1ver==1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> fwrite (pindx,sizeof(struct gaindx),1,mfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (pindx->hinum>0) fwrite(pindx->hipnt,sizeof(gaint),pindx->hinum,mfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (pindx->hfnum>0) fwrite(pindx->hfpnt,sizeof(gafloat),pindx->hfnum,mfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (pindx->intnum>0) fwrite(pindx->intpnt,sizeof(gaint),pindx->intnum,mfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (pindx->fltnum>0) fwrite(pindx->fltpnt,sizeof(gafloat),pindx->fltnum,mfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> fclose (mfile);<br> }<br> else {<br><span class="Apple-tab-span" style="white-space:pre">        </span> rc = wtgmap();<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rc == 601) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf(" grib1map error: overflow in float -> IBM float conversion\n");<br><span class="Apple-tab-span" style="white-space:pre">        </span> fclose (mfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> return (601); <br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> fclose (mfile);<br> }<br> }<br> }<br> return (didmatch);<br> }<br><br>#if GRIB2<br> else /* GRIB2 */ {<br><br> /* Set up g2index and initialize values */<br> g2indx = (struct gag2indx *)malloc(sizeof(struct gag2indx));<br> if (g2indx==NULL) {<br> printf ("grib2map error: unable to allocate memory for g2indx\n");<br> return(1);<br> }<br> g2indx->version = 1; <br> g2indx->g2intnum = ng2elems * pfi->trecs * pfi->dnum[3] * pfi->dnum[4];<br> g2indx->g2intpnt = (gaint *)malloc(sizeof(gaint)*g2indx->g2intnum);<br> if (g2indx->g2intpnt==NULL) {<br> printf ("grib2map error: unable to allocate memory for g2indx->g2intpnt\n");<br> goto err;<br> }<br> for (i=0; i<g2indx->g2intnum; i++) g2indx->g2intpnt[i] = -999;<br><br> /* Break out point for case with E>1 but data files are only templated over T */<br> if (pfi->dnum[4]>1 && pfi->tmplat==1) {<br> /* Loop over all files in the data set */ <br> gfile=NULL;<br> e=1;<br> ens=pfi->ens1; <br> tcur = 0;<br> while (1) { /* loop over all times */<br> /* make sure no file is open */<br> if (gfile!=NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> fclose(gfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> gfile=NULL;<br> }<br> if (tcur==0) { /* first time step */<br><span class="Apple-tab-span" style="white-space:pre">        </span> told = 0;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tcur = 1;<br> }<br> else { /* tcur!=0 */<br><span class="Apple-tab-span" style="white-space:pre">        </span> told = pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1];<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* increment time step until fnums changes */<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (told==pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1] && tcur<=pfi->dnum[3]) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> tcur++;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br> }<br> /* make sure we haven't advanced past end of time axis */<br> if (tcur>pfi->dnum[3]) break;<br><br><br> /* Find the range of t indexes that have the same fnums value.<br><span class="Apple-tab-span" style="white-space:pre">        </span> These are the times that are contained in this particular file */<br> tmin = tcur;<br> tmax = tcur-1;<br> fnum = pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1];<br> if (fnum != -1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (fnum == pfi->fnums[(e-1)*pfi->dnum[3]+tmax]) tmax++;<br><span class="Apple-tab-span" style="white-space:pre">        </span> gr2t(pfi->grvals[3], (gadouble)tcur, &dtim); <br><span class="Apple-tab-span" style="white-space:pre">        </span> gr2t(pfi->grvals[3], ens->gt, &dtimi);<br><span class="Apple-tab-span" style="white-space:pre">        </span> ch = gafndt(pfi->name, &dtim, &dtimi, pfi->abvals[3], pfi->pchsub1, pfi->ens1,tcur,e,&flag);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ch==NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: couldn't determine data file name for e=%d t=%d\n",e,tcur);<br><span class="Apple-tab-span" style="white-space:pre">        </span> goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br> }<br> /* Open this GRIB file and position to start of first record (s/b subroutine) */<br> if (!quiet) printf("grib2map: scanning GRIB2 file: %s \n",ch);<br> fflush(stdout);<br> gfile = fopen(ch,"rb");<br> if (gfile==NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (!quiet) printf ("grib2map warning: could not open GRIB file: %s\n",ch);<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> continue;<br> }<br> gree(ch,"f311a");<br> /* Loop over fields in the grib file and find matches */<br> iseek=0;<br> record=1;<br> while (1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* move to next grib message in file */<br><span class="Apple-tab-span" style="white-space:pre">        </span> seekgb(gfile,iseek,32000,&lskip,&lgrib);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (lgrib == 0) break; /* end loop at EOF or problem */<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* read the message into memory */<br><span class="Apple-tab-span" style="white-space:pre">        </span> cgrib = (unsigned char *)galloc(lgrib,"cgrib2");<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (cgrib == NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: unable to allocate memory for record %d at byte %ld\n",record,iseek); <br><span class="Apple-tab-span" style="white-space:pre">        </span> goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> ret = fseek(gfile,lskip,SEEK_SET);<br><span class="Apple-tab-span" style="white-space:pre">        </span> lengrib = fread(cgrib,sizeof(unsigned char),lgrib,gfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (lengrib < lgrib) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: unable to read record %d at byte %ld\n",record,iseek); goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Get info about grib2 message */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = 0;<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = g2_info(cgrib,listsec0,listsec1,&numfields,&numlocal);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ierr) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: g2_info failed: ierr=%d\n",ierr); goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> for (n=0; n<numfields; n++) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = 0;<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = g2_getfld(cgrib,n+1,unpack,expand,&gfld);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ierr) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: g2_getfld failed: ierr=%d\n",ierr); goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* get statistical process type from grib field */<br><span class="Apple-tab-span" style="white-space:pre">        </span> sp = g2sp(gfld);<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* print out useful codes from grib2 field */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) g2prnt(gfld,record,n+1,sp);<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Check grid properties */<br><span class="Apple-tab-span" style="white-space:pre">        </span> rc = g2grid_check(gfld,pfi,record,n+1);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rc) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break; <br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Check time values in grib field */<br><span class="Apple-tab-span" style="white-space:pre">        </span> it = g2time_check(gfld,listsec1,pfi,record,n+1,tmin,tmax);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (it==-99) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> it = (it-1)*pfi->trecs; /* number of records per time */<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Check if the variable is a match */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ioff = g2var_match(gfld,pfi,sp);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ioff==-999) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* check if ensemble codes match */<br><span class="Apple-tab-span" style="white-space:pre">        </span> e = g2ens_match(gfld,pfi);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (e==-999) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> eoff = (e-1)*pfi->dnum[3]*pfi->trecs; /* number of records per ensemble */<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* fill in the gribmap entry */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf(" MATCH \n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2fill (eoff,it+ioff,ng2elems,iseek,n+1,g2indx);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* free memory containing grib record */<br><span class="Apple-tab-span" style="white-space:pre">        </span> gree(cgrib,"f310");<br><span class="Apple-tab-span" style="white-space:pre">        </span> cgrib=NULL;<br><span class="Apple-tab-span" style="white-space:pre">        </span> record++; /* increment grib record counter */<br><span class="Apple-tab-span" style="white-space:pre">        </span> iseek = lskip+lgrib; /* increment byte offset to next grib msg in file */<br> } /* end of while(1) loop over all fields in the grib message*/<br> } /* end of while loop over all times */<br><br> }<br> else {<br> /* All data sets except those that have E>1 but are templated only over T */<br><br> /* Loop over all files in the data set */ <br> gfile=NULL;<br> for (e=1,ens=pfi->ens1; e<=pfi->dnum[4]; e++,ens++) {<br> tcur = 0;<br> while (1) { /* loop over all times for this ensemble */<br> if (pfi->tmplat) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* make sure no file is open */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (gfile!=NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> fclose(gfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> gfile=NULL;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* advance to first valid time step for this ensemble */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (tcur==0) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> told = 0;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tcur = 1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1] == -1) tcur++; <br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> else { /* tcur!=0 */<br><span class="Apple-tab-span" style="white-space:pre">        </span> told = pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1];<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* increment time step until fnums changes */<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (told==pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1] && tcur<=pfi->dnum[3]) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> tcur++;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* make sure we haven't advanced past end of time axis */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (tcur>pfi->dnum[3]) break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* check if we're past all valid time steps for this ensemble */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if ((told != -1) && (pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1] == -1)) break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Find the range of t indexes that have the same fnums value.<br><span class="Apple-tab-span" style="white-space:pre">        </span> These are the times that are contained in this particular file */<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmin = tcur;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmax = tcur-1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> fnum = pfi->fnums[(e-1)*pfi->dnum[3]+tcur-1];<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (fnum != -1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> while (fnum == pfi->fnums[(e-1)*pfi->dnum[3]+tmax]) tmax++;<br><span class="Apple-tab-span" style="white-space:pre">        </span> gr2t(pfi->grvals[3], (gadouble)tcur, &dtim); <br><span class="Apple-tab-span" style="white-space:pre">        </span> gr2t(pfi->grvals[3], ens->gt, &dtimi);<br><span class="Apple-tab-span" style="white-space:pre">        </span> ch = gafndt(pfi->name, &dtim, &dtimi, pfi->abvals[3], pfi->pchsub1, pfi->ens1,tcur,e,&flag);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ch==NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: couldn't determine data file name for e=%d t=%d\n",e,tcur);<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br> }<br> else { <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* only one data file to open */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ch = pfi->name;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmin = 1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> tmax = pfi->dnum[3];<br> }<br><br> /* Open this GRIB file and position to start of first record (s/b subroutine) */<br> if (!quiet) printf("grib2map: scanning GRIB2 file: %s \n",ch);<br> fflush(stdout);<br> gfile = fopen(ch,"rb");<br> if (gfile==NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (pfi->tmplat) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (!quiet) printf ("grib2map warning: could not open GRIB file: %s\n",ch);<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> continue;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> else {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf ("grib2map error: could not open GRIB file: %s\n",ch);<br><span class="Apple-tab-span" style="white-space:pre">        </span> goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br> }<br> if (pfi->tmplat) gree(ch,"f311");<br><br> /* Loop over fields in the grib file and find matches */<br> iseek=0;<br> record=1;<br> while (1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* move to next grib message in file */<br><span class="Apple-tab-span" style="white-space:pre">        </span> seekgb(gfile,iseek,32000,&lskip,&lgrib);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (lgrib == 0) break; /* end loop at EOF or problem */<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* read the message into memory */<br><span class="Apple-tab-span" style="white-space:pre">        </span> cgrib = (unsigned char *)galloc(lgrib,"cgrib2");<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (cgrib == NULL) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: unable to allocate memory for record %d at byte %ld\n",record,iseek); <br><span class="Apple-tab-span" style="white-space:pre">        </span> goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> ret = fseek(gfile,lskip,SEEK_SET);<br><span class="Apple-tab-span" style="white-space:pre">        </span> lengrib = fread(cgrib,sizeof(unsigned char),lgrib,gfile);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (lengrib < lgrib) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: unable to read record %d at byte %ld\n",record,iseek); goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Get info about grib2 message */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = 0;<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = g2_info(cgrib,listsec0,listsec1,&numfields,&numlocal);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ierr) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: g2_info failed: ierr=%d\n",ierr); goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> for (n=0; n<numfields; n++) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = 0;<br><span class="Apple-tab-span" style="white-space:pre">        </span> ierr = g2_getfld(cgrib,n+1,unpack,expand,&gfld);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ierr) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("grib2map error: g2_getfld failed: ierr=%d\n",ierr); goto err;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* get statistical process type from grib field */<br><span class="Apple-tab-span" style="white-space:pre">        </span> sp = g2sp(gfld);<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* print out useful codes from grib2 field */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) g2prnt(gfld,record,n+1,sp);<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Check grid properties */<br><span class="Apple-tab-span" style="white-space:pre">        </span> rc = g2grid_check(gfld,pfi,record,n+1);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rc) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break; <br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Check time values in grib field */<br><span class="Apple-tab-span" style="white-space:pre">        </span> it = g2time_check(gfld,listsec1,pfi,record,n+1,tmin,tmax);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (it==-99) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> it = (it-1)*pfi->trecs; /* number of records per time */<br><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> /* Check if the variable is a match */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ioff = g2var_match(gfld,pfi,sp);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (ioff==-999) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> if (pfi->tmplat) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* make sure grib codes match for this ensemble */<br><span class="Apple-tab-span" style="white-space:pre">        </span> rc = g2ens_check(ens,gfld);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (rc==1) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> } <br><span class="Apple-tab-span" style="white-space:pre">        </span> else {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* check if ensemble codes match */<br><span class="Apple-tab-span" style="white-space:pre">        </span> e = g2ens_match(gfld,pfi);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (e==-999) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("\n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <span class="Apple-tab-span" style="white-space:pre">        </span> <br><span class="Apple-tab-span" style="white-space:pre">        </span> break;<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> eoff = (e-1)*pfi->dnum[3]*pfi->trecs; /* number of records per ensemble */<br><br><span class="Apple-tab-span" style="white-space:pre">        </span> /* fill in the gribmap entry */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf(" MATCH \n");<br> fflush(stdout);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2fill (eoff,it+ioff,ng2elems,iseek,n+1,g2indx);<br><span class="Apple-tab-span" style="white-space:pre">        </span> g2_free(gfld); <br><br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* free memory containing grib record */<br><span class="Apple-tab-span" style="white-space:pre">        </span> gree(cgrib,"f310");<br> <span class="Apple-tab-span" style="white-space:pre">        </span> cgrib=NULL;<br><span class="Apple-tab-span" style="white-space:pre">        </span> record++; /* increment grib record counter */<br><span class="Apple-tab-span" style="white-space:pre">        </span> iseek = lskip+lgrib; /* increment byte offset to next grib msg in file */<br><br> } /* end of while(1) loop over all fields in the grib message*/<br><br> /* break out if not templating -- only need to scan one grib file */<br> if (!pfi->tmplat) goto done;<br><br> } /* end of while(1) loop over all grib files for a given ensemble member*/<br> } /* end of loop over ensemble members: for (e=1,ens=pfi->ens1; e<=pfi->dnum[4]; e++,ens++) */<br> } /* end of else statement for if (pfi->dnum[4]>1 && pfi->tmplat==1) */<br><br> if (!quiet) printf ("grib2map: reached end of files\n");<br> fflush(stdout);<br><br><br>done: <br> /* check if file not closed */<br> if (gfile!=NULL) {<br> fclose (gfile);<br> gfile=NULL;<br> }<br><br> /* Write out the index file */<br> if (write_map) {<br> rc=wtg2map(pfi,g2indx);<br> if (rc) return (rc);<br> }<br> return(0);<br><br>err: <br> if (g2indx->g2intpnt) gree(g2indx->g2intpnt,"f314");<br> if (g2indx) gree(g2indx,"f315");<br> if (cgrib) gree(cgrib,"f316");<br> return(1);<br> }<br><br>#endif /* matches #if GRIB2 */<br><br>}<br><br><br><br>/* Routine to read a GRIB header and process info */<br><br>gaint gribhdr (struct grhdr *ghdr) {<br> struct dt atim;<br> unsigned char rec[50000],*ch,*gds;<br> gaint i,len ,rc,sign,mant;<br> off_t cpos;<br><br> if (fpos+50>=flen) return(1);<br><br> /* look for data between records BY DEFAULT */ <br> i = 0;<br> fpos += i;<br> rc = fseek(gfile,fpos,0);<br> if (rc) return(50);<br> ch=&rec[0];<br> rc = fread(ch,sizeof(char),4,gfile);<br> while ((fpos < flen-4) && (i < scanlim) && <br><span class="Apple-tab-span" style="white-space:pre">        </span>!(*(ch+0)=='G' && <br><span class="Apple-tab-span" style="white-space:pre">        </span> *(ch+1)=='R' &&<br><span class="Apple-tab-span" style="white-space:pre">        </span> *(ch+2)=='I' &&<br><span class="Apple-tab-span" style="white-space:pre">        </span> *(ch+3)=='B')) {<br> fpos++;<br> i++;<br> rc = fseeko(gfile,fpos,0);<br> if (rc) return(50);<br> rc = fread(ch,sizeof(char),4,gfile);<br> if (rc<4) return(50);<br> } <br><br> if (i == scanlim) {<br> printf("grib1map error: GRIB header not found in scanning between records\n");<br> printf(" try increasing the value of the -s argument\n");<br><br> if (scaneof) return(98);<br> if (scanEOF) return(0);<br> return(52);<br> } <br> else if (fpos == flen-4) {<br> if (scaneof) return(98);<br> if (scanEOF) return(0);<br> return (53);<br> }<br><br> /* SUCCESS redo the initial read */ <br> rc = fseek(gfile,fpos,0);<br> if (rc) return(50);<br> rc = fread(rec,1,8,gfile);<br> if (rc<8) {<br> if (fpos+8 >= flen) return(61);<br> else return(62);<br> }<br><br> cpos = fpos;<br> ghdr->vers = gagby(rec,7,1);<br> if (ghdr->vers>1) {<br> printf ("grib1map error: file is not GRIB version 0 or 1, version number is %i\n",ghdr->vers);<br> if (scaneof) return(98);<br> return (99);<br> }<br><br> if (ghdr->vers==0) {<br> cpos += 4;<br> rc = fseek(gfile,cpos,0);<br> if (rc) return(50);<br> } else {<br> ghdr->len = gagby(rec,4,3);<br> cpos = cpos + 8;<br> rc = fseeko(gfile,cpos,0);<br> if (rc) return(50);<br> }<br><br> /* Get PDS length, read rest of PDS */<br> rc = fread(rec,1,3,gfile);<br> if (rc<3) return(50);<br> len = gagby(rec,0,3);<br> ghdr->pdslen = len;<br> cpos = cpos + len;<br> rc = fread(rec+3,1,len-3,gfile);<br> if (rc<len-3) return(50);<br><br> /* Get info from PDS */<br> ghdr->id = gagby(rec,6,1);<br> ghdr->gdsflg = gagbb(rec+7,0,1);<br> ghdr->bmsflg = gagbb(rec+7,1,1);<br> ghdr->parm = gagby(rec,8,1);<br> ghdr->ltyp = gagby(rec,9,1);<br> ghdr->level = gagby(rec,10,2);<br> ghdr->l1 = gagby(rec,10,1);<br> ghdr->l2 = gagby(rec,11,1);<br> if (mpiflg) { <br> /* use initial time from the descriptor file instead of base time from grib header */<br> ghdr->btim.yr = *(pfi->abvals[3]);<br> ghdr->btim.mo = *(pfi->abvals[3]+1);<br> ghdr->btim.dy = *(pfi->abvals[3]+2);<br> ghdr->btim.hr = *(pfi->abvals[3]+3);<br> ghdr->btim.mn = *(pfi->abvals[3]+4);<br> if (no_min) ghdr->btim.mn = 0;<br> } else {<br> ghdr->btim.yr = gagby(rec,12,1);<br> ghdr->btim.mo = gagby(rec,13,1);<br> ghdr->btim.dy = gagby(rec,14,1);<br> ghdr->btim.hr = gagby(rec,15,1);<br> ghdr->btim.mn = gagby(rec,16,1);<br> if (no_min) ghdr->btim.mn = 0;<br> }<br> if (ghdr->btim.hr>23) ghdr->btim.hr = 0; /* Special for NCAR */<br> if (len>24) {<br> ghdr->cent = gagby(rec,24,1);<br> ghdr->btim.yr = ghdr->btim.yr + (ghdr->cent-1)*100;<br> } else {<br> ghdr->cent = -999;<br> if (!(mpiflg) || !(mfcmn.fullyear)) {<br> if (ghdr->btim.yr>49) ghdr->btim.yr += 1900;<br> if (ghdr->btim.yr<50) ghdr->btim.yr += 2000;<br> }<br> }<br> ghdr->ftu = gagby(rec,17,1);<br> ghdr->tri = gagby(rec,20,1);<br> if (ghdr->tri==10) {<br> ghdr->p1 = gagby(rec,18,2);<br> ghdr->p2 = 0;<br> } else {<br> ghdr->p1 = gagby(rec,18,1);<br> ghdr->p2 = gagby(rec,19,1);<br> }<br><br> ghdr->fcstt = ghdr->p1;<br> if (ghdr->tri>1 && ghdr->tri<6) <br> ghdr->fcstt=ghdr->p2;<br> if ((tauave) && ghdr->tri==3) <br> ghdr->fcstt=ghdr->p1;<br> atim.yr=0; atim.mo=0; atim.dy=0; atim.hr=0; atim.mn=0;<br> if (ghdr->ftu== 0) atim.mn = ghdr->fcstt;<br> else if (ghdr->ftu==13) atim.mn = ghdr->fcstt*15; /*DWD*/<br> else if (ghdr->ftu==14) atim.mn = ghdr->fcstt*30; /*DWD*/<br> else if (ghdr->ftu== 1) atim.hr = ghdr->fcstt;<br> else if (ghdr->ftu==10) atim.hr = ghdr->fcstt*3; /* added 3Hr incr */<br> else if (ghdr->ftu==11) atim.hr = ghdr->fcstt*6; /* added 6Hr incr */ <br> else if (ghdr->ftu==12) atim.hr = ghdr->fcstt*12; /* added 12Hr incr */<br> else if (ghdr->ftu== 2) atim.dy = ghdr->fcstt;<br> else if (ghdr->ftu== 3) atim.mo = ghdr->fcstt;<br> else if (ghdr->ftu== 4) atim.yr = ghdr->fcstt;<br> else ghdr->fcstt = -999;<br><br> /* if notau != 0 then FORCE the valid DTG to be the base DTG */ <br> if (notau) ghdr->fcstt = -999 ;<br><br> /* add the forecast time to the time of this grib field */<br> if (ghdr->fcstt>-900) {<br> timadd(&(ghdr->btim),&atim);<br> ghdr->dtim.yr = atim.yr;<br> ghdr->dtim.mo = atim.mo;<br> ghdr->dtim.dy = atim.dy;<br> ghdr->dtim.hr = atim.hr;<br> ghdr->dtim.mn = atim.mn;<br> } else {<br> ghdr->dtim.yr = ghdr->btim.yr;<br> ghdr->dtim.mo = ghdr->btim.mo;<br> ghdr->dtim.dy = ghdr->btim.dy;<br> ghdr->dtim.hr = ghdr->btim.hr;<br> ghdr->dtim.mn = ghdr->btim.mn;<br> }<br> if (len>25) {<br> ghdr->dsf = (gafloat)gagbb(rec+26,1,15);<br> i = gagbb(rec+26,0,1);<br> if (i) ghdr->dsf = -1.0*ghdr->dsf;<br> ghdr->dsf = pow(10.0,ghdr->dsf);<br> } else ghdr->dsf = 1.0;<br><br> /* If it is there, get info from GDS */<br> if (ghdr->gdsflg) {<br> rc = fread(rec,1,3,gfile);<br> if (rc<3) return(50);<br> len = gagby(rec,0,3);<br> ghdr->gdslen = len;<br> cpos = cpos + len;<br><br> /* handle generic grid where the lon/lats are coded from the GDS */<br> gds = (unsigned char *)malloc(len+3);<br> if (gds==NULL) return(51);<br> rc = fread(gds+3,1,len-3,gfile);<br> if (rc<len-3) return(50);<br> ghdr->gtyp = gagby(gds,4,1);<br> ghdr->gicnt = gagby(gds,6,2);<br> ghdr->gjcnt = gagby(gds,8,2);<br> ghdr->gsf1 = gagbb(gds+27,0,1);<br> ghdr->gsf2 = gagbb(gds+27,1,1);<br> ghdr->gsf3 = gagbb(gds+27,2,1);<br> free(gds);<br> } <br> else ghdr->gdslen = 0;<br><br> /* Get necessary info about BMS if it is there */<br> if (ghdr->bmsflg) {<br> rc = fread(rec,1,6,gfile);<br> if (rc<6) return(50);<br> len = gagby(rec,0,3);<br> ghdr->bmsflg = len;<br> ghdr->bnumr = gagby(rec,4,2);<br> ghdr->bpos = cpos+6;<br> cpos = cpos + len;<br> rc = fseeko(gfile,cpos,0);<br> } <br> else ghdr->bmslen = 0;<br><br> /* Get necessary info from data header */<br> rc = fread(rec,1,11,gfile);<br> if (rc<11) return(50);<br> len = gagby(rec,0,3);<br> ghdr->bdslen = len;<br> ghdr->iflg = gagbb(rec+3,0,2);<br> i = gagby(rec,4,2);<br> if (i>32767) i = 32768-i;<br> ghdr->bsf = pow(2.0,(gafloat)i);<br><br> i = gagby(rec,6,1);<br> sign = 0;<br> if (i>127) {<br> sign = 1;<br> i = i - 128;<br> }<br> mant = gagby(rec,7,3);<br> if (sign) mant = -mant;<br> ghdr->ref = (gafloat)mant * pow(16.0,(gafloat)(i-70));<br><br> ghdr->bnum = gagby(rec,10,1);<br> ghdr->dpos = cpos+11;<br><br> if (ghdr->vers==0) {<br> fpos = fpos + 8 + ghdr->pdslen + ghdr->gdslen +<br> ghdr->bmslen + ghdr->bdslen;<br> } <br> else fpos = fpos + ghdr->len;<br><br> return(0);<br><br>}<br><br>/* Routine to determine the location of the GRIB record in terms of the GrADS data set<br> and fill in the proper values at the proper slot location. */<br><br>gaint gribrec (struct grhdr *ghdr, struct gafile *pfi, struct gaindx *pindx, <br><span class="Apple-tab-span" style="white-space:pre">        </span> gaint tmin, gaint tmax, gaint e) {<br> gadouble (*conv) (gadouble *, gadouble);<br> gadouble z,t;<br> struct gavar *pvar;<br> gaint i,ioff,iz,it,joff,nsiz,flag,eoff;<br><br> /* Verify that we are looking at the proper grid type */<br> joff =0;<br> nsiz = nrec * ng1elems ;<br> if (ghdr->iflg) {<br> if (verb) {<br> printf ("GRIB record contains harmonic or complex packing\n");<br> printf (" Record is skipped.\n");<br> printf (" Variable is %i\n",ghdr->parm);<br> }<br> return(10);<br> }<br> if (pfi->grbgrd==255 || pfi->grbgrd<-900) {<br> if (!ghdr->gdsflg) {<br> if (verb) {<br> printf ("GRIB record contains pre-defined grid type: "); <br> printf ("GrADS descriptor specifies type 255\n");<br> gribpr(ghdr);<br> }<br> return(20);<br> } <br> if ( pfi->ppflag) {<br> if ( ghdr->gicnt != 65535 && <br><span class="Apple-tab-span" style="white-space:pre">        </span> ((ghdr->gicnt != pfi->ppisiz) || (ghdr->gjcnt != pfi->ppjsiz)) ) {<br> if (verb) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf ("GRIB grid size does not match descriptor: "); <br><span class="Apple-tab-span" style="white-space:pre">        </span> gribpr(ghdr);<br> }<br> return(300);<br> }<br> } else {<br> if (ghdr->gicnt != 65535 && <br><span class="Apple-tab-span" style="white-space:pre">        </span> ((ghdr->gicnt != pfi->dnum[0]) || (ghdr->gjcnt != pfi->dnum[1]))) {<br> if (verb) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf ("GRIB grid size does not match descriptor:");<br><span class="Apple-tab-span" style="white-space:pre">        </span> gribpr(ghdr);<br> }<br> return(301);<br> }<br> }<br> } <br> else {<br> /* special case for GRIB grid number (dtype grib NNN) == 29 */<br> if (pfi->grbgrd==29) {<br> if (ghdr->id!=29 && ghdr->id!=30) {<br> if (verb) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("Record has wrong GRIB grid type: ") ; <br><span class="Apple-tab-span" style="white-space:pre">        </span> gribpr(ghdr);<br> }<br> return(400); <br> }<br> if (ghdr->id==29) joff = ng1elems;<br> nsiz = 2 * ng1elems ;<br> } else {<br> if (ghdr->id != pfi->grbgrd) {<br> if (verb) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> printf("%s","Record has wrong GRIB grid type: "); <br><span class="Apple-tab-span" style="white-space:pre">        </span> gribpr(ghdr);<br> }<br> return(401); <br> }<br> }<br> }<br><br> /* Calculate the grid time for this record. <br> If it is non-integer or if it is out of bounds, just return. */<br><br> /* Check for given forecast time, tauoff (the -fhr switch) */<br> if (tauflg && (ghdr->ftu==1 && ghdr->fcstt!=tauoff)) {<br> if (verb) {<br> printf("%s %d","--f-- Forecast Time does not match : ",tauoff);<br> gribpr(ghdr);<br> }<br> return(32);<br> }<br><br> /* Check if base time in grib record matches initial time in descriptor file (the -t0 switch) */<br> if (tau0 &&<br> ((ghdr->btim.yr != btimdd.yr) ||<br> (ghdr->btim.mo != btimdd.mo) ||<br> (ghdr->btim.dy != btimdd.dy) ||<br> (ghdr->btim.hr != btimdd.hr) ||<br> (ghdr->btim.mn != btimdd.mn))) {<br> if (verb) {<br> printf("%s","--b-- Base Time does not match Initial Time in DD: "); <br> gribpr(ghdr);<br> }<br> return(34);<br> }<br><br> /* Check if valid time is within grid time limits */<br> t = t2gr(pfi->abvals[3],&(ghdr->dtim));<br> if (t<0.99 || t>((gafloat)(pfi->dnum[3])+0.01)) {<br> if (verb) {<br> printf("%s","----- Time out of bounds: "); <br> gribpr(ghdr);<br> }<br> return(36);<br> }<br><br> /* Check if valid time is an integer */<br> it = (gaint)(t+0.01);<br> if (fabs((gafloat)it - t)>0.01) {<br> if (verb) {<br> printf("----- Time non-integral. %g %g: ",(gafloat)it,t); <br> gribpr(ghdr);<br> }<br> return(38);<br> }<br><br> /* Check if valid time matches range of times for this file */<br> if (it<tmin || it>tmax) {<br> if (verb) {<br> printf("----- Time out of file limits: "); <br> gribpr(ghdr);<br> }<br> return(39);<br> }<br> it = (it-1)*pfi->trecs;<br> eoff = (e-1)*pfi->dnum[3]*pfi->trecs; /* number of records per ensemble */<br><br> /* See if we can match up this grid with a variable in the data descriptor file */<br> pvar = pfi->pvar1;<br> i = 0;<br> flag=0;<br> while (i<pfi->vnum) {<br> if (pvar->levels>0) { /* multi level data */<br> if (dequal(pvar->units[0],ghdr->parm,1e-8)==0 && <br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[8],ghdr->ltyp,1e-8)==0) {<br> /* look for time range indicator match */<br> if (pvar->units[10] < -900 || dequal(pvar->units[10],ghdr->tri,1e-8)==0) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> conv = pfi->ab2gr[2];<br><span class="Apple-tab-span" style="white-space:pre">        </span> z = conv(pfi->abvals[2],ghdr->level);<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (z>0.99 && z<((gafloat)(pvar->levels)+0.01)) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> iz = (gaint)(z+0.5);<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* check if levels match */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (fabs(z-(gafloat)iz) < 0.01) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> iz = (gaint)(z+0.5);<br><span class="Apple-tab-span" style="white-space:pre">        </span> ioff = pvar->recoff + iz - 1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> gribfill (eoff,it+ioff,joff,nsiz,ghdr,pindx);<br><span class="Apple-tab-span" style="white-space:pre">        </span> flag=1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> i = pfi->vnum + 1; /* Force loop to stop */<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br> }<br> }<br> } <br> else { /* sfc data */<br> if (dequal(pvar->units[0],ghdr->parm,1e-8)==0 && dequal(pvar->units[8],ghdr->ltyp,1e-8)==0) {<br> if ((pvar->units[10] < -900 && <br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[9],ghdr->level,1e-8)==0) ||<br><span class="Apple-tab-span" style="white-space:pre">        </span> (pvar->units[10] > -900 && <br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[9],ghdr->l1,1e-8)==0 && dequal(pvar->units[10],ghdr->l2,1e-8)==0) || <br><span class="Apple-tab-span" style="white-space:pre">        </span> (dequal(pvar->units[10],ghdr->tri,1e-8)==0 && dequal(pvar->units[9],ghdr->level,1e-8)==0)) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> ioff = pvar->recoff;<br><span class="Apple-tab-span" style="white-space:pre">        </span> gribfill (eoff,it+ioff,joff,nsiz,ghdr,pindx);<br><span class="Apple-tab-span" style="white-space:pre">        </span> i = pfi->vnum+1; /* Force loop to stop */<br><span class="Apple-tab-span" style="white-space:pre">        </span> flag=1;<br> }<br> }<br> }<br> pvar++; i++;<br> }<br><br> if (flag && verb) printf("!!!!! MATCH: "); <br> if (!flag && verb) printf("..... NOOOO: "); <br> if (verb) gribpr(ghdr); <br><br> return (flag ? 0 : 1);<br><br>}<br><br><br>/* Routine to fill in values for this record, now that we have found how it matches. <br> We are not handling the time aspect as yet */<br><br>void gribfill (gaint eoff, gaint ioff, gaint joff, gaint nsiz, struct grhdr *ghdr, struct gaindx *pindx) {<br> ioff = nsiz*(eoff+ioff) + joff;<br> *(pindx->intpnt+ioff) = ghdr->dpos;<br> if (ghdr->bmsflg) *(pindx->intpnt+ioff+1) = ghdr->bpos;<br> *(pindx->intpnt+ioff+2) = ghdr->bnum;<br> *(pindx->fltpnt+ioff) = ghdr->dsf;<br> *(pindx->fltpnt+ioff+1) = ghdr->bsf;<br> *(pindx->fltpnt+ioff+2) = ghdr->ref;<br>}<br><br><br>/* Routine to print out fields from the grib header */<br><br>void gribpr(struct grhdr *ghdr) {<br> printf ("% 5i % 10ld % 3i % 1i % 5i % 4i % 4i %-5i % 10i % 10i % 3i ",<br><span class="Apple-tab-span" style="white-space:pre">        </span> irec,fpos,ghdr->id,ghdr->gdsflg,ghdr->bmsflg,ghdr->parm,ghdr->ltyp,<br><span class="Apple-tab-span" style="white-space:pre">        </span> ghdr->level,ghdr->dpos,ghdr->bpos,ghdr->bnum);<br> printf ("btim: %04i%02i%02i%02i:%02i ",ghdr->btim.yr,<br><span class="Apple-tab-span" style="white-space:pre">        </span> ghdr->btim.mo,ghdr->btim.dy,ghdr->btim.hr,ghdr->btim.mn);<br> printf ("tau: % 6i ",ghdr->fcstt);<br> printf ("dtim: %04i%02i%02i%02i:%02i ",ghdr->dtim.yr,<br><span class="Apple-tab-span" style="white-space:pre">        </span> ghdr->dtim.mo,ghdr->dtim.dy,ghdr->dtim.hr,ghdr->dtim.mn);<br> printf("\n");<br>}<br><br><br>/* Routine to write out machine independent grib1 map file */<br><br>gaint wtgmap(void) {<br>gaint i,nb,bcnt,idum;<br>gafloat fdum;<br>unsigned char *map;<br>unsigned char ibmfloat[4];<br><br> /* calculate the size of the version==1 index file */<br> nb = 2 + (4*4) + /* version in byte 2, then 4 ints with number of each data type */<br> pindx->hinum*sizeof(gaint) +<br> pindx->hfnum*sizeof(gaint) +<br> pindx->intnum*sizeof(gaint) +<br> pindx->fltnum*sizeof(gafloat) ;<br><br> /* add additional info */<br> if (g1ver==2) {<br> nb=nb+7; /* base time (+ sec) for compatibility with earlier version 2 maps */<br> nb=nb+8*4; /* grvals for time <-> grid conversion */<br> }<br><br> /* allocate space for the map */<br> map = (unsigned char *)malloc(nb);<br> if (map == NULL) {<br> fprintf(stderr,"grib1map error: memory allocation error creating the map\n");<br> return(60);<br> }<br><br> /* write out the version number and the sizes of the header and index arrays */<br> bcnt=0;<br> gapby(0,map,bcnt,1); bcnt++ ; /* set the first byte to 0 */<br> gapby(g1ver,map,bcnt,1); bcnt++ ; /* set the second byte to the version number */<br> putint(pindx->hinum,map,&bcnt); /* # ints in header */<br> putint(pindx->hfnum,map,&bcnt); /* # floats in header */<br> putint(pindx->intnum,map,&bcnt); /* # index ints */<br> putint(pindx->fltnum,map,&bcnt); /* # index floats */<br><br> if (g1ver==2) {<br> /* write out base time for consistency with earlier version 2 maps */<br> /* base time not needed for version 3 */<br> gapby(btimdd.yr,map,bcnt,2); bcnt+=2 ; /* initial year */<br> gapby(btimdd.mo,map,bcnt,1); bcnt++ ; /* initial month */ <br> gapby(btimdd.dy,map,bcnt,1); bcnt++ ; /* initial day */<br> gapby(btimdd.hr,map,bcnt,1); bcnt++ ; /* initial hour */<br> gapby(btimdd.mn,map,bcnt,1); bcnt++ ; /* initial minute */<br> gapby(0,map,bcnt,1); bcnt++ ; /* initial second */<br> } <br><br> /* write the header */<br> if (pindx->hinum) {<br> for (i=0;i<pindx->hinum;i++) {<br> idum=*(pindx->hipnt+i);<br> putint(idum,map,&bcnt);<br> }<br> }<br><br> /* write the indices */<br> for (i=0;i<pindx->intnum;i++) {<br> idum=*(pindx->intpnt+i);<br> putint(idum,map,&bcnt);<br> }<br> for (i=0;i<pindx->fltnum;i++) {<br> fdum=*(pindx->fltpnt+i);<br> rc=flt2ibm(fdum, ibmfloat); <br> if (rc<0) return(601);<br> memcpy(&map[bcnt],ibmfloat,4); bcnt+=4;<br> }<br><br> if (g1ver==2) {<br> /* write out the factors for converting from grid to absolute time */ <br> /* the conversion vals are not needed for version 3 */<br> for (i=0;i<8;i++) {<br> fdum=*(pfi->grvals[3]+i);<br> rc=flt2ibm(fdum, ibmfloat); <br> if (rc<0) return(601);<br> memcpy(&map[bcnt],ibmfloat,4); bcnt+=4;<br> }<br> } <br><br> /* write to the map file */<br> fwrite(map,1,bcnt,mfile);<br> free(map);<br> return(0);<br><br>}<br><br>/* Routine to dump a 4 byte int into a character stream */<br><br>void putint(gaint dum, unsigned char *buf, gaint *off) {<br> gaint offset;<br><br> offset=*off;<br> if (dum < 0) {<br> dum=-dum;<br> gapby(dum,buf,offset,4);<br> gapbb(1,buf+offset,0,1);<br> } else {<br> gapby(dum,buf,offset,4);<br> }<br> offset+=4;<br> *off=offset;<br><br>}<br><br><br>#if GRIB2<br><br>/* Routine to fill in values for grib2 record, now that we know it matches. */<br>void g2fill (gaint eoff, gaint ioff, gaint ng2elems, g2int iseek, g2int fldnum, <br><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>struct gag2indx *g2indx) {<br> ioff = ng2elems*(eoff+ioff);<br> *(g2indx->g2intpnt+ioff+0) = iseek;<br> *(g2indx->g2intpnt+ioff+1) = fldnum;<br>}<br><br>/* Routine to write out grib2 index file <br><br> g2ver=1 : machine dependent. contains the version number, followed by <br> the array size N, followed by the array of N numbers. <br> All are 4-byte integers (type gaint). <br><br> A test to see if byte-swapping is required<span class="Apple-tab-span" style="white-space:pre">        </span>to read the index file is done<br> in gaddes.c, when the data descriptor file is opened. <br>*/<br><br><br>gaint wtg2map(struct gafile *pfi, struct gag2indx *g2indx) {<br> FILE *mfile;<br> gaint rc;<br><br> /* open the index file */<br> mfile = fopen(pfi->mnam,"wb");<br> if (mfile==NULL) {<br> printf ("error: Unable to open index file: %s\n",pfi->mnam);<br> return(1);<br> } <br><br> printf("grib2map: Writing out the index file \n");<br> /* write the version number */<br> rc = fwrite(&g2indx->version, sizeof(gaint),1,mfile);<br> if (rc!=1) {<br> printf("error: Unable to write version number to index file, rc=%d \n",rc);<br> return(1);<br> } <br> /* write the array size */<br> rc = fwrite(&g2indx->g2intnum,sizeof(gaint),1,mfile);<br> if (rc!=1) {<br> printf("error: Unable to write g2intnum to index file, rc=%d \n",rc);<br> return(1);<br> } <br> /* writhe the array of index values */<br> rc = fwrite(g2indx->g2intpnt,sizeof(gaint),g2indx->g2intnum,mfile);<br> if (rc!=g2indx->g2intnum) {<br> printf("error: Unable to write g2intpnt to index file, rc=%d \n",rc);<br> return(1);<br> } <br> fclose(mfile);<br> return(0);<br><br>}<br><br>/* Checks grid properties for a grib2 field. <br> Returns 0 if ok, 1 if doesn't match descriptor */<br><br>gaint g2grid_check (gribfield *gfld, struct gafile *pfi, gaint r, gaint f) {<br>gaint xsize=0,ysize=0;<br><br> /* Check total number of grid points */<br> if (pfi->grbgrd==255 || pfi->grbgrd<-900) {<br> if (((pfi->ppflag) && (gfld->ngrdpts != pfi->ppisiz * pfi->ppjsiz)) ||<br> ((pfi->ppflag==0) && (gfld->ngrdpts != pfi->dnum[0] * pfi->dnum[1]))) {<br> if (verb) printf ("number of grid points does not match descriptor ");<br> return(1);<br> }<br> } <br> /* Check nx and ny for Lat/Lon, Polar Stereographic, and Lambert Conformal grids */<br> if (pfi->ppflag) {<br> xsize = pfi->ppisiz;<br> ysize = pfi->ppjsiz;<br> } else {<br> xsize = pfi->dnum[0];<br> ysize = pfi->dnum[1];<br> }<br> if (gfld->igdtmpl[7] != -1) {<br> if (gfld->igdtnum==0 || gfld->igdtnum==40 || gfld->igdtnum==20 || gfld->igdtnum==30) {<br> if (gfld->igdtmpl[7] != xsize) {<br><span class="Apple-tab-span" style="white-space:pre">        </span>if (verb) printf ("x dimensions are not equal: nx=%d xsize=%d",gfld->igdtmpl[7],xsize); <br><span class="Apple-tab-span" style="white-space:pre">        </span>return(1);<br> } <br> if (gfld->igdtmpl[8] != ysize) {<br><span class="Apple-tab-span" style="white-space:pre">        </span>if (verb) printf ("y dimensions are not equal: nx=%d xsize=%d",gfld->igdtmpl[8],ysize); <br><span class="Apple-tab-span" style="white-space:pre">        </span>return(1);<br> }<br> }<br> }<br> return(0);<br>}<br><br>/* Checks time metadata in grib2 message. <br> Returns integer value of time axis index if ok, -99 if not */<br>gaint g2time_check (gribfield *gfld, g2int *listsec1, struct gafile *pfi, <br><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span> gaint r, gaint f, gaint tmin, gaint tmax) {<br> struct dt tref,tfld,tvalid;<br> gaint it,tfield;<br> gafloat t;<br><br> /* Get reference time from Section 1 of GRIB message */<br> tref.yr = listsec1[5];<br> tref.mo = listsec1[6];<br> tref.dy = listsec1[7];<br> tref.hr = listsec1[8];<br> tref.mn = listsec1[9];<br> tfield = tfld.yr = tfld.mo = tfld.dy = tfld.hr = tfld.mn = 0; /* initialize */<br> <span class="Apple-tab-span" style="white-space:pre">        </span> <br> if (notau) {<br> /* use reference time as valid time */<br> tvalid.yr = tref.yr;<br> tvalid.mo = tref.mo;<br> tvalid.dy = tref.dy;<br> tvalid.hr = tref.hr;<br> tvalid.mn = tref.mn;<br> }<br> else {<br> /* For fields at a point in time (PDT<8) */<br> if (gfld->ipdtnum < 8) {<br> if (gfld->ipdtmpl[7]== 0) tfld.mn = gfld->ipdtmpl[8]; <br> else if (gfld->ipdtmpl[7]== 13)tfld.mn = gfld->ipdtmpl[8]*15; /*DWD*/<br> else if (gfld->ipdtmpl[7]== 14)tfld.mn = gfld->ipdtmpl[8]*30; /*DWD*/<br> else if (gfld->ipdtmpl[7]== 1) tfld.hr = gfld->ipdtmpl[8];<br> else if (gfld->ipdtmpl[7]== 2) tfld.dy = gfld->ipdtmpl[8];<br> else if (gfld->ipdtmpl[7]== 3) tfld.mo = gfld->ipdtmpl[8];<br> else if (gfld->ipdtmpl[7]== 4) tfld.yr = gfld->ipdtmpl[8];<span class="Apple-tab-span" style="white-space:pre">        </span> <br> else if (gfld->ipdtmpl[7]==10) tfld.hr = gfld->ipdtmpl[8]*3; /* 3Hr incr */<br> else if (gfld->ipdtmpl[7]==11) tfld.hr = gfld->ipdtmpl[8]*6; /* 6Hr incr */ <br> else if (gfld->ipdtmpl[7]==12) tfld.hr = gfld->ipdtmpl[8]*12; /* 2Hr incr */<br> else tfield=-99;<br> if (tfield==-99) {<br><span class="Apple-tab-span" style="white-space:pre">        </span>/* use reference time as valid time */<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.yr = tref.yr;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.mo = tref.mo;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.dy = tref.dy;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.hr = tref.hr;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.mn = tref.mn;<br> }<br> else {<br><span class="Apple-tab-span" style="white-space:pre">        </span>/* add forecast time to reference time to get valid time */<br><span class="Apple-tab-span" style="white-space:pre">        </span>timadd(&tref,&tfld);<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.yr = tfld.yr;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.mo = tfld.mo;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.dy = tfld.dy;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.hr = tfld.hr;<br><span class="Apple-tab-span" style="white-space:pre">        </span>tvalid.mn = tfld.mn;<br> }<br> }<br> /* For fields that are statistically processed over a time interval <br> e.g. averages, accumulations, extremes, et al. (8<=PDT<15) <br> valid time is the end of the overall time interval */<br> else if (gfld->ipdtnum == 8) {<br> tvalid.yr = gfld->ipdtmpl[15];<br> tvalid.mo = gfld->ipdtmpl[16];<br> tvalid.dy = gfld->ipdtmpl[17];<br> tvalid.hr = gfld->ipdtmpl[18];<br> tvalid.mn = gfld->ipdtmpl[19];<br> }<br>/* else if (gfld->ipdtnum == 9) { */<br>/* tvalid.yr = gfld->ipdtmpl[22]; */<br>/* tvalid.mo = gfld->ipdtmpl[23]; */<br>/* tvalid.dy = gfld->ipdtmpl[24]; */<br>/* tvalid.hr = gfld->ipdtmpl[25]; */<br>/* tvalid.mn = gfld->ipdtmpl[26]; */<br>/* } */<br>/* else if (gfld->ipdtnum == 10) { */<br>/* tvalid.yr = gfld->ipdtmpl[16]; */<br>/* tvalid.mo = gfld->ipdtmpl[17]; */<br>/* tvalid.dy = gfld->ipdtmpl[18]; */<br>/* tvalid.hr = gfld->ipdtmpl[19]; */<br>/* tvalid.mn = gfld->ipdtmpl[20]; */<br>/* } */<br> else if (gfld->ipdtnum == 11) {<br> tvalid.yr = gfld->ipdtmpl[18];<br> tvalid.mo = gfld->ipdtmpl[19];<br> tvalid.dy = gfld->ipdtmpl[20];<br> tvalid.hr = gfld->ipdtmpl[21];<br> tvalid.mn = gfld->ipdtmpl[22];<br> }<br> else if (gfld->ipdtnum == 12) {<br> tvalid.yr = gfld->ipdtmpl[17];<br> tvalid.mo = gfld->ipdtmpl[18];<br> tvalid.dy = gfld->ipdtmpl[19];<br> tvalid.hr = gfld->ipdtmpl[20];<br> tvalid.mn = gfld->ipdtmpl[21];<br> }<br> /* else if (gfld->ipdtnum == 13) { */<br> /* tvalid.yr = gfld->ipdtmpl[31]; */<br> /* tvalid.mo = gfld->ipdtmpl[32]; */<br> /* tvalid.dy = gfld->ipdtmpl[33]; */<br> /* tvalid.hr = gfld->ipdtmpl[34]; */<br> /* tvalid.mn = gfld->ipdtmpl[35]; */<br> /* } */<br> /* else if (gfld->ipdtnum == 14) { */<br> /* tvalid.yr = gfld->ipdtmpl[30]; */<br> /* tvalid.mo = gfld->ipdtmpl[31]; */<br> /* tvalid.dy = gfld->ipdtmpl[32]; */<br> /* tvalid.hr = gfld->ipdtmpl[33]; */<br> /* tvalid.mn = gfld->ipdtmpl[34]; */<br> /* } */<br> else {<br> printf("Product Definition Template %ld not handled \n",gfld->ipdtnum);<br> return(-99);<br> } <br> }<br> /* Check if valid time is within grid limits */<br> t = t2gr(pfi->abvals[3],&tvalid);<br> if (t<0.99 || t>((gafloat)(pfi->dnum[3])+0.01)) {<br> if (verb) printf("valid time %4d%02d%02d%02d:%02d (t=%g) is outside grid limits",<br><span class="Apple-tab-span" style="white-space:pre">        </span> tvalid.yr,tvalid.mo,tvalid.dy,tvalid.hr,tvalid.mn,t);<br> return(-99);<br> }<br> /* Check if valid time is an integer */<br> it = (gaint)(t+0.01);<br> if (fabs((gafloat)it - t)>0.01) {<br> if (verb) printf("valid time %4d%02d%02d%02d:%02d (t=%g) has non-integer grid index",<br><span class="Apple-tab-span" style="white-space:pre">        </span> tvalid.yr,tvalid.mo,tvalid.dy,tvalid.hr,tvalid.mn,t);<br> return(-99);<br> }<br> /* Check if valid time matches range of times for this file */<br> if (it<tmin || it>tmax) {<br> if (verb) printf("valid time %4d%02d%02d%02d:%02d (it=%d) is outside file limits (%d-%d)",<br><span class="Apple-tab-span" style="white-space:pre">        </span> tvalid.yr,tvalid.mo,tvalid.dy,tvalid.hr,tvalid.mn,it,tmin,tmax);<br> return(-99);<br> }<br> return (it);<br>}<br><br>/* Loops over variables in descriptor file, looking for match to current grib2 field. <br> If variables match, returns offset, if not, returns -999 */<br><br>gaint g2var_match (gribfield *gfld, struct gafile *pfi, gaint sp) {<br> struct gavar *pvar;<br> gadouble lev1,lev2,z;<br> gadouble (*conv) (gadouble *, gadouble);<br> gaint rc1,rc2,rc3,rc4,rc5;<br> gaint i,ioff,iz;<br><br> /* Get level values from grib field */<br> lev1 = scaled2dbl(gfld->ipdtmpl[10],gfld->ipdtmpl[11]);<br> /* Check if we've got info on 2nd level */<br> if (gfld->ipdtmpl[12] != 255) <br> lev2 = scaled2dbl(gfld->ipdtmpl[13],gfld->ipdtmpl[14]);<br> else <br> lev2 = -999;<br><br> /* See if we match any variables in the descriptor file */<br> pvar = pfi->pvar1;<br> ioff = -999;<br> i = 0;<br> while (i<pfi->vnum) {<br> if (pvar->levels>0) { <br> /* Z-varying data */<br> rc1 = dequal(pvar->units[0],(gadouble)gfld->discipline,1e-8); /* discipline */<br> rc2 = dequal(pvar->units[1],(gadouble)gfld->ipdtmpl[0],1e-8); /* category */<br> rc3 = dequal(pvar->units[2],(gadouble)gfld->ipdtmpl[1],1e-8); /* number */<br> rc4 = dequal(pvar->units[3],(gadouble)sp,1e-8);<span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span> /* SP */<br> rc5 = dequal(pvar->units[8],(gadouble)gfld->ipdtmpl[9],1e-8); /* LTYPE1 */<br> if (rc1==0 && rc2==0 && rc3==0 && rc4==0 && rc5==0) { /* all the above match */<br><span class="Apple-tab-span" style="white-space:pre">        </span><br><span class="Apple-tab-span" style="white-space:pre">        </span>/* get a Z value for level 1 */<br><span class="Apple-tab-span" style="white-space:pre">        </span>conv = pfi->ab2gr[2];<br><span class="Apple-tab-span" style="white-space:pre">        </span>z = conv(pfi->abvals[2],lev1);<br><span class="Apple-tab-span" style="white-space:pre">        </span>if (z>0.99 && z<((gadouble)(pvar->levels)+0.01)) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> iz = (gaint)(z+0.5);<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* make sure Z value for level 1 is an integer */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (fabs(z-(gadouble)iz) < 0.01) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> ioff = pvar->recoff + iz - 1;<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(ioff); <br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span>}<br> }<br> }<br> else { <br> /* non-Z-varying data */<br> rc1 = dequal(pvar->units[0],(gadouble)gfld->discipline,1e-8); /* discipline */<br> rc2 = dequal(pvar->units[1],(gadouble)gfld->ipdtmpl[0],1e-8); /* category */<br> rc3 = dequal(pvar->units[2],(gadouble)gfld->ipdtmpl[1],1e-8); /* number */<br> rc4 = dequal(pvar->units[3],(gadouble)sp,1e-8); /* SP */<br> rc5 = dequal(pvar->units[8],(gadouble)gfld->ipdtmpl[9],1e-8); /* LTYPE1 */<br> if (rc1==0 && rc2==0 && rc3==0 && rc4==0 && rc5==0) { /* all the above match */<br><span class="Apple-tab-span" style="white-space:pre">        </span><br><span class="Apple-tab-span" style="white-space:pre">        </span>/* check if level value(s) match those given in descriptor file */<br><span class="Apple-tab-span" style="white-space:pre">        </span>if (<br><span class="Apple-tab-span" style="white-space:pre">        </span> (pvar->units[9] < -900) /* LVAL not given */<br><span class="Apple-tab-span" style="white-space:pre">        </span> || <br><span class="Apple-tab-span" style="white-space:pre">        </span> (pvar->units[10] < -900 && /* LVAL2 not given */<br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[9],lev1,1e-8)==0) /* and LVAL1 matches */ <br><span class="Apple-tab-span" style="white-space:pre">        </span> || <br><span class="Apple-tab-span" style="white-space:pre">        </span> (pvar->units[10] > -900 && /* LVAL2 is given */<br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[9],lev1,1e-8)==0 && /* and LVAL1 matches */<br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[10],lev2,1e-8)==0) /* and LVAL2 matches */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ||<br><span class="Apple-tab-span" style="white-space:pre">        </span> (pvar->units[10] > -900 && /* LVAL2 is given */<br><span class="Apple-tab-span" style="white-space:pre">        </span> pvar->units[11] > -900 && /* LTYPE2 is given */<br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[9],lev1,1e-8)==0 && /* and LVAL1 matches */<br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[10],lev2,1e-8)==0 && /* and LVAL2 matches */<br><span class="Apple-tab-span" style="white-space:pre">        </span> dequal(pvar->units[11],gfld->ipdtmpl[12],1e-8)==0) /* and LTYPE2 matches */<br><span class="Apple-tab-span" style="white-space:pre">        </span> ) { <br><span class="Apple-tab-span" style="white-space:pre">        </span> ioff = pvar->recoff;<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(ioff);<br><span class="Apple-tab-span" style="white-space:pre">        </span>}<br> }<br> }<br> pvar++; i++;<br> } /* end of loop over variables in descriptor file */<br> return(ioff);<br>}<br><br>/* Loops over ensembles to see if ensemble codes match current grib2 field <br> If size of ensemble dimension is 1, no checks are done, returns e=1.<br> Returns ensemble index e if codes are present and match, -999 otherwise */<br>gaint g2ens_match (gribfield *gfld, struct gafile *pfi) {<br> struct gaens *ens;<br> gaint e;<br> e=1;<br> if (pfi->dnum[4]==1) {<br> e=1;<br> return(e); <br> }<br> else {<br> for (e=1,ens=pfi->ens1; e<=pfi->dnum[4]; e++,ens++) {<br> /* PDT 0 or 8 and no grib codes */<br> if (ens->grbcode[0]==-999 && ens->grbcode[1]==-999 && <br><span class="Apple-tab-span" style="white-space:pre">        </span> (gfld->ipdtnum==0 || gfld->ipdtnum==8)) { <br><span class="Apple-tab-span" style="white-space:pre">        </span>if (verb) printf("pdt=%d ",gfld->ipdtnum);<br><span class="Apple-tab-span" style="white-space:pre">        </span>return(e);<br> }<br> if (ens->grbcode[0]>-900) {<br><span class="Apple-tab-span" style="white-space:pre">        </span>if (ens->grbcode[1]>-900) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* PDT 1 or 11 */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if ((gfld->ipdtnum==1 || gfld->ipdtnum==11) &&<br><span class="Apple-tab-span" style="white-space:pre">        </span> ((ens->grbcode[0] == gfld->ipdtmpl[15]) && <br><span class="Apple-tab-span" style="white-space:pre">        </span> (ens->grbcode[1] == gfld->ipdtmpl[16]))) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("pdt=%d ens=%d,%d ",gfld->ipdtnum,ens->grbcode[0],ens->grbcode[1]);<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(e);<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span>}<br><span class="Apple-tab-span" style="white-space:pre">        </span>else {<br><span class="Apple-tab-span" style="white-space:pre">        </span> /* PDT 2 or 12 */<br><span class="Apple-tab-span" style="white-space:pre">        </span> if ((gfld->ipdtnum==2 || gfld->ipdtnum==12) &&<br><span class="Apple-tab-span" style="white-space:pre">        </span> (ens->grbcode[0] == gfld->ipdtmpl[15])) {<br><span class="Apple-tab-span" style="white-space:pre">        </span> if (verb) printf("pdt=%d ens=%d ",gfld->ipdtnum,ens->grbcode[0]);<br><span class="Apple-tab-span" style="white-space:pre">        </span> return(e);<br><span class="Apple-tab-span" style="white-space:pre">        </span> }<br><span class="Apple-tab-span" style="white-space:pre">        </span>}<br> }<br> }<br> if (verb) {<br> printf("pdt=%d ",gfld->ipdtnum);<br> if (gfld->ipdtnum==1 || gfld->ipdtnum==11) <br><span class="Apple-tab-span" style="white-space:pre">        </span>printf("ens=%d,%d ",gfld->ipdtmpl[15],gfld->ipdtmpl[16]);<br> if (gfld->ipdtnum==2 || gfld->ipdtnum==12) <br><span class="Apple-tab-span" style="white-space:pre">        </span>printf("ens=%d ",gfld->ipdtmpl[15]);<br> }<br> return(-999);<br> }<br>}<br><br>/* Checks ensemble codes, if provided in descriptor file. <br> Returns 0 if ok or not provided, 1 if codes don't match. */<br>gaint g2ens_check (struct gaens *ens, gribfield *gfld) {<br> /* print out ensemble info */<br> if (verb) {<br> printf("pdt=%d ",gfld->ipdtnum);<br> if (gfld->ipdtnum==1 || gfld->ipdtnum==11) <br> printf("ens=%d,%d ",gfld->ipdtmpl[15],gfld->ipdtmpl[16]);<br> if (gfld->ipdtnum==2 || gfld->ipdtnum==12) <br> printf("ens=%d ",gfld->ipdtmpl[15]);<br> }<br> if (ens->grbcode[0]>-900) {<br> if (ens->grbcode[1]>-900) {<br> /* PDT 1 or 11 */<br> if ((gfld->ipdtnum==1 || gfld->ipdtnum==11) &&<br><span class="Apple-tab-span" style="white-space:pre">        </span> ((ens->grbcode[0] == gfld->ipdtmpl[15]) && <br><span class="Apple-tab-span" style="white-space:pre">        </span> (ens->grbcode[1] == gfld->ipdtmpl[16]))) return(0);<br> else return(1);<br> }<br> else {<br> /* PDT 2 or 12 */<br> if ((gfld->ipdtnum==2 || gfld->ipdtnum==12) &&<br><span class="Apple-tab-span" style="white-space:pre">        </span> (ens->grbcode[0] == gfld->ipdtmpl[15])) return(0);<br> else return(1);<br> }<br> }<br> /* PDT 0 or 8 and no grib codes */<br> if (ens->grbcode[0]==-999 && ens->grbcode[1]==-999 &&<br> (gfld->ipdtnum==0 || gfld->ipdtnum==8)) return(0);<br> else return(1);<br>}<br><br>/* Gets the statistical process used to derive a variable.<br> returns -999 for variables "at a point in time" */<br>gaint g2sp (gribfield *gfld) {<br> gaint sp;<br> sp = -999;<br> if (gfld->ipdtnum == 8) sp = gfld->ipdtmpl[23];<br> if (gfld->ipdtnum == 9) sp = gfld->ipdtmpl[30];<br> if (gfld->ipdtnum == 10) sp = gfld->ipdtmpl[24];<br> if (gfld->ipdtnum == 11) sp = gfld->ipdtmpl[26];<br> if (gfld->ipdtnum == 12) sp = gfld->ipdtmpl[25];<br> if (sp==255) sp = -999;<br> return(sp);<br>}<br><br>/* prints out relevant info from a grib2 record */<br>void g2prnt (gribfield *gfld, gaint r, g2int f, gaint sp) {<br> /* print record/field number */<br> printf("%d.%ld: ",r,f);<br> /* print level info */<br> if (gfld->ipdtmpl[10]==-127) <br> printf("lev1=%ld ",gfld->ipdtmpl[9]); /* just print the level1 type */<br> else<br> printf("lev1=%ld,%g ",gfld->ipdtmpl[9],scaled2dbl(gfld->ipdtmpl[10],gfld->ipdtmpl[11]));<br><br> if (gfld->ipdtmpl[12]<255) {<br> if (gfld->ipdtmpl[13]==-127) <br> printf("lev1=%ld ",gfld->ipdtmpl[12]); /* just print the level2 type */<br> else<br> printf("lev2=%ld,%g ",gfld->ipdtmpl[12],scaled2dbl(gfld->ipdtmpl[13],gfld->ipdtmpl[14])); <br> }<br> /* print variable info */<br> if (sp==-999)<br> printf("var=%ld,%ld,%ld ",gfld->discipline,gfld->ipdtmpl[0],gfld->ipdtmpl[1]);<br> else<br> printf("var=%ld,%ld,%ld,%d ",gfld->discipline,gfld->ipdtmpl[0], gfld->ipdtmpl[1],sp);<br>}<br><br><br>#endif /* matches #if GRIB2 */<br><br><br><br>_______________________________________________<br>gradsusr mailing list<br><a href="mailto:gradsusr@gradsusr.org">gradsusr@gradsusr.org</a><br>http://gradsusr.org/mailman/listinfo/gradsusr<br></blockquote></div><br><div apple-content-edited="true"> <span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div>--</div><div>Jennifer M. Adams</div><div>IGES/COLA</div><div>4041 Powder Mill Road, Suite 302</div><div>Calverton, MD 20705</div><div><a href="mailto:jma@cola.iges.org">jma@cola.iges.org</a></div><div><br class="khtml-block-placeholder"></div><br class="Apple-interchange-newline"></span></span></div></span> </div><br></div></body></html>