Beispiel #1
0
    def _get_key_value(self, key):
        """
        Get the value associated with the given key in the GRIB message.

        Args:

        * key:
            The GRIB key to retrieve the value of.

        Returns the value associated with the requested key in the GRIB
        message.

        """
        vector_keys = (
            "codedValues",
            "pv",
            "satelliteSeries",
            "satelliteNumber",
            "instrumentType",
            "scaleFactorOfCentralWaveNumber",
            "scaledValueOfCentralWaveNumber",
        )
        if key in vector_keys:
            res = gribapi.grib_get_array(self._message_id, key)
        elif key in ("typeOfFirstFixedSurface", "typeOfSecondFixedSurface"):
            # By default these values are returned as unhelpful strings but
            # we can use int representation to compare against instead.
            res = gribapi.grib_get(self._message_id, key, int)
        else:
            res = gribapi.grib_get(self._message_id, key)
        return res
Beispiel #2
0
    def _get_key_value(self, key):
        """
        Get the value associated with the given key in the GRIB message.

        Args:

        * key:
            The GRIB key to retrieve the value of.

        Returns the value associated with the requested key in the GRIB
        message.

        """
        vector_keys = ('codedValues', 'pv', 'satelliteSeries',
                       'satelliteNumber', 'instrumentType',
                       'scaleFactorOfCentralWaveNumber',
                       'scaledValueOfCentralWaveNumber',
                       'longitudes', 'latitudes')
        if key in vector_keys:
            res = gribapi.grib_get_array(self._message_id, key)
        elif key == 'bitmap':
            # The bitmap is stored as contiguous boolean bits, one bit for each
            # data point. GRIBAPI returns these as strings, so it must be
            # type-cast to return an array of ints (0, 1).
            res = gribapi.grib_get_array(self._message_id, key, int)
        elif key in ('typeOfFirstFixedSurface', 'typeOfSecondFixedSurface'):
            # By default these values are returned as unhelpful strings but
            # we can use int representation to compare against instead.
            res = gribapi.grib_get(self._message_id, key, int)
        else:
            res = gribapi.grib_get(self._message_id, key)
        return res
Beispiel #3
0
    def _get_key_value(self, key):
        """
        Get the value associated with the given key in the GRIB message.

        Args:

        * key:
            The GRIB key to retrieve the value of.

        Returns the value associated with the requested key in the GRIB
        message.

        """
        vector_keys = ('codedValues', 'pv', 'satelliteSeries',
                       'satelliteNumber', 'instrumentType',
                       'scaleFactorOfCentralWaveNumber',
                       'scaledValueOfCentralWaveNumber', 'longitudes',
                       'latitudes', 'distinctLatitudes')
        if key in vector_keys:
            res = gribapi.grib_get_array(self._message_id, key)
        elif key == 'bitmap':
            # The bitmap is stored as contiguous boolean bits, one bit for each
            # data point. GRIBAPI returns these as strings, so it must be
            # type-cast to return an array of ints (0, 1).
            res = gribapi.grib_get_array(self._message_id, key, int)
        elif key in ('typeOfFirstFixedSurface', 'typeOfSecondFixedSurface'):
            # By default these values are returned as unhelpful strings but
            # we can use int representation to compare against instead.
            res = gribapi.grib_get(self._message_id, key, int)
        else:
            res = gribapi.grib_get(self._message_id, key)
        return res
Beispiel #4
0
def merge_prev_months(month, fin, fouts, writer):
    while True:
        gid = gribapi.grib_new_from_file(fin)
        if (not gid): break
        date = int(gribapi.grib_get(gid, "dataDate"))
        mon = (date % 10000) / 100
        if (mon == month):
            code = make_grib_tuple(gribapi.grib_get(gid, "param"))
            if (code in accum_codes): continue
            fix_Pa_pressure_levels(gid)
            writer(gid, fouts)
        gribapi.grib_release(gid)
Beispiel #5
0
 def _find(gid, **kwargs):
     for k, v in kwargs.iteritems():
         if not grib_is_defined(gid, k):
             return False
         iscontainer = utils.is_container(v)
         iscallable = utils.is_callable(v)
         if (not iscontainer and not iscallable and grib_get(gid, k) == v) or\
                 (iscontainer and grib_get(gid, k) in v) or \
                 (iscallable and v(grib_get(gid, k))):
             continue
         else:
             return False
     return True
Beispiel #6
0
    def _get_value_or_missing(self, key, use_int=False):
        """
        Return value of header element, or None if value is encoded as missing.
        Implementation of Regulations 92.1.4 and 92.1.5 via ECCodes.

        """
        if gribapi.grib_is_missing(self._message_id, key):
            result = None
        else:
            if use_int:
                result = gribapi.grib_get(self._message_id, key, int)
            else:
                result = gribapi.grib_get(self._message_id, key)
        return result
Beispiel #7
0
    def _get_value_or_missing(self, key, use_int=False):
        """
        Return value of header element, or None if value is encoded as missing.
        Implementation of Regulations 92.1.4 and 92.1.5 via ECCodes.

        """
        if gribapi.grib_is_missing(self._message_id, key):
            result = None
        else:
            if use_int:
                result = gribapi.grib_get(self._message_id, key, int)
            else:
                result = gribapi.grib_get(self._message_id, key)
        return result
Beispiel #8
0
    def select_messages(self, **kwargs):
        self._selected_grbs = self._get_gids(**kwargs)

        if len(self._selected_grbs) > 0:
            self._gid_main_res = self._selected_grbs[0]
            grid = GribGridDetails(self._selected_grbs[0])
            # some cumulated messages come with the message at step=0 as instant, to permit aggregation
            # cumulated rainfall rates could have the step zero instant message as kg/m^2, instead of kg/(m^2*s)
            if len(self._selected_grbs) > 1:
                unit = grib_get(self._selected_grbs[1], 'units')
                type_of_step = grib_get(self._selected_grbs[1], 'stepType')
            else:
                type_of_step = grib_get(self._selected_grbs[0], 'stepType')
                unit = grib_get(self._selected_grbs[0], 'units')
            type_of_level = grib_get(self._selected_grbs[0], 'levelType')

            missing_value = grib_get(self._selected_grbs[0], 'missingValue')
            all_values = {}
            all_values_second_res = {}
            grid2 = None
            input_step = self._step_grib
            for g in self._selected_grbs:

                start_step = grib_get(g, 'startStep')
                end_step = grib_get(g, 'endStep')
                points_meridian = grib_get(g, 'Nj')
                if '{}-{}'.format(start_step,
                                  end_step) == self._change_step_at:
                    # second time resolution
                    input_step = self._step_grib2

                key = Step(start_step, end_step, points_meridian, input_step)

                if points_meridian != grid.num_points_along_meridian and grid.get_2nd_resolution(
                ) is None:
                    # found second resolution messages
                    grid2 = GribGridDetails(g)
                    self._gid_ext_res = g

                values = grib_get_double_array(g, 'values')
                if not grid2:
                    all_values[key] = values
                elif points_meridian != grid.num_points_along_meridian:
                    all_values_second_res[key] = values

            if grid2:
                key_2nd_spatial_res = min(all_values_second_res.keys())
                grid.set_2nd_resolution(grid2, key_2nd_spatial_res)
            return Messages(all_values, missing_value, unit, type_of_level,
                            type_of_step, grid, all_values_second_res)
        # no messages found
        else:
            raise ValueError('No messages in grib file')
Beispiel #9
0
 def get_grib_info(self, select_args):
     _gribs_for_utils = self._get_gids(**select_args)
     if len(_gribs_for_utils) > 0:
         type_of_step = grib_get(_gribs_for_utils[1],
                                 'stepType')  # instant, avg, cumul
         # FIXME this is not correct: GRIB missing values needs bitmap
         self._mv = grib_get_double(_gribs_for_utils[0], 'missingValue')
         start_grib, end_grib, self._step_grib, self._step_grib2, self._change_step_at = self._find_start_end_steps(
             _gribs_for_utils)
         for g in _gribs_for_utils:
             grib_release(g)
         _gribs_for_utils = None
         del _gribs_for_utils
         import gc
         gc.collect()
         info = GRIBInfo(input_step=self._step_grib,
                         input_step2=self._step_grib2,
                         change_step_at=self._change_step_at,
                         type_of_param=type_of_step,
                         start=start_grib,
                         end=end_grib,
                         mv=self._mv)
         return info
     # no messages found
     else:
         raise ValueError('No messages found in grib file')
Beispiel #10
0
 def get(self, key, ktype=None):
     """Get value of a given key as its native or specified type."""
     if self.missing(key):
         raise KeyError("Key is missing from message.")
     if gribapi.grib_get_size(self.gid, key) > 1:
         ret = gribapi.grib_get_array(self.gid, key, ktype)
     else:
         ret = gribapi.grib_get(self.gid, key, ktype)
     return ret
Beispiel #11
0
    def _find_start_end_steps(gribs):
        # return input_steps,
        # change step if a second time resolution is found

        start_steps = [
            grib_get(gribs[i], 'startStep') for i in xrange(len(gribs))
        ]
        end_steps = [grib_get(gribs[i], 'endStep') for i in xrange(len(gribs))]
        start_grib = min(start_steps)
        end_grib = max(end_steps)
        ord_end_steps = sorted(end_steps)
        ord_start_steps = sorted(start_steps)
        step = ord_end_steps[1] - ord_end_steps[0]
        step2 = -1
        change_step_at = ''
        for i in xrange(2, len(ord_end_steps)):
            if step2 == -1 and ord_end_steps[i] - ord_end_steps[i - 1] != step:
                # change of time resolution
                step2 = ord_end_steps[i] - ord_end_steps[i - 1]
                change_step_at = '{}-{}'.format(ord_start_steps[i],
                                                ord_end_steps[i])
        return start_grib, end_grib, step, step2, change_step_at
Beispiel #12
0
def get_grib_metadata(filename, shortname, level=None):
    """Restituisce l'elenco dei grib che fanno match con shortname e level passati"""

    grib_get_or_none = lambda gid, key: gribapi.grib_get(gid, key) if gribapi.grib_is_defined(gid, key) else None
    
    with open(filename) as fp:
        # Itero sui messaggi GRIB
        while True:
            gid = gribapi.grib_new_from_file(fp)
            # when None, there are no more messages
            if gid is None:
                break
            # grib message should have these metadata
            if grib_get_or_none(gid, "cfVarName") != shortname:
                continue
            if level is not None and grib_get_or_none(gid, "level") != level:
                continue

            global units
            units=grib_get_or_none(gid, "units")
            
            # custom scaling options
            global scaling_offset
            global scaling_factor

            if units == 'K':
                scaling_offset=-273.15
                units = '°C'

            # converting total cloud cover for cosmo in okta
            if units == '%' and grib_get_or_none(gid, "cfVarName") == 'tcc':
                scaling_factor = 0.08
                units = 'okta'

            # converting total cloud cover for ifs-ecmwf in okta
            if units == '(0 - 1)' and grib_get_or_none(gid, "cfVarName") == 'tcc':
                scaling_factor = 8.0
                units = 'okta'
                
            # converting precipitations in mm
            if units == 'm' and grib_get_or_none(gid, "cfVarName") != 'hzerocl':
                scaling_factor = 0.001
                units = 'mm'

            if units == 'pa':
                scaling_factor = 0.01
                units = 'hPa'

            yield gid, grib_get_or_none(gid, "endStep")
Beispiel #13
0
def read_lsm(res_num, input_path_oifs, output_path_oifs, exp_name_oifs,
             num_fields):
    '''
    This function reads the oifs input file in grib format and save it into a
    list of numpy arrays.
    '''
    print(' Opening Grib inpute file: %s ' % (input_path_oifs, ))
    input_file_oifs = input_path_oifs + 'ICMGG' + exp_name_oifs + 'INIT'
    gid = [None] * num_fields
    gribfield = [None] * num_fields
    with open(input_file_oifs, 'r+') as f:
        keys = ['N', 'shortName']

        for i in range(num_fields):
            gid[i] = gribapi.grib_new_from_file(f)
            if gid[i] is None:
                break

            for key in keys:
                if not gribapi.grib_is_defined(gid[i], key):
                    raise ValueError("Key '%s' was not defined" % key)
                print('%s=%s' % (key, gribapi.grib_get(gid[i], key)))

            shortName = gribapi.grib_get(gid[i], 'shortName')

            if shortName == 'lsm':
                lsm_id = i
            if shortName == 'slt':
                slt_id = i
            if shortName == 'cl':
                cl_id = i

            nres = gribapi.grib_get(gid[i], 'N')
            gribfield[i] = gribapi.grib_get_values(gid[i])

    return (gribfield, lsm_id, slt_id, cl_id, gid)
