Exemple #1
0
    def spatial_mean(self):
        """ Moyenne spatiale en chaque pas de temps pour les champs modelises et observes """
        import cdutil,  cdms2,  MV2,  numpy
        from vacumm.misc.grid import meshweights

        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=-1), axis=-1)
        # --> equivalent a nanmean(nanmean(sst')) sous matlab
        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=1), axis=1)
        # --> equivalent a nanmean(nanmean(sst)) sous matlab
        #print self.modelspatialmean


        self.model.spa_mean = list() #Initialise une liste
        self.obs.spa_mean = list() #Initialise une liste

        # Estimation de la moyenne a chaque pas de temps
        tps = self.obs.getTime()
        for i,  t in enumerate(tps):
            if self.obs[i, :, :].count()==0:
                #self.obs.spa_mean.append(self.obs.fill_value) # voir pour mettre un masque dans la variable a tracer ... ou pour choisir une autre valeur
                self.obs.spa_mean.append(0)
                self.model.spa_mean.append(0)
            else:
                #modif J.Gatti- pour que moyenne spatiale fontionne avec une grille curvilineaire et poids soient en metres

                lonmod=self.model.getLongitude()
                latmod=self.model.getLatitude()
                # lon et lat doivent etre 2D pour que meshweigths calcule des poids en m2
                if lonmod.rank()==1:
                    lonmod,latmod=numpy.meshgrid(lonmod,latmod)

                wtsmod=meshweights(lonmod, latmod, proj=True)
                # la matrice de poids doit avoir le meme type que la variable moyennee (MV dans notre cas)
                wtsmod = MV2.masked_array(wtsmod, MV2.getmask(self.model[i, :, :]), axes=self.model[i, :, :].getAxisList())

                self.model.spa_mean.append(cdutil.averager(self.model[i, :, :], axis='yx', weights=wtsmod))

                lonobs=self.obs.getLongitude()
                latobs=self.obs.getLatitude()

                if lonobs.rank()==1:
                    lonobs,latobs=numpy.meshgrid(lonobs,latobs)

                wtsobs=meshweights(lonobs, latobs, proj=True)
                wtsobs = MV2.masked_array(wtsobs, MV2.getmask(self.obs[i, :, :]), axes=self.obs[i, :, :].getAxisList())

                self.obs.spa_mean.append(cdutil.averager(self.obs[i, :, :], axis='yx', weights=wtsobs))

                #self.model.spa_mean.append(cdutil.averager(self.model[i, :, :],axis='yx'))
                #self.obs.spa_mean.append(cdutil.averager(self.obs[i, :, :],axis='yx'))
                #fin modif J.Gatti


        # Transformation en variable cdms2
        self.model.spa_mean = cdms2.createVariable(self.model.spa_mean,typecode='f',id='model_spa_mean', attributes=dict(units=self.model.units))
        self.obs.spa_mean = cdms2.createVariable(self.obs.spa_mean,typecode='f',id='obs_spa_mean', attributes=dict(units=self.obs.units))


        # Ajout de l'axe des temps correspondant a self...
        self.model.spa_mean.setAxis(0, tps)
        self.obs.spa_mean.setAxis(0, tps)
Exemple #2
0
    def spatial_mean(self):
        """ Moyenne spatiale en chaque pas de temps pour les champs modelises et observes """
        import cdutil, cdms2, MV2, numpy
        from vacumm.misc.grid import meshweights

        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=-1), axis=-1)
        # --> equivalent a nanmean(nanmean(sst')) sous matlab
        #self.modelspatialmean = MV2.average(MV2.average(self.model, axis=1), axis=1)
        # --> equivalent a nanmean(nanmean(sst)) sous matlab
        #print self.modelspatialmean

        self.model.spa_mean = list()  #Initialise une liste
        self.obs.spa_mean = list()  #Initialise une liste

        # Estimation de la moyenne a chaque pas de temps
        tps = self.obs.getTime()
        for i, t in enumerate(tps):
            if self.obs[i, :, :].count() == 0:
                #self.obs.spa_mean.append(self.obs.fill_value) # voir pour mettre un masque dans la variable a tracer ... ou pour choisir une autre valeur
                self.obs.spa_mean.append(0)
                self.model.spa_mean.append(0)
            else:
                #modif J.Gatti- pour que moyenne spatiale fontionne avec une grille curvilineaire et poids soient en metres

                lonmod = self.model.getLongitude()
                latmod = self.model.getLatitude()
                # lon et lat doivent etre 2D pour que meshweigths calcule des poids en m2
                if lonmod.rank() == 1:
                    lonmod, latmod = numpy.meshgrid(lonmod, latmod)

                wtsmod = meshweights(lonmod, latmod, proj=True)
                # la matrice de poids doit avoir le meme type que la variable moyennee (MV dans notre cas)
                wtsmod = MV2.masked_array(
                    wtsmod,
                    MV2.getmask(self.model[i, :, :]),
                    axes=self.model[i, :, :].getAxisList())

                self.model.spa_mean.append(
                    cdutil.averager(self.model[i, :, :],
                                    axis='yx',
                                    weights=wtsmod))

                lonobs = self.obs.getLongitude()
                latobs = self.obs.getLatitude()

                if lonobs.rank() == 1:
                    lonobs, latobs = numpy.meshgrid(lonobs, latobs)

                wtsobs = meshweights(lonobs, latobs, proj=True)
                wtsobs = MV2.masked_array(wtsobs,
                                          MV2.getmask(self.obs[i, :, :]),
                                          axes=self.obs[i, :, :].getAxisList())

                self.obs.spa_mean.append(
                    cdutil.averager(self.obs[i, :, :],
                                    axis='yx',
                                    weights=wtsobs))

                #self.model.spa_mean.append(cdutil.averager(self.model[i, :, :],axis='yx'))
                #self.obs.spa_mean.append(cdutil.averager(self.obs[i, :, :],axis='yx'))
                #fin modif J.Gatti

        # Transformation en variable cdms2
        self.model.spa_mean = cdms2.createVariable(
            self.model.spa_mean,
            typecode='f',
            id='model_spa_mean',
            attributes=dict(units=self.model.units))
        self.obs.spa_mean = cdms2.createVariable(
            self.obs.spa_mean,
            typecode='f',
            id='obs_spa_mean',
            attributes=dict(units=self.obs.units))

        # Ajout de l'axe des temps correspondant a self...
        self.model.spa_mean.setAxis(0, tps)
        self.obs.spa_mean.setAxis(0, tps)
                              id='maxvegetfrac')
print '--------------------------------------------------->>>>>>   4d variable', pft_13.shape
pft_13.long_name = 'Vegetation types'
pft_13.units = '-'

pft13_axislist_pft = pft_13.getAxisList(('veget', 'lat', 'lon'))
timax_slide = cdms2.createAxis(np.asarray([2010], np.float32))
timax_slide.designateTime(calendar=cdtime.MixedCalendar)
timax_slide.id = 'time_counter'
timax_slide.units = 'years since 0-1-1'
#********************************************************************************************************
#********************************************************************************************************
#Get the LUH masking
#mask_file	= cdms2.open('esa_mask.nc')
#esa_mask 	= mask_file('Mask_only',latitude=latrange, longitude=lonrange)
luh_mask = MV2.getmask(primf[0, :, :])
print 'luh_mask', luh_mask.shape
icwtr = MV2.masked_array(icwtr, mask=luh_mask)
#Total land frac in ORCmap  (Sum of all land units in ORCmap is 1)
orc_pft = MV2.masked_array(pft)
for n1 in range(0, 15):
    orc_pft[n1, :, :] = MV2.masked_array(orc_pft[n1, :, :], mask=luh_mask)
    #print n1
lndf_esa = MV2.minimum(MV2.sum(orc_pft, axis=0), 1)
lndf_esa.id = 'lndfrac_esa'
lndf_esa.long_name = 'Sum of all ORCmap PFTs'

#Total land frac in Hurtt data set (sum of all land units in LUH is one after including icwtr)
lndf_hurt = primf[nyears - 1, :, :] + secdf[nyears - 1, :, :] + primn[
    nyears -
    1, :, :] + secdn[nyears - 1, :, :] + c3ann[nyears - 1, :, :] + c4ann[