“As I was riding the train home, I realized that I'd left my keys in the closet at work.”
You're telling a story about a time when you got locked out of your house. You've described the basic situation, and now you want to describe the interesting event that happened.
As I was riding the train home, I realized that I'd left my keys in the closet at work.
Use "as I was ___ing , ___" to describe when something suddenly happened while you were doing some other continuing action. For example:
As I was getting ready for work, I suddenly felt a sharp pain in my chest.
You can put the "as I was" part at the end as well:
She called out to me as I was about to get on the bus.
This is similar to "meanwhile", but "meanwhile" can be used to describe what two (or more) different people did:
James got started on supper. Meanwhile, Emma and I worked on her math homework and Brad went off to his room, probably to play X-box.
"As I was ___ing" is used to describe two things that happened to the same person at the same time. One of the events is continuous ("riding the train"), and the other is an action that happens quickly ("realized that...").
You can "ride" a train, or you can "take" a train. What's the difference?
You use "ride" when you want to talk about the experience of riding, or when you want to describe something that happened while you were riding the train:
I love riding the train in the middle of the day when it's not crowded.
I was riding the train, and this guy next to me started telling me a story about how he had just gotten out of prison.
You use "take" when you want to talk about how you get somewhere:
Just take the #3 train to 72nd Street and I'll meet you there.
Do you think it'll be faster to take the train or try to catch a bus?
To "realize" is to suddenly know something based on your own thinking - not based on what someone tells you or on things that you directly see.
In written English, it's considered correct to use "that" after "realize":
I realized that I didn't have my wallet.
But in spoken English, "that" is often dropped.
When you're telling a story, you describe the setting at a certain time in the past. If you then want to describe something that happened before the setting of the story, you use "I had ___". Here's an example:
I was almost at the train station when I realized that I had left my wallet in my other purse.
You can also talk about things that didn't happen before the setting of the story. In this sentence:
Believe it or not, I had never even traveled abroad when I moved here.
...the basic setting of the story is "when I moved here". The speaker didn't have any travel experience then, although she might have some now.