NCAR / pyngl

Other
138 stars 30 forks source link

Could background color to be changed? #20

Closed jinlx closed 5 years ago

jinlx commented 5 years ago

Hi there,

I was wondering if we could change the background color in a contour. I want to change my blue background color into white one. And I have already tried to change "mpFillDrawOrder", "mpFillColor", "mpFillPatternBackground". But they didn't work. Is there a way to achieve it in ngl?

Thanks, Lixu

rbrownrigg commented 5 years ago

Hi,

I'm unclear as towhead you are asking -- do you mean the color of the map? the color of the background of the plot itself?

Rick

On Mon, Sep 16, 2019 at 7:42 AM Lixu Jin notifications@github.com wrote:

Hi there,

I was wondering if we could change the background color in a contour. I want to change my blue background color into white one. And I have already tried to change "mpFillDrawOrder", "mpFillColor", "mpFillPatternBackground". But they didn't work. Is there a way to achieve it in ngl?

Thanks, Lixu

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/NCAR/pyngl/issues/20?email_source=notifications&email_token=ADLWOXWPSEQPXQ7CHY4F2IDQJ6EM7A5CNFSM4IXCODNKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HLSXJCA, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLWOXUDB2L7LENEAZUWLQDQJ6EM7ANCNFSM4IXCODNA .

jinlx commented 5 years ago

Hi Rick,

Thanks for the quik reply. I want to change the color of the map. As the fig shows below, the color of the fig is blue. So, I want to change it into white. But I don't know how to realize it. Do you have any suggestion on it?

Lixu


image

rbrownrigg commented 5 years ago

Hi Lixu,

The image helps, but can you also post your script?

Rick

On Tue, Sep 17, 2019 at 7:05 AM Lixu Jin notifications@github.com wrote:

Hi Rick,

Thanks for the quik reply. I want to change the color of the map. As the fig shows below, the color of the fig is blue. So, I want to change it into white. But I don't know how to realize it. Do you have any suggestion on it? Lixu [image: image] https://user-images.githubusercontent.com/32994828/65044056-701efe80-d919-11e9-9bf1-620b8a3ccf78.png

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NCAR/pyngl/issues/20?email_source=notifications&email_token=ADLWOXXV4XJX4D5M2OGDQALQKDIYBA5CNFSM4IXCODNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD64OO2A#issuecomment-532211560, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLWOXVL5SGB7DNL2MZMKOTQKDIYBANCNFSM4IXCODNA .

jinlx commented 5 years ago

Sure. My code is posted below. Hope it helps me clarify my question.

Lixu


#from mpl_toolkits.basemap import Basemap
from __future__ import print_function
import os
import numpy as np
import Ngl, Nio

#=============================================================
# Read single dataset
#=============================================================
# Read data
dir    = '/glade/u/home/lixujin/project/GEOS-CHEM/ExtData/HEMCO/GFAS/v2018-09/2018/'
data1  = 'GFAS_201807.nc'
data2  = 'GFAS_201808.nc'
data3  = 'GFAS_201809.nc'
ffile1 = os.path.join(dir, data1)
ffile2 = os.path.join(dir, data2)
ffile3 = os.path.join(dir, data3)
f1     =  Nio.open_file(ffile1, "r")            #-- open data file
f2     =  Nio.open_file(ffile2, "r")            #-- open data file 
f3     =  Nio.open_file(ffile3, "r")            #-- open data file 
co1    =  60*60*24*1e3*f1.variables["cofire"][:,:,:]          #-- first time step
co1    = np.sum(co1,0)
co2    =  60*60*24*1e3*f2.variables["cofire"][:,:,:]          #-- first time step
co2    = np.sum(co2,0)
co3    =  60*60*24*1e3*f3.variables["cofire"][:,:,:]          #-- first time step
co3    = np.sum(co3,0)
co     = (co1+co2+co3)/3
#co = 60*60*24*1e3*(ds1['cofire'].sum('time') + ds2['cofire'].sum('time') + ds3['cofire'].sum('time'))

lat    =  f1.variables["lat"][:]                #-- all latitudes
lon    =  f1.variables["lon"][:]                #-- all longitudes

co_wecan,lon =  Ngl.add_cyclic(co,lon)

#=======================================================================
#-- open a workstation
#========================================================================
wks_type   = "png"
wks        = Ngl.open_wks(wks_type,"co_global")

#=======================================================================
#-- set resources
#=======================================================================
res                    =  Ngl.Resources()      #-- generate an resource object for plot

if hasattr(f1.variables["cofire"],"long_name") and hasattr(f1.variables["cofire"],"units"):
   res.tiMainString = "{} ({})".format(f1.variables["cofire"].long_name,
                                       "ppm")      #-- set main title
minval =  0.                                 #-- minimum contour level
maxval =  10.                                  #-- maximum contour level
inc    =  1.                                 #-- contour level spacing

