Esempio n. 1
0
    def test_12(self):
        """
        Queries: Get Data and Meta

        This example shows how to
        * get the data of all records matching selection creterions
        * then print statistics

        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.fstluk
        rpnpy.librmn.fstd98.fstlir
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

        # Restrict to the minimum the number of messages printed by librmn
        rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)

        # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Get the list of record keys matching nomvar='TT'
            keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12)

            for k in keylist:

                # Get the record meta data
                r = rmn.fstluk(k)
                d = r['d']

                # Decode level info and Print statistics
                rp1 = rmn.DecodeIp(r['ip1'], r['ip2'], r['ip3'])[0]
                level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))

                print(
                    "CB12: %s (level=%s, ip2=%d) mean=%f, std=%f, min=%f, max=%f"
                    % (r['nomvar'], level, r['ip2'], d.mean(), d.std(),
                       d.min(), d.max()))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Esempio n. 2
0
    def test_11(self):
        """
        Queries: Find records, Read meta
        
        This example shows how to
        * get the list of all records matching selection creterions
        * get the metadata and decode it
        * then print some values
        
        See also:
        rpnpy.librmn.fstd98.fstopenall
        rpnpy.librmn.fstd98.fstinl
        rpnpy.librmn.fstd98.fstprm
        rpnpy.librmn.fstd98.DecodeIp
        rpnpy.librmn.fstd98.kindToString
        rpnpy.librmn.base.newdate
        rpnpy.librmn.fstd98.fstcloseall
        rpnpy.librmn.const
        """
        import os, sys
        import rpnpy.librmn.all as rmn

        # Open all RPNStd files in the $ATM_MODEL_DFILES/bcmk/ directory
        ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
        fileName = os.path.join(ATM_MODEL_DFILES, 'bcmk')
        try:
            fileId = rmn.fstopenall(fileName, rmn.FST_RO)
        except:
            sys.stderr.write("Problem opening the file: %s\n" % fileName)
            sys.exit(1)

        try:
            # Get the list of record keys matching nomvar='TT', ip2=12
            keylist = rmn.fstinl(fileId, nomvar='TT', ip2=12)

            for k in keylist:

                # Get the record meta data
                m = rmn.fstprm(k)

                # Decode ip1, and dateo
                (rp1, rp2, rp3) = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])
                level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))
                (yyyymmdd, hhmmsshh) = rmn.newdate(rmn.NEWDATE_STAMP2PRINT,
                                                   m['dateo'])
                dateo = "%8.8d.%8.8d" % (yyyymmdd, hhmmsshh)
                nhours = float(m['npas'] * m['deet']) / 3600.

                print("CB11: %s (%d, %d) level=%s, dateo=%s + %s" %
                      (m['nomvar'], m['ip1'], m['ip2'], level, dateo, nhours))
        except:
            pass
        finally:
            # Close file even if an error occured above
            rmn.fstcloseall(fileId)
Esempio n. 3
0
 def test_11qd(self):
     import os, sys
     import rpnpy.librmn.all as rmn
     ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES').strip()
     fileId = rmn.fstopenall(ATM_MODEL_DFILES + '/bcmk', rmn.FST_RO)
     for k in rmn.fstinl(fileId, nomvar='TT', ip2=12):
         m = rmn.fstprm(k)
         rp1 = rmn.DecodeIp(m['ip1'], m['ip2'], m['ip3'])[0]
         level = "%f %s" % (rp1.v1, rmn.kindToString(rp1.kind))
         dateo = "%8.8d.%8.8d" % rmn.newdate(rmn.NEWDATE_STAMP2PRINT,
                                             m['dateo'])
         print("CB11qd: %s (%d, %d) level=%s, dateo=%s + %s" %
               (m['nomvar'], m['ip1'], m['ip2'], level, dateo,
                float(m['npas'] * m['deet']) / 3600.))
     rmn.fstcloseall(fileId)
Esempio n. 4
0
 def test_DecodeIp(self):
     """DecodeIp should give known result with known input"""
     for pk1, pk2, pk3, ip1, ip2, ip3 in self.ip123knownValues2:
         pkvalues = rmn.DecodeIp(ip1, ip2, ip3)
         self.assertEqual(
             pkvalues[0].kind, pk1.kind,
             "DecodeIp(%d,%d,%d) Got: ip1k=%d expecting=%d : (%f,%f,%d)" %
             (ip1, ip2, ip3, pkvalues[0].kind, pk1.kind, pkvalues[0].v1,
              pkvalues[0].v2, pkvalues[0].kind))
         self.assertEqual(
             pkvalues[1].kind, pk2.kind,
             "DecodeIp(%d,%d,%d) Got: ip2k=%d expecting=%d : (%f,%f,%d)" %
             (ip1, ip2, ip3, pkvalues[1].kind, pk2.kind, pkvalues[1].v1,
              pkvalues[1].v2, pkvalues[1].kind))
         self.assertAlmostEqual(pkvalues[0].v1, pk1.v1, 6)
         self.assertAlmostEqual(pkvalues[1].v1, pk2.v1, 6)
         self.assertAlmostEqual(pkvalues[0].v2, pk1.v2, 6)
         self.assertAlmostEqual(pkvalues[1].v2, pk2.v2, 6)
Esempio n. 5
0
def DecodeIp(ip123):
    """Decoding of ip1+ip2+ip3 files tags triolets into level+times values or interval
        pvalues = Fstdc.DecodeIp([ip1, ip2, ip3])
        @param  [ip1, ip2, ip3], tuple/list of int
        @return pvalues, real decoded level and time values/intervals, units depends on the kind
                pvalues has the format of list/tuple of tuples
                [(rp1.v1, rp1.v2, rp1.kind), (rp2.v1, rp2.v2, rp2.kind), (rp3.v1, rp3.v2, rp3.kind)]
                where v1, v2 are float, kind is an int (named constant KIND_*)
                RP1 will contain a level (or a pair of levels in atmospheric ascending order) in the atmosphere
                RP2 will contain a time (or a pair of times in ascending order)
                RP3.v1 will be the same as RP3.v2 (if RP1 or RP2 contains a pair, RP3 is ignored)
        @exception TypeError
        @exception Fstdc.error
    """
    try:
        (rp1, rp2, rp3) = _rmn.DecodeIp(ip123[0], ip123[1], ip123[2])
        return [(rp1.v1, rp1.v2, rp1.kind), (rp2.v1, rp2.v2, rp2.kind), (rp3.v1, rp3.v2, rp3.kind)]
    except:
        raise error("Proleme decoding ip123 in DecodeIp")
Esempio n. 6
0
def get_model(overpass,
              lonArray,
              latArray,
              fst_dir,
              ctrl_dir=None,
              var='AF',
              threshold=4,
              filestart=00):
    if filestart == 12:
        file_time = (overpass if overpass.minute < 30 else overpass +
                     datetime.timedelta(hours=1)) - datetime.timedelta(
                         hours=12)
        file_path = "{0}/{1}".format(os.path.abspath(fst_dir),
                                     file_time.strftime('%Y%m%d12_0%H'))
    elif filestart == 0:
        file_time = (overpass if overpass.minute < 30 else overpass +
                     datetime.timedelta(hours=1))
        file_path = "{0}/{1}".format(os.path.abspath(fst_dir),
                                     file_time.strftime('%Y%m%d00_0%H'))
    rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST)
    rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_NEAREST)
    fid = rmn.fstopenall(file_path, rmn.FST_RO)
    lonarr = np.array(lonArray, dtype='float32')
    latarr = np.array(latArray, dtype='float32')
    keylist = rmn.fstinl(fid, nomvar=var)
    dir = os.path.abspath(fst_dir)
    ctrl_dir = os.path.abspath(ctrl_dir) if ctrl_dir else None
    # assuming this for now
    ref_lvl = 'sea'
    height = [float('-inf')] * len(lonarr)
    value = [float('-inf')] * len(lonarr)
    points = [{} for _ in lonarr]
    iplist = []
    for key in keylist:
        meta = rmn.fstprm(key)
        iplist.append(
            rmn.DecodeIp(meta['ip1'], meta['ip2'], meta['ip3'])[0].v1)
    sorted_keylist = (x for _, x in sorted(zip(iplist, keylist), reverse=True))
    next(sorted_keylist, None)
    before_val = [float('inf')] * len(lonarr)
    cur_meta, cur_grid, cur_xypos, cur_val = data_from_key(
        next(sorted_keylist), fid, lonarr, latarr)
    if ctrl_dir:
        if filestart == 12:
            ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir),
                                         file_time.strftime('%Y%m%d12_0%H'))
        elif filestart == 0:
            ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir),
                                         file_time.strftime('%Y%m%d00_0%H'))
        ctrl_fid = rmn.fstopenall(ctrl_path, rmn.FST_RO)
        ctrl_keylist = rmn.fstinl(ctrl_fid, nomvar=var)
        sorted_ctrl_keylist = (
            x for _, x in sorted(zip(iplist, ctrl_keylist), reverse=True))
        next(sorted_ctrl_keylist, None)
        _, _, _, ctrl_val = data_from_key(next(sorted_ctrl_keylist), ctrl_fid,
                                          lonarr, latarr)
        cur_val -= ctrl_val
    for progress_ind, after_key in enumerate(sorted_keylist):
        after_meta, after_grid, after_xypos, after_val = data_from_key(
            after_key, fid, lonarr, latarr)
        if ctrl_dir:
            after_ctrl_key = next(sorted_ctrl_keylist)
            _, _, _, after_ctrl_val = data_from_key(after_ctrl_key, ctrl_fid,
                                                    lonarr, latarr)
            after_val -= after_ctrl_val
        for ind, val in enumerate(cur_val):
            if ((val > before_val[ind]) and (val > after_val[ind])
                    and (val >= threshold) and (val > value[ind])):
                try:
                    if int(ind) <= 20:
                        print(
                            'Updating GZ, val: {}, existing val: {}'.format(
                                val, value[ind]), ind, cur_meta['ip1'])
                    gzkey = rmn.fstinf(fid, nomvar='GZ',
                                       ip1=cur_meta['ip1'])['key']
                    gzdata = rmn.fstluk(gzkey)['d']
                    meta = rmn.fstprm(gzkey)
                    meta['iunit'] = fid
                    gz_grid = rmn.ezqkdef(meta)
                    heightList = rmn.gdxysval(gz_grid, cur_xypos['x'],
                                              cur_xypos['y'], gzdata) * 10
                    height[ind] = float(heightList[ind])
                    value[ind] = float(val)
                    #print (height[ind], ind)
                    print(height, value)
                except TypeError:
                    continue
        before_val = cur_val
        cur_meta, cur_grid, cur_xypos, cur_val = after_meta, after_grid, after_xypos, after_val
        print height
    rmn.fstcloseall(fid)
    if ctrl_dir:
        rmn.fstcloseall(ctrl_fid)
    #print(height)
    return height, value