def testSanity(self): """cigaxg(cxgaig(n))==n for all n""" for name, proj, dims, xg, ig in self.knownValues: xgout = Fstdc.cigaxg(proj, ig[0], ig[1], ig[2], ig[3]) igout = Fstdc.cxgaig(proj, xgout[0], xgout[1], xgout[2], xgout[3]) self.assertEqual(igout, ig, name + igout.__repr__() + xgout.__repr__())
def test_Fstdc_ezgetlalo_KnownValues2(self): """Fstdc_ezgetlalo corners should give known result with known input""" (ni,nj) = self.la.shape grtyp='L' grref='L' (ig1,ig2,ig3,ig4) = Fstdc.cxgaig(grtyp,-69.5,180.0,0.5,0.5) hasAxes = 0 doCorners = 1 (i0,j0) = (0,0) (la2,lo2,cla2,clo2) = Fstdc.ezgetlalo((ni,nj),grtyp,(grref,ig1,ig2,ig3,ig4),(None,None),hasAxes,(i0,j0),doCorners) if numpy.any(self.la!=la2): print("\nLAT Expected:\n",self.la) print("LAT Got:\n",la2) if numpy.any(self.lo!=lo2): print("\nLON Expected:\n",self.lo) print("LON Got:\n",lo2) self.assertFalse(numpy.any(self.la!=la2)) self.assertFalse(numpy.any(self.lo!=lo2)) for ic in range(0,4): if numpy.any(self.cla[ic,...]!=cla2[ic,...]): print("\n",ic,'cla') print("LAT Expected:\n",self.cla[ic,...]) print("LAT Got:\n",cla2[ic,...]) self.assertFalse(numpy.any(self.cla[ic,...]!=cla2[ic,...])) if numpy.any(self.clo[ic,...]!=clo2[ic,...]): print("\n",ic,'clo') print("\nLON Expected:\n",self.clo[ic,...]) print("LON Got:\n",clo2[ic,...]) self.assertFalse(numpy.any(self.clo[ic,...]!=clo2[ic,...]))
def test_Fstdc_ezgetlalo_KnownValues2(self): """Fstdc_ezgetlalo corners should give known result with known input""" (ni,nj) = self.la.shape grtyp='L' grref='L' (ig1,ig2,ig3,ig4) = Fstdc.cxgaig(grtyp,-69.5,180.0,0.5,0.5) hasAxes = 0 doCorners = 1 (i0,j0) = (0,0) (la2,lo2,cla2,clo2) = Fstdc.ezgetlalo((ni,nj),grtyp,(grref,ig1,ig2,ig3,ig4),(None,None),hasAxes,(i0,j0),doCorners) if numpy.any(self.la!=la2): print "\nLAT Expected:\n",self.la print "LAT Got:\n",la2 if numpy.any(self.lo!=lo2): print "\nLON Expected:\n",self.lo print "LON Got:\n",lo2 self.assertFalse(numpy.any(self.la!=la2)) self.assertFalse(numpy.any(self.lo!=lo2)) for ic in range(0,4): if numpy.any(self.cla[ic,...]!=cla2[ic,...]): print "\n",ic,'cla' print "LAT Expected:\n",self.cla[ic,...] print "LAT Got:\n",cla2[ic,...] self.assertFalse(numpy.any(self.cla[ic,...]!=cla2[ic,...])) if numpy.any(self.clo[ic,...]!=clo2[ic,...]): print "\n",ic,'clo' print "\nLON Expected:\n",self.clo[ic,...] print "LON Got:\n",clo2[ic,...] self.assertFalse(numpy.any(self.clo[ic,...]!=clo2[ic,...]))
def testLevels_to_ip1KnownValues(self): """level_to_ip1 should give known result with known input""" for lvlnew,lvlold,ipnew,ipold,kind in self.knownValues: (ipnew2,ipold2) = Fstdc.level_to_ip1([lvlnew],kind)[0] self.assertEqual(ipnew2,ipnew) (ipnew2,ipold2) = Fstdc.level_to_ip1([lvlold],kind)[0] self.assertEqual(ipold2,ipold)
def test_Fstdc_ezgetlalo_KnownValues2(self): """Fstdc_ezgetlalo corners should give known result with known input""" (ni, nj) = self.la.shape grtyp = "L" grref = "L" (ig1, ig2, ig3, ig4) = Fstdc.cxgaig(grtyp, -89.5, 180.0, 0.5, 0.5) hasAxes = 0 doCorners = 1 (i0, j0) = (0, 0) (la2, lo2, cla2, clo2) = Fstdc.ezgetlalo( (ni, nj), grtyp, (grref, ig1, ig2, ig3, ig4), (None, None), hasAxes, (i0, j0), doCorners ) if numpy.any(self.la != la2): print self.la print la2 if numpy.any(self.lo != lo2): print self.lo print lo2 self.assertFalse(numpy.any(self.la != la2)) self.assertFalse(numpy.any(self.lo != lo2)) for ic in range(0, 4): if numpy.any(self.cla[ic, ...] != cla2[ic, ...]): print ic, "cla" print self.cla[ic, ...] print cla2[ic, ...] self.assertFalse(numpy.any(self.cla[ic, ...] != cla2[ic, ...])) if numpy.any(self.clo[ic, ...] != clo2[ic, ...]): print ic, "clo" print self.clo[ic, ...] print clo2[ic, ...] self.assertFalse(numpy.any(self.clo[ic, ...] != clo2[ic, ...]))
def testSanity(self): """EncodeIp(DecodeIp(n))==n for all n""" for rp1v1, rp1v2, rp1k, rp2v1, rp2v2, rp2k, ip1, ip2, ip3 in self.knownValues: ((rp1v1b, rp1v2b, rp1kb), (rp2v1b, rp2v2b, rp2kb), (rp3v1b, rp3v2b, rp3kb)) = Fstdc.DecodeIp((ip1, ip2, ip3)) (ip1b, ip2b, ip3b) = Fstdc.EncodeIp([(rp1v1b, rp1v2b, rp1kb), (rp2v1b, rp2v2b, rp2kb), (rp3v1b, rp3v2b, rp3kb)]) self.assertEqual((ip1b, ip2b, ip3b), (ip1, ip2, ip3))
def testSanity(self): """levels_to_ip1(ip1_to_levels(n))==n for all n""" for lvlnew,lvlold,ipnew,ipold,kind in self.knownValues: (lvl2,kind2) = Fstdc.ip1_to_level([ipnew])[0] (ipnew2,ipold2) = Fstdc.level_to_ip1([lvl2],kind2)[0] self.assertEqual(ipnew2,ipnew) (lvl2,kind2) = Fstdc.ip1_to_level([ipold])[0] (ipnew2,ipold2) = Fstdc.level_to_ip1([lvl2],kind2)[0] self.assertEqual(ipold2,ipold)
def testip1_to_levelsKnownValues(self): """ip1_to_level should give known result with known input""" for lvlnew,lvlold,ipnew,ipold,kind in self.knownValues: (lvl2,kind2) = Fstdc.ip1_to_level([ipnew])[0] self.assertEqual(kind2,kind) self.assertAlmostEqual(lvlnew,lvl2,6) (lvl2,kind2) = Fstdc.ip1_to_level([ipold])[0] self.assertEqual(kind2,kind) self.assertAlmostEqual(lvlold,lvl2,6)
def gridL(self,dlalo=0.5,nij=10): """provide grid and rec values for other tests""" grtyp='L' grref=grtyp la0 = 0.-dlalo*(nij/2.) lo0 = 180.-dlalo*(nij/2.) ig14 = (ig1,ig2,ig3,ig4) = Fstdc.cxgaig(grtyp,la0,lo0,dlalo,dlalo) axes = (None,None) hasAxes = 0 ij0 = (1,1) doCorners = 0 (la,lo) = Fstdc.ezgetlalo((nij,nij),grtyp,(grref,ig1,ig2,ig3,ig4),axes,hasAxes,ij0,doCorners) return (grtyp,ig14,(nij,nij),la,lo)
def levels_to_ip1(levels, kind): """Encode level value into ip1 for the specified kind ip1_list = levels_to_ip1(level_list, kind) @param level_list list of level values [units depending on kind] @param kind type of levels [units] to be encoded kind = 0: levels are in height [m] (metres) with respect to sea level kind = 1: levels are in sigma [sg] (0.0 -> 1.0) kind = 2: levels are in pressure [mb] (millibars) kind = 3: levels are in arbitrary code Looks like the following are not suppored yet in the fortran func convip, because they depend upon the local topography kind = 4: levels are in height [M] (metres) with respect to ground level kind = 5: levels are in hybrid coordinates [hy] kind = 6: levels are in theta [th] @return list of encoded level values-tuple ((ip1new, ip1old), ...) @exception TypeError if level_list is not a tuple or list @exception ValueError if kind is not an int in range of allowed kind Example of use (and doctest tests): >>> levels_to_ip1([0., 13.5, 1500., 5525., 12750.], 0) [(15728640, 12001), (8523608, 12004), (6441456, 12301), (6843956, 13106), (5370380, 14551)] >>> levels_to_ip1([0., 0.1, .02, 0.00678, 0.000003], 1) [(32505856, 2000), (27362976, 3000), (28511552, 2200), (30038128, 2068), (32805856, 2000)] >>> levels_to_ip1([1024., 850., 650., 500., 10., 2., 0.3], 2) [(39948288, 1024), (41744464, 850), (41544464, 650), (41394464, 500), (42043040, 10), (43191616, 1840), (44340192, 1660)] """ if not isinstance(levels, (list, tuple)): raise ValueError( 'levels_to_ip1: levels should be a list or a tuple; ' + repr(levels)) if not isinstance(kind, _integer_types): raise TypeError( 'levels_to_ip1: kind should be an int in range [0, 3]; ' + repr(kind)) elif not kind in (0, 1, 2, 3): #(0, 1, 2, 3, 4, 5, 6): raise ValueError( 'levels_to_ip1: kind should be an int in range [0, 3]; ' + repr(kind)) if isinstance(levels, tuple): ip1_list = Fstdc.level_to_ip1(list(levels), kind) else: ip1_list = Fstdc.level_to_ip1(levels, kind) if not ip1_list: raise TypeError( 'levels_to_ip1: wrong args type; levels_to_ip1(levels, kind)') return (ip1_list)
def testEncodeIpKnownErrors(self): """EncodeIp should raise an exeption for Known Errors""" for rp1v1,rp1v2,rp1k,rp2v1,rp2v2,rp2k,ip1,ip2,ip3 in self.knownErrors: try: (ip1b,ip2b,ip3b) = Fstdc.EncodeIp([(rp1v1,rp1v2,rp1k),(rp2v1,rp2v2,rp2k),(0.,0.,Fstdc.KIND_ARBITRARY)]) self.assertEqual((ip1b,ip2b,ip3b),("Except Fstdc.error",)) except Fstdc.error: self.assertEqual("Except Fstdc.error","Except Fstdc.error")
def testEncodeIpKnownValues(self): """EncodeIp should give known result with known input""" for rp1v1, rp1v2, rp1k, rp2v1, rp2v2, rp2k, ip1, ip2, ip3 in self.knownValues: (ip1b, ip2b, ip3b) = Fstdc.EncodeIp([(rp1v1, rp1v2, rp1k), (rp2v1, rp2v2, rp2k), (0., 0., Fstdc.KIND_ARBITRARY) ]) self.assertEqual((ip1b, ip2b, ip3b), (ip1, ip2, ip3))
def testCigaxgKnownValues(self): """Cigaxg should give known result with known input""" for name,proj,dims,xg,ig in self.knownValues: xgout = Fstdc.cigaxg(proj,ig[0],ig[1],ig[2],ig[3]) self.assertAlmostEqual(xgout[0],xg[0],1,name+'[0]'+xgout.__repr__()) self.assertAlmostEqual(xgout[1],xg[1],1,name+'[1]'+xgout.__repr__()) self.assertAlmostEqual(xgout[2],xg[2],1,name+'[2]'+xgout.__repr__()) self.assertAlmostEqual(xgout[3],xg[3],1,name+'[3]'+xgout.__repr__())
def test_Fstdc_ezgetlalo_KnownValues(self): """Fstdc_ezgetlalo should give known result with known input""" (ni,nj) = self.la.shape grtyp='L' grref='L' (ig1,ig2,ig3,ig4) = Fstdc.cxgaig(grtyp,-69.5,180.0,0.5,0.5) hasAxes = 0 doCorners = 0 (i0,j0) = (0,0) (la2,lo2) = Fstdc.ezgetlalo((ni,nj),grtyp,(grref,ig1,ig2,ig3,ig4),(None,None),hasAxes,(i0,j0),doCorners) if numpy.any(self.la!=la2): print "\nLAT Expected:\n",self.la print "LAT Got:\n",la2 if numpy.any(self.lo!=lo2): print "\nLON Expected:\n",self.lo print "LON Got:\n",lo2 self.assertFalse(numpy.any(self.la!=la2)) self.assertFalse(numpy.any(self.lo!=lo2))
def cxgaig(grtyp, xg1, xg2=None, xg3=None, xg4=None): """Encode grid definition values into ig1-4 for the specified grid type (ig1, ig2, ig3, ig4) = cxgaig(grtyp, xg1, xg2, xg3, xg4): (ig1, ig2, ig3, ig4) = cxgaig(grtyp, (xg1, xg2, xg3, xg4)): @param grtyp @param xg1 xg1 value (float) or tuple of the form (xg1, xg2, xg3, xg4) @param xg2 xg2 value (float) @param xg3 xg3 value (float) @param xg4 xg4 value (float) @return Tuple of encoded grid desc values (ig1, ig2, ig3, ig4) @exception TypeError if args are of wrong type @exception ValueError if grtyp is not in ('A', 'B', 'E', 'G', 'L', 'N', 'S') Example of use (and doctest tests): >>> cxgaig('N', 200.5, 200.5, 40000.0, 21.0) (2005, 2005, 2100, 400) >>> cxgaig('N', 200.5, 220.5, 40000.0, 260.0) (400, 1000, 29830, 57333) >>> cxgaig('S', 200.5, 200.5, 40000.0, 21.0) (2005, 2005, 2100, 400) >>> cxgaig('L', -89.5, 180.0, 0.5, 0.5) (50, 50, 50, 18000) >>> ig1234 = (-89.5, 180.0, 0.5, 0.5) >>> cxgaig('L', ig1234) (50, 50, 50, 18000) Example of bad use (and doctest tests): >>> cxgaig('L', -89.5, 180 , 0.5, 0.5) Traceback (most recent call last): ... TypeError: cxgaig error: ig1, ig2, ig3, ig4 should be of type real:(-89.5, 180, 0.5, 0.5) >>> cxgaig('I', -89.5, 180.0, 0.5, 0.5) Traceback (most recent call last): ... ValueError: cxgaig error: grtyp ['I'] must be one of ('A', 'B', 'E', 'G', 'L', 'N', 'S') """ validgrtyp = ('A', 'B', 'E', 'G', 'L', 'N', 'S') #I if xg2 == xg3 == xg4 == None and isinstance( xg1, (list, tuple)) and len(xg1) == 4: (xg1, xg2, xg3, xg4) = xg1 if None in (grtyp, xg1, xg2, xg3, xg4): raise TypeError( 'cxgaig error: missing argument, calling is cxgaig(grtyp, xg1, xg2, xg3, xg4)' ) elif not grtyp in validgrtyp: raise ValueError('cxgaig error: grtyp [' + repr(grtyp) + '] must be one of ' + repr(validgrtyp)) elif not (type(xg1) == type(xg2) == type(xg3) == type(xg4) == type(0.)): raise TypeError( 'cxgaig error: ig1, ig2, ig3, ig4 should be of type real:' + repr((xg1, xg2, xg3, xg4))) else: return (Fstdc.cxgaig(grtyp, xg1, xg2, xg3, xg4))
def cigaxg(grtyp, ig1, ig2=None, ig3=None, ig4=None): """Decode grid definition values into xg1-4 for the specified grid type (xg1, xg2, xg3, xg4) = cigaxg(grtyp, ig1, ig2, ig3, ig4): (xg1, xg2, xg3, xg4) = cigaxg(grtyp, (ig1, ig2, ig3, ig4)): @param grtyp @param ig1 ig1 value (int) or tuple of the form (ig1, ig2, ig3, ig4) @param ig2 ig2 value (int) @param ig3 ig3 value (int) @param ig4 ig4 value (int) @return Tuple of decoded grid desc values (xg1, xg2, xg3, xg4) @exception TypeError if args are of wrong type @exception ValueError if grtyp is not in ('A', 'B', 'E', 'G', 'L', 'N', 'S') Example of use (and doctest tests): >>> cigaxg('N', 2005, 2005, 2100, 400) (200.5, 200.5, 40000.0, 21.0) >>> cigaxg('N', 400, 1000, 29830, 57333) (200.5013427734375, 220.49639892578125, 40000.0, 260.0) >>> cigaxg('S', 2005, 2005, 2100, 400) (200.5, 200.5, 40000.0, 21.0) >>> cigaxg('L', 50, 50, 50, 18000) (-89.5, 180.0, 0.5, 0.5) >>> ig1234 = (50, 50, 50, 18000) >>> cigaxg('L', ig1234) (-89.5, 180.0, 0.5, 0.5) Example of bad use (and doctest tests): >>> cigaxg('L', 50, 50, 50, 18000.) Traceback (most recent call last): ... TypeError: cigaxg error: ig1, ig2, ig3, ig4 should be of type int:(50, 50, 50, 18000.0) >>> cigaxg('I', 50, 50, 50, 18000) Traceback (most recent call last): ... ValueError: cigaxg error: grtyp ['I'] must be one of ('A', 'B', 'E', 'G', 'L', 'N', 'S') """ validgrtyp = ('A', 'B', 'E', 'G', 'L', 'N', 'S') #I if ig2 == ig3 == ig4 == None and isinstance( ig1, (list, tuple)) and len(ig1) == 4: (ig1, ig2, ig3, ig4) = ig1 if None in (grtyp, ig1, ig2, ig3, ig4): raise TypeError( 'cigaxg error: missing argument, calling is cigaxg(grtyp, ig1, ig2, ig3, ig4)' ) elif not grtyp in validgrtyp: raise ValueError('cigaxg error: grtyp [' + repr(grtyp) + '] must be one of ' + repr(validgrtyp)) elif not (type(ig1) == type(ig2) == type(ig3) == type(ig4) == type(0)): raise TypeError( 'cigaxg error: ig1, ig2, ig3, ig4 should be of type int:' + repr((ig1, ig2, ig3, ig4))) else: return (Fstdc.cigaxg(grtyp, ig1, ig2, ig3, ig4))
def testDecodeIpKnownValues(self): """DecodeIp should give known result with known input""" for rp1v1,rp1v2,rp1k,rp2v1,rp2v2,rp2k,ip1,ip2,ip3 in self.knownValues: ((rp1v1b,rp1v2b,rp1kb),(rp2v1b,rp2v2b,rp2kb),(rp3v1b,rp3v2b,rp3kb)) = Fstdc.DecodeIp((ip1,ip2,ip3)) self.assertEqual(rp1kb,rp1k) self.assertEqual(rp2kb,rp2k) self.assertAlmostEqual(rp1v1b,rp1v1,6) self.assertAlmostEqual(rp1v2b,rp1v2,6) self.assertAlmostEqual(rp2v1b,rp2v1,6) self.assertAlmostEqual(rp2v2b,rp2v2,6)
def test_Fstdc_ezgetlalo_Dieze_KnownValues(self): """Fstdc_ezgetlalo with #-grid should give known result with known input""" (ni,nj) = self.la.shape grtyp='#' grref='L' (ig1,ig2,ig3,ig4) = Fstdc.cxgaig(grref,0.,0.,1.,1.) xaxis = self.lo[:,0].reshape((self.lo.shape[0],1)).copy('FORTRAN') yaxis = self.la[0,:].reshape((1,self.la.shape[1])).copy('FORTRAN') hasAxes = 1 doCorners = 0 (i0,j0) = (2,2) (la2,lo2) = Fstdc.ezgetlalo((ni-1,nj-1),grtyp,(grref,ig1,ig2,ig3,ig4),(xaxis,yaxis),hasAxes,(i0,j0),doCorners) if numpy.any(self.la[1:,1:]!=la2): print("\nLAT Expected:\n",self.la[1:,1:]) print("LAT Got:\n",la2) if numpy.any(self.lo[1:,1:]!=lo2): print("\nLON Expected:\n",self.lo[1:,1:]) print("LON Got:\n",lo2) self.assertFalse(numpy.any(self.la[1:,1:]!=la2)) self.assertFalse(numpy.any(self.lo[1:,1:]!=lo2))
def test_Fstdc_ezgetlalo_KnownValues(self): """Fstdc_ezgetlalo should give known result with known input""" (ni, nj) = self.la.shape grtyp = 'L' grref = 'L' (ig1, ig2, ig3, ig4) = Fstdc.cxgaig(grtyp, -89.5, 180.0, 0.5, 0.5) hasAxes = 0 doCorners = 0 (i0, j0) = (0, 0) (la2, lo2) = Fstdc.ezgetlalo((ni, nj), grtyp, (grref, ig1, ig2, ig3, ig4), (None, None), hasAxes, (i0, j0), doCorners) if numpy.any(self.la != la2): print self.la print la2 if numpy.any(self.lo != lo2): print self.lo print lo2 self.assertFalse(numpy.any(self.la != la2)) self.assertFalse(numpy.any(self.lo != lo2))
def test_Fstdc_ezgetlalo_Dieze_KnownValues(self): """Fstdc_ezgetlalo with #-grid should give known result with known input""" (ni,nj) = self.la.shape grtyp='#' grref='L' (ig1,ig2,ig3,ig4) = Fstdc.cxgaig(grref,0.,0.,1.,1.) xaxis = self.lo[:,0].reshape((self.lo.shape[0],1)).copy('FORTRAN') yaxis = self.la[0,:].reshape((1,self.la.shape[1])).copy('FORTRAN') hasAxes = 1 doCorners = 0 (i0,j0) = (2,2) (la2,lo2) = Fstdc.ezgetlalo((ni-1,nj-1),grtyp,(grref,ig1,ig2,ig3,ig4),(xaxis,yaxis),hasAxes,(i0,j0),doCorners) if numpy.any(self.la[1:,1:]!=la2): print "\nLAT Expected:\n",self.la[1:,1:] print "LAT Got:\n",la2 if numpy.any(self.lo[1:,1:]!=lo2): print "\nLON Expected:\n",self.lo[1:,1:] print "LON Got:\n",lo2 self.assertFalse(numpy.any(self.la[1:,1:]!=la2)) self.assertFalse(numpy.any(self.lo[1:,1:]!=lo2))
def levels_to_ip1(levels, kind): """Encode level value into ip1 for the specified kind ip1_list = levels_to_ip1(level_list, kind) @param level_list list of level values [units depending on kind] @param kind type of levels [units] to be encoded kind = 0: levels are in height [m] (metres) with respect to sea level kind = 1: levels are in sigma [sg] (0.0 -> 1.0) kind = 2: levels are in pressure [mb] (millibars) kind = 3: levels are in arbitrary code Looks like the following are not suppored yet in the fortran func convip, because they depend upon the local topography kind = 4: levels are in height [M] (metres) with respect to ground level kind = 5: levels are in hybrid coordinates [hy] kind = 6: levels are in theta [th] @return list of encoded level values-tuple ((ip1new, ip1old), ...) @exception TypeError if level_list is not a tuple or list @exception ValueError if kind is not an int in range of allowed kind Example of use (and doctest tests): >>> levels_to_ip1([0., 13.5, 1500., 5525., 12750.], 0) [(15728640, 12001), (8523608, 12004), (6441456, 12301), (6843956, 13106), (5370380, 14551)] >>> levels_to_ip1([0., 0.1, .02, 0.00678, 0.000003], 1) [(32505856, 2000), (27362976, 3000), (28511552, 2200), (30038128, 2068), (32805856, 2000)] >>> levels_to_ip1([1024., 850., 650., 500., 10., 2., 0.3], 2) [(39948288, 1024), (41744464, 850), (41544464, 650), (41394464, 500), (42043040, 10), (43191616, 1840), (44340192, 1660)] """ if not type(levels) in (type(()), type([])): raise ValueError, 'levels_to_ip1: levels should be a list or a tuple; '+repr(levels) if type(kind) <> type(0): raise TypeError, 'levels_to_ip1: kind should be an int in range [0, 3]; '+repr(kind) elif not kind in (0, 1, 2, 3): #(0, 1, 2, 3, 4, 5, 6): raise ValueError, 'levels_to_ip1: kind should be an int in range [0, 3]; '+repr(kind) if type(levels) == type(()): ip1_list = Fstdc.level_to_ip1(list(levels), kind) else: ip1_list = Fstdc.level_to_ip1(levels, kind) if not ip1_list: raise TypeError, 'levels_to_ip1: wrong args type; levels_to_ip1(levels, kind)' return(ip1_list)
def test_Fstdc_ezgetlalo_Z_KnownValues(self): """Fstdc_ezgetlalo with Z grid should give known result with known input""" (ni, nj) = self.la.shape grtyp = 'Z' grref = 'L' (ig1, ig2, ig3, ig4) = Fstdc.cxgaig(grref, 0., 0., 1., 1.) xaxis = self.lo[:, 0].reshape((self.lo.shape[0], 1)).copy('FORTRAN') yaxis = self.la[0, :].reshape((1, self.la.shape[1])).copy('FORTRAN') hasAxes = 1 doCorners = 0 (i0, j0) = (0, 0) (la2, lo2) = Fstdc.ezgetlalo( (ni, nj), grtyp, (grref, ig1, ig2, ig3, ig4), (xaxis, yaxis), hasAxes, (i0, j0), doCorners) if numpy.any(self.la != la2): print self.la print la2 if numpy.any(self.lo != lo2): print self.lo print lo2 self.assertFalse(numpy.any(self.la != la2)) self.assertFalse(numpy.any(self.lo != lo2))
def test_Fstdc_ezgetlalo_Dieze_KnownValues(self): """Fstdc_ezgetlalo with #-grid should give known result with known input""" (ni, nj) = self.la.shape grtyp = "#" grref = "L" (ig1, ig2, ig3, ig4) = Fstdc.cxgaig(grref, 0.0, 0.0, 1.0, 1.0) xaxis = self.lo[:, 0].reshape((self.lo.shape[0], 1)).copy("FORTRAN") yaxis = self.la[0, :].reshape((1, self.la.shape[1])).copy("FORTRAN") hasAxes = 1 doCorners = 0 (i0, j0) = (2, 2) (la2, lo2) = Fstdc.ezgetlalo( (ni - 1, nj - 1), grtyp, (grref, ig1, ig2, ig3, ig4), (xaxis, yaxis), hasAxes, (i0, j0), doCorners ) if numpy.any(self.la[1:, 1:] != la2): print self.la[1:, 1:] print la2 if numpy.any(self.lo[1:, 1:] != lo2): print self.lo[1:, 1:] print lo2 self.assertFalse(numpy.any(self.la[1:, 1:] != la2)) self.assertFalse(numpy.any(self.lo[1:, 1:] != lo2))
def cigaxg(grtyp, ig1, ig2=None, ig3=None, ig4=None): """Decode grid definition values into xg1-4 for the specified grid type (xg1, xg2, xg3, xg4) = cigaxg(grtyp, ig1, ig2, ig3, ig4): (xg1, xg2, xg3, xg4) = cigaxg(grtyp, (ig1, ig2, ig3, ig4)): @param grtyp @param ig1 ig1 value (int) or tuple of the form (ig1, ig2, ig3, ig4) @param ig2 ig2 value (int) @param ig3 ig3 value (int) @param ig4 ig4 value (int) @return Tuple of decoded grid desc values (xg1, xg2, xg3, xg4) @exception TypeError if args are of wrong type @exception ValueError if grtyp is not in ('A', 'B', 'E', 'G', 'L', 'N', 'S') Example of use (and doctest tests): >>> cigaxg('N', 2005, 2005, 2100, 400) (200.5, 200.5, 40000.0, 21.0) >>> cigaxg('N', 400, 1000, 29830, 57333) (200.5013427734375, 220.4964141845703, 40000.0, 260.0) >>> cigaxg('S', 2005, 2005, 2100, 400) (200.5, 200.5, 40000.0, 21.0) >>> cigaxg('L', 50, 50, 50, 18000) (-89.5, 180.0, 0.5, 0.5) >>> ig1234 = (50, 50, 50, 18000) >>> cigaxg('L', ig1234) (-89.5, 180.0, 0.5, 0.5) Example of bad use (and doctest tests): >>> cigaxg('L', 50, 50, 50, 18000.) Traceback (most recent call last): ... TypeError: cigaxg error: ig1, ig2, ig3, ig4 should be of type int:(50, 50, 50, 18000.0) >>> cigaxg('I', 50, 50, 50, 18000) Traceback (most recent call last): ... ValueError: cigaxg error: grtyp ['I'] must be one of ('A', 'B', 'E', 'G', 'L', 'N', 'S') """ validgrtyp = ('A', 'B', 'E', 'G', 'L', 'N', 'S') #I if ig2 == ig3 == ig4 == None and type(ig1) in (type([]), type(())) and len(ig1) == 4: (ig1, ig2, ig3, ig4) = ig1 if None in (grtyp, ig1, ig2, ig3, ig4): raise TypeError, 'cigaxg error: missing argument, calling is cigaxg(grtyp, ig1, ig2, ig3, ig4)' elif not grtyp in validgrtyp: raise ValueError, 'cigaxg error: grtyp ['+repr(grtyp)+'] must be one of '+repr(validgrtyp) elif not (type(ig1) == type(ig2) == type(ig3) == type(ig4) == type(0)): raise TypeError, 'cigaxg error: ig1, ig2, ig3, ig4 should be of type int:'+repr((ig1, ig2, ig3, ig4)) else: return(Fstdc.cigaxg(grtyp, ig1, ig2, ig3, ig4))
def cxgaig(grtyp, xg1, xg2=None, xg3=None, xg4=None): """Encode grid definition values into ig1-4 for the specified grid type (ig1, ig2, ig3, ig4) = cxgaig(grtyp, xg1, xg2, xg3, xg4): (ig1, ig2, ig3, ig4) = cxgaig(grtyp, (xg1, xg2, xg3, xg4)): @param grtyp @param xg1 xg1 value (float) or tuple of the form (xg1, xg2, xg3, xg4) @param xg2 xg2 value (float) @param xg3 xg3 value (float) @param xg4 xg4 value (float) @return Tuple of encoded grid desc values (ig1, ig2, ig3, ig4) @exception TypeError if args are of wrong type @exception ValueError if grtyp is not in ('A', 'B', 'E', 'G', 'L', 'N', 'S') Example of use (and doctest tests): >>> cxgaig('N', 200.5, 200.5, 40000.0, 21.0) (2005, 2005, 2100, 400) >>> cxgaig('N', 200.5, 220.5, 40000.0, 260.0) (400, 1000, 29830, 57333) >>> cxgaig('S', 200.5, 200.5, 40000.0, 21.0) (2005, 2005, 2100, 400) >>> cxgaig('L', -89.5, 180.0, 0.5, 0.5) (50, 50, 50, 18000) >>> ig1234 = (-89.5, 180.0, 0.5, 0.5) >>> cxgaig('L', ig1234) (50, 50, 50, 18000) Example of bad use (and doctest tests): >>> cxgaig('L', -89.5, 180 , 0.5, 0.5) Traceback (most recent call last): ... TypeError: cxgaig error: ig1, ig2, ig3, ig4 should be of type real:(-89.5, 180, 0.5, 0.5) >>> cxgaig('I', -89.5, 180.0, 0.5, 0.5) Traceback (most recent call last): ... ValueError: cxgaig error: grtyp ['I'] must be one of ('A', 'B', 'E', 'G', 'L', 'N', 'S') """ validgrtyp = ('A', 'B', 'E', 'G', 'L', 'N', 'S') #I if xg2 == xg3 == xg4 == None and type(xg1) in (type([]), type(())) and len(xg1) == 4: (xg1, xg2, xg3, xg4) = xg1 if None in (grtyp, xg1, xg2, xg3, xg4): raise TypeError, 'cxgaig error: missing argument, calling is cxgaig(grtyp, xg1, xg2, xg3, xg4)' elif not grtyp in validgrtyp: raise ValueError, 'cxgaig error: grtyp ['+repr(grtyp)+'] must be one of '+repr(validgrtyp) elif not (type(xg1) == type(xg2) == type(xg3) == type(xg4) == type(0.)): raise TypeError, 'cxgaig error: ig1, ig2, ig3, ig4 should be of type real:'+repr((xg1, xg2, xg3, xg4)) else: return(Fstdc.cxgaig(grtyp, xg1, xg2, xg3, xg4))
def ip1_to_levels(ip1list): """Decode ip1 value into (level, kind) levels_list = ip1_to_levels(ip1list) @param ip1list list of ip1 values to decode @return list of decoded level values-tuple ((level, kind), ...) kind = 0: levels are in height [m] (metres) with respect to sea level kind = 1: levels are in sigma [sg] (0.0 -> 1.0) kind = 2: levels are in pressure [mb] (millibars) kind = 3: levels are in arbitrary code kind = 4: levels are in height [M] (metres) with respect to ground level kind = 5: levels are in hybrid coordinates [hy] kind = 6: levels are in theta [th] @exception TypeError if ip1list is not a tuple or list Example of use (and doctest tests): >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([0, 1, 1000, 1199, 1200, 1201, 9999, 12000, 12001, 12002, 13000])] [(0, 2), (10000000, 2), (10000000000, 2), (10000000, 3), (0, 3), (500, 2), (7999000, 1), (10000000, 1), (0, 0), (50000000, 0), (49950000000, 0)] >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([15728640, 12001, 8523608, 12004, 6441456, 12301, 6843956, 13106, 5370380, 14551])] [(0, 0), (0, 0), (135000000, 0), (150000000, 0), (15000000000, 0), (15000000000, 0), (55250000000, 0), (55250000000, 0), (127500000000, 0), (127500000000, 0)] >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([32505856, 2000, 27362976, 3000, 28511552, 2200, 30038128, 2068, 32805856, 2000])] [(0, 1), (0, 1), (1000000, 1), (1000000, 1), (200000, 1), (200000, 1), (67800, 1), (68000, 1), (30, 1), (0, 1)] >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([39948288, 1024, 41744464, 850, 41544464, 650, 41394464, 500, 42043040, 10, 43191616, 1840, 44340192, 1660])] [(10240000000, 2), (10240000000, 2), (8500000000, 2), (8500000000, 2), (6500000000, 2), (6500000000, 2), (5000000000, 2), (5000000000, 2), (100000000, 2), (100000000, 2), (20000000, 2), (20000000, 2), (3000000, 2), (3000000, 2)] """ if not isinstance(ip1list, (list, tuple)): raise TypeError('ip1_to_levels: levels should be a list or a tuple') if isinstance(ip1list, tuple): levels = Fstdc.ip1_to_level(list(ip1list)) else: levels = Fstdc.ip1_to_level(ip1list) if not levels: raise TypeError( 'ip1_to_levels: wrong args type; ip1_to_levels(ip1list)') return (levels)
def ip1_to_levels(ip1list): """Decode ip1 value into (level, kind) levels_list = ip1_to_levels(ip1list) @param ip1list list of ip1 values to decode @return list of decoded level values-tuple ((level, kind), ...) kind = 0: levels are in height [m] (metres) with respect to sea level kind = 1: levels are in sigma [sg] (0.0 -> 1.0) kind = 2: levels are in pressure [mb] (millibars) kind = 3: levels are in arbitrary code kind = 4: levels are in height [M] (metres) with respect to ground level kind = 5: levels are in hybrid coordinates [hy] kind = 6: levels are in theta [th] @exception TypeError if ip1list is not a tuple or list Example of use (and doctest tests): >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([0, 1, 1000, 1199, 1200, 1201, 9999, 12000, 12001, 12002, 13000])] [(0, 2), (10000000, 2), (10000000000, 2), (10000000, 3), (0, 3), (500, 2), (7999000, 1), (10000000, 1), (0, 0), (50000000, 0), (49950000000, 0)] >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([15728640, 12001, 8523608, 12004, 6441456, 12301, 6843956, 13106, 5370380, 14551])] [(0, 0), (0, 0), (135000000, 0), (150000000, 0), (15000000000, 0), (15000000000, 0), (55250000000, 0), (55250000000, 0), (127500000000, 0), (127500000000, 0)] >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([32505856, 2000, 27362976, 3000, 28511552, 2200, 30038128, 2068, 32805856, 2000])] [(0, 1), (0, 1), (1000000, 1), (1000000, 1), (200000, 1), (200000, 1), (67800, 1), (68000, 1), (30, 1), (0, 1)] >>> [(int(x*10.e6+0.5), y) for x, y in ip1_to_levels([39948288, 1024, 41744464, 850, 41544464, 650, 41394464, 500, 42043040, 10, 43191616, 1840, 44340192, 1660])] [(10240000000, 2), (10240000000, 2), (8500000000, 2), (8500000000, 2), (6500000000, 2), (6500000000, 2), (5000000000, 2), (5000000000, 2), (100000000, 2), (100000000, 2), (20000000, 2), (20000000, 2), (3000000, 2), (3000000, 2)] """ if not type(ip1list) in (type(()), type([])): raise TypeError, 'ip1_to_levels: levels should be a list or a tuple' if type(ip1list) == type(()): levels = Fstdc.ip1_to_level(list(ip1list)) else: levels = Fstdc.ip1_to_level(ip1list) if not levels: raise TypeError, 'ip1_to_levels: wrong args type; ip1_to_levels(ip1list)' return(levels)
def test_Fstdc_exinterp_KnownValues(self): """Fstdc_exinterp should give known result with known input""" (g1_grtyp, g1_ig14, g1_shape, la1, lo1) = self.gridL(0.5, 6) (g2_grtyp, g2_ig14, g2_shape, la2, lo2) = self.gridL(0.25, 8) axes = (None, None) ij0 = (1, 1) g1ig14 = list(g1_ig14) g1ig14.insert(0, g1_grtyp) g2ig14 = list(g2_ig14) g2ig14.insert(0, g2_grtyp) la2b = Fstdc.ezinterp(la1, None, g1_shape, g1_grtyp, g1ig14, axes, 0, ij0, g2_shape, g2_grtyp, g2ig14, axes, 0, ij0, 0) if numpy.any(numpy.abs(la2 - la2b) > self.epsilon): print 'g1:' + repr((g1_grtyp, g1_ig14, g1_shape)) print 'g2:' + repr((g2_grtyp, g2_ig14, g2_shape)) print 'la2:', la2 print 'la2b:', la2b self.assertFalse(numpy.any(numpy.abs(la2 - la2b) > self.epsilon))
def test_Fstdc_exinterp_KnownValues(self): """Fstdc_exinterp should give known result with known input""" (g1_grtyp, g1_ig14, g1_shape, la1, lo1) = self.gridL(0.5, 6) (g2_grtyp, g2_ig14, g2_shape, la2, lo2) = self.gridL(0.25, 8) axes = (None, None) ij0 = (1, 1) g1ig14 = list(g1_ig14) g1ig14.insert(0, g1_grtyp) g2ig14 = list(g2_ig14) g2ig14.insert(0, g2_grtyp) la2b = Fstdc.ezinterp( la1, None, g1_shape, g1_grtyp, g1ig14, axes, 0, ij0, g2_shape, g2_grtyp, g2ig14, axes, 0, ij0, 0 ) if numpy.any(numpy.abs(la2 - la2b) > self.epsilon): print "g1:" + repr((g1_grtyp, g1_ig14, g1_shape)) print "g2:" + repr((g2_grtyp, g2_ig14, g2_shape)) print "la2:", la2 print "la2b:", la2b self.assertFalse(numpy.any(numpy.abs(la2 - la2b) > self.epsilon))
#---------------open netcdf and fst file with same file name-------------------------------- # Open netcdf file mois = ['07', '08', '09', '10', '11', '12'] yi = 2018 yf = 2018 for year in range(yi, yf + 1): for moi in mois: filecdf_path = 'NARR_hgt_lc_' + str(year) + '_' + moi + '_3hrs.nc' filecdf_read = Dataset(filecdf_path, 'r') #levels = [1000, 975, 950, 925, 900, 875, 850, 825, 800, 775, 750, 725, 700, 650, 600, 550, 500, 450, 400, 350, 300, 275, 250, 225, 200, 175, 150, 125, 100] # Create fst file name filefst_path = re.sub('nc', 'fst', filecdf_path) iunit = Fstdc.fstouv(0, filefst_path, 'RND+R/W') latitude = filecdf_read.variables['lat'][:] longitude = filecdf_read.variables['lon'][:] var = filecdf_read.variables['hgt'][:] lev = filecdf_read.variables['level'][:] temps = filecdf_read.variables['time'][:] dt = len(temps) dz = len(lev) for t in range(0, dt): for lv in range(0, dz): temp = var[t, lv, :, :] var2 = np.transpose(temp) tmp = np.array(var2).squeeze()
def testSanity(self): """ConvertIp2P(ConvertP2Ip(n))==n for all n""" for lvlnew,lvlold,ipnew,ipold,kind in self.knownValues: (lvl2,kind2) = Fstdc.ConvertIp2P(ipnew,Fstdc.CONVIP_IP2P_DEFAULT) ipnew2 = Fstdc.ConvertP2Ip(lvl2,kind2,Fstdc.CONVIP_STYLE_NEW) self.assertEqual(ipnew2,ipnew)
def testCxgaigKnownValues(self): """Cxgaig should give known result with known input""" for name,proj,dims,xg,ig in self.knownValues: igout = Fstdc.cxgaig(proj,xg[0],xg[1],xg[2],xg[3]) self.assertEqual(igout,ig,name+igout.__repr__())
def testSanity(self): """cigaxg(cxgaig(n))==n for all n""" for name,proj,dims,xg,ig in self.knownValues: xgout = Fstdc.cigaxg(proj,ig[0],ig[1],ig[2],ig[3]) igout = Fstdc.cxgaig(proj,xgout[0],xgout[1],xgout[2],xgout[3]) self.assertEqual(igout,ig,name+igout.__repr__()+xgout.__repr__())
def testConvertIp2PKnownValues(self): """ConvertIp2P should give known result with known input""" for lvlnew,lvlold,ipnew,ipold,kind in self.knownValues: (lvl2,kind2) = Fstdc.ConvertIp2P(ipnew,Fstdc.CONVIP_IP2P_DEFAULT) self.assertEqual(kind2,kind) self.assertAlmostEqual(lvlnew,lvl2,6)
def testConvertP2IpKnownValues(self): """ConvertP2Ip should give known result with known input""" for lvlnew,lvlold,ipnew,ipold,kind in self.knownValues: ipnew2 = Fstdc.ConvertP2Ip(lvlnew,kind,Fstdc.CONVIP_STYLE_NEW) self.assertEqual(ipnew2,ipnew)