Python Forum
Why is this Python code running twice?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this Python code running twice?
#1
Hi everybody
Why is this code running twice? Is this related with the variables that the code imports from convert_nc_to_csv1?
Thank you.


# Reading the netcdf file
from netCDF4 import Dataset
import numpy as np
import pandas as pd
from convert_nc_to_csv1 import lat_of_station, lon_of_station

data = Dataset (r'G:\My Drive\Python_Projects\NC4 Files code\ERA5-Land hourly data from 1950 to present\ERA5_land_hourly_data_from_2000_to_2010.nc')

data_range = pd.date_range(start = '2000-01-01', end = '2011-01-01', freq='H')

lat = data.variables['latitude'][:]
lon = data.variables['longitude'][:]

sq_diff_lat = (lat - lat_of_station)**2
sq_diff_lon = (lon - lon_of_station)**2

#Identifying the index of the minimum value for lat and lon
min_index_lat = sq_diff_lat.argmin()
min_index_lon = sq_diff_lon.argmin()


df1 = pd.DataFrame(0, columns = ['t2m'], index = data_range)
df = df1.iloc[:-1 , :]
dt = np.arange(0, data.variables['time'].size)

temperature = data.variables['t2m']

for time_index in dt:
    df.iloc[time_index] = temperature[time_index, min_index_lat,min_index_lon]-273.15
    print (time_index)

#Saving the time series to csv
df.to_csv('64_PONTE MESTRAS_3.csv')
Reply
#2
what is convert_nc_to_csv1 and what is its content?
And how do you know it runs twice?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Importing a module may execute code in that module, but this only happens once, not twice. What is your evidence that your code runs twice? Be specific.
Reply
#4
(Jan-31-2022, 03:34 PM)buran Wrote: what is convert_nc_to_csv1 and what is its content?
And how do you know it runs twice?

Hi convert_nc_to_csv1 is another .py file with the value of those two variables: lat_of_station and lon_of_station.
I know it runs twice because I can see the time index value inside the loop in the spyder console running from 1 to 90000 and the returning to 1 and running again until it stops in 90000.
Reply
#5
dt should not have a repeat, but I like to be sure. I would print something at the start of the script (print("Hi Mom")). If you see that twice it would mean the script is running twice. If that happens try running the script from the command line instead of spyder.
mcva likes this post
Reply
#6
(Jan-31-2022, 08:05 PM)deanhystad Wrote: dt should not have a repeat, but I like to be sure. I would print something at the start of the script (print("Hi Mom")). If you see that twice it would mean the script is running twice. If that happens try running the script from the command line instead of spyder.

Hi it only prints (Hi Mom) :) once. But something strange is happening. I have delete this line:

print (time_index)
However in the console I can still see the output of time index once (from 1 to 90000). So I see "Hi Mom" and the output of time index once (from 1 to 90000). This is very strange.

I will let it stay like this....
Thank you for you help
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in running a code akbarza 7 667 Feb-14-2024, 02:57 PM
Last Post: snippsat
  writing and running code in vscode without saving it akbarza 1 397 Jan-11-2024, 02:59 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 536 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Code running many times nad not just one? korenron 4 1,377 Jul-24-2022, 08:12 AM
Last Post: korenron
  Error while running code on VSC maiya 4 3,777 Jul-01-2022, 02:51 PM
Last Post: maiya
  code running for more than an hour now, yet didn't get any result, what should I do? aiden 2 1,515 Apr-06-2022, 03:41 PM
Last Post: Gribouillis
  Python keeps running the old version of the code quest 2 3,789 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime
  My python code is running very slow on millions of records shantanu97 7 2,591 Dec-28-2021, 11:02 AM
Last Post: Larz60+
  I am getting ValueError from running my code PythonMonkey 0 1,482 Dec-26-2021, 06:14 AM
Last Post: PythonMonkey
  rtmidi problem after running the code x times philipbergwerf 1 2,434 Apr-04-2021, 07:07 PM
Last Post: philipbergwerf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020