Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to export desired variables with OpenDrift.run's export_variables parameter #1464

Closed
chironbang opened this issue Dec 19, 2024 · 4 comments

Comments

@chironbang
Copy link

Hello,

I am trying to generate virtual drifters from a velocity field. I have created a .nc file containing the eastward and northward velocity components, the temperature and the salinity. I want the drifters to record the latitude, longitude, time, velocity, temperature, and salinity.
I can run the simulation and export longitude, latitude, and velocity components but not the temperature and the salinity.

You can find the code below. I printed out the reader too.

import numpy as np
from datetime import timedelta
from opendrift.readers import reader_netCDF_CF_generic
from opendrift.models.oceandrift import OceanDrift


output_file = "../data/drifters_data/vd_truth_uvts_25.nc"
# Initialize the OceanDrift model
o = OceanDrift(loglevel=0)

# Load the NetCDF file containing u, v, temperature, and salinity
ncfile = "../data/num_models_data/company_data_uvts.nc"
reader = reader_netCDF_CF_generic.Reader(ncfile)
o.add_reader(reader)

# Print the available variables in the reader for verification
print("Variables in reader:", reader.variables)

# Get the start time from the reader
start_time = reader.start_time

# Seed particles (drifters)
np.random.seed(42)
n_drifters = 10
seed_lons = np.random.uniform(-98, -76.4, n_drifters)  # Adjust longitude range
seed_lats = np.random.uniform(18.09, 31.96, n_drifters)   # Adjust latitude range
o.seed_elements(lon=seed_lons, lat=seed_lats, radius=5000, number=n_drifters, time=start_time)

# Define the simulation duration
simulation_duration = timedelta(days=5)  # Simulate for 5 days

# Run the simulation, explicitly exporting temperature and salinity
o.run(
    time_step=timedelta(hours=1), time_step_output=timedelta(hours=1), steps=10,
    outfile=output_file,
    export_variables=['lon', 'lat', 'sea_water_temperature', 'sea_water_salinity']
)

reader output

Reader: ../data/num_models_data/company_data_uvts.nc
Projection: 
  +proj=latlong
Coverage: [degrees]
  xmin: -98.000000   xmax: -76.400000   step: 0.01   numx: 2160
  ymin: 18.090000   ymax: 31.960000   step: 0.01   numy: 1387
  Corners (lon, lat):
    (-98.00,  31.96)  (-76.40,  31.96)
    (-98.00,  18.09)  (-76.40,  18.09)
Vertical levels [m]: 
  Not specified
Available time range:
  start: 2021-10-01 00:00:00   end: 2022-01-16 00:00:00   step: 1 day, 0:00:00
    108 times (0 missing)
Variables:
  eastward_sea_water_velocity
  northward_sea_water_velocity
  sea_water_temperature
  sea_water_salinity
  x_sea_water_velocity
  y_sea_water_velocity
  sea_water_speed - derived from ['x_sea_water_velocity', 'y_sea_water_velocity']

 0:00:04.0  total
 0:00:00.0  preparing
 0:00:04.0  reading
 0:00:03.8  interpolation
 0:00:00.0  interpolation_time
         0  masking
         

Any insights would be appreciated.
Chiron

@chironbang chironbang changed the title Unable to export desired variables Unable to export desired variables with OpenDrift.run's export_variables parameter Dec 20, 2024
@knutfrode
Copy link
Collaborator

Hi, and sorry for late reply.
I cannot see any reason why temperature and salinity should not be exported to the netCDF file, as these are clearly detected by your reader.
Btw, all variables are written by default, so you may also skip the export_variables keyword to the run method.

What are the contents of your output netCDF file?

@chironbang
Copy link
Author

Hi,
I ran the code again without specifying export_variables, and 37 variables were exported, amongst which there is neither sea_water_temperature nor sea_water_salinity.
I have attached a zip file containing the input and output data as well as the list of variables exported.

Thank you!
data.zip

@chironbang
Copy link
Author

I have fixed the issue.
I just needed to add the temperature and salinity in the required_variables dictionary like this (before seeding elements):

o.required_variables.update({'sea_water_temperature': {'fallback': np.nan, 'profiles': True},
                            'sea_water_salinity': {'fallback': np.nan, 'profiles': True}})

I assume by default, OpenDrift does not include those variables.

@knutfrode
Copy link
Collaborator

Ok, yes that makes sense.
OpenDrift modules only read the variables they need, and then only those can also be exported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants