def test_isfst_openall_fstnbr(self): """isfst_openall_fstnbr should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET) HOME = os.getenv('HOME') a = rmn.isFST(os.path.join(HOME.strip(), '.profile')) self.assertFalse(a, 'isFST should return false on non FST files') ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') myfile = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/2009042700_000') a = rmn.isFST(myfile) self.assertTrue(a, 'isFST should return true on FST files') funit = rmn.fstopenall(myfile, rmn.FST_RO) self.assertTrue(funit > 0, 'fstopenall should return a valid file unit') nrec = rmn.c_fstnbrv(funit) self.assertEqual(nrec, 1083, ' c_fstnbrv found %d/1083 rec ' % nrec) nrec = rmn.fstnbrv(funit) self.assertEqual(nrec, 1083, ' fstnbrv found %d/1083 rec ' % nrec) rmn.fstcloseall(funit)
def test_1(self): """ Open/Close File Note: The following constants may be used to set the file mode: rmn.FST_RW , rmn.FST_RW_OLD , rmn.FST_RO See also: rpnpy.librmn.fstd98.isFST rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/geophy.fst') if not rmn.isFST(fileName): raise rmn.FSTDError("Not an FSTD file: %s " % fileName) # Open try: fileId = rmn.fstopenall(fileName, rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % fileName) # ... # Close rmn.fstcloseall(fileId)
def test_1(self): """ Open/Close File Note: The following constants may be used to set the file mode: rmn.FST_RW , rmn.FST_RW_OLD , rmn.FST_RO See also: rpnpy.librmn.fstd98.isFST rpnpy.librmn.fstd98.fstopenall rpnpy.librmn.fstd98.fstcloseall rpnpy.librmn.fstd98.FSTDError rpnpy.librmn.RMNError rpnpy.librmn.const """ import os import rpnpy.librmn.all as rmn ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') fileName = os.path.join(ATM_MODEL_DFILES.strip(), 'bcmk/geophy.fst') if not rmn.isFST(fileName): raise rmn.FSTDError("Not an FSTD file: %s " % fileName) # Open try: fileId = rmn.fstopenall(fileName,rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % fileName) # ... # Close rmn.fstcloseall(fileId)
def WriteFld(self, outfile, outrecord): filename = outfile if not os.path.isfile(filename): # Check that the output file exists print 'The output file was not found:', filename quit() # Open the file if not rmn.isFST( filename ): # Check that the output file is a standard format file raise rmn.FSTDError("Not an FSTD file: %s " % filename) try: # Open the standard format file fstID = rmn.fstopenall(filename, rmn.FST_RW_OLD) print(fstID) except: raise rmn.FSTDError("File not found/readable: %s" % filename) # Write out the standard format record varname = outrecord['nomvar'] try: rmn.fstecr(fstID, outrecord) except: raise rmn.FSTDError( "Problem writing the record " + varname + " to file: %s" % filename) # Issue a warning message rmn.fstcloseall(fstID) # Close the standard format file return
def test_isfst_openall_fstnbr(self): """isfst_openall_fstnbr should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) ## rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST,rmn.FSTOP_GET) HOME = os.getenv('HOME') a = rmn.isFST(os.path.join(HOME.strip(),'.profile')) self.assertFalse(a,'isFST should return false on non FST files') ATM_MODEL_DFILES = os.getenv('ATM_MODEL_DFILES') myfile = os.path.join(ATM_MODEL_DFILES.strip(),'bcmk/2009042700_000') a = rmn.isFST(myfile) self.assertTrue(a,'isFST should return true on FST files') funit = rmn.fstopenall(myfile,rmn.FST_RO) self.assertTrue(funit>0,'fstopenall should return a valid file unit') nrec = rmn.c_fstnbrv(funit) self.assertEqual(nrec,1083,' c_fstnbrv found %d/1083 rec ' % nrec) nrec = rmn.fstnbrv(funit) self.assertEqual(nrec,1083,' fstnbrv found %d/1083 rec ' % nrec) rmn.fstcloseall(funit)
def test_fstecr_fstinf_fstluk(self): """fstinf, fstluk should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) (la, lo) = self.create_basefile() #wrote 2 recs in that order: la, lo a = rmn.isFST(self.fname) self.assertTrue(a) funit = rmn.fstopenall(self.fname, rmn.FST_RW) nrec = rmn.c_fstnbrv(funit) keylist = rmn.fstinl(funit) kla = rmn.fstinf(funit, nomvar='LA')['key'] la2prm = rmn.fstprm(kla) #,rank=2) la2 = rmn.fstluk(kla) #,rank=2) klo = rmn.fstinf(funit, nomvar='LO') lo2 = rmn.fstluk(klo) #,rank=2) rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(nrec, 2, ' c_fstnbrv found %d/2 rec ' % nrec) self.assertEqual( len(keylist), 2, 'fstinl found %d/2 rec: %s' % (len(keylist), repr(keylist))) self.assertEqual(la2['nomvar'].strip(), la['nomvar'].strip()) self.assertEqual(lo2['nomvar'].strip(), lo['nomvar'].strip()) self.assertEqual(la2['d'].shape, la['d'].shape) self.assertEqual(lo2['d'].shape, lo['d'].shape) if np.any(np.fabs(la2['d'] - la['d']) > self.epsilon): print('la2:', la2['d']) print('la :', la['d']) print(np.fabs(la2['d'] - la['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon)) if np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon): print('lo2:', lo2['d']) print('lo :', lo['d']) print(np.fabs(lo2['d'] - lo['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))
def ReadFld_etiket(self, infile, field, fhr, etik, fst_rec): filename = infile if not os.path.isfile(filename): # Check that the input file exists print 'The input file was not found:', filename quit() # Open the file if not rmn.isFST( filename ): # Check that the input file is a standard format file raise rmn.FSTDError("Not an FSTD file: %s " % filename) try: # Open the standard format file fstID = rmn.fstopenall(filename, rmn.FST_RO) except: raise rmn.FSTDError("File not found/readable: %s" % filename) # Read in the sample field and metadata varname = field try: var_key = rmn.fstinf(fstID, ip2=fhr, nomvar=varname, etiket=etik)[ 'key'] # Find the 1st record with the matching name except: raise rmn.FSTDError( "Problem searching for record " + varname + " in file: %s" % filename) # Issue a warning message if var_key: # Process the current record try: fst_rec = rmn.fstluk(var_key) # Read the record except: raise rmn.FSTDError("Problem defining grid of file: %s" % filename) rmn.fstcloseall(fstID) # Close the standard format file return fst_rec
def test_fstecr_fstinf_fstluk(self): """fstinf, fstluk should give known result with known input""" rmn.fstopt(rmn.FSTOP_MSGLVL,rmn.FSTOPI_MSG_CATAST) (la,lo) = self.create_basefile() #wrote 2 recs in that order: la, lo a = rmn.isFST(self.fname) self.assertTrue(a) funit = rmn.fstopenall(self.fname,rmn.FST_RW) nrec = rmn.c_fstnbrv(funit) keylist = rmn.fstinl(funit) kla = rmn.fstinf(funit,nomvar='LA')['key'] la2prm = rmn.fstprm(kla)#,rank=2) la2 = rmn.fstluk(kla)#,rank=2) klo = rmn.fstinf(funit,nomvar='LO') lo2 = rmn.fstluk(klo)#,rank=2) rmn.fstcloseall(funit) self.erase_testfile() self.assertEqual(nrec,2,' c_fstnbrv found %d/2 rec ' % nrec) self.assertEqual(len(keylist),2,'fstinl found %d/2 rec: %s' % (len(keylist),repr(keylist))) self.assertEqual(la2['nomvar'].strip(),la['nomvar'].strip()) self.assertEqual(lo2['nomvar'].strip(),lo['nomvar'].strip()) self.assertEqual(la2['d'].shape,la['d'].shape) self.assertEqual(lo2['d'].shape,lo['d'].shape) if np.any(np.fabs(la2['d'] - la['d']) > self.epsilon): print('la2:',la2['d']) print('la :',la['d']) print(np.fabs(la2['d'] - la['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon)) if np.any(np.fabs(lo2['d'] - lo['d']) > self.epsilon): print('lo2:',lo2['d']) print('lo :',lo['d']) print(np.fabs(lo2['d'] - lo['d'])) self.assertFalse(np.any(np.fabs(la2['d'] - la['d']) > self.epsilon))