Beispiel #14
0
 def get(self, key, type=None):
     """Get value of a given key as its native or specified type."""
     if self.missing(key):
         raise KeyError("Key is missing from message.")
     if key == "values":
         ret = gribapi.grib_get_values(self.gid)
     else:
         try:
             ret = gribapi.grib_get(self.gid, key, type)
         # This is an ugly hack because the library does not differentiate
         # exception types
         except gribapi.GribInternalError, e:
             if e.msg == "Passed array is too small":
                 ret = gribapi.grib_get_array(self.gid, key, type)
             else:
                 raise e
Beispiel #15
0
 def get(self, key, type=None):
     """Get value of a given key as its native or specified type."""
     if self.missing(key):
         raise KeyError("Key is missing from message.")
     if key == "values":
         ret = gribapi.grib_get_values(self.gid)
     else:
         try:
             ret = gribapi.grib_get(self.gid, key, type)
         # This is an ugly hack because the library does not differentiate
         # exception types
         except gribapi.GribInternalError, e:
             if e.msg == "Passed array is too small":
                 ret = gribapi.grib_get_array(self.gid, key, type)
             else:
                 raise e
Beispiel #16
0
def ls_grib(src_path, keys):
    keys = ['name', 'level', 'typeOfLevel'] + keys

    messages = defaultdict(list)
    with open(src_path, 'rb') as fin:
        while True:
            gid = gribapi.grib_new_from_file(fin)
            if gid is None:
                break

            for k in keys:
                try:
                    val = gribapi.grib_get(gid, k)
                    messages[k].append(val)
                except Exception as e:
                    logging.error('Failed to get key (%s). Either key is not available for the message or it is an '
                                  'array type.' % k)

    return messages, keys
Beispiel #17
0
    def get_computed_key(self, key):
        """
        Get the computed value associated with the given key in the GRIB
        message.

        Args:

        * key:
            The GRIB key to retrieve the value of.

        Returns the value associated with the requested key in the GRIB
        message.

        """
        vector_keys = ('longitudes', 'latitudes', 'distinctLatitudes')
        if key in vector_keys:
            res = gribapi.grib_get_array(self._message_id, key)
        else:
            res = gribapi.grib_get(self._message_id, key)
        return res
Beispiel #18
0
    def get_computed_key(self, key):
        """
        Get the computed value associated with the given key in the GRIB
        message.

        Args:

        * key:
            The GRIB key to retrieve the value of.

        Returns the value associated with the requested key in the GRIB
        message.

        """
        vector_keys = ('longitudes', 'latitudes', 'distinctLatitudes')
        if key in vector_keys:
            res = gribapi.grib_get_array(self._message_id, key)
        else:
            res = gribapi.grib_get(self._message_id, key)
        return res