# background color
res.mpFillOn                = True
res.mpFillDrawOrder         = "PreDraw"
res.mpFillColor             = "white"
#res.mpFillPatternBackground = -1
# contour resources
res.cnFillOn              =  True              #-- turn on contour fill.
res.cnLinesOn             =  False             #-- turn off contour lines
res.cnLineLabelsOn        =  False             #-- turn off line labels.
res.cnInfoLabelOn         =  False             #-- turn off info label.
res.cnLevelSelectionMode  = "ManualLevels"     #-- select manual level selection mode
res.cnMinLevelValF        =  minval            #-- minimum contour value
res.cnMaxLevelValF        =  maxval            #-- maximum contour value
res.cnLevelSpacingF       =  inc               #-- contour increment
res.cnFillPalette         = "rainbow"          #-- choose color map
res.mpGridSpacingF        =  30                #-- map grid spacing
res.sfXArray              =  lon               #-- longitude locations of data
res.sfYArray              =  lat               #-- latitude locations of data
res.lbOrientation         = "Horizontal"       #-- labelbar orientation

#==================================================================================
#plotting
#==================================================================================
contour = Ngl.contour_map(wks,co_wecan,res)
Ngl.end()
rbrownrigg commented 5 years ago

Hi,

Is it possible that your data has missing values but that the variable co_wecan does not have a _FillValue attribute to reflect that, and thus those values are being interpreted as data and colored accordingly. That might explain the blue "background".

Rick

On Tue, Sep 17, 2019 at 7:23 AM Lixu Jin notifications@github.com wrote:

Sure. My code is posted below. Hope it helps.

`# import packages

from mpl_toolkits.basemap import Basemap

from future import print_function import os import numpy as np import Ngl, Nio

=============================================================

Read single dataset

=============================================================

Read data

dir = '/glade/u/home/lixujin/project/GEOS-CHEM/ExtData/HEMCO/GFAS/v2018-09/2018/' data1 = 'GFAS_201807.nc' data2 = 'GFAS_201808.nc' data3 = 'GFAS_201809.nc' ffile1 = os.path.join(dir, data1) ffile2 = os.path.join(dir, data2) ffile3 = os.path.join(dir, data3) f1 = Nio.open_file(ffile1, "r") #-- open data file f2 = Nio.open_file(ffile2, "r") #-- open data file f3 = Nio.open_file(ffile3, "r") #-- open data file co1 = 6060241e3f1.variables["cofire"][:,:,:] #-- first time step co1 = np.sum(co1,0) co2 = 6060241e3f2.variables["cofire"][:,:,:] #-- first time step co2 = np.sum(co2,0) co3 = 6060241e3f3.variables["cofire"][:,:,:] #-- first time step co3 = np.sum(co3,0) co = co1+co2+co3

co = 6060241e3(ds1['cofire'].sum('time') + ds2['cofire'].sum('time')

  • ds3['cofire'].sum('time'))

lat = f1.variables["lat"][:] #-- all latitudes lon = f1.variables["lon"][:] #-- all longitudes

co_wecan,lon = Ngl.add_cyclic(co,lon)

=======================================================================

-- open a workstation

========================================================================

wks_type = "png" wks = Ngl.open_wks(wks_type,"co_global")

=======================================================================

-- set resources

=======================================================================

res = Ngl.Resources() #-- generate an resource object for plot

if hasattr(f1.variables["cofire"],"long_name") and hasattr(f1.variables["cofire"],"units"): res.tiMainString = "{} ({})".format(f1.variables["cofire"].long_name, "ppm") #-- set main title minval = 0. #-- minimum contour level maxval = 10. #-- maximum contour level inc = 1. #-- contour level spacing background color

res.mpFillOn = True res.mpFillDrawOrder = "PreDraw" res.mpFillColor = "white"

res.mpFillPatternBackground = -1

contour resources

res.cnFillOn = True #-- turn on contour fill. res.cnLinesOn = False #-- turn off contour lines res.cnLineLabelsOn = False #-- turn off line labels. res.cnInfoLabelOn = False #-- turn off info label. res.cnLevelSelectionMode = "ManualLevels" #-- select manual level selection mode res.cnMinLevelValF = minval #-- minimum contour value res.cnMaxLevelValF = maxval #-- maximum contour value res.cnLevelSpacingF = inc #-- contour increment res.cnFillPalette = "rainbow" #-- choose color map res.mpGridSpacingF = 30 #-- map grid spacing res.sfXArray = lon #-- longitude locations of data res.sfYArray = lat #-- latitude locations of data res.lbOrientation = "Horizontal" #-- labelbar orientation

==================================================================================

plotting

==================================================================================

contour = Ngl.contour_map(wks,co_wecan,res) Ngl.end() `

Lixu

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NCAR/pyngl/issues/20?email_source=notifications&email_token=ADLWOXQAMS5YX2AQY4RS5PLQKDK6HA5CNFSM4IXCODNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD64QGSY#issuecomment-532218699, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLWOXS7DEAS3RC54I62ZKTQKDK6HANCNFSM4IXCODNA .

jinlx commented 5 years ago

Yes, you are right. The GFAS inventory does have missing value. So, that's why I get blue 'background'. Still, If there is any method can reset those blue missing part into white?

Lixu

rbrownrigg commented 5 years ago

