read_nc.py
from netCDF4 import Dataset
import numpy as np
file = 'file.nc'
nc_obj= Dataset( file )
print ( nc_obj)
print ( nc_obj. dimensions)
print ( '---------------------------------------' )
for var_name in nc_obj. variables. keys( ) :
print ( f'var name: { var_name} ' )
var = nc_obj. variables[ var_name]
for arrt_name in var. ncattrs( ) :
print ( f'arrt name: { arrt_name} , arrt value: { var. __dict__[ arrt_name] } ' )
print ( '' )
print ( '---------------------------------------' )
for var_name in nc_obj. variables. keys( ) :
var = nc_obj. variables[ var_name]
data = var[ : ]
data = np. array( data)
if '_FillValue' in var. __dict__:
fillvalue = var. __dict__[ '_FillValue' ]
data = np. where( data== fillvalue, data, np. nan)
if 'missing_value' in var. __dict__:
missing_value = var. __dict__[ 'missing_value' ]
data = np. where( data== missing_value, data, np. nan)
if 'valid_min' in var. __dict__:
valid_min = var. __dict__[ 'valid_min' ]
data = np. where( data< valid_min, data, np. nan)
if 'valid_max' in var. __dict__:
valid_max = var. __dict__[ 'valid_max' ]
data = np. where( data> valid_max, data, np. nan)
if 'valid_range' in var. __dict__:
valid_range = var. __dict__[ 'valid_range' ]
data = np. where( data< valid_range[ 0 ] , data, np. nan)
data = np. where( data> valid_range[ 1 ] , data, np. nan)
scale_factor = var. __dict__. get( 'scale_factor' , 1.0 )
add_offset = var. __dict__. get( 'add_offset' , 0.0 )
data = data * scale_factor + add_offset
print ( data)
print ( '' )
print ( '---------------------------------------' )
change_nc.py
from netCDF4 import Dataset
ncFile = 'test.nc'
dataset = Dataset( ncFile, 'r+' )
dataset. variables[ 'lon' ] [ 0 ] = 99.9
dataset. variables[ 'chlor_a' ] [ 0 , 0 ] = 99.9
for key in dataset. variables. keys( ) :
print ( dataset. variables[ key] )
dataset. close( )
nc2rgb.py
from netCDF4 import Dataset
from time import time
from PIL import Image, ImageDraw
import numpy as np
import math
def getInd ( sample, value ) :
number= len ( sample)
if value<= sample[ 0 ] :
ind= 0
elif value>= sample[ number- 1 ] :
ind= sample[ number- 1 ]
else :
i= 1
while value> sample[ i] :
i+= 1
ind= i- 1 + ( value- sample[ i- 1 ] ) / ( sample[ i] - sample[ i- 1 ] )
return ind
def getColor ( color, ind ) :
number= len ( color)
if ind<= 0 :
r, g, b = color[ 0 ]
elif ind>= number- 1 :
r, g, b = color[ number- 1 ]
else :
ind1 = math. floor( ind)
ind2 = ind1+ 1
r = round ( color[ ind1] [ 0 ] * ( ind2- ind) + color[ ind2] [ 0 ] * ( ind- ind1) )
g = round ( color[ ind1] [ 1 ] * ( ind2- ind) + color[ ind2] [ 1 ] * ( ind- ind1) )
b = round ( color[ ind1] [ 2 ] * ( ind2- ind) + color[ ind2] [ 2 ] * ( ind- ind1) )
return r, g, b
start = time( )
print ( "Start: " + str ( start) )
color = ( ( 147 , 0 , 108 ) ,
( 111 , 0 , 144 ) ,
( 72 , 0 , 183 ) ,
( 33 , 0 , 222 ) ,
( 0 , 10 , 255 ) ,
( 0 , 74 , 255 ) ,
( 0 , 144 , 255 ) ,
( 0 , 213 , 255 ) ,
( 0 , 255 , 215 ) ,
( 0 , 255 , 119 ) ,
( 0 , 255 , 15 ) ,
( 96 , 255 , 0 ) ,
( 200 , 255 , 0 ) ,
( 255 , 235 , 0 ) ,
( 255 , 183 , 0 ) ,
( 255 , 131 , 0 ) ,
( 255 , 79 , 0 ) ,
( 255 , 31 , 0 ) ,
( 230 , 0 , 0 ) ,
( 165 , 0 , 0 ) ,
( 105 , 0 , 0 ) )
sample= ( 0.0000 ,
0.0471 ,
0.0980 ,
0.1490 ,
0.2000 ,
0.2471 ,
0.2980 ,
0.3490 ,
0.4000 ,
0.4471 ,
0.4980 ,
0.5490 ,
0.6000 ,
0.6471 ,
0.6980 ,
0.7490 ,
0.8000 ,
0.8471 ,
0.8980 ,
0.9490 ,
1.0000 )
file = 'V2020214.L3m_DAY_JPSS1_CHL_chlor_a_9km.nc'
dataSet= Dataset( file )
chl= dataSet. variables[ 'chlor_a' ]
fillValue= chl. _FillValue
validMin= chl. valid_min
validMax= chl. valid_max
displayMin= chl. display_min
displayMax= chl. display_max
displayScale= chl. display_scale
chlValue= np. array( chl)
tmp = chlValue. flatten( )
tmp[ tmp== fillValue] = np. nan
tmp[ tmp< validMin] = np. nan
tmp[ tmp> validMax] = np. nan
nanInd = np. isnan( tmp)
tmp[ tmp< displayMin] = displayMin
tmp[ tmp> displayMax] = displayMax
tmp[ nanInd] = displayMin
if displayScale== 'log' :
tmp = ( np. log( tmp) - np. log( displayMin) ) / ( np. log( displayMax) - np. log( displayMin) )
else :
tmp = ( tmp- displayMin) / ( displayMax- displayMin)
backColor= ( 0 , 0 , 0 )
nanColor= ( 255 , 255 , 255 )
width = 4320
height = 2160
image = Image. new( 'RGB' , ( width, height) , backColor)
draw = ImageDraw. Draw( image)
for y in range ( height) :
for x in range ( width) :
value = x + width * y
ind = getInd( sample, tmp[ value] )
r, g, b = getColor( color, ind )
if nanInd[ value] :
draw. point( ( x, y) , fill= nanColor)
else :
draw. point( ( x, y) , fill= ( r, g, b) )
image. save( 'chl.png' , 'png' )
stop = time( )
print ( "Stop: " + str ( stop) )
print ( str ( stop- start) + "秒" )