@@ -555,6 +555,9 @@ def __init__(self, polygons, dh, name='cartesian2d', mask=None):
555555 self .xs = xs
556556 self .ys = ys
557557
558+ def __eq__ (self , other ):
559+ return self .to_dict () == other .to_dict ()
560+
558561 @property
559562 def num_nodes (self ):
560563 """ Number of polygons in region """
@@ -650,13 +653,36 @@ def to_dict(self):
650653 adict = {
651654 'name' : str (self .name ),
652655 'dh' : float (self .dh ),
653- 'polygons' : [{'lat' : float (poly .origin [1 ]), 'lon' : float (poly .origin [0 ])} for poly in self .polygons ]
656+ 'polygons' : [{'lat' : float (poly .origin [1 ]), 'lon' : float (poly .origin [0 ])} for poly in self .polygons ],
657+ 'class_id' : self .__class__ .__name__
654658 }
655659 return adict
656660
657661 @classmethod
658662 def from_dict (cls , adict ):
659- raise NotImplementedError ("Todo!" )
663+ """ Creates a region object from a dictionary """
664+ origins = adict .get ('polygons' , None )
665+ dh = adict .get ('dh' , None )
666+ magnitudes = adict .get ('magnitudes' , None )
667+ name = adict .get ('name' , 'CartesianGrid2D' )
668+
669+ if origins is None :
670+ raise AttributeError ("cannot create region object without origins" )
671+ if dh is None :
672+ raise AttributeError ("cannot create region without dh" )
673+ if origins is not None :
674+ try :
675+ origins = numpy .array ([[adict ['lon' ], adict ['lat' ]] for adict in origins ])
676+ except :
677+ raise TypeError ('origins must be numpy array like.' )
678+ if magnitudes is not None :
679+ try :
680+ magnitudes = numpy .array (magnitudes )
681+ except :
682+ raise TypeError ('magnitudes must be numpy array like.' )
683+
684+ out = cls .from_origins (origins , dh = dh , magnitudes = magnitudes , name = name )
685+ return out
660686
661687 @classmethod
662688 def from_origins (cls , origins , dh = None , magnitudes = None , name = None ):
0 commit comments