Well I think you just need to add a "_FillValue" attribute to the variable co_wecan:

co_wecan._FillValue = N # where "N" is the value denoting missing values

On Tue, Sep 17, 2019 at 7:34 AM Lixu Jin notifications@github.com wrote:

Yes, you are right. The GFAS inventory does have missing value. So, that's why I get blue 'background'. Still, If there is any method can reset those blue missing part into white?

Lixu

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NCAR/pyngl/issues/20?email_source=notifications&email_token=ADLWOXSKLAHEIT6ZWBF4P2DQKDMH3A5CNFSM4IXCODNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD64RLCQ#issuecomment-532223370, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLWOXVSSSSKG3SSFNMGY5LQKDMH3ANCNFSM4IXCODNA .

jinlx commented 5 years ago

Hi Rick,

Here is what I did. I use ncdump to check the missing value and attribute it to _FillValue: co_wecan.missing_value = -1.e-31 co_wecan._FillValue = co_wecan.missing_value But it did not work. Is there anything wrong?

Here is what I get by type ncdump.


dimensions:
        lon = 3600 ;
        lat = 1800 ;
        time = 31 ;
variables:
        int time(time) ;
                time:units = "hours since 1970-01-01 00:00:0.0" ;
                time:long_name = "time" ;
                time:calendar = "gregorian" ;
        float lat(lat) ;
                lat:units = "degrees_north" ;
                lat:long_name = "latitude" ;
        float lon(lon) ;
                lon:units = "degrees_east" ;
                lon:long_name = "longitude" ;
        float cofire(time, lat, lon) ;
                cofire:_FillValue = -1.e-31f ;
                cofire:units = "kg/m2/s" ;
                cofire:long_name = "Wildfire flux of Carbon Monoxide" ;
                cofire:missing_value = -1.e-31f ;
rbrownrigg commented 5 years ago

Well, at this point, I don't know what to suggest. Did you print(co_wecan) to make sure everything is what you think it is? It really does look like mising values are being treated as data -- PyNGL's normal behaviour would not be to color the background of a plot in blue.

Perhaps someone else has a suggestion....

Rick

On Tue, Sep 17, 2019 at 8:14 AM Lixu Jin notifications@github.com wrote:

Hi Rick,

Here is what I did. I use ncdump to check the missing value and attribute it to _FillValue: co_wecan.missing_value = -1.e-31 co_wecan._FillValue = co_wecan.missing_value But it did not work. Is there anything wrong?

Here is what I get by type ncdump. '''' dimensions: lon = 3600 ; lat = 1800 ; time = 31 ; variables: int time(time) ; time:units = "hours since 1970-01-01 00:00:0.0" ; time:long_name = "time" ; time:calendar = "gregorian" ; float lat(lat) ; lat:units = "degrees_north" ; lat:long_name = "latitude" ; float lon(lon) ; lon:units = "degrees_east" ; lon:long_name = "longitude" ; float cofire(time, lat, lon) ; cofire:_FillValue = -1.e-31f ; cofire:units = "kg/m2/s" ; cofire:long_name = "Wildfire flux of Carbon Monoxide" ; cofire:missing_value = -1.e-31f ;

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NCAR/pyngl/issues/20?email_source=notifications&email_token=ADLWOXRI4TFJACI4OXCIOZLQKDQ3LA5CNFSM4IXCODNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD64VOEA#issuecomment-532240144, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLWOXTWWV7V3Y3KH52OCKLQKDQ3LANCNFSM4IXCODNA .

jinlx commented 5 years ago

Hi Rick,

After I print the data, it doesn't show any attributes information. So, I think it is caused by numpy. So, is it possible that still keeping those information after using numpy?

Lixu

rbrownrigg commented 5 years ago

I can certainly believe that numpy is dropping attributes and co_wecan is a numpy-calculated quantity. As you may know, numpy uses the notion of "masked arrays" to represent missing values. The _FillValue and Missing_value attributes are NetCDF conventions. I would think PyNIO/PyNGL would do the right thing in converting between NetCDF and numpy conventions, but I don't know. There are several options that can control PyNIO's behavior (search for "MaskedArrayMode"):

https://www.pyngl.ucar.edu/Nio.shtml

I guess one thing to check its to print the variables before and after numpy calculations and make sure you're getting masked arrays in all cases.

Perhaps someone with more numpy experience can contribute to the discussion here....

Rick

On Tue, Sep 17, 2019 at 1:32 PM Lixu Jin notifications@github.com wrote:

Hi Rick,

After I print the data, it doesn't show any attributes information. So, I think it is caused by numpy. So, is it possible that still keeping those information after using numpy?

Lixu

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/NCAR/pyngl/issues/20?email_source=notifications&email_token=ADLWOXU77KQPFWTYFBW3M4TQKEWDVA5CNFSM4IXCODNKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD65UVWQ#issuecomment-532368090, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLWOXUZFAVDCL5FJAULPETQKEWDVANCNFSM4IXCODNA .

jinlx commented 5 years ago

-Rick: Thanks for all the information. I really appreciate it!

Lixu