Example #1
0
 def testfnomfclosKnownValues(self):
     """fnomfclos should give known result with known input"""
     for mypath, itype, iunit in self.knownValues:
         iout = rmn.fnom(mypath, rmn.FST_RO)
         iout2 = rmn.fclos(iout)
         self.assertEqual((iout, iout2), (iunit, 0), mypath + ':' + repr(
             (iout, iout2)) + ' != ' + repr((iunit, 0)))
Example #2
0
 def testfnomfclosKnownValues(self):
     """fnom fclos should give known result with known input"""
     for mypath, itype, iunit in self.knownValues:
         funit = rmn.fnom(self.getFN(mypath), rmn.FST_RO)
         rmn.fclos(funit)
         self.assertTrue(funit > 900 and funit <= 999,
                          mypath+':'+repr(funit)+' != '+repr(iunit))
Example #3
0
 def testfnomfclosKnownValues(self):
     """fnom fclos should give known result with known input"""
     for mypath, itype, iunit in self.knownValues:
         funit = rmn.fnom(self.getFN(mypath), rmn.FST_RO)
         rmn.fclos(funit)
         self.assertTrue(funit > 900 and funit <= 999,
                         mypath + ':' + repr(funit) + ' != ' + repr(iunit))
Example #4
0
 def testmrfopnclsKnownValues(self):
     """mrfopn mrfcls should give known result with known input"""
     for mypath, itype, iunit in self.knownValues:
         rmn.mrfopt(rmn.FSTOP_MSGLVL, rmn.FSTOPS_MSG_FATAL)
         funit  = rmn.fnom(self.getFN(mypath), rmn.FST_RO)
         nbrp   = rmn.mrfopn(funit, rmn.BURP_MODE_READ)
         rmn.mrfcls(funit)
         rmn.fclos(funit)
         self.assertEqual(nbrp, 47544)
Example #5
0
 def testmrfvoiKnownValues(self):
     """mrfvoi should give known result with known input"""
     RPNPY_NOLONGTEST = os.getenv('RPNPY_NOLONGTEST', None)
     if RPNPY_NOLONGTEST:
         return
     for mypath, itype, iunit in self.knownValues:
         funit = rmn.fnom(self.getFN(mypath), rmn.FST_RO)
         rmn.mrfvoi(funit)
         rmn.fclos(funit)
Example #6
0
 def testmrfopnclsKnownValues(self):
     """mrfopn mrfcls should give known result with known input"""
     for mypath, itype, iunit in self.knownValues:
         rmn.mrfopt(rmn.FSTOP_MSGLVL, rmn.BURPOP_MSG_FATAL)
         funit = rmn.fnom(self.getFN(mypath), rmn.FST_RO)
         nbrp = rmn.mrfopn(funit, rmn.BURP_MODE_READ)
         rmn.mrfcls(funit)
         rmn.fclos(funit)
         self.assertEqual(nbrp, 47544)
Example #7
0
 def testmrfnbrKnownValues(self):
     """mrfnbr mrfmxl mrfbfl should give known result with known input"""
     for mypath, itype, iunit in self.knownValues:
         funit   = rmn.fnom(self.getFN(mypath), rmn.FST_RO)
         nbrp    = rmn.mrfnbr(funit)
         maxlen  = rmn.mrfmxl(funit)
         maxlen2 = rmn.mrfbfl(funit)
         ## https://wiki.cmc.ec.gc.ca/wiki/Probl%C3%A8me_avec_les_fonctions_de_manipulation_de_fichiers_BURP_dans_RMNLIB
         maxlen = max(64, maxlen)+10
         rmn.fclos(funit)
         self.assertEqual(nbrp, 47544)
         self.assertEqual(maxlen,  6208+10)
         self.assertEqual(maxlen2, 6208+10)
Example #8
0
 def testmrfnbrKnownValues(self):
     """mrfnbr mrfmxl mrfbfl should give known result with known input"""
     for mypath, itype, iunit in self.knownValues:
         funit = rmn.fnom(self.getFN(mypath), rmn.FST_RO)
         nbrp = rmn.mrfnbr(funit)
         maxlen = rmn.mrfmxl(funit)
         maxlen2 = rmn.mrfbfl(funit)
         ## https://wiki.cmc.ec.gc.ca/wiki/Probl%C3%A8me_avec_les_fonctions_de_manipulation_de_fichiers_BURP_dans_RMNLIB
         maxlen = max(64, maxlen) + 10
         rmn.fclos(funit)
         self.assertEqual(nbrp, 47544)
         self.assertEqual(maxlen, 6208 + 10)
         self.assertEqual(maxlen2, 6208 + 10)
