@@ -263,6 +263,7 @@ def cmorise_ocean(self, file_paths, compound_name, cmor_dataset_json, mip_table)
263263 # Write data to CMOR
264264 data = np .moveaxis (data , 0 , - 1 )
265265 cmor .write (cmorVar , data , ntimes_passed = len (time_numeric ))
266+
266267
267268 # Finalize and save the file
268269 filename = cmor .close (cmorVar , file_name = True )
@@ -330,7 +331,7 @@ def cmorise(self, file_paths, compound_name, cmor_dataset_json, mip_table):
330331 # Not all variable have a time component (e.g. fx, Ofx)
331332 time_axis = axes .pop ("time" , None )
332333 if time_axis :
333- time_axis = axes .pop ("time" )
334+ # time_axis = axes.pop("time")
334335 time_numeric = ds [time_axis ].values
335336 time_units = ds [time_axis ].attrs ["units" ]
336337 time_bnds = ds [ds [time_axis ].attrs ["bounds" ]].values
@@ -344,8 +345,13 @@ def cmorise(self, file_paths, compound_name, cmor_dataset_json, mip_table):
344345 inpath = ipth ,
345346 set_verbosity = cmor .CMOR_NORMAL ,
346347 netcdf_file_action = cmor .CMOR_REPLACE ,
348+ logfile = "cmor_debug.log"
347349 )
348350
351+ out_dir = "/g/data/tm70/yz9299/MOPPeR_outputs"
352+ os .makedirs (out_dir , exist_ok = True )
353+ # cmor.set_cur_dataset_attribute("outpath", out_dir)
354+
349355 cmor .dataset_json (cmor_dataset_json )
350356 current_dir = os .path .dirname (os .path .abspath (__file__ ))
351357 grid_table_id = cmor .load_table (
@@ -369,7 +375,7 @@ def cmorise(self, file_paths, compound_name, cmor_dataset_json, mip_table):
369375 latitude_vertices = lat_bnds ,
370376 longitude_vertices = lon_bnds ,
371377 )
372- cmor_axes .append (grid_id )
378+ # cmor_axes.append(grid_id)
373379
374380 # Now, load the Omon table to set up the time axis and variable
375381 current_dir = os .path .dirname (os .path .abspath (__file__ ))
@@ -381,31 +387,48 @@ def cmorise(self, file_paths, compound_name, cmor_dataset_json, mip_table):
381387 cmorTime = cmor .axis (
382388 "time" , coord_vals = time_numeric , cell_bounds = time_bnds , units = time_units
383389 )
384- cmor_axes .append (cmorTime )
390+ # cmor_axes.append(cmorTime)
385391
386- if axes :
387- for axis , dim in axes .items ():
388- coord_vals = var [dim ].values
389- try :
390- cell_bounds = var [var [dim ].attrs ["bounds" ]].values
391- except KeyError :
392- cell_bounds = None
393- axis_units = var [dim ].attrs ["units" ]
394- cmor_axis = cmor .axis (
395- axis ,
396- coord_vals = coord_vals ,
397- cell_bounds = cell_bounds ,
398- units = axis_units ,
399- )
400- cmor_axes .append (cmor_axis )
392+ # Append coordinates and transpose data axes
393+ if data .ndim == 3 :
394+ cmor_axes .append (grid_id )
395+ cmor_axes .append (cmorTime )
396+ data = np .transpose (data , (1 , 2 , 0 ))
397+
398+ elif data .ndim == 4 :
399+ cmor_axes .append (grid_id )
400+ if axes :
401+ for axis , dim in axes .items ():
402+ coord_vals = var [dim ].values
403+ try :
404+ cell_bounds = var [var [dim ].attrs ["bounds" ]].values
405+ except KeyError :
406+ cell_bounds = None
407+ print (axis , dim )
408+ if axis == "depth_coord" :
409+ depth = coord_vals
410+ bounds = np .zeros ((len (depth ), 2 ))
411+ bounds [1 :, 0 ] = 0.5 * (depth [1 :] + depth [:- 1 ])
412+ bounds [:- 1 , 1 ] = 0.5 * (depth [1 :] + depth [:- 1 ])
413+ bounds [0 , 0 ] = max (0.0 , depth [0 ] - (depth [1 ] - depth [0 ]) / 2 )
414+ bounds [- 1 , 1 ] = depth [- 1 ] + (depth [- 1 ] - depth [- 2 ]) / 2
415+ cell_bounds = bounds
416+ axis_units = "m"
417+ cmor_axis = cmor .axis (
418+ axis ,
419+ coord_vals = coord_vals ,
420+ cell_bounds = cell_bounds ,
421+ units = axis_units ,
422+ )
423+ cmor_axes .append (cmor_axis )
424+ cmor_axes .append (cmorTime )
425+ data = np .transpose (data ,(2 , 3 , 1 , 0 ))
401426
402427 # Define CMOR variable
403428 cmorVar = cmor .variable (cmor_name , variable_units , cmor_axes , positive = positive )
404429
405430 # Write data to CMOR
406- data = np .moveaxis (data , 0 , - 1 )
407- ntimes_passed = len (time_numeric ) if time_axis else 0
408- cmor .write (cmorVar , data , ntimes_passed = ntimes_passed )
431+ cmor .write (cmorVar , data , ntimes_passed = len (time_numeric ))
409432
410433 # Finalize and save the file
411434 filename = cmor .close (cmorVar , file_name = True )
0 commit comments