def plotData():

    #
    #  G R I B A P I
    #
    f = open(INPUT)
    gid = gribapi.grib_new_from_file(f)
    values = gribapi.grib_get_values(gid)
    #    print values.dtype
    #    print values.shape

    Ni = gribapi.grib_get(gid, 'Ni')
    Nj = gribapi.grib_get(gid, 'Nj')
    print 'GRIB_API: %d (%dx%d=%d) values found in %s' % (len(values), Nj, Ni,
                                                          Nj * Ni, INPUT)
    latitudeOfFirstGridPointInDegrees = gribapi.grib_get(
        gid, 'latitudeOfFirstGridPointInDegrees')
    longitudeOfFirstGridPointInDegrees = gribapi.grib_get(
        gid, 'longitudeOfFirstGridPointInDegrees')
    latitudeOfLastGridPointInDegrees = gribapi.grib_get(
        gid, 'latitudeOfLastGridPointInDegrees')
    longitudeOfLastGridPointInDegrees = gribapi.grib_get(
        gid, 'longitudeOfLastGridPointInDegrees')
    jDirectionIncrementInDegrees = gribapi.grib_get(
        gid, 'jDirectionIncrementInDegrees')
    iDirectionIncrementInDegrees = gribapi.grib_get(
        gid, 'iDirectionIncrementInDegrees')

    #    for key in ('max','min','average'):
    #        print ' %s=%.10e' % (key,gribapi.grib_get(gid,key))

    gribapi.grib_release(gid)
    f.close()

    #
    #  M A G I C S
    #
    Magics.init()
    Magics.setc("OUTPUT_FORMAT", "ps")
    Magics.setc("OUTPUT_NAME", "py_arrays_from_grib")

    Magics.setr("INPUT_FIELD_INITIAL_LATITUDE",
                latitudeOfFirstGridPointInDegrees)
    Magics.setr("INPUT_FIELD_INITIAL_LONGITUDE",
                longitudeOfFirstGridPointInDegrees)
    Magics.setr("INPUT_FIELD_LATITUDE_STEP", -jDirectionIncrementInDegrees)
    Magics.setr("INPUT_FIELD_LONGITUDE_STEP", iDirectionIncrementInDegrees)
    values2 = numpy.array(values - 273.15)  # convert degree K to C
    #    print values2.dtype
    #    print values2.shape
    val = values2.reshape(Nj, Ni)
    #    print val.dtype
    #    print val.shape
    Magics.set2r("INPUT_FIELD", val)

    #    Magics.setc ("contour_grid_value_plot",         "on")
    #    Magics.setc ("contour_grid_value_plot_type",    "value")
    #    Magics.seti ("contour_grid_value_lat_frequency", 8)
    #    Magics.seti ("contour_grid_value_lon_frequency", 8)
    #    Magics.setr ("contour_grid_value_height",        0.3)
    #    Magics.setc ("contour",          "off")
    Magics.cont()

    Magics.coast()
    Magics.finalize()
Beispiel #20
0
def merge_cur_months(month, fin1, fin2, fouts, writer):
    gidinst, gidcum = -1, -1
    timinst, timcum = -1, -1
    procinst, proccum = True, True
    instmode = False
    while True:
        if (instmode):
            if (procinst):
                if (gidinst != -1): gribapi.grib_release(gidinst)
                gidinst = gribapi.grib_new_from_file(fin1)
            if (not gidinst):
                break
            time = int(gribapi.grib_get(gidinst, "dataTime"))
            if (timinst == -1): timinst = time
            if (time != timinst):
                timinst = time
                procinst = False
                instmode = False
                continue
            else:
                procinst = True
            code = make_grib_tuple(gribapi.grib_get(gidinst, "param"))
            if (code in accum_codes):
                continue
            date = int(gribapi.grib_get(gidinst, "dataDate"))
            mon = (date % 10**4) / 10**2
            if (mon == month):
                fix_Pa_pressure_levels(gidinst)
                writer(gidinst, fouts)
        else:
            if (proccum):
                if (gidinst != -1): gribapi.grib_release(gidcum)
                gidcum = gribapi.grib_new_from_file(fin2)
            if (not gidcum):
                procinst = False
                instmode = True
                continue
            time = int(gribapi.grib_get(gidcum, "dataTime"))
            if (timcum == -1): timcum = time
            if (time != timcum):
                timcum = time
                proccum = False
                instmode = True
                continue
            else:
                proccum = True
            code = make_grib_tuple(gribapi.grib_get(gidcum, "param"))
            if (code not in accum_codes): continue
            if (timeshift > 0):
                date = int(gribapi.grib_get(gidcum, "dataDate"))
                mon = (date % 10**4) / 10**2
                newdate = date
                newtime = time - 100 * timeshift
                if (newtime < 0):
                    curdate = datetime.date(date / 10**4, mon, date % 10**2)
                    prevdate = curdate - datetime.timedelta(days=1)
                    mon = prevdate.month
                    newdate = prevdate.year * 10**4 + mon * 10**2 + prevdate.day
                    newtime = 2400 + newtime
                gribapi.grib_set(gidcum, "dataDate", newdate)
                gribapi.grib_set(gidcum, "dataTime", newtime)
            if (mon == month):
                fix_Pa_pressure_levels(gidcum)
                writer(gidcum, fouts)
Beispiel #21
0
def _is_quasi_regular_grib(grib_message):
    """Detect GRIB 'thinned' a.k.a 'reduced' a.k.a 'quasi-regular' grid."""
    reduced_grids = ("reduced_ll", "reduced_gg")
    return gribapi.grib_get(grib_message, 'gridType') in reduced_grids
