This tutorial shows how to convert MODIS data in binary format provided by JASMES service to netCDF.
JASMES data must be downloaded before the tutorial, and user registration is required to use JASMES data.
The sample scripts use Python3.
The tutorial shows an example of execution under Linux, but the sample scripts can also be used under Windows.
For details, please refer to "1. How to use the sample scripts" in this tutorial.
JAXA is not responsible for any actions taken by users using the contents of this website (including scripts).
Please refer to the following page for details of JASMES user registration. JASMES users can use all data published in JASMES services.
In this tutorial, as an example, we will create netCDF that converts MODIS data(filename : '*_le').
JASMES User Registration
JASMES Data Access
JASMES Data List
0. Introduction
Please use this tutorial as follows for your purposes.
・If you want to convert JASMES MODIS data to netCDF
Execute the sample script according to "1. How to use sample script"
・If you want to know what the sample script does
See "2. Description of script processing"
1. How to use sample script
1-1. Sample script download
These are sample scripts used in this tutorial. Please download it from the buttons bellow.
This tutorial provides script for "*_le" files, but the netCDF conversion method(2-2) is common.
For information on how to read binary files(2-1), please refer to the scripts listed in this page, as it depends on the type of file.
The sample scripts use Python3, and the Python libraries are NumPy, ctypes, and netCDF4.
The tutorial shows an example of execution under Linux, but the sample scripts can also be used under Windows.
Sample scripts may be freely modified and redistributed.
Sample Script (filename:'*_le') Download
1-2. Execution enviroment
The tutorial shows an example of execution under Linux.
The sample scripts use Python3.
The following is the runtime environment and Python libraries used when testing the sample scripts.
Linux:Red Hat Enterprise Linux(8.4)
Python:Python 3.6.8
Python standard libraries:ctypes
Python external libraries:NumPy, netCDF *Installation required
1-3. Target data list
To get JASMES data, you need to register as a user here. Please refer to here for how to get data after user registration.
Please refer to the list of data provided by JASMES for details on the area, resolution, and statistical period (Daily, Monthly, etc.) of each product, if necessary.
1-4. How to run the sample script
Execute the sample script with the input file path as arguments.
A netCDF with the input file name appended with ".nc" will be output in the same directory as the script.
Please refer ti the instructions at the beginning of the sample script.
$ ./MODIS_le_bin2nc.py <Input>
Argument 1 Input
Specify the full path of binary file to be input.
An example of command execution is shown below.
[DIR] : Data input
$ ./MODIS_le_bin2nc.py [DIR]/MDS02SSH_A20230101Av1_v811_7200_3601_CHLA_le
2. Description of script processing
From here, the various processes of the sample scripts are explained.
2-1. Reading binary data with Python
The header is specified according to the format of JASMES MODIS data shown.
Since JASMES MODIS data is formatted to include necessary information such as pixel size in the header section, it is necessary to read the header.
Please note that this is specific to JASMES MODIS data.
# Header format
class headerFormat(c.Structure):
_fields_ = [
("npixel" , c.c_char*6 ),
("nline" , c.c_char*6 ),
("lon_min", c.c_char*8 ),
("lat_max", c.c_char*8 ),
("reso" , c.c_char*8 ),
("slope" , c.c_char*12),
("offset ", c.c_char*12),
("dum1 ", c.c_char*1),
("prod ", c.c_char*8),
("dum2 ", c.c_char*1),
("file ", c.c_char*40)
]
arguments (input binary file path).
argvs = sys.argv
# argument ERROR
if len(argvs) != 2:
print("argument error: 1:input file, 2:output file, 3:area (Global / Japan / Thai)")
sys.exit(9)
else:
binfile = argvs[1]
# parameter pick
# 0 1 2 3 4 5 6
# MDS02SSH_A20250501Av1_v811_7200_3601_OLST_le
infile = os.path.basename(binfile)
output = infile + '.nc'
parts = infile.split('_')
X_SIZE = int(parts[3])
Y_SIZE = int(parts[4])
Binary file reading is performed.
After reading the header information into format_data according to the file, the data portions is read into binarr, skipping the header portion.
# 1 line * 2byte
HEADER_SIZE = X_SIZE*2
format_data = headerFormat()
# Binary file open
with open(binfile, 'rb') as f:
# Read header
f.readinto(format_data)
# Header skip
f.seek(HEADER_SIZE)
# Read binary data as unsiged-2byte integer
binarr = np.fromfile(f, dtype=np.uint16)
The necessary values of the headers read in format_data are retrieved.
# Header value set
X_SIZE = np.uint16(format_data.npixel)
Y_SIZE = np.uint16(format_data.nline)
UpperLeft_LON = np.float32(format_data.lon_min)
UpperLeft_LAT = np.float32(format_data.lat_max)
RESO = np.float32(format_data.reso)
SLOPE = np.float32(format_data.slope)
OFFSET = np.float32(format_data.offset)
PROD = format_data.prod.decode('utf-8').rstrip('\x00')
PROD = PROD.replace(' ','')
2-2. Output netCDF with Python
Open netCDF for output. Define the dimensions of netCDF using the header information X_SIZE and Y_SIZE obtained in 2-1.
# netCDF
with nc.Dataset(output, 'w', format='NETCDF4') as ds:
# 次元の定義
ds.createDimension('longitude', X_SIZE)
ds.createDimension('latitude', Y_SIZE)
Define and set values for netCDF variables.
# Define netCDF variables
longitudes = ds.createVariable('longitude','f4',('longitude',))
latitudes = ds.createVariable('latitude','f4',('latitude',))
data_var = ds.createVariable(PROD, 'i2', ('latitude', 'longitude'))
# Set values for netCDF variables
longitudes[:] = np.linspace(0,360,X_SIZE)
latitudes[:] = np.linspace(-90,90,Y_SIZE)
data_var[:,:] = orgdat
Add netCDF attributes.
Here, only the Slope/Offset obtained from headers is added, but you can add more if necessary.
# Add netCDF attributes
data_var.scale_factor = SLOPE
data_var.offset = OFFSET