OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
copy_l2file.py
Go to the documentation of this file.
1 #Creator: Emerson Sirk (emerson.a.sirk@nasa.gov)
2 
3 #Last Edited: 1/14/22
4 
5 from netCDF4 import Dataset
6 from create_images import *
7 from MDN.parameters import get_args
8 from MDN.utils import get_sensor_bands
9 import os
10 
11 def create_copy(in_file, out_file, prod_select):
12 # This program takes an input l2 file with geophysical data and creates an output L2 file
13 # with the same global attributes and groups as the input. The geophysical data is replaced
14 # by the product data generated by a MDN neural network.
15 #in_file - the directory path of the input netcdf L2 file
16 #out_file - the directory path for the output netcdf L2 file
17 #prod_select - needs to be a string with products separated by commas. Options are chl, tss, cdom, pc.
18 
19  source = Dataset(in_file)
20  copy = Dataset(out_file, mode='w')
21 
22  # Create the dimensions of the file
23  for name, dim in source.dimensions.items():
24  copy.createDimension(name, len(dim) if not dim.isunlimited() else None)
25 
26  # Copy the global attributes
27  copy.setncatts({a:source.getncattr(a) for a in source.ncattrs()})
28 
29  #copy over other groups from input file to output file
30  for gname in source.groups:
31 
32  #check to make sure it is not referencing input geophysical varibales
33  if (gname == 'geophysical_data') == False:
34  copy.createGroup(gname) #create new group in output file with same name
35 
36  #copy all variables in the group
37  for vname in source[gname].variables:
38  copy.createVariable('/'+gname+'/'+vname, source[gname][vname].datatype, source[gname][vname].dimensions)
39  copy[gname][vname][:] = source[gname][vname][:]
40 
41  #create output data group
42  copy.createGroup("geophysical_data")
43 
44  # Create the variables in the output data
45 
46  sensor = source.instrument
47  kwargs = {
48  'sensor' : sensor,
49  'product' : 'chl,tss,cdom,pc', #all available products
50  'sat_bands' : True,
51  'use_ratio' : True,
52  'use_excl_Rrs' : True,
53  'model_loc' : os.environ['OCDATAROOT'] + '/aquaverse'
54  }
55 
56  # Load the bands required for the given sensor
57  req_bands = get_sensor_bands(sensor, get_args(**kwargs, use_cmdline = False))
58 
59  #calculate data
60  input_data = source['geophysical_data']
61  bands = sorted([int(k.replace('Rrs_', '')) for k in input_data.variables.keys() if 'Rrs_' in k])
62  Rrs = extract_data(input_data, bands, req_bands)
63 
64  # Generate product estimates - 'slices' contains the index of each product within 'products'
65  products, slices = image_estimates(Rrs, **kwargs, use_cmdline = False)
66 
67 
68  product_list = prod_select.split(',') #only user selected products
69  unit_list = {'chl':'unit', 'tss':'unit', 'cdom':'unit', 'pc':'unit'}
70 
71  #assign variables from calculated data products
72  for product_name in product_list:
73  key = product_name
74  idx = slices[key]
75  #type = copy.createVLType(np.float64, product_name)
76  type = np.float64
77  var = copy.createVariable('/geophysical_data/'+product_name, type,("number_of_lines","pixels_per_line"))
78  var.units = unit_list[key]
79  var_data = products[...,idx]
80  s = var_data.shape
81  var_data = var_data.reshape((s[0],s[1]))
82  var[:] = var_data
83 
84  # Save the file
85  copy.close()
def get_sensor_bands(sensor, args=None)
Definition: meta.py:114
def image_estimates(data, sensor=None, function=apply_model, rhos=False, anc=False, **kwargs)
def extract_data(image, avail_bands, req_bands, allow_neg=False, key='Rrs')
def create_copy(in_file, out_file, prod_select)
Definition: copy_l2file.py:11
def get_args(kwargs={}, use_cmdline=True, **kwargs2)
Definition: parameters.py:100