22
22
# Nina Hakansson <nina.hakansson@smhi.se>
23
23
# Adam.Dybbroe <adam.dybbroe@smhi.se>
24
24
25
- """Functions to convert MERSI-2 level-1 data to a NWCSAF/PPS level-1c formatet netCDF/CF file."""
25
+ """Functions to convert MERSI-2/3 level-1 data to a NWCSAF/PPS level-1c formated netCDF/CF file."""
26
26
27
+ import logging
27
28
import os
28
29
import time
30
+
31
+ import numpy as np
29
32
from satpy .scene import Scene
30
- from level1c4pps import (get_encoding , compose_filename ,
33
+ from level1c4pps import (ANGLE_ATTRIBUTES ,
34
+ compose_filename ,
35
+ convert_angles ,
36
+ get_encoding ,
37
+ get_header_attrs ,
38
+ rename_latitude_longitude ,
31
39
set_header_and_band_attrs_defaults ,
32
- ANGLE_ATTRIBUTES , rename_latitude_longitude ,
33
- update_angle_attributes , get_header_attrs ,
34
- convert_angles )
35
- import pyspectral # testing that pyspectral is available # noqa: F401
36
- import logging
40
+ update_angle_attributes )
37
41
38
42
# Example:
39
43
# tf2019234102243.FY3D-X_MERSI_GEOQK_L1B.HDF
42
46
# tf2019234102243.FY3D-X_MERSI_0250M_L1B.HDF
43
47
#
44
48
45
- logger = logging .getLogger ('mersi22pps' )
49
+ logger = logging .getLogger ('mersi2pps' )
50
+
51
+ SENSOR = {'FY3D' : 'merci-2' ,
52
+ 'FY3F' : 'merci-3' ,
53
+ 'FY3H' : 'merci-3' }
54
+
55
+ SATPY_READER = {'merci-2' : 'mersi2_l1b' ,
56
+ 'merci-3' : 'mersi3_l1b' }
46
57
47
- PLATFORM_SHORTNAMES = {'FY3D' : 'FY-3D' }
48
- # BANDNAMES = ['%d' % (chn+1) for chn in range(25)]
49
58
BANDNAMES = ['3' , '4' , '5' , '6' , '20' , '22' , '23' , '24' , '25' ]
50
59
51
60
REFL_BANDS = ['3' , '4' , '5' , '6' ]
63
72
'24' : 'ch_tb11' ,
64
73
'25' : 'ch_tb12' }
65
74
66
- MERSI2_LEVEL1_FILE_PATTERN = 'tf{start_time:%Y%j%H%M%S}.{platform_shortname:4s}-X_MERSI_{dataset}_L1B.HDF'
67
-
68
-
69
- def get_encoding_mersi2 (scene ):
70
- """Get netcdf encoding for all datasets."""
71
- return get_encoding (scene ,
72
- BANDNAMES ,
73
- PPS_TAGNAMES ,
74
- chunks = None )
75
-
76
75
77
76
def set_header_and_band_attrs (scene , orbit_n = 0 ):
78
77
"""Set and delete some attributes."""
79
78
irch = scene ['24' ]
80
- nimg = set_header_and_band_attrs_defaults (scene , BANDNAMES , PPS_TAGNAMES , REFL_BANDS , irch , orbit_n = orbit_n )
81
- scene .attrs ['source' ] = "mersi22pps.py"
79
+ nimg = set_header_and_band_attrs_defaults (scene ,
80
+ BANDNAMES ,
81
+ PPS_TAGNAMES ,
82
+ REFL_BANDS ,
83
+ irch ,
84
+ orbit_n = orbit_n )
85
+ scene .attrs ['source' ] = "mersi2pps.py"
82
86
return nimg
83
87
84
88
85
89
def remove_broken_data (scene ):
86
90
"""Set bad data to nodata."""
87
- import numpy as np
88
91
for band in BANDNAMES :
89
92
if band in REFL_BANDS :
90
93
continue
@@ -93,42 +96,40 @@ def remove_broken_data(scene):
93
96
scene [band ].values = scene [band ].values + remove
94
97
95
98
99
+ def get_sensor (scene_file ):
100
+ """Get sensor associated to the scene file."""
101
+ for platform , sensor in SENSOR .items ():
102
+ if platform in scene_file :
103
+ return sensor
104
+ logger .info ("Failed to determine sensor associated to scene file: '%s'" , scene_file )
105
+ return None
106
+
107
+
96
108
def process_one_scene (scene_files , out_path , engine = 'h5netcdf' , orbit_n = 0 ):
97
109
"""Make level 1c files in PPS-format."""
98
110
tic = time .time ()
99
- scn_ = Scene (
100
- reader = 'mersi2_l1b' ,
101
- filenames = scene_files )
102
-
103
- scn_ .load (BANDNAMES + ['latitude' , 'longitude' ] + ANGLE_NAMES , resolution = 1000 )
104
-
105
- # Remove bad data at first and last column
106
- remove_broken_data (scn_ )
107
-
108
- # one ir channel
109
- irch = scn_ ['24' ]
110
-
111
- # Set header and band attributes
112
- set_header_and_band_attrs (scn_ , orbit_n = orbit_n )
113
-
114
- # Rename longitude, latitude to lon, lat.
115
- rename_latitude_longitude (scn_ )
116
-
117
- # Convert angles to PPS
118
- convert_angles (scn_ , delete_azimuth = True )
119
- update_angle_attributes (scn_ , irch )
111
+ sensor = get_sensor (os .path .basename (scene_files [0 ]))
112
+ reader = SATPY_READER [sensor ]
113
+ scene = Scene (reader = reader , filenames = scene_files )
114
+ scene .load (BANDNAMES + ['latitude' , 'longitude' ] + ANGLE_NAMES , resolution = 1000 )
115
+ remove_broken_data (scene )
116
+ irch = scene ['24' ] # one ir channel
117
+ set_header_and_band_attrs (scene , orbit_n = orbit_n )
118
+ rename_latitude_longitude (scene )
119
+ convert_angles (scene , delete_azimuth = True )
120
+ update_angle_attributes (scene , irch )
120
121
for angle in ['sunzenith' , 'satzenith' , 'azimuthdiff' ]:
121
- scn_ [angle ].attrs ['file_key' ] = ANGLE_ATTRIBUTES ['mersi2_file_key ' ][angle ]
122
+ scene [angle ].attrs ['file_key' ] = ANGLE_ATTRIBUTES ['mersi_file_key ' ][angle ]
122
123
123
- filename = compose_filename (scn_ , out_path , instrument = 'mersi2' , band = irch )
124
- scn_ .save_datasets (writer = 'cf' ,
124
+ filename = compose_filename (scene , out_path , instrument = sensor .replace ('-' , '' ), band = irch )
125
+ encoding = get_encoding (scene , BANDNAMES , PPS_TAGNAMES , chunks = None )
126
+ attrs = get_header_attrs (scene , band = irch , sensor = sensor )
127
+ scene .save_datasets (writer = 'cf' ,
125
128
filename = filename ,
126
- header_attrs = get_header_attrs ( scn_ , band = irch , sensor = 'mersi-2' ) ,
129
+ header_attrs = attrs ,
127
130
engine = engine ,
128
131
include_lonlats = False ,
129
132
flatten_attrs = True ,
130
- encoding = get_encoding_mersi2 (scn_ ))
131
- print ("Saved file {:s} after {:3.1f} seconds" .format (
132
- os .path .basename (filename ),
133
- time .time ()- tic ))
133
+ encoding = encoding )
134
+ print (f"Saved file { os .path .basename (filename )} after { time .time () - tic :3.1f} seconds" )
134
135
return filename
0 commit comments