diff --git a/notebooks/conversion_output/convert_MIMICA_output_to_dephy_format.ipynb b/notebooks/conversion_output/convert_MIMICA_output_to_dephy_format.ipynb new file mode 100644 index 00000000..90028361 --- /dev/null +++ b/notebooks/conversion_output/convert_MIMICA_output_to_dephy_format.ipynb @@ -0,0 +1,1237 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "import os, pickle, io\n", + "import numpy as np\n", + "import xarray as xr\n", + "import matplotlib.pyplot as plt\n", + "import datetime\n", + "import re\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "native_outputs_path='/proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_NEW_SIMULATIONS/'\n", + "converted_outputs_path='/proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED/'\n", + "converted_outputs_path_time_mod='/proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED_time_mod/'\n", + "#COMBLE_part2='/proj/mimica-chalmers-comble/users/x_abaro/COMBLE/'" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "Output_statistic = {\n", + " 'ps': { 'std_name': 'surface_pressure', 'var_id': 'ps', 'units': 'Pa', 'dim': 'time'},\n", + " 'ts': { 'std_name': 'surface_temperature', 'var_id': 'ts', 'units': 'K' , 'dim': 'time'},\n", + " 'ustar': { 'std_name': 'surface_friction_velocity', 'var_id': 'ustar', 'units': 'm s-1', 'dim': 'time'},\n", + " 'z0' : { 'std_name': 'surface_roughness_length_for_momentum_in_air', 'var_id': 'z0', 'units': 'm', 'dim': 'time'},\n", + " 'z0h': { 'std_name': 'surface_roughness_length_for_heat_in_air', 'var_id': 'z0h', 'units': 'm', 'dim': 'time'},\n", + " 'z0q': { 'std_name': 'surface_roughness_length_for_humidity_in_air', 'var_id': 'z0q', 'units': 'm', 'dim': 'time'},\n", + " 'hfss': { 'std_name': 'surface_upward_sensible_heat_flux', 'var_id': 'hfss', 'units': 'W m-2', 'dim': 'time'},\n", + " 'hfls': { 'std_name': 'surface_upward_latent_heat_flux', 'var_id': 'hfls', 'units': 'W m-2', 'dim': 'time'},\n", + " 'ol' : { 'std_name': 'obukhov_length', 'var_id': 'ol', 'units': 'm', 'dim': 'time'},\n", + " 'lwpc': { 'std_name': 'atmosphere_mass_content_of_liquid_cloud_water', 'var_id': 'lwpc', 'units': 'kg m-2', 'dim': 'time'},\n", + " 'lwpr': { 'std_name': 'atmosphere_mass_content_of_rain_water', 'var_id': 'lwpr', 'units': 'kg m-2', 'dim': 'time'},\n", + " 'iwp' : { 'std_name': 'atmosphere_mass_content_of_ice_water', 'var_id': 'iwp', 'units': 'kg m-2', 'dim': 'time'},\n", + " 'clt' : { 'std_name': 'cloud_area_fraction', 'var_id': 'clt', 'units': '1', 'dim': 'time'},\n", + " 'od' : { 'std_name': 'optical_depth', 'var_id': 'od', 'units': '1', 'dim': 'time'},\n", + " 'odlc' : { 'std_name': 'optical_depth_of_liquid_cloud', 'var_id': 'odlc', 'units': '1', 'dim': 'time'},\n", + " 'pr' : { 'std_name': 'precipitation_flux_at_surface', 'var_id': 'pr', 'units': 'kg m-2 s-1', 'dim': 'time'},\n", + " 'pri' : { 'std_name': 'precipitation_flux_at_surface_in_ice_phase', 'var_id': 'pri', 'units': 'kg m-2 s-1', 'dim': 'time'},\n", + " 'rlut' : { 'std_name': 'toa_outgoing_longwave_flux', 'var_id': 'rlut', 'units': 'W m-2', 'dim': 'time'},\n", + " 'rlds' : { 'std_name': 'surface_downwelling_longwave_flux', 'var_id': 'rlds', 'units': 'W m-2', 'dim': 'time'},\n", + " 'rlus' : { 'std_name': 'surface_upwelling_longwave_flux', 'var_id': 'rlus', 'units': 'W m-2', 'dim': 'time'},\n", + " 'ssaf' : { 'std_name': 'surface_sea_spray_number_flux', 'var_id': 'ssaf', 'units': 'm-2 s-1', 'dim': 'time'},\n", + " 'pa' : { 'std_name': 'air_pressure', 'var_id': 'pa', 'units': 'Pa', 'dim': 'time, height'},\n", + " 'ua' : { 'std_name': 'eastward_wind', 'var_id': 'ua', 'units': 'm s-1', 'dim': 'time, height'},\n", + " 'va' : { 'std_name': 'northward_wind', 'var_id': 'va', 'units': 'm s-1', 'dim': 'time, height'},\n", + " 'rhoa' : { 'std_name': 'air_dry_density', 'var_id': 'rhoa', 'units': 'kg m-3', 'dim': 'time, height'},\n", + " 'ta' : { 'std_name': 'air_temperature', 'var_id': 'ta', 'units': 'K', 'dim': 'time, height'},\n", + " 'qv' : { 'std_name': 'water_vapor_mixing_ratio', 'var_id': 'qv', 'units': 'kg kg-1', 'dim': 'time, height'},\n", + " 'hur' : { 'std_name': 'relative_humidity', 'var_id': 'hur', 'units': '1', 'dim': 'time, height'},\n", + " 'huri' : { 'std_name': 'relative_humidity_over_ice', 'var_id': 'huri', 'units': '1', 'dim': 'time, height'},\n", + " 'theta' : { 'std_name': 'air_potential_temperature', 'var_id': 'theta', 'units': 'K', 'dim': 'time, height'},\n", + " 'tke_res': { 'std_name': 'specific_turbulent_kinetic_energy_resolved', 'var_id': 'tke_res', 'units': 'm2 s-2', 'dim': 'time, height'},\n", + " 'tke_sgs': { 'std_name': 'specific_turbulent_kinetic_energy_sgs', 'var_id': 'tke_sgs', 'units': 'm2 s-2', 'dim': 'time, height'},\n", + " 'qlc': { 'std_name': 'mass_mixing_ratio_of_cloud_liquid_water_in_air','var_id': 'qlc', 'units': 'kg kg-1', 'dim': 'time, height'},\n", + " 'qlr': { 'std_name': 'mass_mixing_ratio_of_rain_water_in_air', 'var_id': 'qlr', 'units': 'kg kg-1', 'dim': 'time, height'},\n", + " 'qic': { 'std_name': 'mass_mixing_ratio_of_cloud_ice_in_air', 'var_id': 'qic', 'units': 'kg kg-1', 'dim': 'time, height'},\n", + " 'qis': { 'std_name': 'mass_mixing_ratio_of_snow_in_air', 'var_id': 'qis', 'units': 'kg kg-1', 'dim': 'time, height'},\n", + " 'qig': { 'std_name': 'mass_mixing_ratio_of_graupel_in_air', 'var_id': 'qig', 'units': 'kg kg-1', 'dim': 'time, height'},\n", + " 'nlc': { 'std_name': 'number_of_liquid_cloud_droplets_in_air', 'var_id': 'nlc', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'nlr': { 'std_name': 'number_of_rain_drops_in_air', 'var_id': 'nlr', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'nic': { 'std_name': 'number_of_cloud_ice_crystals_in_air', 'var_id': 'nic', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'nis': { 'std_name': 'number_of_snow_crystals_in_air', 'var_id': 'nis', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'nig': { 'std_name': 'number_of_graupel_crystals_in_air', 'var_id': 'nig', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'na1': { 'std_name': 'number_of_total_aerosol_mode1', 'var_id': 'na1', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'na2': { 'std_name': 'number_of_total_aerosol_mode2', 'var_id': 'na2', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'na3': { 'std_name': 'number_of_total_aerosol_mode3', 'var_id': 'na3', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'nlcic': { 'std_name': 'number_of_liquid_cloud_droplets_in_cloud', 'var_id': 'nlcic', 'units': 'kg-1', 'dim': 'time, height'},\n", + " 'niic' : { 'std_name': 'number_of_ice_crystals_in_cloud', 'var_id': 'niic', 'units': 'kg-1', 'dim': 'time, height'}\n", + "}\n", + "\n", + "\n", + "Output_2D = {\n", + " 'time' : {'std_name': 'time' , 'var_id': 'time' , 'units': 's'},\n", + " 'x' : {'std_name': 'x' , 'var_id': 'x' , 'units': 'm'},\n", + " 'y' : {'std_name': 'y' , 'var_id': 'y' , 'units': 'm'},\n", + " 'hfss' : {'std_name': 'surface_upward_sensible_heat_flux' , 'var_id': 'hfss' , 'units': 'W m-2' , 'dimensions': ['time', 'x', 'y']},\n", + " 'hfls' : {'std_name': 'surface_upward_latent_heat_flux' , 'var_id': 'hfls' , 'units': 'W m-2' , 'dimensions': ['time', 'x', 'y']},\n", + " 'ustar': {'std_name': 'surface_friction_velocity' , 'var_id': 'ustar' , 'units': 'm s-1' , 'dimensions': ['time', 'x', 'y']},\n", + " 'us' : {'std_name': 'surface_eastward_wind' , 'var_id': 'us' , 'units': 'm s-1' , 'dimensions': ['time', 'x', 'y']},\n", + " 'vs' : {'std_name': 'surface_northward_wind' , 'var_id': 'vs' , 'units': 'm s-1' , 'dimensions': ['time', 'x', 'y']},\n", + " 'pr' : {'std_name': 'precipitation_flux_at_surface' , 'var_id': 'pr' , 'units': 'kg m-2 s-1' , 'dimensions': ['time', 'x', 'y']},\n", + " 'lwp' :{'std_name': 'atmosphere_mass_content_of_liquid_water' , 'var_id': 'lwp' , 'units': 'kg m-2' , 'dimensions': ['time', 'x', 'y']},\n", + " 'iwp' :{'std_name': 'atmosphere_mass_content_of_ice' , 'var_id': 'iwp' , 'units': 'kg m-2' , 'dimensions': ['time', 'x', 'y']},\n", + " 'opt' :{'std_name': 'atmosphere_optical_thickness' , 'var_id': 'opt' , 'units': ' ' , 'dimensions': ['time', 'x', 'y']},\n", + " 'alb' :{'std_name': 'pseudo-albedo' , 'var_id': 'alb' , 'units': ' ' , 'dimensions': ['time', 'x', 'y']},\n", + " 'olr11':{'std_name': 'toa_outgoing_longwave_flux_atmospheric_window', 'var_id': 'olr11' , 'units': 'K' , 'dimensions': ['time', 'x', 'y']}\n", + "}\n", + "\n", + "Output_3D = {\n", + " 'time': {'std_name': 'time' , 'var_id': 'time', 'units': 's'},\n", + " 'x' : {'std_name': 'x' , 'var_id': 'x' , 'units': 'm'},\n", + " 'y' : {'std_name': 'y' , 'var_id': 'y' , 'units': 'm'},\n", + " 'zf' : {'std_name': 'height' , 'var_id': 'zf' , 'units': 'm', 'dimensions': ['time']},\n", + " 'pa' : {'std_name': 'air_pressure' , 'var_id': 'pa' , 'units': 'Pa', 'dimensions': ['time', 'height']},\n", + " 'ua' : {'std_name': 'eastward_wind' , 'var_id': 'ua' , 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'va' : {'std_name': 'northward_wind' , 'var_id': 'va' , 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'wa' : {'std_name': 'upward_air_velocity' , 'var_id': 'wa' , 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'ta' : {'std_name': 'air_temperature' , 'var_id': 'ta' , 'units': 'K', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'rhoa': {'std_name': 'air_dry_density' , 'var_id': 'rhoa', 'units': 'kg m^-3', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'qv' : {'std_name': 'water_vapor_mixing_ratio' , 'var_id': 'qv', 'units': 'kg kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'qlc' : {'std_name': 'mass_mixing_ratio_of_cloud_liquid_water_in_air' , 'var_id': 'qlc', 'units': 'kg kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'qlr' : {'std_name': 'mass_mixing_ratio_of_rain_water_in_air' , 'var_id': 'qlr', 'units': 'kg kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'qic' : {'std_name': 'mass_mixing_ratio_of_cloud_ice_in_air' , 'var_id': 'qic', 'units': 'kg kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'qis' : {'std_name': 'mass_mixing_ratio_of_snow_in_air' , 'var_id': 'qis', 'units': 'kg kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'qig' : {'std_name': 'mass_mixing_ratio_of_graupel_in_air' , 'var_id': 'qig', 'units': 'kg kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'na' : {'std_name': 'number_of_dry_aerosol_in_air' , 'var_id': 'na', 'units': 'kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'nlc' : {'std_name': 'number_of_cloud_droplets_in_air' , 'var_id': 'nlc', 'units': 'kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'nlr' : {'std_name': 'number_of_rain_droplets_in_air' , 'var_id': 'nlr', 'units': 'kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'nic' : {'std_name': 'number_of_cloud_ice_crystals_in_air' , 'var_id': 'nic', 'units': 'kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'nis' : {'std_name': 'number_of_snow_crystals_in_air' , 'var_id': 'nis', 'units': 'kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'nig' : {'std_name': 'number_of_graupel_crystals_in_air' , 'var_id': 'nig', 'units': 'kg^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'eps' : {'std_name': 'dissipation_rate_of_turbulent_kinetic_energy' , 'var_id': 'eps', 'units': 'm^2 s^-3', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'vmlc': {'std_name': 'mass_weighted_fall_speed_of_liquid_cloud_water' , 'var_id': 'vmlc', 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'vmlr': {'std_name': 'mass_weighted_fall_speed_of_rain' , 'var_id': 'vmlr', 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'vmic': {'std_name': 'mass_weighted_fall_speed_of_ice_cloud' , 'var_id': 'vmic', 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'vmis': {'std_name': 'mass_weighted_fall_speed_of_snow' , 'var_id': 'vmis', 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']},\n", + " 'vmig': {'std_name': 'mass_weighted_fall_speed_of_graupel' , 'var_id': 'vmig', 'units': 'm s^-1', 'dimensions': ['time', 'height', 'x', 'y']}\n", + "}\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "def convert_TS_to_xarray(TS_output):\n", + "\n", + " data=np.genfromtxt(TS_output, skip_header=1)\n", + "\n", + " # Read the variable names from the first line\n", + " with open(TS_output, 'r') as file:\n", + " header = file.readline().strip()\n", + " variable_names = header.split()[2:] # Exclude the # symbol and the 'time'\n", + "\n", + " # Extract time and data for each variable\n", + " time = data[:, 0]\n", + " variables_data = data[:,1:]\n", + "\n", + " # Create an xarray dataset\n", + " ds = xr.Dataset()\n", + "\n", + " # Add time as a dimension\n", + " ds['time'] = time\n", + "\n", + " # Add variables as data variables\n", + " for ii, var_name in enumerate(variable_names):\n", + " ds[var_name] = (['time'], variables_data[:,ii])\n", + "\n", + " file.close()\n", + "\n", + " return(ds)\n", + "\n", + "\n", + "def Output_add_attributes(output_data,output_name,mixed_phase):\n", + " \n", + " if mixed_phase:\n", + "\n", + " Output_statistic_attr={'title': 'Mean output for '+output_name,\n", + " 'reference': 'COMBLE',\n", + " 'author': 'Alejandro Baro Perez (alejandro.baro.perez@misu.su.se)',\n", + " 'version': 'Created on 2026-05-27',\n", + " 'format_version': 'DEPHY SCM format version 1',}\n", + " \n", + " else:\n", + " \n", + " Output_statistic_attr={'case': 'COMBLE_CAO','title': 'Mean output for '+output_name,\n", + " 'reference': 'COMBLE',\n", + " 'author': 'Alejandro Baro Perez (alejandro.baro.perez@misu.su.se)',\n", + " 'version': 'Created on 2026-05-27',\n", + " 'format_version': 'DEPHY SCM format version 1',} \n", + " \n", + " \n", + " output_data.attrs=Output_statistic_attr\n", + " \n", + " return(output_data)\n", + "\n", + "\n", + "def convert_time_new(output_xarray,yyear,mmonth,dday,hhour,time_format):\n", + " \n", + " time_arr=output_xarray.time.values\n", + " \n", + " if(time_format=='days'):\n", + " t_origin = datetime.datetime(year=yyear, month=mmonth, day=dday, hour=hhour)\n", + " date_list = [datetime.timedelta(days=x) + t_origin for x in time_arr[:].astype(np.float64)]\n", + " if(time_format=='seconds'):\n", + " t_origin = datetime.datetime(year=yyear, month=mmonth, day=dday, hour=hhour)\n", + " date_list = [datetime.timedelta(seconds=x) + t_origin for x in time_arr[:].astype(np.float64)]\n", + " \n", + " # Set seconds and microseconds to zero\n", + " date_list = [dt.replace(second=0, microsecond=0) for dt in date_list]\n", + " \n", + " date_list=np.asarray(date_list)\n", + " \n", + " output_xarray=output_xarray.assign(time=np.asarray(date_list))\n", + "\n", + " return(output_xarray)\n", + "\n", + "\n", + "\n", + "\n", + "#=============================================================================================================\n", + "\n", + "# OUTPUT STATISTIC\n", + "# domain-mean quantities and statistics every 10 minutes (time-averages reported at the end of each period)\n", + "\n", + "def create_stat_output(MIMICA_prof,MIMICA_prof_cl,MIMICA_surf,MIMICA_TS,output_name,mixed_phase):\n", + " \n", + " #Ensuring that the time values are the same for prof,prof_cl,surf and TS outputs\n", + " MIMICA_TS=MIMICA_TS.assign_coords(time=MIMICA_surf.time.values)\n", + " MIMICA_prof=MIMICA_prof.assign_coords(time=MIMICA_surf.time.values)\n", + " MIMICA_prof_cl=MIMICA_prof_cl.assign_coords(time=MIMICA_surf.time.values)\n", + "\n", + " Statistical_output = xr.Dataset()\n", + " \n", + " #Adding the surface pressure (it is constant in MIMICA)\n", + " psurf=99544.5 #[Pa]\n", + " ps_array=np.full(MIMICA_surf.time.shape,psurf)\n", + " ps_dataset = xr.Dataset({\"ps\": (\"time\",ps_array,)},coords={\"time\": MIMICA_surf.time.values},) \n", + " \n", + " Statistical_output['ps']=ps_dataset.ps\n", + " Statistical_output.ps.attrs['standard_name']='surface_pressure'\n", + " Statistical_output.ps.attrs['units']='Pa' \n", + " \n", + " # Friction velocity, surface fluxes\n", + " DEPHY_name=['ts','ustar', 'z0','z0h','hfss','hfls', 'ol','rlut', 'rlus' , 'rlds']\n", + " MIMICA_name=['SST','U_star', 'Z0_m','z0_h','SHF','LHF','L_MO','OLR', 'Surface_LW_up','Surface_LW_down']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " Statistical_output[dephy]=MIMICA_TS[mimica]\n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name'] \n", + " Statistical_output[dephy].attrs['units']=Output_statistic[dephy]['units'] \n", + " \n", + " # Water paths\n", + " DEPHY_name=['lwpc','lwpr','iwp']\n", + " MIMICA_name=['LWPc','LWPr', 'IWP'] \n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " Statistical_output[dephy]=MIMICA_TS[mimica]\n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name'] \n", + " Statistical_output[dephy].attrs['units']=Output_statistic[dephy]['units'] \n", + " \n", + " #Cloud area fraction\n", + " Statistical_output['clt']=MIMICA_TS['C_COL']\n", + " Statistical_output['clt'].attrs['standard_name']= Output_statistic['clt']['std_name'] \n", + " Statistical_output['clt'].attrs['units']=Output_statistic['clt']['units'] \n", + " \n", + " #Optical depths\n", + " Statistical_output['od'] =(MIMICA_surf['OPTC']+MIMICA_surf['OPTI']+MIMICA_surf['OPTR']).mean(dim=('Y','X'))\n", + " Statistical_output['od'].attrs['standard_name']= Output_statistic['od']['std_name'] \n", + " Statistical_output['od'].attrs['units']=Output_statistic['od']['units']\n", + " \n", + " Statistical_output['odlc'] =MIMICA_surf['OPTC'].mean(dim=('Y','X'))\n", + " Statistical_output['odlc'].attrs['standard_name']= Output_statistic['odlc']['std_name'] \n", + " Statistical_output['odlc'].attrs['units']=Output_statistic['odlc']['units']\n", + " \n", + " # Surface flux of aerosols MIMICA\n", + " if 'Surf_naer_flux' in MIMICA_surf:\n", + " Statistical_output['ssaf']=MIMICA_surf['Surf_naer_flux'].mean(dim=('Y','X'))\n", + " Statistical_output['ssaf'].attrs['standard_name']= Output_statistic['ssaf']['std_name'] \n", + " Statistical_output['ssaf'].attrs['units']= Output_statistic['ssaf']['units']\n", + " \n", + " \n", + " # U,V,temperature,potential temperature,water vapor mixing fraction(ratio)\n", + " DEPHY_name=['pa', 'ua', 'va', 'rhoa','ta','theta','tke_res','tke_sgs']\n", + " MIMICA_name=['P_tot','U', 'V','RHO','T','PT' , 'TKE' , 'TKE_sgs']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " Statistical_output[dephy]=MIMICA_prof[mimica]\n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name'] \n", + " Statistical_output[dephy].attrs['units']= Output_statistic[dephy]['units'] \n", + " \n", + " ## Precipitation flux. Change from precipitation rate (MIMICA) to precipitation flux\n", + " #Considering that\n", + " # density of water = 997 kg/m3\n", + " # density of ice = 917 kg/m3 \n", + " # precipitation rates in MIMICA are given in = mm/d = 1e-3/86400 [m/s]\n", + " # Mulitplying the density of water or ice by the precipitation rates in MIMICA we can get the precipitation fluxes\n", + " # in units [kg m-2 s-1]\n", + " # Then precipitation rates in MIMICA should be multiplied by \n", + " # a factor = 0.997/86400 if water\n", + " # a factor = 0.917/86400 if ice\n", + " \n", + " water_factor= 0.997/86400\n", + " ice_factor= 0.917/86400\n", + " \n", + " DEPHY_name=['pr','pri']\n", + " MIMICA_name=['R_rate','R_ice_rate']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " if mixed_phase:\n", + " #Assuming that the liquid phase precipitation in negligible in the mixed phase case\n", + " Statistical_output[dephy]=ice_factor*MIMICA_TS[mimica] \n", + " else:\n", + " Statistical_output[dephy]=water_factor*MIMICA_TS[mimica] \n", + " \n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name']\n", + " Statistical_output[dephy].attrs['units']= Output_statistic[dephy]['units'] \n", + " \n", + " # Relative humidity, and relative humidity over ice:\n", + " DEPHY_name=['hur','huri']\n", + " MIMICA_name=['RH','RHi'] \n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " Statistical_output[dephy]=1e-2*MIMICA_prof[mimica]\n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name']\n", + " Statistical_output[dephy].attrs['units']= Output_statistic[dephy]['units'] \n", + "\n", + " # Mixing fraction from MIMICA are directly mass mixing ratio in DEPHY\n", + " DEPHY_name=[ 'qv','qlc','qlr','qic','qis','qig']\n", + " MIMICA_name=['Qv','Qc', 'Qr', 'Qi', 'Qs', 'Qg']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " if mimica in MIMICA_prof:\n", + " Statistical_output[dephy]=MIMICA_prof[mimica]/MIMICA_prof['RHO']\n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name']\n", + " Statistical_output[dephy].attrs['units']= Output_statistic[dephy]['units'] \n", + "\n", + " # Hydrometeors number density: \n", + " DEPHY_name=['nlc','nlr','nic','nis','nig']\n", + " MIMICA_name=['Nc','Nr','Ni','Ns','Ng'] \n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " if mimica in MIMICA_prof:\n", + " Statistical_output[dephy]=MIMICA_prof[mimica]/MIMICA_prof['RHO']\n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name'] \n", + " Statistical_output[dephy].attrs['units']= Output_statistic[dephy]['units'] \n", + " \n", + " # Hydrometeors number density in cloud:\n", + " DEPHY_name=['nlcic','niic']\n", + " MIMICA_name=['Nc','Ni'] \n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " if mimica in MIMICA_prof_cl:\n", + " #Statistical_output[dephy]=MIMICA_prof_cl[mimica]/MIMICA_prof_cl['RHO']\n", + " Statistical_output[dephy]=save_division(MIMICA_prof_cl[mimica],MIMICA_prof_cl['RHO'])\n", + " Statistical_output[dephy].attrs['standard_name']= Output_statistic[dephy]['std_name'] \n", + " Statistical_output[dephy].attrs['units']= Output_statistic[dephy]['units'] \n", + "\n", + " # Number of total aerosol in air #For later !activated + unactivated???\n", + " if 'N_AERO1' in MIMICA_prof:\n", + " DEPHY_name=['na1','na2','na3']\n", + " MIMICA_name=['N_AERO1','N_AERO2','N_AERO3']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " Statistical_output[dephy]=MIMICA_prof[mimica]\n", + " Statistical_output[dephy].attrs['units']= Output_statistic[dephy]['units']\n", + "\n", + " # Renaming vertical coordinate \n", + " Statistical_output=Statistical_output.rename({'Z': 'zf'})\n", + " Statistical_output.zf.attrs['standard_name']='height'\n", + " Statistical_output.zf.attrs['comment']='altitude of mid level points above sea surface'\n", + "\n", + " # Adding the global attributes to the file\n", + " Statistical_output=Output_add_attributes(Statistical_output,output_name,mixed_phase)\n", + "\n", + " # Changing time format\n", + " Statistical_output=convert_time_new(Statistical_output,2020,3,12,22,'seconds')\n", + " \n", + " return Statistical_output\n", + "\n", + "def create_2D_output(MIMICA_surf,output_name,mixed_phase):\n", + " \n", + " D2_output = xr.Dataset()\n", + " \n", + " # Friction velocity, surface fluxes\n", + " DEPHY_name=['ustar','hfss','hfls']\n", + " MIMICA_name=['U_star','SHF','LHF']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " D2_output[dephy]=MIMICA_surf[mimica]\n", + " D2_output[dephy].attrs['standard_name']= Output_2D[dephy]['std_name'] \n", + " D2_output[dephy].attrs['units']= Output_2D[dephy]['units'] \n", + " \n", + " # Liquid water path, ice water path\n", + " DEPHY_name=['lwp','iwp']\n", + " MIMICA_name=['LWP','IWP']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " D2_output[dephy]=MIMICA_surf[mimica]\n", + " D2_output[dephy].attrs['standard_name']= Output_2D[dephy]['std_name'] \n", + " D2_output[dephy].attrs['units']= Output_2D[dephy]['units'] \n", + " \n", + " # Precipitation flux. Change from precipitation rate (MIMICA) to precipitation flux\n", + " water_factor= 0.997/86400\n", + " ice_factor= 0.917/86400\n", + " \n", + " if mixed_phase:\n", + " D2_output['pr']=ice_factor*MIMICA_surf['R_rate']\n", + " else:\n", + " D2_output['pr']=water_factor*MIMICA_surf['R_rate'] \n", + " \n", + " D2_output['pr'].attrs['standard_name']= Output_2D['pr']['std_name']\n", + " D2_output['pr'].attrs['units']= Output_2D['pr']['units'] \n", + " \n", + " # Atmosphere optical thickness\n", + " OPT= MIMICA_surf['OPTC']+MIMICA_surf['OPTI']+MIMICA_surf['OPTR']\n", + " D2_output['opt'] = OPT \n", + " D2_output['opt'].attrs['standard_name']= Output_2D['opt']['std_name'] \n", + " D2_output['opt'].attrs['units']= Output_2D['opt']['units'] \n", + " \n", + " #Pseudo-albedo\n", + " D2_output['alb'] = OPT/(OPT+13.)\n", + " D2_output['alb'].attrs['standard_name']= Output_2D['alb']['std_name'] \n", + " D2_output['alb'].attrs['units']= Output_2D['alb']['units'] \n", + " \n", + " # Outgoing longwave radiation in atmospheric window \n", + " D2_output['olr11'] = MIMICA_surf['OLR_11mum']\n", + " D2_output['olr11'].attrs['starndard_name']= Output_2D['olr11']['std_name']\n", + " D2_output['olr11'].attrs['units']= Output_2D['olr11']['units'] \n", + " D2_output['olr11'].attrs['description']= 'Outgoing longwave flux in window containing 10.8 micrometers (wavelength range 10-12.5 micrometers)'\n", + " \n", + " # Surface winds u,v\n", + " D2_output['us'] = MIMICA_surf['U_zlevel_1']\n", + " D2_output['us'].attrs['starndard_name']= Output_2D['us']['std_name'] \n", + " D2_output['us'].attrs['units']= Output_2D['us']['units']\n", + " \n", + " D2_output['vs'] = MIMICA_surf['V_zlevel_1']\n", + " D2_output['vs'].attrs['starndard_name']= Output_2D['vs']['std_name'] \n", + " D2_output['vs'].attrs['units']= Output_2D['vs']['units']\n", + " \n", + " # Changing time format\n", + " D2_output=convert_time_new(D2_output,2020,3,12,22,'seconds')\n", + " \n", + " # Adding the global attributes to the file\n", + " D2_output=Output_add_attributes(D2_output,output_name,mixed_phase)\n", + " \n", + " \n", + " return D2_output\n", + "\n", + "\n", + "def create_3D_output(MIMICA_3D,output_name,mixed_phase):\n", + " \n", + " D3_output = xr.Dataset()\n", + "\n", + " # U,V,temperature,potential temperature,water vapor mixing fraction(ratio)\n", + " DEPHY_name=[ 'pa','ua','va','wa','ta','rhoa']\n", + " MIMICA_name=['P_tot','U', 'V','W', 'T','RHO']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " D3_output[dephy]=MIMICA_3D[mimica]\n", + " D3_output[dephy].attrs['standard_name']= Output_3D[dephy]['std_name'] \n", + " D3_output[dephy].attrs['units']= Output_3D[dephy]['units'] \n", + " \n", + " # Mixing fraction from MIMICA are directly mass mixing ratio in DEPHY\n", + " DEPHY_name=[ 'qv','qlc','qlr']\n", + " MIMICA_name=['Qv','Qc', 'Qr' ]\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " D3_output[dephy]=MIMICA_3D[mimica]\n", + " D3_output[dephy].attrs['standard_name']= Output_3D[dephy]['std_name']\n", + " D3_output[dephy].attrs['units']= Output_3D[dephy]['units'] \n", + " \n", + " # Mixing fraction from MIMICA are directly mass mixing ratio in DEPHY [ice phase]\n", + " if 'Qi' in MIMICA_3D:\n", + " DEPHY_name=[ 'qic','qis','qig']\n", + " MIMICA_name=['Qi', 'Qs', 'Qg' ]\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " D3_output[dephy]=MIMICA_3D[mimica]\n", + " D3_output[dephy].attrs['standard_name']= Output_3D[dephy]['std_name']\n", + " D3_output[dephy].attrs['units']= Output_3D[dephy]['units'] \n", + " \n", + " # Hydrometeors number density: #Question here\n", + " DEPHY_name=['nlc','nlr']\n", + " MIMICA_name=['Nc','Nr'] \n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " D3_output[dephy]=MIMICA_3D[mimica]\n", + " D3_output[dephy].attrs['standard_name']= Output_3D[dephy]['std_name'] \n", + " D3_output[dephy].attrs['units']= Output_3D[dephy]['units'] \n", + " \n", + " # Hydrometeors number density (ice phase) \n", + " if 'Ni' in MIMICA_3D: \n", + " DEPHY_name=['nic','nis','nig']\n", + " MIMICA_name=['Ni','Ns','Ng'] \n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " D3_output[dephy]=MIMICA_3D[mimica]\n", + " D3_output[dephy].attrs['standard_name']= Output_3D[dephy]['std_name'] \n", + " D3_output[dephy].attrs['units']= Output_3D[dephy]['units'] \n", + " \n", + " #Check these variables. Are they mass weighted? \n", + " DEPHY_name=[ 'vmlc','vmlr','vmic','vmis','vmig']\n", + " MIMICA_name=[ 'Vpc','Vpr','Vpi','Vps','Vpg']\n", + " for dephy,mimica in zip(DEPHY_name,MIMICA_name):\n", + " if mimica in MIMICA_3D:\n", + " D3_output[dephy]=MIMICA_3D[mimica]\n", + " D3_output[dephy].attrs['standard_name']= Output_3D[dephy]['std_name']\n", + " D3_output[dephy].attrs['units']= Output_3D[dephy]['units'] \n", + " \n", + " \n", + " # Changing time format\n", + " D3_output=convert_time_new(D3_output,2020,3,12,22,'seconds')\n", + " \n", + " # Adding the global attributes to the file\n", + " D3_output=Output_add_attributes(D3_output,output_name,mixed_phase)\n", + " \n", + "\n", + " \n", + " return D3_output\n", + " \n", + " \n", + " \n", + "def save_division(numerator,denominator):\n", + "\n", + " # Replace zeros in the denominator with NaN to avoid division by zero\n", + " safe_denominator = denominator.where(denominator != 0, np.nan)\n", + "\n", + " # Perform the division\n", + " resulting_variable = numerator / safe_denominator\n", + "\n", + " # Replace NaN values in the result with zero\n", + " resulting_variable = resulting_variable.fillna(0)\n", + "\n", + " # Assign the resulting variable back to the dataset if needed\n", + " #COMBLE_FixN_256_new_prof_cl['resulting_variable'] = resulting_variable\n", + " \n", + " return resulting_variable \n", + "\n", + "\n", + "def convert_time_from_minutes_to_seconds_new(file, file_time_modified):\n", + "\n", + " # Open dataset\n", + " ds = xr.open_dataset(file)\n", + " \n", + " # Force correct encoding\n", + " ds.time.encoding[\"units\"] = \"seconds since 2020-03-12 22:00:00\"\n", + " ds.time.encoding[\"calendar\"] = \"proleptic_gregorian\" \n", + " \n", + "\n", + " # Save modified dataset\n", + " ds.to_netcdf(file_time_modified)\n", + "\n", + " return ds \n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Simulation with ice part 1: FixN" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Reading MIMICA native outputs\n", + "COMBLE_FixN_256_prof= xr.open_dataset(native_outputs_path+'COMBLE_FixN_256x256/OUTPUT/profiles_tot.nc')\n", + "COMBLE_FixN_256_prof_cl=xr.open_dataset(native_outputs_path+'COMBLE_FixN_256x256/OUTPUT/profiles_cl.nc')\n", + "COMBLE_FixN_256_surf= xr.open_dataset(native_outputs_path+'COMBLE_FixN_256x256/OUTPUT/slice_surf.nc')\n", + "COMBLE_FixN_256_3D= xr.open_dataset(native_outputs_path+'COMBLE_FixN_256x256/OUTPUT/COMBLE_MPI.nc')\n", + "COMBLE_FixN_256_TS= xr.open_dataset(native_outputs_path+'COMBLE_FixN_256x256/OUTPUT/T_S.nc')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "Stat_output_FixN_256=create_stat_output(COMBLE_FixN_256_prof,COMBLE_FixN_256_prof_cl,COMBLE_FixN_256_surf,\n", + " COMBLE_FixN_256_TS,'MIMICA_Lx25_dx100_FixN_alt',True)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "OUTPUT_2D_FixN_256=create_2D_output(COMBLE_FixN_256_surf,'MIMICA_Lx25_dx100_FixN_2D_alt',True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "OUTPUT_3D_FixN_256=create_3D_output(COMBLE_FixN_256_3D,'MIMICA_Lx25_dx100_FixN_3D_alt',True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Create netcdf files for FixN" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "metadata": {} + }, + "outputs": [], + "source": [ + "Stat_output_FixN_256.to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "OUTPUT_2D_FixN_256.to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN_2D'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "OUTPUT_3D_FixN_256.to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN_3D'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Simulation without ice part 1 : FixN noice" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "COMBLE_FixN_noice_256_prof= xr.open_dataset(native_outputs_path+'COMBLE_FixN_noice_256x256/OUTPUT/profiles_tot.nc')\n", + "COMBLE_FixN_noice_256_prof_cl=xr.open_dataset(native_outputs_path+'COMBLE_FixN_noice_256x256/OUTPUT/profiles_cl.nc')\n", + "COMBLE_FixN_noice_256_surf= xr.open_dataset(native_outputs_path+'COMBLE_FixN_noice_256x256/OUTPUT/slice_surf.nc')\n", + "COMBLE_FixN_noice_256_3D= xr.open_dataset(native_outputs_path+'COMBLE_FixN_noice_256x256/OUTPUT/COMBLE_MPI.nc')\n", + "COMBLE_FixN_noice_256_TS= xr.open_dataset(native_outputs_path+'COMBLE_FixN_noice_256x256/OUTPUT/T_S.nc')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "Stat_output_FixN_noice_256=create_stat_output(COMBLE_FixN_noice_256_prof,COMBLE_FixN_noice_256_prof_cl,COMBLE_FixN_noice_256_surf,\n", + " COMBLE_FixN_noice_256_TS,'MIMICA_Lx25_dx100_FixN_noice',False)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "OUTPUT_2D_FixN_noice_256=create_2D_output(COMBLE_FixN_noice_256_surf,'MIMICA_Lx25_dx100_FixN_noice_2D',False)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "OUTPUT_3D_FixN_noice_256=create_3D_output(COMBLE_FixN_noice_256_3D,'MIMICA_Lx25_dx100_FixN_noice_3D',False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Create netcdf files for FixN noice" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "Stat_output_FixN_noice_256.to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN_noice'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "OUTPUT_2D_FixN_noice_256.to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN_noice_2D'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "OUTPUT_3D_FixN_noice_256.to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN_noice_3D'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Taking the last time step of the 3D outputs for the first iteration:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "OUTPUT_3D_FixN_256.isel(time=20).to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN_3D_at_18UTC'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "OUTPUT_3D_FixN_noice_256.isel(time=20).to_netcdf(path=converted_outputs_path+'MIMICA_Lx25_dx100_FixN_noice_3D_at_18UTC'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Converting the time from minutes to seconds" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Converting time in file MIMICA_Lx25_dx100_FixN_noice.nc saving in /proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED_time_mod/MIMICA_Lx25_dx100_FixN_noice.nc\n", + "Converting time in file MIMICA_Lx25_dx100_FixN_noice_2D.nc saving in /proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED_time_mod/MIMICA_Lx25_dx100_FixN_noice_2D.nc\n", + "Converting time in file MIMICA_Lx25_dx100_FixN_noice_3D.nc saving in /proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED_time_mod/MIMICA_Lx25_dx100_FixN_noice_3D.nc\n" + ] + } + ], + "source": [ + "#Doing this only because Tim Juliano could not read properly the netcdf files after the conversion\n", + "file_names=[ 'MIMICA_Lx25_dx100_FixN.nc','MIMICA_Lx25_dx100_FixN_2D.nc', 'MIMICA_Lx25_dx100_FixN_3D.nc', \n", + " 'MIMICA_Lx25_dx100_FixN_noice.nc','MIMICA_Lx25_dx100_FixN_noice_2D.nc', 'MIMICA_Lx25_dx100_FixN_noice_3D.nc' ]\n", + "\n", + "for ii_file in file_names:\n", + " print(\"Converting time in file \",ii_file, \"saving in\",converted_outputs_path_time_mod+ii_file)\n", + " convert_time_from_minutes_to_seconds_new(converted_outputs_path+ii_file,converted_outputs_path_time_mod+ii_file) \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Part 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#COMBLE_Part2_Prog_Na_prof=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_new/OUTPUT/profiles_tot.nc')\n", + "#COMBLE_Part2_Prog_Na_prof_cl=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_new/OUTPUT/profiles_cl.nc')\n", + "#COMBLE_Part2_Prog_Na_surf=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_new/OUTPUT/slice_surf.nc')\n", + "#COMBLE_Part2_Prog_Na_3D=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_new/OUTPUT/COMBLE_MPI.nc')\n", + "#COMBLE_Part2_Prog_Na_TS=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_new/OUTPUT/T_S.nc')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Stat_output_COMBLE_Part2_Prog_Na=create_stat_output(COMBLE_Part2_Prog_Na_prof,COMBLE_Part2_Prog_Na_prof_cl,COMBLE_Part2_Prog_Na_surf,\n", + "# COMBLE_Part2_Prog_Na_TS,'MIMICA_Lx25_dx100_Prog_Na_alt',True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_2D_COMBLE_Part2_Prog_Na=create_2D_output(COMBLE_Part2_Prog_Na_surf,'MIMICA_Lx25_dx100_Prog_Na_2D_alt',True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_3D_COMBLE_Part2_Prog_Na=create_3D_output(COMBLE_Part2_Prog_Na_3D,'MIMICA_Lx25_dx100_Prog_Na_3D_alt',True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#COMBLE_Part2_Prog_NaNi_prof=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNaNi/OUTPUT/profiles_tot.nc')\n", + "#COMBLE_Part2_Prog_NaNi_prof_cl=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNaNi/OUTPUT/profiles_cl.nc')\n", + "#COMBLE_Part2_Prog_NaNi_surf=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNaNi/OUTPUT/slice_surf.nc')\n", + "#COMBLE_Part2_Prog_NaNi_3D=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNaNi/OUTPUT/COMBLE_MPI.nc')\n", + "#COMBLE_Part2_Prog_NaNi_TS=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNaNi/OUTPUT/T_S.nc')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Stat_output_COMBLE_Part2_Prog_NaNi=create_stat_output(COMBLE_Part2_Prog_NaNi_prof,COMBLE_Part2_Prog_NaNi_prof_cl,COMBLE_Part2_Prog_NaNi_surf,\n", + "# COMBLE_Part2_Prog_NaNi_TS,'MIMICA_Lx25_dx100_Prog_NaNi_alt',True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_2D_COMBLE_Part2_Prog_NaNi=create_2D_output(COMBLE_Part2_Prog_NaNi_surf,'MIMICA_Lx25_dx100_Prog_NaNi_2D_alt',True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_3D_COMBLE_Part2_Prog_NaNi=create_3D_output(COMBLE_Part2_Prog_NaNi_3D,'MIMICA_Lx25_dx100_Prog_NaNi_3D_alt',True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#COMBLE_Part2_Prog_Na_noice_prof=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_noice/OUTPUT/profiles_tot.nc')\n", + "#COMBLE_Part2_Prog_Na_noice_prof_cl=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_noice/OUTPUT/profiles_cl.nc')\n", + "#COMBLE_Part2_Prog_Na_noice_surf=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_noice/OUTPUT/slice_surf.nc')\n", + "#COMBLE_Part2_Prog_Na_noice_3D=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_noice/OUTPUT/COMBLE_MPI.nc')\n", + "#COMBLE_Part2_Prog_Na_noice_TS=xr.open_dataset(COMBLE_part2+'COMBLE_Part2_ProgNa_noice/OUTPUT/T_S.nc')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Stat_output_COMBLE_Part2_Prog_Na_noice=create_stat_output(COMBLE_Part2_Prog_Na_noice_prof,COMBLE_Part2_Prog_Na_noice_prof_cl,COMBLE_Part2_Prog_Na_noice_surf,\n", + "# COMBLE_Part2_Prog_Na_noice_TS,'MIMICA_Lx25_dx100_Prog_Na_noice',False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_2D_COMBLE_Part2_Prog_Na_noice=create_2D_output(COMBLE_Part2_Prog_Na_noice_surf,'MIMICA_Lx25_dx100_Prog_Na_noice_2D',False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_3D_COMBLE_Part2_Prog_Na_noice=create_3D_output(COMBLE_Part2_Prog_Na_noice_3D,'MIMICA_Lx25_dx100_Prog_Na_noice_3D',False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create netcdf files" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Prog_Na" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The file /proj/mimica-chalmers-comble/users/x_abaro/COMBLE/Simulations_Folder_COMBLE/MIMICA_Lx25_dx100_Prog_Na_alt.nc has been deleted.\n" + ] + } + ], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_Na_alt'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#Stat_output_COMBLE_Part2_Prog_Na.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_alt'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The file /proj/mimica-chalmers-comble/users/x_abaro/COMBLE/Simulations_Folder_COMBLE/MIMICA_Lx25_dx100_Prog_Na_2D_alt.nc has been deleted.\n" + ] + } + ], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_Na_2D_alt'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#OUTPUT_2D_COMBLE_Part2_Prog_Na.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_2D_alt'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_Na_3D_alt'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#OUTPUT_3D_COMBLE_Part2_Prog_Na.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_3D_alt'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_3D_COMBLE_Part2_Prog_Na.isel(time=20).to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_3D_alt_at_18UTC'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Prog_NaNi" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_NaNi_alt'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#Stat_output_COMBLE_Part2_Prog_NaNi.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_NaNi_alt'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_NaNi_2D_alt'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#OUTPUT_2D_COMBLE_Part2_Prog_NaNi.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_NaNi_2D_alt'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_NaNi_3D_alt'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#OUTPUT_3D_COMBLE_Part2_Prog_NaNi.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_NaNi_3D_alt'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_3D_COMBLE_Part2_Prog_NaNi.isel(time=20).to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_NaNi_3D_alt_at_18UTC'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Prog_Na_noice" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_Na_noice'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#Stat_output_COMBLE_Part2_Prog_Na_noice.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_noice'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_Na_noice_2D'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#OUTPUT_2D_COMBLE_Part2_Prog_Na_noice.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_noice_2D'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#file_path = simulation_path+'MIMICA_Lx25_dx100_Prog_Na_noice_3D'+'.nc'\n", + "#\n", + "#if os.path.exists(file_path):\n", + "# os.remove(file_path)\n", + "# print(f\"The file {file_path} has been deleted.\")\n", + "#\n", + "#OUTPUT_3D_COMBLE_Part2_Prog_Na_noice.to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_noice_3D'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#OUTPUT_3D_COMBLE_Part2_Prog_Na_noice.isel(time=20).to_netcdf(path=simulation_path+'MIMICA_Lx25_dx100_Prog_Na_noice_3D_at_18UTC'+'.nc',mode='w',engine=\"netcdf4\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def convert_time_from_minutes_to_seconds(file,file_time_modified):\n", + " # Open dataset\n", + " ds = xr.open_dataset(file) # Replace with actual file\n", + "\n", + " # Check if 'units' attribute exists\n", + " if 'units' in ds['time'].attrs:\n", + " # Extract the reference time from the attribute\n", + " import re\n", + " match = re.search(r\"since\\s(.+)\", ds['time'].attrs['units'])\n", + " if match:\n", + " reference_time_str = match.group(1)\n", + " reference_time = np.datetime64(reference_time_str)\n", + " else:\n", + " raise ValueError(\"Could not extract reference time from units attribute.\")\n", + " else:\n", + " # If units attribute is missing, define a reference time manually\n", + " print(\"Warning: No 'units' attribute found. Setting reference time manually.\")\n", + " reference_time = ds['time'].values[0] # Use the first time entry as reference\n", + " reference_time_str = str(reference_time) # Convert to string\n", + "\n", + " # Ensure time is in datetime64 format\n", + " ds['time'] = reference_time + (ds['time'] - reference_time) * 60 # Convert from minutes to seconds\n", + "\n", + " # Update metadata to reflect seconds instead of minutes\n", + " ds['time'].attrs['units'] = f\"seconds since {reference_time_str}\"\n", + " \n", + " # Move 'units' from attributes to encoding\n", + " ds['time'].encoding['units'] = ds['time'].attrs.pop('units')\n", + "\n", + " # Save the updated dataset\n", + " ds.to_netcdf(file_time_modified)\n", + "\n", + " # Print the updated time values and attributes\n", + " #print(ds['time'].values) # Ensure values are still datetime64[ns]\n", + " #print(ds['time'].attrs) # Check that units now say \"seconds since ...\"\n", + " \n", + " #return ds\n", + "\n", + "\n", + "def convert_time_from_minutes_to_seconds_new(file, file_time_modified):\n", + "\n", + " # Open dataset\n", + " ds = xr.open_dataset(file)\n", + " \n", + " # Force correct encoding\n", + " ds.time.encoding[\"units\"] = \"seconds since 2020-03-12 22:00:00\"\n", + " ds.time.encoding[\"calendar\"] = \"proleptic_gregorian\" \n", + " \n", + "\n", + " # Save modified dataset\n", + " ds.to_netcdf(file_time_modified)\n", + "\n", + " return ds" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Converting time in file MIMICA_Lx25_dx100_FixN.nc saving in /proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED_time_mod/MIMICA_Lx25_dx100_FixN.nc\n", + "Converting time in file MIMICA_Lx25_dx100_FixN_2D.nc saving in /proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED_time_mod/MIMICA_Lx25_dx100_FixN_2D.nc\n", + "Converting time in file MIMICA_Lx25_dx100_FixN_3D.nc saving in /proj/bolinc/users/x_abaro/MIMICA_radiation/COMBLE_CONVERTED_time_mod/MIMICA_Lx25_dx100_FixN_3D.nc\n" + ] + } + ], + "source": [ + "#file_names=['MIMICA_Lx25_dx100_FixN_alt.nc','MIMICA_Lx25_dx100_FixN_noice.nc','MIMICA_Lx25_dx100_Prog_Na_alt.nc','MIMICA_Lx25_dx100_Prog_NaNi_alt.nc',\n", + "# 'MIMICA_Lx25_dx100_Prog_Na_noice.nc']\n", + "\n", + "file_names=[ 'MIMICA_Lx25_dx100_FixN.nc','MIMICA_Lx25_dx100_FixN_2D.nc', 'MIMICA_Lx25_dx100_FixN_3D.nc' ]\n", + "\n", + "for ii_file in file_names:\n", + " print(\"Converting time in file \",ii_file, \"saving in\",converted_outputs_path_time_mod+ii_file)\n", + " convert_time_from_minutes_to_seconds_new(converted_outputs_path+ii_file,converted_outputs_path_time_mod+ii_file) \n", + "# print(\" \")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "myenv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/output_les/mimica/sandbox/MIMICA_Lx25_dx100_FixN.nc b/output_les/mimica/sandbox/MIMICA_Lx25_dx100_FixN.nc new file mode 100644 index 00000000..59f5db9f Binary files /dev/null and b/output_les/mimica/sandbox/MIMICA_Lx25_dx100_FixN.nc differ