Beispiel #22
0
#Setting of the output file name
output = magics.output(output_formats=['png'], output_name='mars-array')

#Setting the projection attributes
europe = magics.mmap(subpage_lower_left_longitude=-20.,
                     subpage_upper_right_longitude=20.00,
                     subpage_upper_right_latitude=30.,
                     subpage_map_projection="cylindrical",
                     subpage_lower_left_latitude=70.)

file = open('z500.grb')

#Getting the first  message from the file
field = grib.grib_new_from_file(file)

nj = grib.grib_get(field, "Nj")
ni = grib.grib_get(field, "Ni")

metadata = {
    "paramId": grib.grib_get(field, "paramId"),
    "units": grib.grib_get(field, "units"),
    "typeOfLevel": grib.grib_get(field, "typeOfLevel"),
    "marsType": grib.grib_get(field, "marsType"),
    "marsClass": grib.grib_get(field, "marsClass"),
    "marsStream": grib.grib_get_string(field, "marsStream"),
    "level": grib.grib_get(field, "level")
}

firstlat = grib.grib_get(field, "latitudeOfFirstGridPointInDegrees")
steplat = -grib.grib_get(field, "jDirectionIncrementInDegrees")
Beispiel #23
0
    def get_phenomena(self):

        phen_list = []
        phenomenon =\
        {
         "id" : "",
         "attribute_count" : "",
         "attributes" :[]
        }

        phen_keys = [
                      "paramId",
                      "cfNameECMF",
                      "cfName",
                      "cfVarName",
                      "units",
                      "nameECMF",
                      "name"
                    ]

        phen_attr =\
        {
         "name" : "",
         "value": ""
        }

        try:
            fd = open(self.file_path)

            while 1:
                gid = gapi.grib_new_from_file(fd)
                if gid is None: break

                phen_attr_list = []
                attr_count = 0
                for key in phen_keys:

                    if not gapi.grib_is_defined(gid, key):
                        continue

                    value = str(gapi.grib_get(gid, key))
                    if len(key) < util.MAX_ATTR_LENGTH \
                       and len(value) < util.MAX_ATTR_LENGTH \
                       and util.is_valid_phen_attr(value):

                        phen_attr["name"] = str(key.strip())
                        phen_attr["value"] = str(unicode(value).strip())

                        if phen_attr not in phen_attr_list:
                            phen_attr_list.append(phen_attr.copy())
                            attr_count = attr_count + 1

                if len(phen_attr_list) > 0:
                    new_phenomenon = phenomenon.copy() 
                    new_phenomenon["attributes"] = phen_attr_list
                    new_phenomenon["attribute_count"] = attr_count

                    if new_phenomenon not in phen_list:
                        phen_list.append(new_phenomenon)


                gapi.grib_release(gid)

            fd.close()

            return phen_list

        except Exception:
            return None