Example #9
0
def fstouv(iunit, filename, options):
    """Interface to fstouv and fnom to open a RPN 2000 Standard File
    iunit = Fstdc.fstouv(iunit, filename, options)
    @param iunit unit number of the file handle, 0 for a new one (int)
    @param filename (string)
    @param option type of file and R/W options (string)
    @return File unit number (int), NULL on error
    @exception TypeError
    @exception Fstdc.error
    """
    try:
        iunit = _rmn.fnom(filename, options, iunit)
        _rmn.fstouv(iunit, options)
        return iunit
    except:
        raise error("Failed to open file: " + filename)
Example #10
0
def fstouv(iunit, filename, options):
    """Interface to fstouv and fnom to open a RPN 2000 Standard File
    iunit = Fstdc.fstouv(iunit, filename, options)
    @param iunit unit number of the file handle, 0 for a new one (int)
    @param filename (string)
    @param option type of file and R/W options (string)
    @return File unit number (int), NULL on error
    @exception TypeError
    @exception Fstdc.error
    """
    try:
        iunit = _rmn.fnom(filename, options, iunit)
        _rmn.fstouv(iunit, options)
        return iunit
    except:
        raise error("Failed to open file: " + filename)
def build_fst(params, y_int, m_int):
    '''
    (dict) -> (int, dict)
    builds the file as per the parameters defined in the params dict
    
    returns the file_id
    '''

    # makes an empty .fst file
    day = time.gmtime()
    temp = ''
    for x in day:
        temp += str(x)
    #new_nc = '/home/ords/aq/alh002/pyscripts/workdir/pv_files/TEST5.fst'
    new_nc = '/home/ords/aq/alh002/pyscripts/workdir/pv_files/SHIFTED_POTVOR_file_{0}_{1}.fst'.format(
        y_int + 2008, m_int + 1)
    tmp = open(new_nc, 'w+')
    tmp.close()
    output_file = new_nc

    try:
        file_id = rmn.fnom(output_file)
        open_fst = rmn.fstouv(file_id, rmn.FST_RW)
        print(file_id, open_fst)

        MACC_grid = rmn.encodeGrid(params)
        print("Grids created.")
        print 'Grid Shape:' + str(MACC_grid['shape'])

        rmn.writeGrid(file_id, MACC_grid)
        toc_record = vgd.vgd_new_pres(const_pressure,
                                      ip1=MACC_grid['ig1'],
                                      ip2=MACC_grid['ig2'])
        vgd.vgd_write(toc_record, file_id)

        return file_id, MACC_grid

    except:
        rmn.fstfrm(file_id)
        rmn.fclos(file_id)
        raise
