Ignore:
Timestamp:
Jul 7, 2025, 1:39:13 PM (23 hours ago)
Author:
afalco
Message:

Pluto: updated plots scripts.
Fixed some issues with reading XIOS, etc.
Included display_netcdf.py tool from Mars PCM.
AF

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LMDZ.PLUTO/util/script_figures/FV3_utils.py

    r3823 r3833  
    33pi=math.pi
    44import matplotlib
    5 matplotlib.use('TKAgg')
    6 
    7 name="../Xhistins2015_short"
     5matplotlib.use('Agg')
     6from input import *
     7from display_netcdf import *
    88
    99def fmtsci(x, pos):
     
    12011201
    12021202def printvar(infile):
    1203     # Get all variable names for a netCDF file ---
     1203    '''Get all variable names for a netCDF file '''
    12041204    print("Variables:")
    12051205    variableNames = list(infile.variables.keys());
     
    12081208
    12091209def getind(myloc,field):
    1210     # get a specific index in the lat, lon, time or pfull 1D field
     1210    '''get a specific index in the lat, lon, time or pfull 1D field'''
    12111211    res=[]
    12121212    for loc in np.atleast_1d(myloc):
     
    12201220# getinds=np.vectorize(getind, excluded="field")
    12211221
    1222 def getvar(nc1,var,times=None,tim=None,longitudes=None,lon=None,t_mean=False,l_mean=False):
     1222def getvar(nc1,var,times=None,longitudes=None,latitudes=None,altitudes=None,t_mean=False,l_mean=False):
     1223    """Get variables from netcdf file and slice it up!
     1224
     1225    Args:
     1226        nc1 : netcdf identifier
     1227        var (str): variable name
     1228        times (list, optional): times to select. Defaults to None.
     1229        longitudes (list, optional): longitudes to select. Defaults to None.
     1230        latitudes (list, optional): latitudes to select. Defaults to None.
     1231        t_mean (bool, optional): Apply mean on time dimension. Defaults to False.
     1232        l_mean (bool, optional): Apply mean on longitude dimension. Defaults to False.
     1233
     1234    Returns:
     1235        array: variable data array
     1236    """
     1237   
    12231238    if var == "latitude" and var not in nc1.variables: var="lat"
    12241239    if var == "longitude" and var not in nc1.variables: var="lon"
     
    12271242    if var == "phisinit" and var not in nc1.variables: var="phisfi"
    12281243
    1229     myvar = nc1.variables[var][:]
    1230     try:
    1231         t=getind(times,tim)
    1232         if len(t)==2:
    1233             myvar = myvar[t[0]:t[1]+1]
    1234         else:
    1235             myvar = myvar[t]
    1236     except:
    1237         # print("no time selected")
    1238         pass
    1239     try:
    1240         l=getind(longitudes,lon)
    1241         if len(l)==2:
    1242             myvar = myvar[...,l[0]:l[1]+1]
    1243         else:
    1244             myvar = myvar[...,l]
    1245     except:
    1246         # print("no longitude selected")
    1247         pass
    1248     if t_mean: myvar=np.mean(myvar,axis=0) # temporal mean
    1249     if l_mean: myvar=np.mean(myvar,axis=-1) # longitudinal mean
     1244    var_data = nc1.variables[var]
     1245    dims = var_data.dimensions
     1246    myvar = var_data[:]
     1247
     1248    if times or longitudes or latitudes:
     1249        slicer = [slice(None)] * len(dims)
     1250
     1251        time_it = (times,TIME_DIMS)
     1252        alt_it = (altitudes,ALT_DIMS)
     1253        lon_it = (longitudes,LON_DIMS)
     1254        lat_it = (latitudes,LAT_DIMS)
     1255
     1256        # iterate over dimensions to pick indices to select
     1257        for values, dim in (time_it, lon_it, lat_it, alt_it):
     1258            try:
     1259                dim_idx = find_dim_index(dims, dim)
     1260                tmp_var = find_coord_var(nc1, dim)
     1261                file_values = nc1.variables[tmp_var][:]
     1262                i = getind(values, file_values)
     1263                print(dim, i)
     1264                if len(i)==2: # range of indices
     1265                    slicer[dim_idx] = slice(min(i),max(i)+1)
     1266                else: # specific index
     1267                    slicer[dim_idx] = i
     1268            except:
     1269                continue
     1270       
     1271        # slice the variable according to the indices
     1272        myvar = myvar[tuple(slicer)]
     1273       
     1274    if l_mean: # longitudinal mean
     1275        myvar=np.mean(myvar,axis=find_dim_index(dims, LON_DIMS))
     1276    if t_mean: # temporal mean
     1277        myvar=np.mean(myvar,axis=find_dim_index(dims, TIME_DIMS))
    12501278    print(np.shape(myvar))
    12511279    try:
Note: See TracChangeset for help on using the changeset viewer.