Beispiel #24
0
    def get_metadata_level3(self):

        phen_list = []
        phenomenon =\
        {
         "id" : "",
         "attribute_count" : "",
         "attributes" :[]
        }

        phen_attr =\
        {
         "name" : "",
         "value": ""
        }

        lat_f_l = []
        lon_f_l = []
        lat_l_l = []
        lon_l_l = []
        date_d_l = []
        date_t_l = []

        phen_keys = [
                      "paramId",
                      "cfNameECMF",
                      "cfName",
                      "cfVarName",
                      "units",
                      "nameECMF",
                      "name",
                      "Ni",
                      "Nj",
                      "latitudeOfFirstGridPointInDegrees",
                      "longitudeOfFirstGridPointInDegrees",
                      "latitudeOfLastGridPointInDegrees",
                      "longitudeOfLastGridPointInDegrees",
                      "dataDate",
                      "dataTime"
                    ]
        try:
            fd = open(self.file_path)

            while 1:
                gid = gapi.grib_new_from_file(fd)
                if gid is None: break

                phen_attr_list = []
                attr_count = 0
                for key in phen_keys:

                    if not gapi.grib_is_defined(gid, key):
                        continue

                    value = str(gapi.grib_get(gid, key))

                    #So the file contains many records but all report the
                    #same spatial and temporal information. Only complete distinct records 
                    #will be stored i.e the one that contain the full list of parameter
                    #and are unique. If evety record has got different spatial and temporal
                    #then th eindex must change because currently there is only on geo_shape_field.
                    if key == "latitudeOfFirstGridPointInDegrees":
                        lat_f_l.append(value)
                    elif key == "longitudeOfFirstGridPointInDegrees":
                        lon_f_l.append(value)
                    elif key == "latitudeOfLastGridPointInDegrees":
                        lat_l_l.append(value)
                    elif key =="longitudeOfLastGridPointInDegrees":
                        lon_l_l.append(value)
                    elif key == "dataDate":
                        date_d_l.append(value)
                    elif key == "dataTime":
                        date_t_l.append(value)
                    else:
                        if    len(key) < util.MAX_ATTR_LENGTH \
                          and len(value) < util.MAX_ATTR_LENGTH \
                          and util.is_valid_phen_attr(value):

                            phen_attr["name"] = str(key.strip())
                            phen_attr["value"] = str(unicode(value).strip())

                            if phen_attr not in phen_attr_list:
                                phen_attr_list.append(phen_attr.copy())
                                attr_count = attr_count + 1

                if len(phen_attr_list) > 0:
                    new_phenomenon = phenomenon.copy() 
                    new_phenomenon["attributes"] = phen_attr_list
                    new_phenomenon["attribute_count"] = attr_count

                    if new_phenomenon not in phen_list:
                        phen_list.append(new_phenomenon)

                gapi.grib_release(gid)

            fd.close()

            if len(lat_f_l) > 0 \
               and len(lon_f_l) > 0  \
               and len(lat_l_l) > 0  \
               and len(lon_l_l) > 0  \
               and len(date_d_l) > 0 \
               and len(date_t_l):

                geospatial_dict = {}
                geospatial_dict["type"] = "envelope"

                temporal_dict = {} 
                lat_f = min(lat_f_l)
                lon_f = min(lon_f_l)
                lat_l = max(lat_l_l)
                lon_l = max(lon_l_l)

                date_d = min(date_d_l)
                date_t = min(date_t_l)

                if float(lon_l) > 180:
                    lon_l = (float(lon_l) -180) - 180


                geospatial_dict["coordinates"] = [[round(float(lon_f), 3), round(float(lat_f), 3)], [round(float(lon_l), 3), round(float(lat_l), 3) ]]

                temporal_dict["start_time"] = date_d
                temporal_dict["end_time"] = date_t

                return (phen_list, geospatial_dict, temporal_dict)
            else:
                return (phen_list,)

        except Exception as ex:
            return None
Beispiel #25
0
#####

# OPEN FIRST GRIB FILE, GET MESSAGE HANDLERS AND CLOSE
f = open(filePaths[0], 'r')
gribapi.grib_multi_support_on()
mcount = gribapi.grib_count_in_file(f)  # number of messages in file
gids = [gribapi.grib_new_from_file(f) for i in range(mcount)]
f.close()
#####

# GET NAME AND LEVEL OF EACH MESSAGE
varNames = []
levels = []
for i in range(mcount):
    gid = gids[i]
    varNames.append(gribapi.grib_get(gid, 'shortName'))
    levels.append(gribapi.grib_get(gid, 'level'))
#####

# GET REQUIRED GIDS (AT REQUIRED LEVELS)
gidPRMSL = varNames.index("prmsl") + 1  # Pressure reduced to mean sea level
# Height
gidHGT = np.flipud([
    i + 1 for i in range(len(varNames))
    if (varNames[i] == 'gh' and levels[i] in levsIncl)
])
# Temperature
gidTMP = np.flipud([
    i + 1 for i in range(len(varNames))
    if (varNames[i] == 't' and levels[i] in levsIncl)
])
Beispiel #26
0
def _is_quasi_regular_grib(grib_message):
    """Detect GRIB 'thinned' a.k.a 'reduced' a.k.a 'quasi-regular' grid."""
    reduced_grids = ("reduced_ll", "reduced_gg")
    return gribapi.grib_get(grib_message, 'gridType') in reduced_grids
Beispiel #27
0
def fix_Pa_pressure_levels(gid):
    levtype = gribapi.grib_get(gid, "indicatorOfTypeOfLevel", int)
    if (levtype == 210): gribapi.grib_set(gid, "indicatorOfTypeOfLevel", 99)