Example #12
0
def plotFSTs(season=season, spcs=spcs, spcsFiles=spcsFiles, outputName = outputName, saveDir=saveDir):
    # print minimum outputs
    rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST)

    mInds = []
    for m in season:
        mInds += [monthList.index(m)]

    if os.path.exists(saveDir) == False:
        nu.makeDir(saveDir)

    for spcInd, nomvar in enumerate(spcs):
        try:
            filename = os.path.join(saveDir, 'output_file_{0}_{1}.fst'.format(outputName, nomvar))
            print('Creating and saving to {}'.format(filename))
            tmp = open(filename, 'w+'); tmp.close()

            output_file = filename
            file_id = rmn.fnom(output_file)
            open_fst = rmn.fstouv(file_id, rmn.FST_RW)
            open_file = spcsFiles[spcInd]
            print "Parameter: " + nomvar
            seaSpcData = get_var(pyg.open(open_file), nomvar, mInds)
            nc_lnsp = pyg.open(lnsp_file)
            pressures = get_pressures(nc_lnsp, mInds)

            timelen, levlen, latlen, lonlen = seaSpcData.shape
            #NOTE: uncomment the following three lines to prep data for basemap use
            #lonShiftSSData = shift_lon(seaSpcData)
            #vertInterpSSData = vert_interp(pressures, lonShiftSSData)
            #meanSSData = np.mean(vertInterpSSData, axis=0)
            #NOTE: uncommment the following four liness to use for fst plotting
            vertInterpSSData = vert_interp(pressures, seaSpcData)
            meanSSData = np.mean(vertInterpSSData, axis=0)  # temp
            for lvl, ray in enumerate(meanSSData):
                meanSSData[lvl] = np.flipud(ray)
            scaleFac = scaleSpcs[allSpcs.index(nomvar)]
            scaledSSData = meanSSData*scaleFac

            #define grid for this file - note that the MACC grid in the file is
            #defined for lons -180 to 180, but the python defGrid_L can't deal
            #with that and defines the grid from 0 to 360 so will have to reorder
            #the MACC fields a bit, or they end up 180 deg out of phase
            # Also necessary to add one more longitude to wrap around
            dlatlon = 360./lonlen   # this is equal to the resolution of the grid

            params0 = {
                    'grtyp' : 'Z',
                    'grref' : 'L',
                    'nj'    : latlen,
                    'ni'    : lonlen,
                    'lat0'  : -90.,
                    'lon0'  : 0.,
                    'dlat'  : dlatlon,
                    'dlon'  : dlatlon
                    }

            MACC_grid= rmn.encodeGrid(params0)
            print("Grids created.")
            print 'Grid Shape:' + str(MACC_grid['shape'])

            # copies the default record
            new_record = rmn.FST_RDE_META_DEFAULT.copy()
            tic_record = rmn.FST_RDE_META_DEFAULT.copy()
            tac_record = rmn.FST_RDE_META_DEFAULT.copy()

            try:
                rmn.writeGrid(file_id, MACC_grid)

                tac = rmn.fstinl(file_id, nomvar='>>')[0]
                tic = rmn.fstinl(file_id, nomvar='^^')[0]

                tic_record.update(rmn.fstprm(tic))
                tac_record.update(rmn.fstprm(tac))

                tic_record.update({'datyp' : rmn.FST_DATYP_LIST['float']})
                tac_record.update({'datyp' : rmn.FST_DATYP_LIST['float']})

                rmn.fsteff(tic)
                rmn.fsteff(tac)

                tic_record.update({'d': MACC_grid['ay']})
                tac_record.update({'d': MACC_grid['ax']})
                toc_record = vgd.vgd_new_pres(const_pressure, ip1=MACC_grid['ig1'], ip2=MACC_grid['ig2'])

                rmn.fstecr(file_id, tic_record)  # write the dictionary record to the file as a new record
                rmn.fstecr(file_id, tac_record)  # write the dictionary record to the file as a new record
                vgd.vgd_write(toc_record, file_id)

            except:
                raise

            for rp1 in xrange(len(const_pressure)):  # writes a record for every level (as a different ip1)
                try:
                    # converts rp1 into a ip1 with pressure kind
                    ip1 = rmn.convertIp(rmn.CONVIP_ENCODE, const_pressure[rp1], rmn.KIND_PRESSURE)
                    new_record.update(MACC_grid)
                    new_record.update({  # Update with specific meta
                        'nomvar': nomvar,
                        'typvar': 'C',
                        'etiket': 'MACCRean',
                        'ni'    : MACC_grid['ni'],
                        'nj'    : MACC_grid['nj'],
                        'ig1'   : tic_record['ip1'],
                        'ig2'   : tic_record['ip2'],
                        'ig3'   : tic_record['ip3'],
                        'ig4'   : tic_record['ig4'],
                        'dateo' : rmn.newdate(rmn.NEWDATE_PRINT2STAMP, 20120101, 0000000),
                        'deet'  : 0,  # Timestep in sec
                        'ip1'   : ip1
                        })

                    #tmp_nparray = np.asfortranarray(monthly_mean[rp1])
                    tmp = scaledSSData[rp1]
                    tmp = np.transpose(tmp)
                    # data array is structured as tmp = monthly_mean[level] where monthly_mean is [level, lat, lon]
                    new_record.update({'d': tmp.astype(np.float32)}) # Updates with data array in the form (lon x lat)

                    print "Defined a new record with dimensions ({0}, {1})".format(new_record['ni'], new_record['nj'])
                    rmn.fstecr(file_id, new_record)  # write the dictionary record to the file as a new record

                except:
                    #rmn.closeall(file_id)
                    rmn.fstfrm(file_id)
                    rmn.fclos(file_id)
                    raise
            rmn.fstfrm(file_id)
            rmn.fclos(file_id)
            print('{} complete~'.format(filename))
        except:
            rmn.fstfrm(file_id)
            rmn.fclos(file_id)
            raise
    print('Finished plotting all FSTs. ')